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.sample;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.concurrent.Future;
025:
026: import javax.xml.namespace.QName;
027: import javax.xml.transform.Result;
028: import javax.xml.transform.Source;
029: import javax.xml.transform.Transformer;
030: import javax.xml.transform.TransformerFactory;
031: import javax.xml.transform.stream.StreamResult;
032: import javax.xml.transform.stream.StreamSource;
033: import javax.xml.ws.AsyncHandler;
034: import javax.xml.ws.Binding;
035: import javax.xml.ws.BindingProvider;
036: import javax.xml.ws.Dispatch;
037: import javax.xml.ws.Response;
038: import javax.xml.ws.Service;
039: import javax.xml.ws.handler.Handler;
040: import javax.xml.ws.handler.HandlerResolver;
041: import javax.xml.ws.handler.PortInfo;
042: import javax.xml.ws.soap.SOAPFaultException;
043:
044: import java.io.StringReader;
045: import java.io.StringWriter;
046: import junit.framework.TestCase;
047:
048: import org.apache.axis2.jaxws.sample.addnumbershandler.AddNumbersClientLogicalHandler;
049: import org.apache.axis2.jaxws.sample.addnumbershandler.AddNumbersClientLogicalHandler2;
050: import org.apache.axis2.jaxws.sample.addnumbershandler.AddNumbersClientLogicalHandler3;
051: import org.apache.axis2.jaxws.sample.addnumbershandler.AddNumbersClientLogicalHandler4;
052: import org.apache.axis2.jaxws.sample.addnumbershandler.AddNumbersClientProtocolHandler;
053: import org.apache.axis2.jaxws.sample.addnumbershandler.AddNumbersHandlerPortType;
054: import org.apache.axis2.jaxws.sample.addnumbershandler.AddNumbersHandlerService;
055: import org.apache.axis2.jaxws.TestLogger;
056: import org.test.addnumbershandler.AddNumbersHandlerResponse;
057:
058: /**
059: * @author rott
060: *
061: */
062: public class AddNumbersHandlerTests extends TestCase {
063:
064: String axisEndpoint = "http://localhost:8080/axis2/services/AddNumbersHandlerService";
065:
066: /**
067: * Client app sends 10, 10 as params to sum. No client-side handlers are configured
068: * for this scenario. The server-side AddNumbersLogicalHandler is instantiated with a
069: * variable "deduction" with value 1. Upon class initialization using PostConstruct
070: * annotation, that internal variable is changed to value 2. The inbound AddNumbersLogicalHandler
071: * subtracts 1 from the first param, then outbound it subtracts 2 from the result sum.
072: *
073: * This test accomplishes three things (which also carry over to other tests since they all use
074: * the same endpoint and server-side handlers:
075: * 1) PostConstruct annotation honored in the handler framework for handler instantiation
076: * 2) AddNumbersLogicalHandler also sets two message context properties, one with APPLICATION
077: * scope, which the endpoint checks.
078: * 3) Handlers are sharing properties, both APPLICATION scoped and HANDLER scoped
079: * 3) General handler framework functionality; make sure handlers are instantiated and called
080: */
081: public void testAddNumbersHandler() {
082: try {
083: TestLogger.logger
084: .debug("----------------------------------");
085: TestLogger.logger.debug("test: " + getName());
086:
087: AddNumbersHandlerService service = new AddNumbersHandlerService();
088: AddNumbersHandlerPortType proxy = service
089: .getAddNumbersHandlerPort();
090:
091: BindingProvider p = (BindingProvider) proxy;
092: p.getRequestContext().put(
093: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
094: axisEndpoint);
095: int total = proxy.addNumbersHandler(10, 10);
096:
097: assertEquals(
098: "With handler manipulation, total should be 3 less than a proper sumation.",
099: 17, total);
100: TestLogger.logger
101: .debug("Total (after handler manipulation) = "
102: + total);
103: TestLogger.logger
104: .debug("----------------------------------");
105: } catch (Exception e) {
106: e.printStackTrace();
107: fail(e.getMessage());
108: }
109: }
110:
111: public void testAddNumbersHandlerDispatch() {
112: try {
113: QName serviceName = new QName(
114: "http://org/test/addnumbershandler",
115: "AddNumbersHandlerService");
116: QName portName = new QName(
117: "http://org/test/addnumbershandler",
118: "AddNumbersHandlerPort");
119:
120: Service myService = Service.create(serviceName);
121: myService.addPort(portName, null, axisEndpoint);
122: Dispatch<Source> myDispatch = myService.createDispatch(
123: portName, Source.class, Service.Mode.PAYLOAD);
124:
125: // set handler chain for binding provider
126: Binding binding = ((BindingProvider) myDispatch)
127: .getBinding();
128:
129: // create a new list or use the existing one
130: List<Handler> handlers = binding.getHandlerChain();
131:
132: if (handlers == null)
133: handlers = new ArrayList<Handler>();
134: handlers.add(new AddNumbersClientLogicalHandler());
135: handlers.add(new AddNumbersClientProtocolHandler());
136: binding.setHandlerChain(handlers);
137:
138: //Invoke the Dispatch
139: TestLogger.logger.debug(">> Invoking Async Dispatch");
140: Source response = myDispatch.invoke(createRequestSource());
141: String resString = getString(response);
142: if (!resString.contains("<return>16</return>")) {
143: fail("Response string should contain <return>17</return>, but does not. The resString was: \""
144: + resString + "\"");
145: }
146:
147: TestLogger.logger
148: .debug("----------------------------------");
149: } catch (Exception e) {
150: e.printStackTrace();
151: fail();
152: }
153: }
154:
155: /*
156: * JAXWS 9.2.1.1 conformance test
157: */
158: public void testAddNumbersHandlerResolver() {
159: try {
160: TestLogger.logger
161: .debug("----------------------------------");
162: TestLogger.logger.debug("test: " + getName());
163:
164: AddNumbersHandlerService service = new AddNumbersHandlerService();
165:
166: AddNumbersHandlerPortType proxy = service
167: .getAddNumbersHandlerPort();
168:
169: service.setHandlerResolver(new MyHandlerResolver());
170:
171: BindingProvider p = (BindingProvider) proxy;
172:
173: /*
174: * despite setting MyHandlerResolver on the service, we should get an empty
175: * list from the getBinding().getHandlerChain() call below. JAXWS 9.2.1.1 conformance
176: */
177: List<Handler> list = p.getBinding().getHandlerChain();
178:
179: assertTrue(
180: "List should be empty. We've not conformed to JAXWS 9.2.1.1.",
181: list.isEmpty());
182:
183: TestLogger.logger
184: .debug("----------------------------------");
185: } catch (Exception e) {
186: e.printStackTrace();
187: fail(e.getMessage());
188: }
189: }
190:
191: // TODO: disabled until handler support is more complete
192: public void testAddNumbersHandlerWithFault() {
193: try {
194: TestLogger.logger
195: .debug("----------------------------------");
196: TestLogger.logger.debug("test: " + getName());
197:
198: AddNumbersHandlerService service = new AddNumbersHandlerService();
199: AddNumbersHandlerPortType proxy = service
200: .getAddNumbersHandlerPort();
201:
202: BindingProvider p = (BindingProvider) proxy;
203: p.getRequestContext().put(
204: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
205: axisEndpoint);
206: // value 99 triggers the handler to throw an exception, but does
207: // NOT trigger the AddNumbersHandler.handlefault method.
208: // The spec does not call the handlefault method of a handler that
209: // causes a flow reversal
210: int total = proxy.addNumbersHandler(99, 10);
211:
212: fail("We should have got an exception due to the handler.");
213: } catch (Exception e) {
214: e.printStackTrace();
215: assertTrue("Exception should be SOAPFaultException",
216: e instanceof SOAPFaultException);
217: assertEquals(((SOAPFaultException) e).getMessage(),
218: "AddNumbersLogicalHandler2 was here");
219: }
220: TestLogger.logger.debug("----------------------------------");
221: }
222:
223: /**
224: * testAddNumbersClientHandler performs the same tests as testAddNumbersHandler, except
225: * that two client-side handlers are also inserted into the flow. The inbound AddNumbersClientLogicalHandler
226: * checks that the properties set here in this method (the client app) and the properties set in the
227: * outbound AddNumbersClientProtocolHandler are accessible. These properties are also checked here in
228: * the client app. AddNumbersClientLogicalHandler also subtracts 1 from the sum on the inbound flow.
229: */
230: public void testAddNumbersClientHandler() {
231: try {
232: TestLogger.logger
233: .debug("----------------------------------");
234: TestLogger.logger.debug("test: " + getName());
235:
236: AddNumbersHandlerService service = new AddNumbersHandlerService();
237: AddNumbersHandlerPortType proxy = service
238: .getAddNumbersHandlerPort();
239:
240: BindingProvider p = (BindingProvider) proxy;
241:
242: p.getRequestContext().put(
243: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
244: axisEndpoint);
245: p.getRequestContext().put("myClientKey", "myClientVal");
246:
247: List<Handler> handlers = p.getBinding().getHandlerChain();
248: if (handlers == null)
249: handlers = new ArrayList<Handler>();
250: handlers.add(new AddNumbersClientLogicalHandler());
251: handlers.add(new AddNumbersClientProtocolHandler());
252: p.getBinding().setHandlerChain(handlers);
253:
254: int total = proxy.addNumbersHandler(10, 10);
255:
256: // see if I can get an APPLICATION scoped property set during outbound flow. I should be able to do this according to 4.2.1
257:
258: // TODO: assert is now commented out. This property is set by a client outbound handler, and I don't think it
259: // should be available on the request or response contexts.
260: //assertNotNull("Should be able to retrieve APPLICATION scoped property, but could not.", ((String)p.getRequestContext().get("AddNumbersClientProtocolHandlerOutboundAppScopedProperty")));
261:
262: // should NOT be able to get this HANDLER scoped property though
263: assertNull(
264: "Should not be able to retrieve HANDLER scoped property, but was able.",
265: (String) p
266: .getResponseContext()
267: .get(
268: "AddNumbersClientProtocolHandlerOutboundHandlerScopedProperty"));
269: // should be able to get this APPLICATION scoped property set during inbound flow
270: assertNotNull(
271: "Should be able to retrieve APPLICATION scoped property, but could not.",
272: (String) p
273: .getResponseContext()
274: .get(
275: "AddNumbersClientProtocolHandlerInboundAppScopedProperty"));
276: // should NOT be able to get this HANDLER scoped property though
277: assertNull(
278: "Should not be able to retrieve HANDLER scoped property, but was able.",
279: (String) p
280: .getResponseContext()
281: .get(
282: "AddNumbersClientProtocolHandlerInboundHandlerScopedProperty"));
283: // should be able to get this APPLICATION scoped property set by this client
284: assertNotNull(
285: "Should be able to retrieve APPLICATION scoped property, but could not.",
286: (String) p.getRequestContext().get("myClientKey"));
287:
288: assertEquals(
289: "With handler manipulation, total should be 4 less than a proper sumation.",
290: 16, total);
291: TestLogger.logger
292: .debug("Total (after handler manipulation) = "
293: + total);
294: TestLogger.logger
295: .debug("----------------------------------");
296: } catch (Exception e) {
297: e.printStackTrace();
298: fail();
299: }
300: }
301:
302: /*
303: * uses a custom HandlerResolver instead of the default. MyHandlerResolver
304: * puts the AddNumbersClientLogicalHandler and AddNumbersClientProtocolHandler
305: * in the flow. Results should be the same as testAddNumbersClientHandler.
306: */
307: public void testAddNumbersClientHandlerMyResolver() {
308: try {
309: TestLogger.logger
310: .debug("----------------------------------");
311: TestLogger.logger.debug("test: " + getName());
312:
313: AddNumbersHandlerService service = new AddNumbersHandlerService();
314: service.setHandlerResolver(new MyHandlerResolver());
315:
316: AddNumbersHandlerPortType proxy = service
317: .getAddNumbersHandlerPort();
318:
319: BindingProvider p = (BindingProvider) proxy;
320:
321: p.getRequestContext().put(
322: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
323: axisEndpoint);
324:
325: int total = proxy.addNumbersHandler(10, 10);
326:
327: assertEquals(
328: "With handler manipulation, total should be 4 less than a proper sumation.",
329: 16, total);
330: TestLogger.logger
331: .debug("Total (after handler manipulation) = "
332: + total);
333: TestLogger.logger
334: .debug("----------------------------------");
335: } catch (Exception e) {
336: e.printStackTrace();
337: fail();
338: }
339: }
340:
341: // TODO: disabled until handler support is more complete
342: public void testAddNumbersClientProtoAndLogicalHandler() {
343: try {
344: TestLogger.logger
345: .debug("----------------------------------");
346: TestLogger.logger.debug("test: " + getName());
347:
348: AddNumbersHandlerService service = new AddNumbersHandlerService();
349: AddNumbersHandlerPortType proxy = service
350: .getAddNumbersHandlerPort();
351:
352: BindingProvider p = (BindingProvider) proxy;
353:
354: p.getRequestContext().put(
355: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
356: axisEndpoint);
357:
358: List<Handler> handlers = p.getBinding().getHandlerChain();
359: if (handlers == null)
360: handlers = new ArrayList<Handler>();
361: handlers.add(new AddNumbersClientLogicalHandler());
362: handlers.add(new AddNumbersClientProtocolHandler());
363: p.getBinding().setHandlerChain(handlers);
364:
365: // value 102 triggers an endpoint exception, which will run through the server outbound
366: // handleFault methods, then client inbound handleFault methods
367: int total = proxy.addNumbersHandler(102, 10);
368:
369: fail("should have got an exception, but didn't");
370: } catch (Exception e) {
371: e.printStackTrace();
372: assertTrue("Exception should be SOAPFaultException",
373: e instanceof SOAPFaultException);
374: //AXIS2-2417 - assertEquals(((SOAPFaultException)e).getMessage(), "AddNumbersLogicalHandler2 was here");
375: assertEquals(
376: ((SOAPFaultException) e).getMessage(),
377: "Got value 101. "
378: + "AddNumbersHandlerPortTypeImpl.addNumbersHandler method is "
379: + "correctly throwing this exception as part of testing");
380:
381: }
382: TestLogger.logger.debug("----------------------------------");
383: }
384:
385: public void testAddNumbersClientHandlerWithFault() {
386: try {
387: TestLogger.logger
388: .debug("----------------------------------");
389: TestLogger.logger.debug("test: " + getName());
390:
391: AddNumbersHandlerService service = new AddNumbersHandlerService();
392: AddNumbersHandlerPortType proxy = service
393: .getAddNumbersHandlerPort();
394:
395: BindingProvider p = (BindingProvider) proxy;
396:
397: p.getRequestContext().put(
398: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
399: axisEndpoint);
400:
401: List<Handler> handlers = p.getBinding().getHandlerChain();
402: if (handlers == null)
403: handlers = new ArrayList<Handler>();
404: handlers.add(new AddNumbersClientLogicalHandler4());
405: handlers.add(new AddNumbersClientLogicalHandler3());
406: handlers.add(new AddNumbersClientLogicalHandler());
407:
408: p.getBinding().setHandlerChain(handlers);
409:
410: int total = proxy.addNumbersHandler(99, 10);
411:
412: fail("Should have got an exception, but we didn't.");
413: TestLogger.logger
414: .debug("----------------------------------");
415: } catch (Exception e) {
416: e.printStackTrace();
417: assertTrue("Exception should be SOAPFaultException",
418: e instanceof SOAPFaultException);
419: assertEquals(((SOAPFaultException) e).getMessage(),
420: "I don't like the value 99");
421: }
422: }
423:
424: /**
425: * test results should be the same as testAddNumbersClientHandler, except that
426: * AddNumbersClientLogicalHandler2 doubles the first param on outbound. Async, of course.
427: *
428: */
429: public void testAddNumbersClientHandlerAsync() {
430: try {
431: TestLogger.logger
432: .debug("----------------------------------");
433: TestLogger.logger.debug("test: " + getName());
434:
435: AddNumbersHandlerService service = new AddNumbersHandlerService();
436: AddNumbersHandlerPortType proxy = service
437: .getAddNumbersHandlerPort();
438:
439: BindingProvider p = (BindingProvider) proxy;
440:
441: p.getRequestContext().put(
442: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
443: axisEndpoint);
444:
445: List<Handler> handlers = p.getBinding().getHandlerChain();
446: if (handlers == null)
447: handlers = new ArrayList<Handler>();
448: handlers.add(new AddNumbersClientLogicalHandler());
449: handlers.add(new AddNumbersClientLogicalHandler2());
450: handlers.add(new AddNumbersClientProtocolHandler());
451: p.getBinding().setHandlerChain(handlers);
452:
453: AddNumbersHandlerAsyncCallback callback = new AddNumbersHandlerAsyncCallback();
454: Future<?> future = proxy.addNumbersHandlerAsync(10, 10,
455: callback);
456:
457: while (!future.isDone()) {
458: Thread.sleep(1000);
459: TestLogger.logger.debug("Async invocation incomplete");
460: }
461:
462: int total = callback.getResponseValue();
463:
464: assertEquals(
465: "With handler manipulation, total should be 26.",
466: 26, total);
467: TestLogger.logger
468: .debug("Total (after handler manipulation) = "
469: + total);
470: TestLogger.logger
471: .debug("----------------------------------");
472: } catch (Exception e) {
473: e.printStackTrace();
474: fail(e.toString());
475: }
476: }
477:
478: public void testOneWay() {
479: try {
480: TestLogger.logger
481: .debug("----------------------------------");
482: TestLogger.logger.debug("test: " + getName());
483:
484: AddNumbersHandlerService service = new AddNumbersHandlerService();
485: AddNumbersHandlerPortType proxy = service
486: .getAddNumbersHandlerPort();
487:
488: BindingProvider bp = (BindingProvider) proxy;
489: bp.getRequestContext().put(
490: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
491: axisEndpoint);
492: proxy.oneWayInt(11);
493: TestLogger.logger
494: .debug("----------------------------------");
495: } catch (Exception e) {
496: e.printStackTrace();
497: fail();
498: }
499: }
500:
501: /*
502: * A callback implementation that can be used to collect the exceptions
503: */
504: class AddNumbersHandlerAsyncCallback implements
505: AsyncHandler<AddNumbersHandlerResponse> {
506:
507: private Exception exception;
508: private int retVal;
509:
510: public void handleResponse(
511: Response<AddNumbersHandlerResponse> response) {
512: try {
513: TestLogger.logger
514: .debug("FaultyAsyncHandler.handleResponse() was called");
515: AddNumbersHandlerResponse r = response.get();
516: TestLogger.logger
517: .debug("No exception was thrown from Response.get()");
518: retVal = r.getReturn();
519: } catch (Exception e) {
520: TestLogger.logger.debug("An exception was thrown: "
521: + e.getClass());
522: exception = e;
523: }
524: }
525:
526: public int getResponseValue() {
527: return retVal;
528: }
529:
530: public Exception getException() {
531: return exception;
532: }
533: }
534:
535: class MyHandlerResolver implements HandlerResolver {
536:
537: public List<Handler> getHandlerChain(PortInfo portinfo) {
538: ArrayList<Handler> handlers = new ArrayList<Handler>();
539: handlers.add(new AddNumbersClientLogicalHandler());
540: handlers.add(new AddNumbersClientProtocolHandler());
541: return handlers;
542: }
543:
544: }
545:
546: private String getString(Source source) throws Exception {
547: if (source == null) {
548: return null;
549: }
550: StringWriter writer = new StringWriter();
551: Transformer t = TransformerFactory.newInstance()
552: .newTransformer();
553: Result result = new StreamResult(writer);
554: t.transform(source, result);
555: return writer.getBuffer().toString();
556:
557: }
558:
559: /**
560: * Create a Source request to be used by Dispatch<Source>
561: */
562: private Source createRequestSource() {
563:
564: String reqString = null;
565:
566: String ns = "http://org/test/addnumbershandler";
567: String operation = "addNumbersHandler";
568:
569: reqString = "<" + operation + " xmlns=\"" + ns + "\">"
570: + "<arg0>10</arg0><arg1>10</arg1>" + "</" + operation
571: + ">";
572:
573: return new StreamSource(new StringReader(reqString));
574: }
575:
576: public void testAddNumbersHandlerHandlerResolver() {
577: try {
578: System.out.println("----------------------------------");
579: System.out.println("test: " + getName());
580: AddNumbersHandlerService service = new AddNumbersHandlerService(); // will give NPE:
581: List<Handler> handlers = service.getHandlerResolver()
582: .getHandlerChain(null);
583: assertNotNull(
584: "Default handlers list should not be null but empty.",
585: handlers);
586: System.out.println("----------------------------------");
587: } catch (Exception e) {
588: e.printStackTrace();
589: fail(e.getMessage());
590: }
591: }
592:
593: }
|