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