001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.saaj;
020:
021: import org.custommonkey.xmlunit.XMLTestCase;
022:
023: import javax.xml.soap.MessageFactory;
024: import javax.xml.soap.MimeHeaders;
025: import javax.xml.soap.SOAPMessage;
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028:
029: /**
030: *
031: */
032: public class MessageFactoryTest extends XMLTestCase {
033: private MessageFactory mf = null;
034:
035: protected void setUp() throws Exception {
036: mf = MessageFactory.newInstance();
037: }
038:
039: public void testCreateMessage() {
040: try {
041: ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
042: ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
043:
044: SOAPMessage msg1 = mf.createMessage();
045: msg1.writeTo(baos1);
046:
047: MimeHeaders headers = new MimeHeaders();
048: headers.addHeader("Content-Type", "text/xml");
049:
050: // Create SOAPMessage from MessageFactory object using InputStream
051: SOAPMessage msg2 = mf.createMessage(headers,
052: new ByteArrayInputStream(baos1.toString()
053: .getBytes()));
054: if (msg2 == null) {
055: fail();
056: }
057: msg2.writeTo(baos2);
058:
059: if (!(baos1.toString().equals(baos2.toString()))) {
060: fail();
061: }
062: } catch (Exception e) {
063: fail();
064: }
065: }
066:
067: public void testCreateMessage2() {
068: try {
069: ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
070: ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
071:
072: final String XML_STRING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
073: + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"
074: + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
075: + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
076: + " <soapenv:Header>\n"
077: + " <shw:Hello xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n"
078: + " <shw:Myname>Tony</shw:Myname>\n"
079: + " </shw:Hello>\n"
080: + " </soapenv:Header>\n"
081: + " <soapenv:Body>\n"
082: + "<shw:Address shw:t='test' xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n"
083: + "<shw:City>GENT</shw:City>\n"
084: + "</shw:Address>\n"
085: + "</soapenv:Body>\n"
086: + "</soapenv:Envelope>";
087:
088: MimeHeaders headers = new MimeHeaders();
089: headers.addHeader("Content-Type", "text/xml");
090:
091: SOAPMessage msg1 = mf.createMessage(headers,
092: new ByteArrayInputStream(XML_STRING.getBytes()));
093: msg1.writeTo(baos1);
094:
095: // Create SOAPMessage from MessageFactory object using InputStream
096: SOAPMessage msg2 = mf.createMessage(headers,
097: new ByteArrayInputStream(baos1.toString()
098: .getBytes()));
099: if (msg2 == null) {
100: fail();
101: }
102: msg2.writeTo(baos2);
103:
104: this .assertXMLEqual(baos1.toString(), baos2.toString());
105: } catch (Exception e) {
106: e.printStackTrace();
107: fail();
108: }
109: }
110:
111: public void testNewInstane() {
112: try {
113: MessageFactory mf = MessageFactory.newInstance();
114: assertNotNull(mf);
115: ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
116:
117: SOAPMessage msg1 = mf.createMessage();
118: msg1.writeTo(baos1);
119:
120: MimeHeaders headers = new MimeHeaders();
121: headers.addHeader("Content-Type", "text/xml");
122:
123: } catch (Exception e) {
124: fail("Exception: " + e);
125: }
126: }
127: }
|