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: */package org.apache.cxf.systest.jaxws;
019:
020: import java.io.InputStream;
021: import java.lang.reflect.UndeclaredThrowableException;
022: import java.net.HttpURLConnection;
023: import java.net.URISyntaxException;
024: import java.net.URL;
025: import java.net.URLEncoder;
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.concurrent.ExecutionException;
029: import java.util.concurrent.Executor;
030: import java.util.concurrent.ExecutorService;
031: import java.util.concurrent.Executors;
032: import java.util.concurrent.Future;
033: import java.util.logging.Logger;
034:
035: import javax.xml.namespace.QName;
036: import javax.xml.ws.AsyncHandler;
037: import javax.xml.ws.BindingProvider;
038: import javax.xml.ws.Response;
039: import javax.xml.ws.Service;
040: import javax.xml.ws.WebServiceException;
041: import javax.xml.xpath.XPathConstants;
042:
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Node;
045:
046: //import org.apache.cxf.Bus;
047: import org.apache.cxf.binding.soap.Soap11;
048: import org.apache.cxf.endpoint.Client;
049: import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
050: import org.apache.cxf.helpers.XMLUtils;
051: import org.apache.cxf.helpers.XPathUtils;
052: import org.apache.cxf.message.Message;
053: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
054: import org.apache.hello_world_soap_http.BadRecordLitFault;
055: import org.apache.hello_world_soap_http.DocLitBare;
056: import org.apache.hello_world_soap_http.Greeter;
057: import org.apache.hello_world_soap_http.NoSuchCodeLitFault;
058: import org.apache.hello_world_soap_http.SOAPService;
059: import org.apache.hello_world_soap_http.SOAPServiceBogusAddressTest;
060: import org.apache.hello_world_soap_http.SOAPServiceDocLitBare;
061: import org.apache.hello_world_soap_http.SOAPServiceMultiPortTypeTest;
062: import org.apache.hello_world_soap_http.types.BareDocumentResponse;
063: import org.apache.hello_world_soap_http.types.GreetMeLaterResponse;
064: import org.junit.BeforeClass;
065: import org.junit.Test;
066:
067: public class ClientServerTest extends AbstractBusClientServerTestBase {
068:
069: static final Logger LOG = Logger.getLogger(ClientServerTest.class
070: .getName());
071: private final QName serviceName = new QName(
072: "http://apache.org/hello_world_soap_http", "SOAPService");
073: private final QName portName = new QName(
074: "http://apache.org/hello_world_soap_http", "SoapPort");
075:
076: private final QName fakePortName = new QName(
077: "http://apache.org/hello_world_soap_http", "FackPort");
078:
079: private final QName portName1 = new QName(
080: "http://apache.org/hello_world_soap_http", "SoapPort2");
081:
082: @BeforeClass
083: public static void startServers() throws Exception {
084: // set up configuration to enable schema validation
085: URL url = ClientServerTest.class
086: .getResource("fault-stack-trace.xml");
087: assertNotNull("cannot find test resource", url);
088: defaultConfigFileName = url.toString();
089:
090: assertTrue("server did not launch correctly",
091: launchServer(Server.class));
092: }
093:
094: @Test
095: public void testBasicConnection() throws Exception {
096:
097: SOAPService service = new SOAPService();
098: assertNotNull(service);
099:
100: Greeter greeter = service.getPort(portName, Greeter.class);
101:
102: try {
103: greeter.greetMe("test");
104:
105: String reply = greeter.sayHi();
106: assertNotNull("no response received from service", reply);
107: assertEquals("Bonjour", reply);
108: } catch (UndeclaredThrowableException ex) {
109: throw (Exception) ex.getCause();
110: }
111: BindingProvider bp = (BindingProvider) greeter;
112: Map<String, Object> responseContext = bp.getResponseContext();
113: Integer responseCode = (Integer) responseContext
114: .get(Message.RESPONSE_CODE);
115: assertEquals(200, responseCode.intValue());
116: }
117:
118: @Test
119: public void testNillable() throws Exception {
120: SOAPService service = new SOAPService();
121: assertNotNull(service);
122:
123: Greeter greeter = service.getPort(portName, Greeter.class);
124:
125: try {
126: String reply = greeter.testNillable("test", 100);
127: assertEquals("test", reply);
128: reply = greeter.testNillable(null, 100);
129: assertNull(reply);
130: } catch (UndeclaredThrowableException ex) {
131: throw (Exception) ex.getCause();
132: }
133:
134: }
135:
136: @Test
137: public void testAddPortWithSpecifiedSoap12Binding()
138: throws Exception {
139: Service service = Service.create(serviceName);
140: service.addPort(fakePortName,
141: javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING,
142: "http://localhost:9009/SoapContext/SoapPort");
143: Greeter greeter = service.getPort(fakePortName, Greeter.class);
144:
145: String response = new String("Bonjour");
146: try {
147: greeter.greetMe("test");
148: String reply = greeter.sayHi();
149: assertNotNull("no response received from service", reply);
150: assertEquals(response, reply);
151: } catch (UndeclaredThrowableException ex) {
152: throw (Exception) ex.getCause();
153: }
154:
155: }
156:
157: @Test
158: public void testAddPortWithSpecifiedSoap11Binding()
159: throws Exception {
160: Service service = Service.create(serviceName);
161: service.addPort(fakePortName,
162: javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING,
163: "http://localhost:9000/SoapContext/SoapPort");
164: Greeter greeter = service.getPort(fakePortName, Greeter.class);
165:
166: String response = new String("Bonjour");
167: try {
168: greeter.greetMe("test");
169: String reply = greeter.sayHi();
170: assertNotNull("no response received from service", reply);
171: assertEquals(response, reply);
172: } catch (UndeclaredThrowableException ex) {
173: throw (Exception) ex.getCause();
174: }
175:
176: }
177:
178: @Test
179: public void testAddPort() throws Exception {
180: Service service = Service.create(serviceName);
181: service.addPort(fakePortName,
182: "http://schemas.xmlsoap.org/soap/",
183: "http://localhost:9000/SoapContext/SoapPort");
184: Greeter greeter = service.getPort(fakePortName, Greeter.class);
185:
186: String response = new String("Bonjour");
187: try {
188: greeter.greetMe("test");
189: String reply = greeter.sayHi();
190: assertNotNull("no response received from service", reply);
191: assertEquals(response, reply);
192: } catch (UndeclaredThrowableException ex) {
193: throw (Exception) ex.getCause();
194: }
195: }
196:
197: @Test
198: public void testGetPortOneParam() throws Exception {
199:
200: URL url = getClass().getResource("/wsdl/hello_world.wsdl");
201: Service service = Service.create(url, serviceName);
202:
203: Greeter greeter = service.getPort(Greeter.class);
204: String response = new String("Bonjour");
205:
206: try {
207: greeter.greetMe("test");
208: String reply = greeter.sayHi();
209: assertNotNull("no response received from service", reply);
210: assertEquals(response, reply);
211: } catch (UndeclaredThrowableException ex) {
212: throw (Exception) ex.getCause();
213: }
214: }
215:
216: @Test
217: public void testDocLitBareConnection() throws Exception {
218:
219: SOAPServiceDocLitBare service = new SOAPServiceDocLitBare();
220: assertNotNull(service);
221:
222: DocLitBare greeter = service.getPort(portName1,
223: DocLitBare.class);
224: try {
225:
226: BareDocumentResponse bareres = greeter
227: .testDocLitBare("MySimpleDocument");
228: assertNotNull("no response for operation testDocLitBare",
229: bareres);
230: assertEquals("CXF", bareres.getCompany());
231: assertTrue(bareres.getId() == 1);
232: } catch (UndeclaredThrowableException ex) {
233: throw (Exception) ex.getCause();
234: }
235: }
236:
237: @Test
238: public void testBasicConnectionAndOneway() throws Exception {
239: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
240: assertNotNull(wsdl);
241:
242: SOAPService service = new SOAPService(wsdl, serviceName);
243: assertNotNull(service);
244:
245: Greeter greeter = service.getPort(portName, Greeter.class);
246:
247: String response1 = new String("Hello Milestone-");
248: String response2 = new String("Bonjour");
249: try {
250: for (int idx = 0; idx < 1; idx++) {
251: String greeting = greeter.greetMe("Milestone-" + idx);
252: assertNotNull("no response received from service",
253: greeting);
254: String exResponse = response1 + idx;
255: assertEquals(exResponse, greeting);
256:
257: String reply = greeter.sayHi();
258: assertNotNull("no response received from service",
259: reply);
260: assertEquals(response2, reply);
261:
262: greeter.greetMeOneWay("Milestone-" + idx);
263:
264: }
265: } catch (UndeclaredThrowableException ex) {
266: throw (Exception) ex.getCause();
267: }
268: }
269:
270: @Test
271: public void testBasicConnection2() throws Exception {
272: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
273: assertNotNull(wsdl);
274:
275: SOAPService service = new SOAPService(wsdl, serviceName);
276: assertNotNull(service);
277:
278: //getPort only passing in SEI
279: Greeter greeter = service.getPort(Greeter.class);
280:
281: String response1 = new String("Hello Milestone-");
282: String response2 = new String("Bonjour");
283: try {
284: for (int idx = 0; idx < 5; idx++) {
285: String greeting = greeter.greetMe("Milestone-" + idx);
286: assertNotNull("no response received from service",
287: greeting);
288: String exResponse = response1 + idx;
289: assertEquals(exResponse, greeting);
290:
291: String reply = greeter.sayHi();
292: assertNotNull("no response received from service",
293: reply);
294: assertEquals(response2, reply);
295:
296: greeter.greetMeOneWay("Milestone-" + idx);
297:
298: }
299: } catch (UndeclaredThrowableException ex) {
300: throw (Exception) ex.getCause();
301: }
302: }
303:
304: @Test
305: public void testAsyncPollingCall() throws Exception {
306: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
307: assertNotNull(wsdl);
308:
309: SOAPService service = new SOAPService(wsdl, serviceName);
310:
311: assertNotNull(service);
312:
313: Greeter greeter = service.getPort(portName, Greeter.class);
314:
315: assertNotNull(service);
316:
317: long before = System.currentTimeMillis();
318:
319: long delay = 3000;
320: Response<GreetMeLaterResponse> r1 = greeter
321: .greetMeLaterAsync(delay);
322: Response<GreetMeLaterResponse> r2 = greeter
323: .greetMeLaterAsync(delay);
324:
325: long after = System.currentTimeMillis();
326:
327: assertTrue("Duration of calls exceeded " + (2 * delay) + " ms",
328: after - before < (2 * delay));
329:
330: // first time round, responses should not be available yet
331: assertFalse("Response already available.", r1.isDone());
332: assertFalse("Response already available.", r2.isDone());
333:
334: // after three seconds responses should be available
335: long waited = 0;
336: while (waited < (delay + 1000)) {
337: try {
338: Thread.sleep(500);
339: } catch (InterruptedException ex) {
340: // ignore
341: }
342: if (r1.isDone() && r2.isDone()) {
343: break;
344: }
345: waited += 500;
346: }
347: assertTrue("Response is not available.", r1.isDone());
348: assertTrue("Response is not available.", r2.isDone());
349: }
350:
351: @Test
352: public void testAsyncSynchronousPolling() throws Exception {
353: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
354: assertNotNull(wsdl);
355:
356: SOAPService service = new SOAPService(wsdl, serviceName);
357: assertNotNull(service);
358:
359: final String expectedString = new String("Hello, finally!");
360:
361: class Poller extends Thread {
362: Response<GreetMeLaterResponse> response;
363: int tid;
364:
365: Poller(Response<GreetMeLaterResponse> r, int t) {
366: response = r;
367: tid = t;
368: }
369:
370: public void run() {
371: if (tid % 2 > 0) {
372: while (!response.isDone()) {
373: try {
374: Thread.sleep(100);
375: } catch (InterruptedException ex) {
376: // ignore
377: }
378: }
379: }
380: GreetMeLaterResponse reply = null;
381: try {
382: reply = response.get();
383: } catch (Exception ex) {
384: fail("Poller " + tid + " failed with " + ex);
385: }
386: assertNotNull("Poller " + tid
387: + ": no response received from service", reply);
388: String s = reply.getResponseType();
389: assertEquals(expectedString, s);
390: }
391: }
392:
393: Greeter greeter = (Greeter) service.getPort(portName,
394: Greeter.class);
395: long before = System.currentTimeMillis();
396:
397: long delay = 3000;
398:
399: Response<GreetMeLaterResponse> response = greeter
400: .greetMeLaterAsync(delay);
401: long after = System.currentTimeMillis();
402:
403: assertTrue("Duration of calls exceeded " + delay + " ms", after
404: - before < delay);
405:
406: // first time round, responses should not be available yet
407: assertFalse("Response already available.", response.isDone());
408:
409: Poller[] pollers = new Poller[4];
410: for (int i = 0; i < pollers.length; i++) {
411: pollers[i] = new Poller(response, i);
412: }
413: for (Poller p : pollers) {
414: p.start();
415: }
416:
417: for (Poller p : pollers) {
418: p.join();
419: }
420:
421: }
422:
423: static class MyHandler implements
424: AsyncHandler<GreetMeLaterResponse> {
425: static int invocationCount;
426: private String replyBuffer;
427:
428: public void handleResponse(
429: Response<GreetMeLaterResponse> response) {
430: invocationCount++;
431: try {
432: GreetMeLaterResponse reply = response.get();
433: replyBuffer = reply.getResponseType();
434: } catch (InterruptedException ex) {
435: ex.printStackTrace();
436: } catch (ExecutionException ex) {
437: ex.printStackTrace();
438: }
439: }
440:
441: String getReplyBuffer() {
442: return replyBuffer;
443: }
444: }
445:
446: @Test
447: public void testAsyncCallUseProperAssignedExecutor()
448: throws Exception {
449: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
450: assertNotNull(wsdl);
451:
452: SOAPService service = new SOAPService(wsdl, serviceName);
453:
454: class TestExecutor implements Executor {
455:
456: private int count;
457:
458: public void execute(Runnable command) {
459: count++;
460: LOG.info("asyn call time " + count);
461: command.run();
462: }
463:
464: public int getCount() {
465: return count;
466: }
467: }
468: Executor executor = new TestExecutor();
469: service.setExecutor(executor);
470: assertNotNull(service);
471: assertSame(executor, service.getExecutor());
472:
473: assertEquals(((TestExecutor) executor).getCount(), 0);
474: try {
475: Greeter greeter = (Greeter) service.getPort(portName,
476: Greeter.class);
477: for (int i = 0; i < 5; i++) {
478: greeter.greetMeAsync("asyn call" + i);
479: }
480: } catch (UndeclaredThrowableException ex) {
481: throw (Exception) ex.getCause();
482: }
483:
484: assertEquals(((TestExecutor) executor).getCount(), 5);
485: }
486:
487: @Test
488: public void testAsyncCallWithHandler() throws Exception {
489: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
490: assertNotNull(wsdl);
491:
492: SOAPService service = new SOAPService(wsdl, serviceName);
493: assertNotNull(service);
494:
495: MyHandler h = new MyHandler();
496: MyHandler.invocationCount = 0;
497:
498: String expectedString = new String("Hello, finally!");
499: try {
500: Greeter greeter = (Greeter) service.getPort(portName,
501: Greeter.class);
502: long before = System.currentTimeMillis();
503: long delay = 3000;
504: Future<?> f = greeter.greetMeLaterAsync(delay, h);
505: long after = System.currentTimeMillis();
506: assertTrue("Duration of calls exceeded " + delay + " ms",
507: after - before < delay);
508: // first time round, responses should not be available yet
509: assertFalse("Response already available.", f.isDone());
510:
511: int i = 0;
512: while (!f.isDone() && i < 50) {
513: Thread.sleep(100);
514: i++;
515: }
516: assertEquals(
517: "callback was not executed or did not return the expected result",
518: expectedString, h.getReplyBuffer());
519: } catch (UndeclaredThrowableException ex) {
520: throw (Exception) ex.getCause();
521: }
522: assertEquals(1, MyHandler.invocationCount);
523:
524: }
525:
526: @Test
527: public void testAsyncCallWithHandlerAndMultipleClients()
528: throws Exception {
529: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
530: assertNotNull(wsdl);
531:
532: SOAPService service = new SOAPService(wsdl, serviceName);
533:
534: assertNotNull(service);
535:
536: final MyHandler h = new MyHandler();
537: MyHandler.invocationCount = 0;
538:
539: final String expectedString = new String("Hello, finally!");
540:
541: class Poller extends Thread {
542: Future<?> future;
543: int tid;
544:
545: Poller(Future<?> f, int t) {
546: future = f;
547: tid = t;
548: }
549:
550: public void run() {
551: if (tid % 2 > 0) {
552: while (!future.isDone()) {
553: try {
554: Thread.sleep(100);
555: } catch (InterruptedException ex) {
556: // ignore
557: }
558: }
559: }
560: try {
561: future.get();
562: } catch (Exception ex) {
563: fail("Poller " + tid + " failed with " + ex);
564: }
565: assertEquals(
566: "callback was not executed or did not return the expected result",
567: expectedString, h.getReplyBuffer());
568: }
569: }
570:
571: Greeter greeter = (Greeter) service.getPort(portName,
572: Greeter.class);
573: long before = System.currentTimeMillis();
574: long delay = 3000;
575: Future<?> f = greeter.greetMeLaterAsync(delay, h);
576: long after = System.currentTimeMillis();
577: assertTrue("Duration of calls exceeded " + delay + " ms", after
578: - before < delay);
579: // first time round, responses should not be available yet
580: assertFalse("Response already available.", f.isDone());
581:
582: Poller[] pollers = new Poller[4];
583: for (int i = 0; i < pollers.length; i++) {
584: pollers[i] = new Poller(f, i);
585: }
586: for (Poller p : pollers) {
587: p.start();
588: }
589:
590: for (Poller p : pollers) {
591: p.join();
592: }
593: assertEquals(1, MyHandler.invocationCount);
594: }
595:
596: @Test
597: public void testFaults() throws Exception {
598: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
599: assertNotNull(wsdl);
600:
601: SOAPService service = new SOAPService(wsdl, serviceName);
602: ExecutorService ex = Executors.newFixedThreadPool(1);
603: service.setExecutor(ex);
604: assertNotNull(service);
605:
606: String noSuchCodeFault = "NoSuchCodeLitFault";
607: String badRecordFault = "BadRecordLitFault";
608:
609: Greeter greeter = service.getPort(portName, Greeter.class);
610: for (int idx = 0; idx < 2; idx++) {
611: try {
612: greeter.testDocLitFault(noSuchCodeFault);
613: fail("Should have thrown NoSuchCodeLitFault exception");
614: } catch (NoSuchCodeLitFault nslf) {
615: assertNotNull(nslf.getFaultInfo());
616: assertNotNull(nslf.getFaultInfo().getCode());
617: }
618:
619: try {
620: greeter.testDocLitFault(badRecordFault);
621: fail("Should have thrown BadRecordLitFault exception");
622: } catch (BadRecordLitFault brlf) {
623: BindingProvider bp = (BindingProvider) greeter;
624: Map<String, Object> responseContext = bp
625: .getResponseContext();
626: String contentType = (String) responseContext
627: .get(Message.CONTENT_TYPE);
628: assertEquals("text/xml; charset=utf-8", contentType);
629: Integer responseCode = (Integer) responseContext
630: .get(Message.RESPONSE_CODE);
631: assertEquals(500, responseCode.intValue());
632: assertNotNull(brlf.getFaultInfo());
633: assertEquals("BadRecordLitFault", brlf.getFaultInfo());
634: }
635:
636: }
637:
638: }
639:
640: @Test
641: public void testFaultStackTrace() throws Exception {
642: System.setProperty("cxf.config.file.url", getClass()
643: .getResource("fault-stack-trace.xml").toString());
644: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
645: assertNotNull(wsdl);
646: SOAPService service = new SOAPService(wsdl, serviceName);
647: ExecutorService ex = Executors.newFixedThreadPool(1);
648: service.setExecutor(ex);
649: assertNotNull(service);
650: Greeter greeter = service.getPort(portName, Greeter.class);
651: try {
652: // trigger runtime exception throw of implementor method
653: greeter.testDocLitFault("");
654: fail("Should have thrown Runtime exception");
655: } catch (WebServiceException e) {
656: assertEquals("can't get back original message",
657: "Unknown source", e.getCause().getMessage());
658: assertTrue(e.getCause().getStackTrace().length > 0);
659: }
660: }
661:
662: @Test
663: public void testGetSayHi() throws Exception {
664: HttpURLConnection httpConnection = getHttpConnection("http://localhost:9000/SoapContext/SoapPort/sayHi");
665: httpConnection.connect();
666:
667: httpConnection.connect();
668:
669: assertEquals(200, httpConnection.getResponseCode());
670:
671: assertEquals("text/xml; charset=utf-8", httpConnection
672: .getContentType());
673: assertEquals("OK", httpConnection.getResponseMessage());
674:
675: InputStream in = httpConnection.getInputStream();
676: assertNotNull(in);
677:
678: Document doc = XMLUtils.parse(in);
679: assertNotNull(doc);
680:
681: Map<String, String> ns = new HashMap<String, String>();
682: ns.put("soap", Soap11.SOAP_NAMESPACE);
683: ns.put("ns2", "http://apache.org/hello_world_soap_http/types");
684: XPathUtils xu = new XPathUtils(ns);
685: Node body = (Node) xu.getValue("/soap:Envelope/soap:Body", doc,
686: XPathConstants.NODE);
687: assertNotNull(body);
688: String response = (String) xu.getValue(
689: "//ns2:sayHiResponse/ns2:responseType/text()", body,
690: XPathConstants.STRING);
691: assertEquals("Bonjour", response);
692: }
693:
694: @Test
695: public void testGetGreetMe() throws Exception {
696: HttpURLConnection httpConnection = getHttpConnection("http://localhost:9000/SoapContext/SoapPort/greetMe/requestType/cxf");
697: httpConnection.connect();
698:
699: assertEquals(200, httpConnection.getResponseCode());
700:
701: assertEquals("text/xml; charset=utf-8", httpConnection
702: .getContentType());
703: assertEquals("OK", httpConnection.getResponseMessage());
704:
705: InputStream in = httpConnection.getInputStream();
706: assertNotNull(in);
707:
708: Document doc = XMLUtils.parse(in);
709: assertNotNull(doc);
710:
711: Map<String, String> ns = new HashMap<String, String>();
712: ns.put("soap", Soap11.SOAP_NAMESPACE);
713: ns.put("ns2", "http://apache.org/hello_world_soap_http/types");
714: XPathUtils xu = new XPathUtils(ns);
715: Node body = (Node) xu.getValue("/soap:Envelope/soap:Body", doc,
716: XPathConstants.NODE);
717: assertNotNull(body);
718: String response = (String) xu.getValue(
719: "//ns2:greetMeResponse/ns2:responseType/text()", body,
720: XPathConstants.STRING);
721: assertEquals("Hello cxf", response);
722: }
723:
724: @Test
725: public void testGetWSDL() throws Exception {
726: String url = "http://localhost:9000/SoapContext/SoapPort?wsdl";
727: HttpURLConnection httpConnection = getHttpConnection(url);
728: httpConnection.connect();
729:
730: assertEquals(200, httpConnection.getResponseCode());
731:
732: assertEquals("text/xml", httpConnection.getContentType());
733: assertEquals("OK", httpConnection.getResponseMessage());
734:
735: InputStream in = httpConnection.getInputStream();
736: assertNotNull(in);
737:
738: Document doc = XMLUtils.parse(in);
739: assertNotNull(doc);
740:
741: }
742:
743: @Test
744: public void testGetGreetMeFromQuery() throws Exception {
745: String url = "http://localhost:9000/SoapContext/SoapPort/greetMe?requestType="
746: + URLEncoder.encode("cxf (was CeltixFire)", "UTF-8");
747:
748: HttpURLConnection httpConnection = getHttpConnection(url);
749: httpConnection.connect();
750:
751: assertEquals(200, httpConnection.getResponseCode());
752:
753: assertEquals("text/xml; charset=utf-8", httpConnection
754: .getContentType());
755: assertEquals("OK", httpConnection.getResponseMessage());
756:
757: InputStream in = httpConnection.getInputStream();
758: assertNotNull(in);
759:
760: Document doc = XMLUtils.parse(in);
761: assertNotNull(doc);
762:
763: Map<String, String> ns = new HashMap<String, String>();
764: ns.put("soap", Soap11.SOAP_NAMESPACE);
765: ns.put("ns2", "http://apache.org/hello_world_soap_http/types");
766: XPathUtils xu = new XPathUtils(ns);
767: Node body = (Node) xu.getValue("/soap:Envelope/soap:Body", doc,
768: XPathConstants.NODE);
769: assertNotNull(body);
770: String response = (String) xu.getValue(
771: "//ns2:greetMeResponse/ns2:responseType/text()", body,
772: XPathConstants.STRING);
773: assertEquals("Hello cxf (was CeltixFire)", response);
774: }
775:
776: @Test
777: public void testBasicAuth() throws Exception {
778: Service service = Service.create(serviceName);
779: service.addPort(fakePortName,
780: "http://schemas.xmlsoap.org/soap/",
781: "http://localhost:9000/SoapContext/SoapPort");
782: Greeter greeter = service.getPort(fakePortName, Greeter.class);
783:
784: try {
785: BindingProvider bp = (BindingProvider) greeter;
786: bp.getRequestContext().put(
787: BindingProvider.USERNAME_PROPERTY, "BJ");
788: bp.getRequestContext().put(
789: BindingProvider.PASSWORD_PROPERTY, "pswd");
790: String s = greeter.greetMe("secure");
791: assertEquals("Hello BJ", s);
792: } catch (UndeclaredThrowableException ex) {
793: throw (Exception) ex.getCause();
794: }
795: }
796:
797: @Test
798: public void testBogusAddress() throws Exception {
799: String realAddress = "http://localhost:9015/SoapContext/SoapPort";
800: SOAPServiceBogusAddressTest service = new SOAPServiceBogusAddressTest();
801: Greeter greeter = service.getSoapPort();
802: BindingProvider bp = (BindingProvider) greeter;
803: bp.getRequestContext().put(
804: BindingProvider.ENDPOINT_ADDRESS_PROPERTY, realAddress);
805: greeter.greetMe("test");
806:
807: //should persist
808: greeter.greetMe("test");
809:
810: bp.getRequestContext().remove(
811: BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
812:
813: try {
814: greeter.greetMe("test");
815: fail("Should fail");
816: } catch (WebServiceException f) {
817: // expected
818: }
819:
820: bp.getRequestContext().put(
821: BindingProvider.ENDPOINT_ADDRESS_PROPERTY, realAddress);
822: String reply = greeter.sayHi();
823: assertNotNull("no response received from service", reply);
824: assertEquals("Bonjour", reply);
825:
826: }
827:
828: @Test
829: public void testDynamicClientFactory() {
830: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
831: assertNotNull(wsdl);
832: String wsdlUrl = null;
833: try {
834: wsdlUrl = wsdl.toURI().toString();
835: } catch (URISyntaxException e) {
836: e.printStackTrace();
837: fail("Can't get the hello_world.wsdl url");
838: }
839: try {
840: //TODO test fault exceptions
841: DynamicClientFactory dcf = DynamicClientFactory
842: .newInstance();
843: Client client = dcf.createClient(wsdlUrl, serviceName,
844: portName);
845: client.invoke("greetMe", "test");
846: Object[] result = client.invoke("sayHi");
847: assertNotNull("no response received from service", result);
848: System.out.println(result[0]);
849: assertEquals("Bonjour", result[0]);
850: } catch (Exception e) {
851: e.printStackTrace();
852: fail("There is some excpetion happened ");
853: }
854: }
855:
856: @Test
857: public void testMultiPorts() throws Exception {
858: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
859: assertNotNull(wsdl);
860: QName sname = new QName(
861: "http://apache.org/hello_world_soap_http",
862: "SOAPServiceMultiPortTypeTest");
863: SOAPServiceMultiPortTypeTest service = new SOAPServiceMultiPortTypeTest(
864: wsdl, sname);
865:
866: BareDocumentResponse resp = service.getDocLitBarePort()
867: .testDocLitBare("CXF");
868: assertNotNull(resp);
869: assertEquals("CXF", resp.getCompany());
870:
871: String result = service.getGreeterPort().greetMe("CXF");
872: assertEquals("Hello CXF", result);
873: }
874:
875: }
|