1 package org.objectweb.celtix.bus.jaxws; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.net.URL ; 6 import java.util.Map ; 7 import java.util.concurrent.ExecutionException ; 8 import java.util.concurrent.Executor ; 9 import java.util.concurrent.Future ; 10 import java.util.concurrent.TimeUnit ; 11 import java.util.concurrent.TimeoutException ; 12 13 import javax.wsdl.Port; 14 import javax.wsdl.WSDLException; 15 import javax.xml.namespace.QName ; 16 import javax.xml.soap.MessageFactory ; 17 import javax.xml.soap.SOAPException ; 18 import javax.xml.soap.SOAPMessage ; 19 import javax.xml.ws.AsyncHandler; 20 import javax.xml.ws.ProtocolException; 21 import javax.xml.ws.Response; 22 import javax.xml.ws.Service; 23 import javax.xml.ws.handler.MessageContext; 24 import javax.xml.ws.soap.SOAPFaultException; 25 26 import junit.framework.TestCase; 27 28 import org.objectweb.celtix.Bus; 29 import org.objectweb.celtix.bindings.DataBindingCallback.Mode; 30 import org.objectweb.celtix.bindings.ResponseCallback; 31 import org.objectweb.celtix.bus.bindings.TestOutputStreamContext; 32 import org.objectweb.celtix.bus.bindings.soap.SOAPClientBinding; 33 import org.objectweb.celtix.context.InputStreamMessageContext; 34 import org.objectweb.celtix.context.OutputStreamMessageContext; 35 import org.objectweb.celtix.transports.ClientTransport; 36 import org.objectweb.celtix.ws.addressing.EndpointReferenceType; 37 import org.objectweb.celtix.wsdl.EndpointReferenceUtils; 38 39 40 public class DispatchImplTest<T> extends TestCase { 41 42 Bus bus; 43 EndpointReferenceType epr; 44 Executor executor; 45 46 public DispatchImplTest(String name) { 47 super(name); 48 } 49 50 protected void setUp() throws Exception { 51 super.setUp(); 52 bus = Bus.init(); 53 URL wsdlUrl = getClass().getResource("/wsdl/hello_world.wsdl"); 54 QName serviceName = new QName ("http://objectweb.org/hello_world_soap_http", "SOAPService"); 55 epr = EndpointReferenceUtils.getEndpointReference(wsdlUrl, serviceName, "SoapPort"); 56 executor = bus.getWorkQueueManager().getAutomaticWorkQueue(); 57 } 58 59 protected void tearDown() throws Exception { 60 super.tearDown(); 61 bus.shutdown(true); 62 bus = null; 63 executor = null; 64 epr = null; 65 } 66 67 public void testDispatchImpl() throws Exception { 68 69 70 DispatchImpl dispImpl = 71 new DispatchImpl<SOAPMessage >(bus, epr, Service.Mode.MESSAGE, SOAPMessage .class, executor); 72 73 assertNotNull(dispImpl); 74 } 75 76 public void testGetRequestContext() throws Exception { 77 78 DispatchImpl<SOAPMessage > dispImpl = 79 new DispatchImpl<SOAPMessage >(bus, epr, Service.Mode.MESSAGE, SOAPMessage .class, executor); 80 81 Map <String , Object > m = dispImpl.getRequestContext(); 82 83 assertNotNull(m); 84 } 85 86 public void testGetResponseContext() throws Exception { 87 DispatchImpl<SOAPMessage > dispImpl = 88 new DispatchImpl<SOAPMessage >(bus, epr, Service.Mode.MESSAGE, SOAPMessage .class, executor); 89 90 Map <String , Object > m = dispImpl.getResponseContext(); 91 92 assertNotNull(m); 93 } 94 95 public void testInvoke() throws Exception { 96 97 InputStream is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml"); 98 SOAPMessage soapReqMsg = MessageFactory.newInstance().createMessage(null, is); 99 assertNotNull(soapReqMsg); 100 101 TestDispatchImpl<SOAPMessage > dispImpl = 102 new TestDispatchImpl<SOAPMessage >(bus, epr, Service.Mode.MESSAGE, SOAPMessage .class, executor); 103 SOAPMessage soapRespMsg = dispImpl.invoke(soapReqMsg); 104 assertNotNull(soapRespMsg); 105 assertEquals("Message should contain TestSOAPInputMessage", 106 soapRespMsg.getSOAPBody().getTextContent(), "TestSOAPInputMessage"); 107 } 108 109 public void testInvokeForFaults() throws Exception { 110 111 InputStream is = 112 getClass().getResourceAsStream("../bindings/soap/resources/BadRecordDocLiteral.xml"); 113 SOAPMessage soapReqMsg = MessageFactory.newInstance().createMessage(null, is); 114 assertNotNull(soapReqMsg); 115 116 TestDispatchImpl<SOAPMessage > dispImpl = 117 new TestDispatchImpl<SOAPMessage >(bus, epr, Service.Mode.MESSAGE, SOAPMessage .class, executor); 118 try { 119 dispImpl.invoke(soapReqMsg); 120 fail("Expecting a instance of ProtocolException"); 121 } catch (ProtocolException pe) { 122 assertTrue("Should be instance of SOAPFaultException", pe instanceof SOAPFaultException); 123 SOAPFaultException sfe = (SOAPFaultException)pe; 124 assertNotNull("Should have a details obj", sfe.getFault()); 125 assertEquals( 126 new QName ("http://schemas.xmlsoap.org/soap/envelope/", "Server"), 127 sfe.getFault().getFaultCodeAsQName()); 128 assertEquals("Test Exception", sfe.getFault().getFaultString()); 129 } 130 is.close(); 131 } 132 133 public void testInvokeOneWay() throws Exception { 134 135 InputStream is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml"); 136 SOAPMessage soapReqMsg = MessageFactory.newInstance().createMessage(null, is); 137 assertNotNull(soapReqMsg); 138 139 TestDispatchImpl<SOAPMessage > dispImpl = 140 new TestDispatchImpl<SOAPMessage >(bus, epr, Service.Mode.MESSAGE, SOAPMessage .class, executor); 141 dispImpl.invokeOneWay(soapReqMsg); 142 } 143 144 public void testInvokeAsync() throws Exception { 145 146 InputStream is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml"); 147 SOAPMessage soapReqMsg = MessageFactory.newInstance().createMessage(null, is); 148 assertNotNull(soapReqMsg); 149 150 TestDispatchImpl<SOAPMessage > dispImpl = 151 new TestDispatchImpl<SOAPMessage >(bus, epr, Service.Mode.MESSAGE, SOAPMessage .class, executor); 152 Response response = dispImpl.invokeAsync(soapReqMsg); 153 assertNotNull(response); 154 SOAPMessage soapRespMsg = (SOAPMessage )response.get(); 155 assertEquals("Message should contain TestSOAPInputMessage", 156 soapRespMsg.getSOAPBody().getTextContent(), "TestSOAPInputMessage"); 157 } 158 159 public void testInvokeAsyncCallback() throws Exception { 160 161 InputStream is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml"); 162 SOAPMessage soapReqMsg = MessageFactory.newInstance().createMessage(null, is); 163 assertNotNull(soapReqMsg); 164 165 TestDispatchImpl<SOAPMessage > dispImpl = 166 new TestDispatchImpl<SOAPMessage >(bus, epr, Service.Mode.MESSAGE, SOAPMessage .class, executor); 167 TestHandler testHandler = new TestHandler(); 168 Future <?> future = dispImpl.invokeAsync(soapReqMsg, testHandler); 169 assertNotNull(future); 170 while (!future.isDone()) { 171 } 173 assertEquals("Message should contain TestSOAPInputMessage", 174 testHandler.getReplyBuffer(), "TestSOAPInputMessage"); 175 } 176 177 class TestDispatchImpl<X> extends DispatchImpl<X> { 178 179 private Mode mode; 180 private Class <X> cl; 181 182 TestDispatchImpl(Bus b, EndpointReferenceType r, Service.Mode m, Class <X> clazz, Executor e) { 183 super(b, r, m, clazz, e); 184 mode = Mode.fromServiceMode(m); 185 cl = clazz; 186 } 187 188 protected void init() { 189 try { 190 cb = new TestClientBinding(bus, epr); 191 callback = new DynamicDataBindingCallback(cl, mode); 192 } catch (WSDLException e) { 193 e.printStackTrace(); 194 } catch (IOException e) { 195 e.printStackTrace(); 196 } 197 } 198 199 public TestClientBinding getTestClientBinding() { 200 return (TestClientBinding)cb; 201 } 202 203 } 204 205 class TestClientBinding extends SOAPClientBinding { 206 207 private TestClientTransport testClientTransport; 208 private Bus bus; 209 private EndpointReferenceType epr; 210 211 public TestClientBinding(Bus b, EndpointReferenceType ref) throws WSDLException, IOException { 212 super(b, ref); 213 bus = b; 214 epr = ref; 215 } 216 217 protected ClientTransport createTransport(EndpointReferenceType ref) 218 throws WSDLException, IOException { 219 testClientTransport = new TestClientTransport(bus, epr); 220 return testClientTransport; 221 } 222 223 public ClientTransport getTestTransport() { 224 return testClientTransport; 225 } 226 227 } 228 229 class TestClientTransport implements ClientTransport { 230 231 public TestClientTransport(Bus mybus, EndpointReferenceType ref) 232 throws WSDLException, IOException { 233 } 234 235 public void invokeOneway(OutputStreamMessageContext context) throws IOException { 236 InputStreamMessageContext ismc = context.getCorrespondingInputStreamContext(); 237 InputStream in = ismc.getInputStream(); 238 try { 239 SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(null, in); 240 assertEquals("Message should contain TestSOAPInputMessage", 241 soapMessage.getSOAPBody().getTextContent(), 242 "TestSOAPInputMessage"); 243 } catch (IOException e) { 244 e.printStackTrace(); 245 } catch (SOAPException e) { 246 e.printStackTrace(); 247 } 248 249 } 250 251 public InputStreamMessageContext invoke(OutputStreamMessageContext context) throws IOException { 252 return context.getCorrespondingInputStreamContext(); 253 254 } 255 256 public Future <InputStreamMessageContext> invokeAsync(OutputStreamMessageContext context, 257 Executor e) throws IOException { 258 InputStreamMessageContext ismc = context.getCorrespondingInputStreamContext(); 259 return new TestInputStreamMessageContextFuture(ismc); 260 } 261 262 public OutputStreamMessageContext createOutputStreamContext(MessageContext context) 263 throws IOException { 264 return new TestOutputStreamContext(null, context); 265 } 266 267 public void finalPrepareOutputStreamContext(OutputStreamMessageContext context) throws IOException { 268 270 } 271 272 public void shutdown() { 273 275 } 276 277 public EndpointReferenceType getDecoupledEndpoint() throws IOException { 278 return null; 280 } 281 282 public Port getPort() { 283 return null; 285 } 286 287 public EndpointReferenceType getTargetEndpoint() { 288 return null; 290 } 291 292 public ResponseCallback getResponseCallback() { 293 return null; 295 } 296 } 297 298 class TestInputStreamMessageContextFuture 299 implements Future <InputStreamMessageContext> { 300 301 private InputStreamMessageContext inputStreamMessageContext; 302 303 public TestInputStreamMessageContextFuture(InputStreamMessageContext ismc) { 304 inputStreamMessageContext = ismc; 305 } 306 307 public boolean cancel(boolean mayInterruptIfRunning) { 308 return false; 310 } 311 312 public boolean isCancelled() { 313 return false; 315 } 316 317 public boolean isDone() { 318 return false; 320 } 321 322 public InputStreamMessageContext get() throws InterruptedException , ExecutionException { 323 return inputStreamMessageContext; 324 } 325 326 public InputStreamMessageContext get(long timeout, TimeUnit unit) 327 throws InterruptedException , ExecutionException , TimeoutException { 328 return null; 329 } 330 331 } 332 333 class TestHandler implements AsyncHandler<SOAPMessage > { 334 335 String replyBuffer; 336 337 public void handleResponse(Response<SOAPMessage > response) { 338 try { 339 SOAPMessage reply = response.get(); 340 replyBuffer = reply.getSOAPBody().getTextContent(); 341 } catch (Exception e) { 342 e.printStackTrace(); 343 } 344 } 345 346 public String getReplyBuffer() { 347 return replyBuffer; 348 } 349 } 350 351 } 352 | Popular Tags |