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.util.concurrent.Future;
023:
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.stream.XMLInputFactory;
027: import javax.xml.stream.XMLStreamReader;
028: import javax.xml.transform.Source;
029: import javax.xml.transform.dom.DOMSource;
030: import javax.xml.ws.Dispatch;
031: import javax.xml.ws.Response;
032: import javax.xml.ws.Service;
033: import javax.xml.ws.WebServiceException;
034:
035: import junit.framework.TestCase;
036: import org.apache.axis2.jaxws.message.util.Reader2Writer;
037: import org.apache.axis2.jaxws.TestLogger;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Node;
040:
041: /**
042: * This class tests the JAX-WS Dispatch with various forms of the
043: * javax.xml.transform.dom.DOMSource
044: */
045: public class DOMSourceDispatch extends TestCase {
046:
047: private static final XMLInputFactory inputFactory = XMLInputFactory
048: .newInstance();
049:
050: public void testSyncPayloadMode() throws Exception {
051: TestLogger.logger
052: .debug("---------------------------------------");
053: TestLogger.logger.debug("test: " + getName());
054:
055: // Initialize the JAX-WS client artifacts
056: Service svc = Service
057: .create(DispatchTestConstants.QNAME_SERVICE);
058: svc.addPort(DispatchTestConstants.QNAME_PORT, null,
059: DispatchTestConstants.URL);
060: Dispatch<Source> dispatch = svc.createDispatch(
061: DispatchTestConstants.QNAME_PORT, Source.class,
062: Service.Mode.PAYLOAD);
063:
064: // Create the DOMSource
065: DOMSource request = createDOMSourceFromString(DispatchTestConstants.sampleBodyContent);
066:
067: TestLogger.logger.debug(">> Invoking sync Dispatch");
068: Source response = dispatch.invoke(request);
069: assertNotNull("dispatch invoke returned null", response);
070:
071: // Turn the Source into a String so we can check it
072: String responseText = createStringFromSource(response);
073: TestLogger.logger.debug(responseText);
074:
075: // Check to make sure the content is correct
076: assertTrue(!responseText.contains("soap"));
077: assertTrue(!responseText.contains("Envelope"));
078: assertTrue(!responseText.contains("Body"));
079: assertTrue(responseText.contains("echoStringResponse"));
080: }
081:
082: public void testSyncMessageMode() throws Exception {
083: TestLogger.logger
084: .debug("---------------------------------------");
085: TestLogger.logger.debug("test: " + getName());
086:
087: // Initialize the JAX-WS client artifacts
088: Service svc = Service
089: .create(DispatchTestConstants.QNAME_SERVICE);
090: svc.addPort(DispatchTestConstants.QNAME_PORT, null,
091: DispatchTestConstants.URL);
092: Dispatch<Source> dispatch = svc.createDispatch(
093: DispatchTestConstants.QNAME_PORT, Source.class,
094: Service.Mode.MESSAGE);
095:
096: // Create the DOMSource
097: DOMSource request = createDOMSourceFromString(DispatchTestConstants.sampleSoapMessage);
098:
099: TestLogger.logger.debug(">> Invoking sync Dispatch");
100: Source response = dispatch.invoke(request);
101: assertNotNull("dispatch invoke returned null", response);
102:
103: // Turn the Source into a String so we can check it
104: String responseText = createStringFromSource(response);
105: TestLogger.logger.debug(responseText);
106:
107: // Check to make sure the content is correct
108: assertTrue(responseText.contains("soap"));
109: assertTrue(responseText.contains("Envelope"));
110: assertTrue(responseText.contains("Body"));
111: assertTrue(responseText.contains("echoStringResponse"));
112: }
113:
114: public void testAsyncCallbackPayloadMode() throws Exception {
115: TestLogger.logger
116: .debug("---------------------------------------");
117: TestLogger.logger.debug("test: " + getName());
118:
119: // Initialize the JAX-WS client artifacts
120: Service svc = Service
121: .create(DispatchTestConstants.QNAME_SERVICE);
122: svc.addPort(DispatchTestConstants.QNAME_PORT, null,
123: DispatchTestConstants.URL);
124: Dispatch<Source> dispatch = svc.createDispatch(
125: DispatchTestConstants.QNAME_PORT, Source.class,
126: Service.Mode.PAYLOAD);
127:
128: // Create the DOMSource
129: DOMSource request = createDOMSourceFromString(DispatchTestConstants.sampleBodyContent);
130:
131: // Setup the callback for async responses
132: AsyncCallback<Source> callbackHandler = new AsyncCallback<Source>();
133:
134: TestLogger.logger
135: .debug(">> Invoking async (callback) Dispatch");
136: Future<?> monitor = dispatch.invokeAsync(request,
137: callbackHandler);
138:
139: while (!monitor.isDone()) {
140: TestLogger.logger
141: .debug(">> Async invocation still not complete");
142: Thread.sleep(1000);
143: }
144:
145: Source response = callbackHandler.getValue();
146: assertNotNull(response);
147:
148: // Turn the Source into a String so we can check it
149: String responseText = createStringFromSource(response);
150: TestLogger.logger.debug(responseText);
151:
152: // Check to make sure the content is correct
153: assertTrue(!responseText.contains("soap"));
154: assertTrue(!responseText.contains("Envelope"));
155: assertTrue(!responseText.contains("Body"));
156: assertTrue(responseText.contains("echoStringResponse"));
157: }
158:
159: public void testAsyncCallbackMessageMode() throws Exception {
160: TestLogger.logger
161: .debug("---------------------------------------");
162: TestLogger.logger.debug("test: " + getName());
163:
164: // Initialize the JAX-WS client artifacts
165: Service svc = Service
166: .create(DispatchTestConstants.QNAME_SERVICE);
167: svc.addPort(DispatchTestConstants.QNAME_PORT, null,
168: DispatchTestConstants.URL);
169: Dispatch<Source> dispatch = svc.createDispatch(
170: DispatchTestConstants.QNAME_PORT, Source.class,
171: Service.Mode.MESSAGE);
172:
173: // Create the DOMSource
174: DOMSource request = createDOMSourceFromString(DispatchTestConstants.sampleSoapMessage);
175:
176: // Setup the callback for async responses
177: AsyncCallback<Source> callbackHandler = new AsyncCallback<Source>();
178:
179: TestLogger.logger
180: .debug(">> Invoking async (callback) Dispatch");
181: Future<?> monitor = dispatch.invokeAsync(request,
182: callbackHandler);
183:
184: while (!monitor.isDone()) {
185: TestLogger.logger
186: .debug(">> Async invocation still not complete");
187: Thread.sleep(1000);
188: }
189:
190: Source response = callbackHandler.getValue();
191: assertNotNull(response);
192:
193: // Turn the Source into a String so we can check it
194: String responseText = createStringFromSource(response);
195: TestLogger.logger.debug(responseText);
196:
197: // Check to make sure the content is correct
198: assertTrue(responseText.contains("soap"));
199: assertTrue(responseText.contains("Envelope"));
200: assertTrue(responseText.contains("Body"));
201: assertTrue(responseText.contains("echoStringResponse"));
202: }
203:
204: public void testAsyncPollingPayloadMode() throws Exception {
205: TestLogger.logger
206: .debug("---------------------------------------");
207: TestLogger.logger.debug("test: " + getName());
208:
209: // Initialize the JAX-WS client artifacts
210: Service svc = Service
211: .create(DispatchTestConstants.QNAME_SERVICE);
212: svc.addPort(DispatchTestConstants.QNAME_PORT, null,
213: DispatchTestConstants.URL);
214: Dispatch<Source> dispatch = svc.createDispatch(
215: DispatchTestConstants.QNAME_PORT, Source.class,
216: Service.Mode.PAYLOAD);
217:
218: // Create the DOMSource
219: DOMSource request = createDOMSourceFromString(DispatchTestConstants.sampleBodyContent);
220:
221: TestLogger.logger.debug(">> Invoking async (polling) Dispatch");
222: Response<Source> asyncResponse = dispatch.invokeAsync(request);
223:
224: while (!asyncResponse.isDone()) {
225: TestLogger.logger
226: .debug(">> Async invocation still not complete");
227: Thread.sleep(1000);
228: }
229:
230: Source response = asyncResponse.get();
231: assertNotNull(response);
232:
233: // Turn the Source into a String so we can check it
234: String responseText = createStringFromSource(response);
235: TestLogger.logger.debug(responseText);
236:
237: // Check to make sure the content is correct
238: assertTrue(!responseText.contains("soap"));
239: assertTrue(!responseText.contains("Envelope"));
240: assertTrue(!responseText.contains("Body"));
241: assertTrue(responseText.contains("echoStringResponse"));
242: }
243:
244: public void testAsyncPollingMessageMode() throws Exception {
245: TestLogger.logger
246: .debug("---------------------------------------");
247: TestLogger.logger.debug("test: " + getName());
248:
249: // Initialize the JAX-WS client artifacts
250: Service svc = Service
251: .create(DispatchTestConstants.QNAME_SERVICE);
252: svc.addPort(DispatchTestConstants.QNAME_PORT, null,
253: DispatchTestConstants.URL);
254: Dispatch<Source> dispatch = svc.createDispatch(
255: DispatchTestConstants.QNAME_PORT, Source.class,
256: Service.Mode.MESSAGE);
257:
258: // Create the DOMSource
259: DOMSource request = createDOMSourceFromString(DispatchTestConstants.sampleSoapMessage);
260:
261: TestLogger.logger
262: .debug(">> Invoking async (callback) Dispatch");
263: Response<Source> asyncResponse = dispatch.invokeAsync(request);
264:
265: while (!asyncResponse.isDone()) {
266: TestLogger.logger
267: .debug(">> Async invocation still not complete");
268: Thread.sleep(1000);
269: }
270:
271: Source response = asyncResponse.get();
272: assertNotNull(response);
273:
274: // Turn the Source into a String so we can check it
275: String responseText = createStringFromSource(response);
276: TestLogger.logger.debug(responseText);
277:
278: // Check to make sure the content is correct
279: assertTrue(responseText.contains("soap"));
280: assertTrue(responseText.contains("Envelope"));
281: assertTrue(responseText.contains("Body"));
282: assertTrue(responseText.contains("echoStringResponse"));
283: }
284:
285: public void testOneWayPayloadMode() throws Exception {
286: TestLogger.logger
287: .debug("---------------------------------------");
288: TestLogger.logger.debug("test: " + getName());
289:
290: // Initialize the JAX-WS client artifacts
291: Service svc = Service
292: .create(DispatchTestConstants.QNAME_SERVICE);
293: svc.addPort(DispatchTestConstants.QNAME_PORT, null,
294: DispatchTestConstants.URL);
295: Dispatch<Source> dispatch = svc.createDispatch(
296: DispatchTestConstants.QNAME_PORT, Source.class,
297: Service.Mode.PAYLOAD);
298:
299: // Create the DOMSource
300: DOMSource request = createDOMSourceFromString(DispatchTestConstants.sampleBodyContent);
301:
302: TestLogger.logger.debug(">> Invoking One Way Dispatch");
303: dispatch.invokeOneWay(request);
304: }
305:
306: public void testOneWayMessageMode() throws Exception {
307: TestLogger.logger
308: .debug("---------------------------------------");
309: TestLogger.logger.debug("test: " + getName());
310:
311: // Initialize the JAX-WS client artifacts
312: Service svc = Service
313: .create(DispatchTestConstants.QNAME_SERVICE);
314: svc.addPort(DispatchTestConstants.QNAME_PORT, null,
315: DispatchTestConstants.URL);
316: Dispatch<Source> dispatch = svc.createDispatch(
317: DispatchTestConstants.QNAME_PORT, Source.class,
318: Service.Mode.MESSAGE);
319:
320: // Create the DOMSource
321: DOMSource request = createDOMSourceFromString(DispatchTestConstants.sampleSoapMessage);
322:
323: TestLogger.logger.debug(">> Invoking One Way Dispatch");
324: dispatch.invokeOneWay(request);
325: }
326:
327: public void testBadDOMSource() throws Exception {
328: TestLogger.logger
329: .debug("---------------------------------------");
330: TestLogger.logger.debug("test: " + getName());
331:
332: // Initialize the JAX-WS client artifacts
333: Service svc = Service
334: .create(DispatchTestConstants.QNAME_SERVICE);
335: svc.addPort(DispatchTestConstants.QNAME_PORT, null,
336: DispatchTestConstants.URL);
337: Dispatch<Source> dispatch = svc.createDispatch(
338: DispatchTestConstants.QNAME_PORT, Source.class,
339: Service.Mode.PAYLOAD);
340:
341: // Create the DOMSource
342: DOMSource request = new DOMSource();
343:
344: try {
345: dispatch.invokeOneWay(request);
346: fail("WebServiceException was expected");
347: } catch (WebServiceException e) {
348: TestLogger.logger
349: .debug("A Web Service Exception was expected: "
350: + e.toString());
351: assertTrue(e.getMessage() != null);
352: } catch (Exception e) {
353: fail("WebServiceException was expected, but received " + e);
354: }
355:
356: }
357:
358: /**
359: * Create a DOMSource with the provided String as the content
360: * @param input
361: * @return
362: */
363: private DOMSource createDOMSourceFromString(String input)
364: throws Exception {
365: byte[] bytes = input.getBytes();
366: ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
367:
368: DocumentBuilderFactory domFactory = DocumentBuilderFactory
369: .newInstance();
370: domFactory.setNamespaceAware(true);
371: DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
372: Document domTree = domBuilder.parse(stream);
373: Node node = domTree.getDocumentElement();
374:
375: DOMSource domSource = new DOMSource(node);
376: return domSource;
377: }
378:
379: /**
380: * Create a String from the provided Source
381: * @param input
382: * @return
383: */
384: private String createStringFromSource(Source input)
385: throws Exception {
386: XMLStreamReader reader = inputFactory
387: .createXMLStreamReader(input);
388: Reader2Writer r2w = new Reader2Writer(reader);
389: String text = r2w.getAsString();
390: return text;
391: }
392: }
|