1 package org.objectweb.celtix.systest.basic; 2 3 import java.lang.reflect.UndeclaredThrowableException ; 4 import java.net.URL ; 5 import java.util.concurrent.ExecutionException ; 6 import java.util.concurrent.ExecutorService ; 7 import java.util.concurrent.Executors ; 8 import java.util.concurrent.Future ; 9 10 import javax.xml.namespace.QName ; 11 import javax.xml.ws.AsyncHandler; 13 import javax.xml.ws.Response; 14 15 import junit.framework.Test; 16 import junit.framework.TestSuite; 17 18 import org.objectweb.celtix.systest.common.ClientServerSetupBase; 19 import org.objectweb.celtix.systest.common.ClientServerTestBase; 20 import org.objectweb.hello_world_soap_http.BadRecordLitFault; 21 import org.objectweb.hello_world_soap_http.Greeter; 22 import org.objectweb.hello_world_soap_http.NoSuchCodeLitFault; 23 import org.objectweb.hello_world_soap_http.SOAPService; 24 import org.objectweb.hello_world_soap_http.types.BareDocumentResponse; 25 import org.objectweb.hello_world_soap_http.types.GreetMeSometimeResponse; 26 27 public class ClientServerTest extends ClientServerTestBase { 28 29 private final QName serviceName = new QName ("http://objectweb.org/hello_world_soap_http", 30 "SOAPService"); 31 private final QName portName = new QName ("http://objectweb.org/hello_world_soap_http", 32 "SoapPort"); 33 34 public static Test suite() throws Exception { 35 TestSuite suite = new TestSuite(ClientServerTest.class); 36 return new ClientServerSetupBase(suite) { 37 public void startServers() throws Exception { 38 assertTrue("server did not launch correctly", launchServer(Server.class)); 39 } 40 }; 41 } 42 public void testBasicConnection() throws Exception { 43 URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); 44 assertNotNull(wsdl); 45 46 SOAPService service = new SOAPService(wsdl, serviceName); 47 assertNotNull(service); 48 49 Greeter greeter = service.getPort(portName, Greeter.class); 50 51 String response1 = new String ("Hello Milestone-"); 52 String response2 = new String ("Bonjour"); 53 try { 54 for (int idx = 0; idx < 5; idx++) { 55 String greeting = greeter.greetMe("Milestone-" + idx); 56 assertNotNull("no response received from service", greeting); 57 String exResponse = response1 + idx; 58 assertEquals(exResponse, greeting); 59 60 String reply = greeter.sayHi(); 61 assertNotNull("no response received from service", reply); 62 assertEquals(response2, reply); 63 64 greeter.greetMeOneWay("Milestone-" + idx); 65 66 BareDocumentResponse bareres = greeter.testDocLitBare("MySimpleDocument"); 67 assertNotNull("no response for operation testDocLitBare", bareres); 68 assertEquals("Celtix", bareres.getCompany()); 69 assertTrue(bareres.getId() == 1); 70 71 } 72 } catch (UndeclaredThrowableException ex) { 73 throw (Exception )ex.getCause(); 74 } 75 } 76 77 public void testAsyncPollingCall() throws Exception { 78 URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); 79 assertNotNull(wsdl); 80 81 SOAPService service = new SOAPService(wsdl, serviceName); 82 assertNotNull(service); 83 ExecutorService executor = Executors.newFixedThreadPool(5); 84 service.setExecutor(executor); 85 assertNotNull(service); 86 87 String expectedString = new String ("How are you Joe"); 88 try { 89 Greeter greeter = (Greeter)service.getPort(portName, Greeter.class); 90 91 Response<GreetMeSometimeResponse> response = greeter.greetMeSometimeAsync("Joe"); 92 while (!response.isDone()) { 93 Thread.sleep(100); 94 } 95 GreetMeSometimeResponse reply = response.get(); 96 assertNotNull("no response received from service", reply); 97 String s = reply.getResponseType(); 98 assertEquals(expectedString, s); 99 } catch (UndeclaredThrowableException ex) { 100 throw (Exception )ex.getCause(); 101 } 102 executor.shutdown(); 103 } 104 105 public void testAsyncSynchronousPolling() throws Exception { 106 URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); 107 assertNotNull(wsdl); 108 109 SOAPService service = new SOAPService(wsdl, serviceName); 110 assertNotNull(service); 111 ExecutorService executor = Executors.newFixedThreadPool(5); 112 service.setExecutor(executor); 113 assertNotNull(service); 114 115 final String expectedString = new String ("How are you Joe"); 116 117 class Poller extends Thread { 118 Response<GreetMeSometimeResponse> response; 119 int tid; 120 121 Poller(Response<GreetMeSometimeResponse> r, int t) { 122 response = r; 123 tid = t; 124 } 125 public void run() { 126 if (tid % 2 > 0) { 127 while (!response.isDone()) { 128 try { 129 Thread.sleep(100); 130 } catch (InterruptedException ex) { 131 } 133 } 134 } 135 GreetMeSometimeResponse reply = null; 136 try { 137 reply = response.get(); 138 } catch (Exception ex) { 139 fail("Poller " + tid + " failed with " + ex); 140 } 141 assertNotNull("Poller " + tid + ": no response received from service", reply); 142 String s = reply.getResponseType(); 143 assertEquals(expectedString, s); 144 } 145 } 146 147 Greeter greeter = (Greeter)service.getPort(portName, Greeter.class); 148 Response<GreetMeSometimeResponse> response = greeter.greetMeSometimeAsync("Joe"); 149 150 Poller[] pollers = new Poller[4]; 151 for (int i = 0; i < pollers.length; i++) { 152 pollers[i] = new Poller(response, i); 153 } 154 for (Poller p : pollers) { 155 p.start(); 156 } 157 158 for (Poller p : pollers) { 159 p.join(); 160 } 161 162 executor.shutdown(); 163 } 164 static class MyHandler implements AsyncHandler<GreetMeSometimeResponse> { 165 static int invocationCount; 166 private String replyBuffer; 167 168 public void handleResponse(Response<GreetMeSometimeResponse> response) { 169 invocationCount++; 170 try { 171 GreetMeSometimeResponse reply = response.get(); 172 replyBuffer = reply.getResponseType(); 173 } catch (InterruptedException ex) { 174 ex.printStackTrace(); 175 } catch (ExecutionException ex) { 176 ex.printStackTrace(); 177 } 178 } 179 180 String getReplyBuffer() { 181 return replyBuffer; 182 } 183 } 184 185 public void testAsyncCallWithHandler() throws Exception { 186 URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); 187 assertNotNull(wsdl); 188 189 SOAPService service = new SOAPService(wsdl, serviceName); 190 ExecutorService executor = Executors.newFixedThreadPool(5); 191 service.setExecutor(executor); 192 assertNotNull(service); 193 194 MyHandler h = new MyHandler(); 195 MyHandler.invocationCount = 0; 196 197 String expectedString = new String ("How are you Joe"); 198 try { 199 Greeter greeter = (Greeter)service.getPort(portName, Greeter.class); 200 Future <?> f = greeter.greetMeSometimeAsync("Joe", h); 201 int i = 0; 202 while (!f.isDone() && i < 20) { 203 Thread.sleep(100); 204 i++; 205 } 206 assertEquals("callback was not executed or did not return the expected result", 207 expectedString, h.getReplyBuffer()); 208 } catch (UndeclaredThrowableException ex) { 209 throw (Exception )ex.getCause(); 210 } 211 assertEquals(1, MyHandler.invocationCount); 212 executor.shutdown(); 213 } 214 public void testAsyncCallWithHandlerAndMultipleClients() throws Exception { 215 URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); 216 assertNotNull(wsdl); 217 218 SOAPService service = new SOAPService(wsdl, serviceName); 219 ExecutorService executor = Executors.newFixedThreadPool(5); 220 service.setExecutor(executor); 221 assertNotNull(service); 222 223 final MyHandler h = new MyHandler(); 224 MyHandler.invocationCount = 0; 225 226 final String expectedString = new String ("How are you Joe"); 227 228 class Poller extends Thread { 229 Future <?> future; 230 int tid; 231 232 Poller(Future <?> f, int t) { 233 future = f; 234 tid = t; 235 } 236 public void run() { 237 if (tid % 2 > 0) { 238 while (!future.isDone()) { 239 try { 240 Thread.sleep(100); 241 } catch (InterruptedException ex) { 242 } 244 } 245 } 246 try { 247 future.get(); 248 } catch (Exception ex) { 249 fail("Poller " + tid + " failed with " + ex); 250 } 251 assertEquals("callback was not executed or did not return the expected result", 252 expectedString, h.getReplyBuffer()); 253 } 254 } 255 256 Greeter greeter = (Greeter)service.getPort(portName, Greeter.class); 257 Future <?> f = greeter.greetMeSometimeAsync("Joe", h); 258 259 Poller[] pollers = new Poller[4]; 260 for (int i = 0; i < pollers.length; i++) { 261 pollers[i] = new Poller(f, i); 262 } 263 for (Poller p : pollers) { 264 p.start(); 265 } 266 267 for (Poller p : pollers) { 268 p.join(); 269 } 270 assertEquals(1, MyHandler.invocationCount); 271 executor.shutdown(); 272 } 273 274 275 276 public void testFaults() throws Exception { 277 URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); 278 assertNotNull(wsdl); 279 280 SOAPService service = new SOAPService(wsdl, serviceName); 281 ExecutorService ex = Executors.newFixedThreadPool(1); 282 service.setExecutor(ex); 283 assertNotNull(service); 284 285 String noSuchCodeFault = "NoSuchCodeLitFault"; 286 String badRecordFault = "BadRecordLitFault"; 287 288 Greeter greeter = service.getPort(portName, Greeter.class); 289 for (int idx = 0; idx < 2; idx++) { 290 try { 291 greeter.testDocLitFault(noSuchCodeFault); 292 fail("Should have thrown NoSuchCodeLitFault exception"); 293 } catch (NoSuchCodeLitFault nslf) { 294 assertNotNull(nslf.getFaultInfo()); 295 assertNotNull(nslf.getFaultInfo().getCode()); 296 } 297 298 try { 299 greeter.testDocLitFault(badRecordFault); 300 fail("Should have thrown BadRecordLitFault exception"); 301 } catch (BadRecordLitFault brlf) { 302 assertNotNull(brlf.getFaultInfo()); 303 } 304 } 305 306 } 307 308 public static void main(String [] args) { 309 junit.textui.TestRunner.run(ClientServerTest.class); 310 } 311 312 313 314 341 342 343 344 345 } 346 | Popular Tags |