KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > jaxws > DispatchImplTest


1 package org.objectweb.celtix.bus.jaxws;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.concurrent.ExecutionException JavaDoc;
8 import java.util.concurrent.Executor JavaDoc;
9 import java.util.concurrent.Future JavaDoc;
10 import java.util.concurrent.TimeUnit JavaDoc;
11 import java.util.concurrent.TimeoutException JavaDoc;
12
13 import javax.wsdl.Port;
14 import javax.wsdl.WSDLException;
15 import javax.xml.namespace.QName JavaDoc;
16 import javax.xml.soap.MessageFactory JavaDoc;
17 import javax.xml.soap.SOAPException JavaDoc;
18 import javax.xml.soap.SOAPMessage JavaDoc;
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 JavaDoc executor;
45
46     public DispatchImplTest(String JavaDoc name) {
47         super(name);
48     }
49
50     protected void setUp() throws Exception JavaDoc {
51         super.setUp();
52         bus = Bus.init();
53         URL JavaDoc wsdlUrl = getClass().getResource("/wsdl/hello_world.wsdl");
54         QName JavaDoc serviceName = new QName JavaDoc("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 JavaDoc {
60         super.tearDown();
61         bus.shutdown(true);
62         bus = null;
63         executor = null;
64         epr = null;
65     }
66
67     public void testDispatchImpl() throws Exception JavaDoc {
68         
69
70         DispatchImpl dispImpl =
71             new DispatchImpl<SOAPMessage JavaDoc>(bus, epr, Service.Mode.MESSAGE, SOAPMessage JavaDoc.class, executor);
72         
73         assertNotNull(dispImpl);
74     }
75
76     public void testGetRequestContext() throws Exception JavaDoc {
77         
78         DispatchImpl<SOAPMessage JavaDoc> dispImpl =
79             new DispatchImpl<SOAPMessage JavaDoc>(bus, epr, Service.Mode.MESSAGE, SOAPMessage JavaDoc.class, executor);
80         
81         Map JavaDoc<String JavaDoc, Object JavaDoc> m = dispImpl.getRequestContext();
82         
83         assertNotNull(m);
84     }
85
86     public void testGetResponseContext() throws Exception JavaDoc {
87         DispatchImpl<SOAPMessage JavaDoc> dispImpl =
88             new DispatchImpl<SOAPMessage JavaDoc>(bus, epr, Service.Mode.MESSAGE, SOAPMessage JavaDoc.class, executor);
89         
90         Map JavaDoc<String JavaDoc, Object JavaDoc> m = dispImpl.getResponseContext();
91         
92         assertNotNull(m);
93     }
94
95     public void testInvoke() throws Exception JavaDoc {
96         
97         InputStream JavaDoc is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml");
98         SOAPMessage JavaDoc soapReqMsg = MessageFactory.newInstance().createMessage(null, is);
99         assertNotNull(soapReqMsg);
100         
101         TestDispatchImpl<SOAPMessage JavaDoc> dispImpl =
102             new TestDispatchImpl<SOAPMessage JavaDoc>(bus, epr, Service.Mode.MESSAGE, SOAPMessage JavaDoc.class, executor);
103         SOAPMessage JavaDoc 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 JavaDoc {
110         
111         InputStream JavaDoc is =
112             getClass().getResourceAsStream("../bindings/soap/resources/BadRecordDocLiteral.xml");
113         SOAPMessage JavaDoc soapReqMsg = MessageFactory.newInstance().createMessage(null, is);
114         assertNotNull(soapReqMsg);
115         
116         TestDispatchImpl<SOAPMessage JavaDoc> dispImpl =
117             new TestDispatchImpl<SOAPMessage JavaDoc>(bus, epr, Service.Mode.MESSAGE, SOAPMessage JavaDoc.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 JavaDoc("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 JavaDoc {
134         
135         InputStream JavaDoc is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml");
136         SOAPMessage JavaDoc soapReqMsg = MessageFactory.newInstance().createMessage(null, is);
137         assertNotNull(soapReqMsg);
138         
139         TestDispatchImpl<SOAPMessage JavaDoc> dispImpl =
140             new TestDispatchImpl<SOAPMessage JavaDoc>(bus, epr, Service.Mode.MESSAGE, SOAPMessage JavaDoc.class, executor);
141         dispImpl.invokeOneWay(soapReqMsg);
142     }
143     
144     public void testInvokeAsync() throws Exception JavaDoc {
145         
146         InputStream JavaDoc is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml");
147         SOAPMessage JavaDoc soapReqMsg = MessageFactory.newInstance().createMessage(null, is);
148         assertNotNull(soapReqMsg);
149         
150         TestDispatchImpl<SOAPMessage JavaDoc> dispImpl =
151             new TestDispatchImpl<SOAPMessage JavaDoc>(bus, epr, Service.Mode.MESSAGE, SOAPMessage JavaDoc.class, executor);
152         Response response = dispImpl.invokeAsync(soapReqMsg);
153         assertNotNull(response);
154         SOAPMessage JavaDoc soapRespMsg = (SOAPMessage JavaDoc)response.get();
155         assertEquals("Message should contain TestSOAPInputMessage",
156                      soapRespMsg.getSOAPBody().getTextContent(), "TestSOAPInputMessage");
157     }
158     
159     public void testInvokeAsyncCallback() throws Exception JavaDoc {
160         
161         InputStream JavaDoc is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml");
162         SOAPMessage JavaDoc soapReqMsg = MessageFactory.newInstance().createMessage(null, is);
163         assertNotNull(soapReqMsg);
164         
165         TestDispatchImpl<SOAPMessage JavaDoc> dispImpl =
166             new TestDispatchImpl<SOAPMessage JavaDoc>(bus, epr, Service.Mode.MESSAGE, SOAPMessage JavaDoc.class, executor);
167         TestHandler testHandler = new TestHandler();
168         Future JavaDoc<?> future = dispImpl.invokeAsync(soapReqMsg, testHandler);
169         assertNotNull(future);
170         while (!future.isDone()) {
171             //wait till done
172
}
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 JavaDoc<X> cl;
181
182         TestDispatchImpl(Bus b, EndpointReferenceType r, Service.Mode m, Class JavaDoc<X> clazz, Executor JavaDoc 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 JavaDoc 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 JavaDoc {
212             super(b, ref);
213             bus = b;
214             epr = ref;
215         }
216
217         protected ClientTransport createTransport(EndpointReferenceType ref)
218             throws WSDLException, IOException JavaDoc {
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 JavaDoc {
233         }
234
235         public void invokeOneway(OutputStreamMessageContext context) throws IOException JavaDoc {
236             InputStreamMessageContext ismc = context.getCorrespondingInputStreamContext();
237             InputStream JavaDoc in = ismc.getInputStream();
238             try {
239                 SOAPMessage JavaDoc soapMessage = MessageFactory.newInstance().createMessage(null, in);
240                 assertEquals("Message should contain TestSOAPInputMessage",
241                              soapMessage.getSOAPBody().getTextContent(),
242                              "TestSOAPInputMessage");
243             } catch (IOException JavaDoc e) {
244                 e.printStackTrace();
245             } catch (SOAPException JavaDoc e) {
246                 e.printStackTrace();
247             }
248             
249         }
250
251         public InputStreamMessageContext invoke(OutputStreamMessageContext context) throws IOException JavaDoc {
252             return context.getCorrespondingInputStreamContext();
253             
254         }
255
256         public Future JavaDoc<InputStreamMessageContext> invokeAsync(OutputStreamMessageContext context,
257                                                              Executor JavaDoc e) throws IOException JavaDoc {
258             InputStreamMessageContext ismc = context.getCorrespondingInputStreamContext();
259             return new TestInputStreamMessageContextFuture(ismc);
260         }
261
262         public OutputStreamMessageContext createOutputStreamContext(MessageContext context)
263             throws IOException JavaDoc {
264             return new TestOutputStreamContext(null, context);
265         }
266
267         public void finalPrepareOutputStreamContext(OutputStreamMessageContext context) throws IOException JavaDoc {
268             // TODO Auto-generated method stub
269

270         }
271
272         public void shutdown() {
273             // TODO Auto-generated method stub
274

275         }
276
277         public EndpointReferenceType getDecoupledEndpoint() throws IOException JavaDoc {
278             // TODO Auto-generated method stub
279
return null;
280         }
281
282         public Port getPort() {
283             // TODO Auto-generated method stub
284
return null;
285         }
286
287         public EndpointReferenceType getTargetEndpoint() {
288             // TODO Auto-generated method stub
289
return null;
290         }
291         
292         public ResponseCallback getResponseCallback() {
293             // TODO Auto-generated method stub
294
return null;
295         }
296     }
297     
298     class TestInputStreamMessageContextFuture
299         implements Future JavaDoc<InputStreamMessageContext> {
300         
301         private InputStreamMessageContext inputStreamMessageContext;
302         
303         public TestInputStreamMessageContextFuture(InputStreamMessageContext ismc) {
304             inputStreamMessageContext = ismc;
305         }
306
307         public boolean cancel(boolean mayInterruptIfRunning) {
308             // TODO Auto-generated method stub
309
return false;
310         }
311
312         public boolean isCancelled() {
313             // TODO Auto-generated method stub
314
return false;
315         }
316
317         public boolean isDone() {
318             // TODO Auto-generated method stub
319
return false;
320         }
321
322         public InputStreamMessageContext get() throws InterruptedException JavaDoc, ExecutionException JavaDoc {
323             return inputStreamMessageContext;
324         }
325
326         public InputStreamMessageContext get(long timeout, TimeUnit JavaDoc unit)
327             throws InterruptedException JavaDoc, ExecutionException JavaDoc, TimeoutException JavaDoc {
328             return null;
329         }
330         
331     }
332     
333     class TestHandler implements AsyncHandler<SOAPMessage JavaDoc> {
334         
335         String JavaDoc replyBuffer;
336         
337         public void handleResponse(Response<SOAPMessage JavaDoc> response) {
338             try {
339                 SOAPMessage JavaDoc reply = response.get();
340                 replyBuffer = reply.getSOAPBody().getTextContent();
341             } catch (Exception JavaDoc e) {
342                 e.printStackTrace();
343             }
344         }
345         
346         public String JavaDoc getReplyBuffer() {
347             return replyBuffer;
348         }
349     }
350
351 }
352
Popular Tags