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.jaxws.dispatch;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.transform.Source;
026: import javax.xml.transform.Transformer;
027: import javax.xml.transform.TransformerFactory;
028: import javax.xml.transform.stream.StreamResult;
029: import javax.xml.transform.stream.StreamSource;
030: import javax.xml.ws.Dispatch;
031: import javax.xml.ws.Service;
032: import javax.xml.ws.Service.Mode;
033: import javax.xml.ws.soap.SOAPBinding;
034: import javax.xml.ws.soap.SOAPFaultException;
035:
036: import junit.framework.TestCase;
037:
038: /**
039: * This class uses the JAX-WS Dispatch API to test sending and receiving
040: * messages using SOAP 1.2.
041: */
042: public class SOAP12Dispatch extends TestCase {
043:
044: private static final QName QNAME_SERVICE = new QName(
045: "http://org/apache/axis2/jaxws/test/SOAP12",
046: "SOAP12Service");
047: private static final QName QNAME_PORT = new QName(
048: "http://org/apache/axis2/jaxws/test/SOAP12", "SOAP12Port");
049: private static final String URL_ENDPOINT = "http://localhost:8080/axis2/services/SOAP12ProviderService";
050:
051: private static final String sampleRequest = "<test:echoString xmlns:test=\"http://org/apache/axis2/jaxws/test/SOAP12\">"
052: + "<test:input>SAMPLE REQUEST MESSAGE</test:input>"
053: + "</test:echoString>";
054: private static final String sampleEnvelopeHead = "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">"
055: + "<soapenv:Header /><soapenv:Body>";
056: private static final String sampleEnvelopeHead_MustUnderstand = "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">"
057: + "<soapenv:Header>"
058: + "<soapenv:codeHeaderSOAP12 soapenv:mustUnderstand=\"true\">"
059: + "<code>default</code>"
060: + "</soapenv:codeHeaderSOAP12>"
061: + "</soapenv:Header>" + "<soapenv:Body>";
062: private static final String sampleEnvelopeTail = "</soapenv:Body></soapenv:Envelope>";
063: private static final String sampleEnvelope = sampleEnvelopeHead
064: + sampleRequest + sampleEnvelopeTail;
065:
066: private static final String sampleEnvelope_MustUnderstand = sampleEnvelopeHead_MustUnderstand
067: + sampleRequest + sampleEnvelopeTail;
068:
069: public SOAP12Dispatch(String name) {
070: super (name);
071: }
072:
073: /**
074: * Test sending a SOAP 1.2 request in PAYLOAD mode
075: */
076: public void testSOAP12DispatchPayloadMode() throws Exception {
077: // Create the JAX-WS client needed to send the request
078: Service service = Service.create(QNAME_SERVICE);
079: service.addPort(QNAME_PORT, SOAPBinding.SOAP12HTTP_BINDING,
080: URL_ENDPOINT);
081: Dispatch<Source> dispatch = service.createDispatch(QNAME_PORT,
082: Source.class, Mode.PAYLOAD);
083:
084: // Create the Source object with the payload contents. Since
085: // we're in PAYLOAD mode, we don't have to worry about the envelope.
086: byte[] bytes = sampleRequest.getBytes();
087: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
088: StreamSource request = new StreamSource(bais);
089:
090: // Send the SOAP 1.2 request
091: Source response = dispatch.invoke(request);
092:
093: // Convert the response to a more consumable format
094: ByteArrayOutputStream baos = new ByteArrayOutputStream();
095: StreamResult result = new StreamResult(baos);
096:
097: TransformerFactory factory = TransformerFactory.newInstance();
098: Transformer trans = factory.newTransformer();
099: trans.transform(response, result);
100:
101: // Check to make sure the contents are correct. Again, since we're
102: // in PAYLOAD mode, we shouldn't have anything related to the envelope
103: // in the return, just the contents of the Body.
104: String responseText = baos.toString();
105: assertTrue(!responseText.contains("soap"));
106: assertTrue(!responseText.contains("Envelope"));
107: assertTrue(!responseText.contains("Body"));
108: assertTrue(responseText.contains("echoStringResponse"));
109: }
110:
111: /**
112: * Test sending a SOAP 1.2 request in MESSAGE mode
113: */
114: public void testSOAP12DispatchMessageMode() throws Exception {
115: // Create the JAX-WS client needed to send the request
116: Service service = Service.create(QNAME_SERVICE);
117: service.addPort(QNAME_PORT, SOAPBinding.SOAP12HTTP_BINDING,
118: URL_ENDPOINT);
119: Dispatch<Source> dispatch = service.createDispatch(QNAME_PORT,
120: Source.class, Mode.MESSAGE);
121:
122: // Create the Source object with the message contents. Since
123: // we're in MESSAGE mode, we'll need to make sure we create this
124: // with the right protocol.
125: byte[] bytes = sampleEnvelope.getBytes();
126: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
127: StreamSource request = new StreamSource(bais);
128:
129: Source response = dispatch.invoke(request);
130:
131: // Convert the response to a more consumable format
132: ByteArrayOutputStream baos = new ByteArrayOutputStream();
133: StreamResult result = new StreamResult(baos);
134:
135: TransformerFactory factory = TransformerFactory.newInstance();
136: Transformer trans = factory.newTransformer();
137: trans.transform(response, result);
138:
139: // Check to make sure the contents of the message are correct
140: String responseText = baos.toString();
141: assertTrue(responseText.contains("soap"));
142: assertTrue(responseText.contains("Body"));
143: assertTrue(responseText.contains("Envelope"));
144: assertTrue(responseText.contains("echoStringResponse"));
145:
146: // Check to make sure the message returned had the right protocol version
147: // TODO: Need to determine whether or not we should be using the hard
148: // coded URLs here, or whether we should be using a constant for the
149: // purposes of the test.
150: assertTrue(responseText
151: .contains("http://www.w3.org/2003/05/soap-envelope"));
152: assertTrue(!responseText
153: .contains("http://schemas.xmlsoap.org/soap/envelope"));
154: }
155:
156: /**
157: * Test sending a SOAP 1.2 request in MESSAGE mode
158: */
159: public void testSOAP12DispatchMessageMode_MustUnderstand()
160: throws Exception {
161: // Create the JAX-WS client needed to send the request
162: Service service = Service.create(QNAME_SERVICE);
163: service.addPort(QNAME_PORT, SOAPBinding.SOAP12HTTP_BINDING,
164: URL_ENDPOINT);
165: Dispatch<Source> dispatch = service.createDispatch(QNAME_PORT,
166: Source.class, Mode.MESSAGE);
167:
168: // Create the Source object with the message contents. Since
169: // we're in MESSAGE mode, we'll need to make sure we create this
170: // with the right protocol.
171: byte[] bytes = sampleEnvelope_MustUnderstand.getBytes();
172: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
173: StreamSource request = new StreamSource(bais);
174:
175: SOAPFaultException e = null;
176: try {
177: Source response = dispatch.invoke(request);
178: } catch (SOAPFaultException ex) {
179: e = ex;
180: }
181:
182: assertNotNull(
183: "We should have an exception, but none was thrown.", e);
184: assertEquals("FaultCode should be \"MustUnderstand\"",
185: "MustUnderstand", e.getFault().getFaultCodeAsQName()
186: .getLocalPart());
187:
188: }
189: }
|