001: package org.objectweb.celtix.systest.basic;
002:
003: import java.lang.reflect.UndeclaredThrowableException;
004: import java.net.URL;
005: import java.util.concurrent.ExecutionException;
006: import java.util.concurrent.ExecutorService;
007: import java.util.concurrent.Executors;
008: import java.util.concurrent.Future;
009:
010: import javax.xml.namespace.QName; //import javax.xml.ws.AsyncHandler;
011: import javax.xml.ws.AsyncHandler;
012: import javax.xml.ws.Response;
013:
014: import junit.framework.Test;
015: import junit.framework.TestSuite;
016:
017: import org.objectweb.celtix.systest.common.ClientServerSetupBase;
018: import org.objectweb.celtix.systest.common.ClientServerTestBase;
019: import org.objectweb.hello_world_soap_http.BadRecordLitFault;
020: import org.objectweb.hello_world_soap_http.Greeter;
021: import org.objectweb.hello_world_soap_http.NoSuchCodeLitFault;
022: import org.objectweb.hello_world_soap_http.SOAPService;
023: import org.objectweb.hello_world_soap_http.types.BareDocumentResponse;
024: import org.objectweb.hello_world_soap_http.types.GreetMeSometimeResponse;
025:
026: public class ClientServerTest extends ClientServerTestBase {
027:
028: private final QName serviceName = new QName(
029: "http://objectweb.org/hello_world_soap_http", "SOAPService");
030: private final QName portName = new QName(
031: "http://objectweb.org/hello_world_soap_http", "SoapPort");
032:
033: public static Test suite() throws Exception {
034: TestSuite suite = new TestSuite(ClientServerTest.class);
035: return new ClientServerSetupBase(suite) {
036: public void startServers() throws Exception {
037: assertTrue("server did not launch correctly",
038: launchServer(Server.class));
039: }
040: };
041: }
042:
043: public void testBasicConnection() throws Exception {
044: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
045: assertNotNull(wsdl);
046:
047: SOAPService service = new SOAPService(wsdl, serviceName);
048: assertNotNull(service);
049:
050: Greeter greeter = service.getPort(portName, Greeter.class);
051:
052: String response1 = new String("Hello Milestone-");
053: String response2 = new String("Bonjour");
054: try {
055: for (int idx = 0; idx < 5; idx++) {
056: String greeting = greeter.greetMe("Milestone-" + idx);
057: assertNotNull("no response received from service",
058: greeting);
059: String exResponse = response1 + idx;
060: assertEquals(exResponse, greeting);
061:
062: String reply = greeter.sayHi();
063: assertNotNull("no response received from service",
064: reply);
065: assertEquals(response2, reply);
066:
067: greeter.greetMeOneWay("Milestone-" + idx);
068:
069: BareDocumentResponse bareres = greeter
070: .testDocLitBare("MySimpleDocument");
071: assertNotNull(
072: "no response for operation testDocLitBare",
073: bareres);
074: assertEquals("Celtix", bareres.getCompany());
075: assertTrue(bareres.getId() == 1);
076:
077: }
078: } catch (UndeclaredThrowableException ex) {
079: throw (Exception) ex.getCause();
080: }
081: }
082:
083: public void testAsyncPollingCall() throws Exception {
084: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
085: assertNotNull(wsdl);
086:
087: SOAPService service = new SOAPService(wsdl, serviceName);
088: assertNotNull(service);
089: ExecutorService executor = Executors.newFixedThreadPool(5);
090: service.setExecutor(executor);
091: assertNotNull(service);
092:
093: String expectedString = new String("How are you Joe");
094: try {
095: Greeter greeter = (Greeter) service.getPort(portName,
096: Greeter.class);
097:
098: Response<GreetMeSometimeResponse> response = greeter
099: .greetMeSometimeAsync("Joe");
100: while (!response.isDone()) {
101: Thread.sleep(100);
102: }
103: GreetMeSometimeResponse reply = response.get();
104: assertNotNull("no response received from service", reply);
105: String s = reply.getResponseType();
106: assertEquals(expectedString, s);
107: } catch (UndeclaredThrowableException ex) {
108: throw (Exception) ex.getCause();
109: }
110: executor.shutdown();
111: }
112:
113: public void testAsyncSynchronousPolling() throws Exception {
114: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
115: assertNotNull(wsdl);
116:
117: SOAPService service = new SOAPService(wsdl, serviceName);
118: assertNotNull(service);
119: ExecutorService executor = Executors.newFixedThreadPool(5);
120: service.setExecutor(executor);
121: assertNotNull(service);
122:
123: final String expectedString = new String("How are you Joe");
124:
125: class Poller extends Thread {
126: Response<GreetMeSometimeResponse> response;
127: int tid;
128:
129: Poller(Response<GreetMeSometimeResponse> r, int t) {
130: response = r;
131: tid = t;
132: }
133:
134: public void run() {
135: if (tid % 2 > 0) {
136: while (!response.isDone()) {
137: try {
138: Thread.sleep(100);
139: } catch (InterruptedException ex) {
140: // ignore
141: }
142: }
143: }
144: GreetMeSometimeResponse reply = null;
145: try {
146: reply = response.get();
147: } catch (Exception ex) {
148: fail("Poller " + tid + " failed with " + ex);
149: }
150: assertNotNull("Poller " + tid
151: + ": no response received from service", reply);
152: String s = reply.getResponseType();
153: assertEquals(expectedString, s);
154: }
155: }
156:
157: Greeter greeter = (Greeter) service.getPort(portName,
158: Greeter.class);
159: Response<GreetMeSometimeResponse> response = greeter
160: .greetMeSometimeAsync("Joe");
161:
162: Poller[] pollers = new Poller[4];
163: for (int i = 0; i < pollers.length; i++) {
164: pollers[i] = new Poller(response, i);
165: }
166: for (Poller p : pollers) {
167: p.start();
168: }
169:
170: for (Poller p : pollers) {
171: p.join();
172: }
173:
174: executor.shutdown();
175: }
176:
177: static class MyHandler implements
178: AsyncHandler<GreetMeSometimeResponse> {
179: static int invocationCount;
180: private String replyBuffer;
181:
182: public void handleResponse(
183: Response<GreetMeSometimeResponse> response) {
184: invocationCount++;
185: try {
186: GreetMeSometimeResponse reply = response.get();
187: replyBuffer = reply.getResponseType();
188: } catch (InterruptedException ex) {
189: ex.printStackTrace();
190: } catch (ExecutionException ex) {
191: ex.printStackTrace();
192: }
193: }
194:
195: String getReplyBuffer() {
196: return replyBuffer;
197: }
198: }
199:
200: public void testAsyncCallWithHandler() throws Exception {
201: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
202: assertNotNull(wsdl);
203:
204: SOAPService service = new SOAPService(wsdl, serviceName);
205: ExecutorService executor = Executors.newFixedThreadPool(5);
206: service.setExecutor(executor);
207: assertNotNull(service);
208:
209: MyHandler h = new MyHandler();
210: MyHandler.invocationCount = 0;
211:
212: String expectedString = new String("How are you Joe");
213: try {
214: Greeter greeter = (Greeter) service.getPort(portName,
215: Greeter.class);
216: Future<?> f = greeter.greetMeSometimeAsync("Joe", h);
217: int i = 0;
218: while (!f.isDone() && i < 20) {
219: Thread.sleep(100);
220: i++;
221: }
222: assertEquals(
223: "callback was not executed or did not return the expected result",
224: expectedString, h.getReplyBuffer());
225: } catch (UndeclaredThrowableException ex) {
226: throw (Exception) ex.getCause();
227: }
228: assertEquals(1, MyHandler.invocationCount);
229: executor.shutdown();
230: }
231:
232: public void testAsyncCallWithHandlerAndMultipleClients()
233: throws Exception {
234: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
235: assertNotNull(wsdl);
236:
237: SOAPService service = new SOAPService(wsdl, serviceName);
238: ExecutorService executor = Executors.newFixedThreadPool(5);
239: service.setExecutor(executor);
240: assertNotNull(service);
241:
242: final MyHandler h = new MyHandler();
243: MyHandler.invocationCount = 0;
244:
245: final String expectedString = new String("How are you Joe");
246:
247: class Poller extends Thread {
248: Future<?> future;
249: int tid;
250:
251: Poller(Future<?> f, int t) {
252: future = f;
253: tid = t;
254: }
255:
256: public void run() {
257: if (tid % 2 > 0) {
258: while (!future.isDone()) {
259: try {
260: Thread.sleep(100);
261: } catch (InterruptedException ex) {
262: // ignore
263: }
264: }
265: }
266: try {
267: future.get();
268: } catch (Exception ex) {
269: fail("Poller " + tid + " failed with " + ex);
270: }
271: assertEquals(
272: "callback was not executed or did not return the expected result",
273: expectedString, h.getReplyBuffer());
274: }
275: }
276:
277: Greeter greeter = (Greeter) service.getPort(portName,
278: Greeter.class);
279: Future<?> f = greeter.greetMeSometimeAsync("Joe", h);
280:
281: Poller[] pollers = new Poller[4];
282: for (int i = 0; i < pollers.length; i++) {
283: pollers[i] = new Poller(f, i);
284: }
285: for (Poller p : pollers) {
286: p.start();
287: }
288:
289: for (Poller p : pollers) {
290: p.join();
291: }
292: assertEquals(1, MyHandler.invocationCount);
293: executor.shutdown();
294: }
295:
296: public void testFaults() throws Exception {
297: URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
298: assertNotNull(wsdl);
299:
300: SOAPService service = new SOAPService(wsdl, serviceName);
301: ExecutorService ex = Executors.newFixedThreadPool(1);
302: service.setExecutor(ex);
303: assertNotNull(service);
304:
305: String noSuchCodeFault = "NoSuchCodeLitFault";
306: String badRecordFault = "BadRecordLitFault";
307:
308: Greeter greeter = service.getPort(portName, Greeter.class);
309: for (int idx = 0; idx < 2; idx++) {
310: try {
311: greeter.testDocLitFault(noSuchCodeFault);
312: fail("Should have thrown NoSuchCodeLitFault exception");
313: } catch (NoSuchCodeLitFault nslf) {
314: assertNotNull(nslf.getFaultInfo());
315: assertNotNull(nslf.getFaultInfo().getCode());
316: }
317:
318: try {
319: greeter.testDocLitFault(badRecordFault);
320: fail("Should have thrown BadRecordLitFault exception");
321: } catch (BadRecordLitFault brlf) {
322: assertNotNull(brlf.getFaultInfo());
323: }
324: }
325:
326: }
327:
328: public static void main(String[] args) {
329: junit.textui.TestRunner.run(ClientServerTest.class);
330: }
331:
332: /*
333:
334: public static void main(String[] args) {
335: ClientServerTest cst = new ClientServerTest();
336:
337: if ("client".equals(args[0])) {
338: try {
339: cst.testAsyncPollingCall();
340: } catch (Exception ex) {
341: ex.printStackTrace();
342: }
343: System.err.println("Exiting...........");
344: System.exit(0);
345: } else if ("server".equals(args[0])) {
346: try {
347: // cst.setUp();
348: cst.onetimeSetUp();
349: } catch (Exception ex) {
350: ex.printStackTrace();
351: }
352: } else {
353: System.out.println("Invaid arg");
354: }
355:
356: }
357:
358: */
359:
360: }
|