001: package org.objectweb.celtix.bus.jaxws;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.net.URL;
006: import java.util.Map;
007: import java.util.concurrent.ExecutionException;
008: import java.util.concurrent.Executor;
009: import java.util.concurrent.Future;
010: import java.util.concurrent.TimeUnit;
011: import java.util.concurrent.TimeoutException;
012:
013: import javax.wsdl.Port;
014: import javax.wsdl.WSDLException;
015: import javax.xml.namespace.QName;
016: import javax.xml.soap.MessageFactory;
017: import javax.xml.soap.SOAPException;
018: import javax.xml.soap.SOAPMessage;
019: import javax.xml.ws.AsyncHandler;
020: import javax.xml.ws.ProtocolException;
021: import javax.xml.ws.Response;
022: import javax.xml.ws.Service;
023: import javax.xml.ws.handler.MessageContext;
024: import javax.xml.ws.soap.SOAPFaultException;
025:
026: import junit.framework.TestCase;
027:
028: import org.objectweb.celtix.Bus;
029: import org.objectweb.celtix.bindings.DataBindingCallback.Mode;
030: import org.objectweb.celtix.bindings.ResponseCallback;
031: import org.objectweb.celtix.bus.bindings.TestOutputStreamContext;
032: import org.objectweb.celtix.bus.bindings.soap.SOAPClientBinding;
033: import org.objectweb.celtix.context.InputStreamMessageContext;
034: import org.objectweb.celtix.context.OutputStreamMessageContext;
035: import org.objectweb.celtix.transports.ClientTransport;
036: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
037: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
038:
039: public class DispatchImplTest<T> extends TestCase {
040:
041: Bus bus;
042: EndpointReferenceType epr;
043: Executor executor;
044:
045: public DispatchImplTest(String name) {
046: super (name);
047: }
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: bus = Bus.init();
052: URL wsdlUrl = getClass().getResource("/wsdl/hello_world.wsdl");
053: QName serviceName = new QName(
054: "http://objectweb.org/hello_world_soap_http",
055: "SOAPService");
056: epr = EndpointReferenceUtils.getEndpointReference(wsdlUrl,
057: serviceName, "SoapPort");
058: executor = bus.getWorkQueueManager().getAutomaticWorkQueue();
059: }
060:
061: protected void tearDown() throws Exception {
062: super .tearDown();
063: bus.shutdown(true);
064: bus = null;
065: executor = null;
066: epr = null;
067: }
068:
069: public void testDispatchImpl() throws Exception {
070:
071: DispatchImpl dispImpl = new DispatchImpl<SOAPMessage>(bus, epr,
072: Service.Mode.MESSAGE, SOAPMessage.class, executor);
073:
074: assertNotNull(dispImpl);
075: }
076:
077: public void testGetRequestContext() throws Exception {
078:
079: DispatchImpl<SOAPMessage> dispImpl = new DispatchImpl<SOAPMessage>(
080: bus, epr, Service.Mode.MESSAGE, SOAPMessage.class,
081: executor);
082:
083: Map<String, Object> m = dispImpl.getRequestContext();
084:
085: assertNotNull(m);
086: }
087:
088: public void testGetResponseContext() throws Exception {
089: DispatchImpl<SOAPMessage> dispImpl = new DispatchImpl<SOAPMessage>(
090: bus, epr, Service.Mode.MESSAGE, SOAPMessage.class,
091: executor);
092:
093: Map<String, Object> m = dispImpl.getResponseContext();
094:
095: assertNotNull(m);
096: }
097:
098: public void testInvoke() throws Exception {
099:
100: InputStream is = getClass().getResourceAsStream(
101: "GreetMeDocLiteralReq.xml");
102: SOAPMessage soapReqMsg = MessageFactory.newInstance()
103: .createMessage(null, is);
104: assertNotNull(soapReqMsg);
105:
106: TestDispatchImpl<SOAPMessage> dispImpl = new TestDispatchImpl<SOAPMessage>(
107: bus, epr, Service.Mode.MESSAGE, SOAPMessage.class,
108: executor);
109: SOAPMessage soapRespMsg = dispImpl.invoke(soapReqMsg);
110: assertNotNull(soapRespMsg);
111: assertEquals("Message should contain TestSOAPInputMessage",
112: soapRespMsg.getSOAPBody().getTextContent(),
113: "TestSOAPInputMessage");
114: }
115:
116: public void testInvokeForFaults() throws Exception {
117:
118: InputStream is = getClass().getResourceAsStream(
119: "../bindings/soap/resources/BadRecordDocLiteral.xml");
120: SOAPMessage soapReqMsg = MessageFactory.newInstance()
121: .createMessage(null, is);
122: assertNotNull(soapReqMsg);
123:
124: TestDispatchImpl<SOAPMessage> dispImpl = new TestDispatchImpl<SOAPMessage>(
125: bus, epr, Service.Mode.MESSAGE, SOAPMessage.class,
126: executor);
127: try {
128: dispImpl.invoke(soapReqMsg);
129: fail("Expecting a instance of ProtocolException");
130: } catch (ProtocolException pe) {
131: assertTrue("Should be instance of SOAPFaultException",
132: pe instanceof SOAPFaultException);
133: SOAPFaultException sfe = (SOAPFaultException) pe;
134: assertNotNull("Should have a details obj", sfe.getFault());
135: assertEquals(new QName(
136: "http://schemas.xmlsoap.org/soap/envelope/",
137: "Server"), sfe.getFault().getFaultCodeAsQName());
138: assertEquals("Test Exception", sfe.getFault()
139: .getFaultString());
140: }
141: is.close();
142: }
143:
144: public void testInvokeOneWay() throws Exception {
145:
146: InputStream is = getClass().getResourceAsStream(
147: "GreetMeDocLiteralReq.xml");
148: SOAPMessage soapReqMsg = MessageFactory.newInstance()
149: .createMessage(null, is);
150: assertNotNull(soapReqMsg);
151:
152: TestDispatchImpl<SOAPMessage> dispImpl = new TestDispatchImpl<SOAPMessage>(
153: bus, epr, Service.Mode.MESSAGE, SOAPMessage.class,
154: executor);
155: dispImpl.invokeOneWay(soapReqMsg);
156: }
157:
158: public void testInvokeAsync() throws Exception {
159:
160: InputStream is = getClass().getResourceAsStream(
161: "GreetMeDocLiteralReq.xml");
162: SOAPMessage soapReqMsg = MessageFactory.newInstance()
163: .createMessage(null, is);
164: assertNotNull(soapReqMsg);
165:
166: TestDispatchImpl<SOAPMessage> dispImpl = new TestDispatchImpl<SOAPMessage>(
167: bus, epr, Service.Mode.MESSAGE, SOAPMessage.class,
168: executor);
169: Response response = dispImpl.invokeAsync(soapReqMsg);
170: assertNotNull(response);
171: SOAPMessage soapRespMsg = (SOAPMessage) response.get();
172: assertEquals("Message should contain TestSOAPInputMessage",
173: soapRespMsg.getSOAPBody().getTextContent(),
174: "TestSOAPInputMessage");
175: }
176:
177: public void testInvokeAsyncCallback() throws Exception {
178:
179: InputStream is = getClass().getResourceAsStream(
180: "GreetMeDocLiteralReq.xml");
181: SOAPMessage soapReqMsg = MessageFactory.newInstance()
182: .createMessage(null, is);
183: assertNotNull(soapReqMsg);
184:
185: TestDispatchImpl<SOAPMessage> dispImpl = new TestDispatchImpl<SOAPMessage>(
186: bus, epr, Service.Mode.MESSAGE, SOAPMessage.class,
187: executor);
188: TestHandler testHandler = new TestHandler();
189: Future<?> future = dispImpl
190: .invokeAsync(soapReqMsg, testHandler);
191: assertNotNull(future);
192: while (!future.isDone()) {
193: //wait till done
194: }
195: assertEquals("Message should contain TestSOAPInputMessage",
196: testHandler.getReplyBuffer(), "TestSOAPInputMessage");
197: }
198:
199: class TestDispatchImpl<X> extends DispatchImpl<X> {
200:
201: private Mode mode;
202: private Class<X> cl;
203:
204: TestDispatchImpl(Bus b, EndpointReferenceType r,
205: Service.Mode m, Class<X> clazz, Executor e) {
206: super (b, r, m, clazz, e);
207: mode = Mode.fromServiceMode(m);
208: cl = clazz;
209: }
210:
211: protected void init() {
212: try {
213: cb = new TestClientBinding(bus, epr);
214: callback = new DynamicDataBindingCallback(cl, mode);
215: } catch (WSDLException e) {
216: e.printStackTrace();
217: } catch (IOException e) {
218: e.printStackTrace();
219: }
220: }
221:
222: public TestClientBinding getTestClientBinding() {
223: return (TestClientBinding) cb;
224: }
225:
226: }
227:
228: class TestClientBinding extends SOAPClientBinding {
229:
230: private TestClientTransport testClientTransport;
231: private Bus bus;
232: private EndpointReferenceType epr;
233:
234: public TestClientBinding(Bus b, EndpointReferenceType ref)
235: throws WSDLException, IOException {
236: super (b, ref);
237: bus = b;
238: epr = ref;
239: }
240:
241: protected ClientTransport createTransport(
242: EndpointReferenceType ref) throws WSDLException,
243: IOException {
244: testClientTransport = new TestClientTransport(bus, epr);
245: return testClientTransport;
246: }
247:
248: public ClientTransport getTestTransport() {
249: return testClientTransport;
250: }
251:
252: }
253:
254: class TestClientTransport implements ClientTransport {
255:
256: public TestClientTransport(Bus mybus, EndpointReferenceType ref)
257: throws WSDLException, IOException {
258: }
259:
260: public void invokeOneway(OutputStreamMessageContext context)
261: throws IOException {
262: InputStreamMessageContext ismc = context
263: .getCorrespondingInputStreamContext();
264: InputStream in = ismc.getInputStream();
265: try {
266: SOAPMessage soapMessage = MessageFactory.newInstance()
267: .createMessage(null, in);
268: assertEquals(
269: "Message should contain TestSOAPInputMessage",
270: soapMessage.getSOAPBody().getTextContent(),
271: "TestSOAPInputMessage");
272: } catch (IOException e) {
273: e.printStackTrace();
274: } catch (SOAPException e) {
275: e.printStackTrace();
276: }
277:
278: }
279:
280: public InputStreamMessageContext invoke(
281: OutputStreamMessageContext context) throws IOException {
282: return context.getCorrespondingInputStreamContext();
283:
284: }
285:
286: public Future<InputStreamMessageContext> invokeAsync(
287: OutputStreamMessageContext context, Executor e)
288: throws IOException {
289: InputStreamMessageContext ismc = context
290: .getCorrespondingInputStreamContext();
291: return new TestInputStreamMessageContextFuture(ismc);
292: }
293:
294: public OutputStreamMessageContext createOutputStreamContext(
295: MessageContext context) throws IOException {
296: return new TestOutputStreamContext(null, context);
297: }
298:
299: public void finalPrepareOutputStreamContext(
300: OutputStreamMessageContext context) throws IOException {
301: // TODO Auto-generated method stub
302:
303: }
304:
305: public void shutdown() {
306: // TODO Auto-generated method stub
307:
308: }
309:
310: public EndpointReferenceType getDecoupledEndpoint()
311: throws IOException {
312: // TODO Auto-generated method stub
313: return null;
314: }
315:
316: public Port getPort() {
317: // TODO Auto-generated method stub
318: return null;
319: }
320:
321: public EndpointReferenceType getTargetEndpoint() {
322: // TODO Auto-generated method stub
323: return null;
324: }
325:
326: public ResponseCallback getResponseCallback() {
327: // TODO Auto-generated method stub
328: return null;
329: }
330: }
331:
332: class TestInputStreamMessageContextFuture implements
333: Future<InputStreamMessageContext> {
334:
335: private InputStreamMessageContext inputStreamMessageContext;
336:
337: public TestInputStreamMessageContextFuture(
338: InputStreamMessageContext ismc) {
339: inputStreamMessageContext = ismc;
340: }
341:
342: public boolean cancel(boolean mayInterruptIfRunning) {
343: // TODO Auto-generated method stub
344: return false;
345: }
346:
347: public boolean isCancelled() {
348: // TODO Auto-generated method stub
349: return false;
350: }
351:
352: public boolean isDone() {
353: // TODO Auto-generated method stub
354: return false;
355: }
356:
357: public InputStreamMessageContext get()
358: throws InterruptedException, ExecutionException {
359: return inputStreamMessageContext;
360: }
361:
362: public InputStreamMessageContext get(long timeout, TimeUnit unit)
363: throws InterruptedException, ExecutionException,
364: TimeoutException {
365: return null;
366: }
367:
368: }
369:
370: class TestHandler implements AsyncHandler<SOAPMessage> {
371:
372: String replyBuffer;
373:
374: public void handleResponse(Response<SOAPMessage> response) {
375: try {
376: SOAPMessage reply = response.get();
377: replyBuffer = reply.getSOAPBody().getTextContent();
378: } catch (Exception e) {
379: e.printStackTrace();
380: }
381: }
382:
383: public String getReplyBuffer() {
384: return replyBuffer;
385: }
386: }
387:
388: }
|