KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > systest > handlers > HandlerInvocationTest


1 package org.objectweb.celtix.systest.handlers;
2
3 import java.net.URL JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import javax.xml.bind.JAXBContext;
7 import javax.xml.namespace.QName JavaDoc;
8 import javax.xml.ws.BindingProvider;
9 import javax.xml.ws.LogicalMessage;
10 import javax.xml.ws.ProtocolException;
11 import javax.xml.ws.handler.Handler;
12 import javax.xml.ws.handler.LogicalMessageContext;
13 import javax.xml.ws.handler.MessageContext;
14
15 import junit.framework.Test;
16 import junit.framework.TestSuite;
17
18 import org.objectweb.celtix.BusException;
19 import org.objectweb.celtix.systest.common.ClientServerSetupBase;
20 import org.objectweb.celtix.systest.common.ClientServerTestBase;
21 import org.objectweb.handler_test.HandlerTest;
22 import org.objectweb.handler_test.HandlerTestService;
23 import org.objectweb.handler_test.PingException;
24 import org.objectweb.handler_test.types.PingResponse;
25 import org.objectweb.hello_world_soap_http.types.GreetMe;
26
27
28 public class HandlerInvocationTest extends ClientServerTestBase {
29     
30     private final QName JavaDoc serviceName = new QName JavaDoc("http://objectweb.org/hello_world_soap_http",
31                                           "HandlerTestService");
32     private final QName JavaDoc portName = new QName JavaDoc("http://objectweb.org/hello_world_soap_http", "SoapPort");
33     
34     private URL JavaDoc wsdl;
35     private HandlerTestService service;
36     private HandlerTest handlerTest;
37
38
39     public static Test suite() throws Exception JavaDoc {
40         TestSuite suite = new TestSuite(HandlerInvocationTest.class);
41         return new ClientServerSetupBase(suite) {
42             public void startServers() throws Exception JavaDoc {
43                 assertTrue("server did not launch correctly", launchServer(Server.class));
44             }
45         };
46     }
47
48
49     public void setUp() throws BusException {
50         try {
51             super.setUp();
52             
53             wsdl = HandlerInvocationTest.class.getResource("/wsdl/handler_test.wsdl");
54             service = new HandlerTestService(wsdl, serviceName);
55             handlerTest = service.getPort(portName, HandlerTest.class);
56             if (!"testHandlersInvoked".equals(getName())) {
57                 addHandlersToChain((BindingProvider)handlerTest, new TestStreamHandler(false));
58             }
59         } catch (Exception JavaDoc ex) {
60             ex.printStackTrace();
61             fail(ex.toString());
62         }
63     }
64
65
66     public void testHandlersInvoked() throws PingException {
67         
68         TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false);
69         TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false);
70         TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
71         TestSOAPHandler soapHandler2 = new TestSOAPHandler(false);
72         TestStreamHandler streamHandler = new TestStreamHandler(false);
73
74         addHandlersToChain((BindingProvider)handlerTest, handler1, handler2,
75                            soapHandler1, soapHandler2,
76                            streamHandler);
77         
78         List JavaDoc<String JavaDoc> resp = handlerTest.ping();
79         assertNotNull(resp);
80
81         assertEquals("handle message was not invoked", 2, handler1.getHandleMessageInvoked());
82         assertEquals("handle message was not invoked", 2, handler2.getHandleMessageInvoked());
83         assertEquals("handle message was not invoked", 2, soapHandler1.getHandleMessageInvoked());
84         assertEquals("handle message was not invoked", 2, soapHandler2.getHandleMessageInvoked());
85         assertEquals("handle message was not invoked", 2, streamHandler.getHandleMessageInvoked());
86         assertTrue("close must be called", handler1.isCloseInvoked());
87         assertTrue("close must be called", handler2.isCloseInvoked());
88         assertTrue("close must be called", soapHandler1.isCloseInvoked());
89         assertTrue("close must be called", soapHandler2.isCloseInvoked());
90         assertTrue("close must be called", streamHandler.isCloseInvoked());
91
92         // the server has encoded into the response the order in
93
// which the handlers have been invoked, parse it and make
94
// sure everything is ok
95

96         // expected order for inbound interceptors
97
//
98
// note: the stream handler does not add itself to the list on
99
// the way out of the server. It compresses the message so
100
// the fact that we are here indicates that it has
101
// participated correctly in the message exchange.
102
String JavaDoc[] handlerNames = {"streamHandler5", "soapHandler4", "soapHandler3", "handler2", "handler1",
103                                  "servant",
104                                  "handler1", "handler2", "soapHandler3", "soapHandler4"};
105         
106         assertEquals(handlerNames.length, resp.size());
107
108         Iterator JavaDoc iter = resp.iterator();
109         for (String JavaDoc expected : handlerNames) {
110             assertEquals(expected, iter.next());
111         }
112     }
113     
114
115     public void testLogicalHandlerStopProcessing() throws PingException {
116
117         final String JavaDoc clientHandlerMessage = "handler2 client side";
118
119         TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false);
120         TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {
121             public boolean handleMessage(LogicalMessageContext ctx) {
122                 super.handleMessage(ctx);
123                 try {
124                     Boolean JavaDoc outbound = (Boolean JavaDoc)ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
125                     if (outbound) {
126                         LogicalMessage msg = ctx.getMessage();
127                         assertNotNull("logical message is null", msg);
128                         JAXBContext jaxbCtx = JAXBContext.newInstance(GreetMe.class.getPackage().getName());
129                         PingResponse resp = new PingResponse();
130                         resp.getHandlersInfo().add(clientHandlerMessage);
131
132                         msg.setPayload(resp, jaxbCtx);
133                     }
134
135                 } catch (Exception JavaDoc e) {
136                     e.printStackTrace();
137                     fail(e.toString());
138                 }
139                 return false;
140             }
141         };
142         
143         addHandlersToChain((BindingProvider)handlerTest, handler1, handler2);
144
145         List JavaDoc<String JavaDoc> resp = handlerTest.ping();
146         assertEquals(clientHandlerMessage, resp.get(0));
147         
148         assertEquals("handler must be invoked for inbound & outbound message",
149                      2, handler1.getHandleMessageInvoked());
150         assertEquals("second handler must be invoked exactly once", 1, handler2.getHandleMessageInvoked());
151         assertTrue("close must be called", handler1.isCloseInvoked());
152         assertTrue("close must be called", handler2.isCloseInvoked());
153     }
154
155
156     public void testLogicalHandlerStopProcessingServerSide() throws PingException {
157
158         String JavaDoc[] expectedHandlers = {"streamHandler5", "soapHandler4", "soapHandler3", "handler2",
159                                      "soapHandler3", "soapHandler4"};
160
161         List JavaDoc<String JavaDoc> resp = handlerTest.pingWithArgs("handler2 inbound stop");
162
163         assertEquals(expectedHandlers.length, resp.size());
164        
165         int i = 0;
166         for (String JavaDoc expected : expectedHandlers) {
167             assertEquals(expected, resp.get(i++));
168         }
169
170
171         String JavaDoc[] expectedHandlers1 = {"streamHandler5", "soapHandler4", "soapHandler3", "soapHandler4"};
172         resp = handlerTest.pingWithArgs("soapHandler3 inbound stop");
173         assertEquals(expectedHandlers1.length, resp.size());
174         i = 0;
175         for (String JavaDoc expected : expectedHandlers1) {
176             assertEquals(expected, resp.get(i++));
177         }
178     }
179
180
181     public void testLogicalHandlerThrowsProtocolException() throws Exception JavaDoc {
182
183         final String JavaDoc clientHandlerMessage = "handler1 client side";
184
185         TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false) {
186             public boolean handleMessage(LogicalMessageContext ctx) {
187                 super.handleMessage(ctx);
188                 throw new ProtocolException(clientHandlerMessage);
189             }
190         };
191         TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false);
192         
193         addHandlersToChain((BindingProvider)handlerTest, handler1, handler2);
194
195         try {
196             handlerTest.ping();
197             fail("did not get expected exception");
198         } catch (ProtocolException e) {
199             assertEquals(clientHandlerMessage, e.getMessage());
200         }
201         assertTrue(!handler2.isHandleFaultInvoked());
202         assertTrue(handler1.isCloseInvoked());
203         assertTrue(!handler2.isCloseInvoked());
204     }
205
206
207
208     public void testLogicalHandlerThrowsProtocolExceptionServerSide() throws PingException {
209         try {
210             handlerTest.pingWithArgs("handler2 inbound throw javax.xml.ws.ProtocolException");
211             fail("did not get expected exception");
212         } catch (ProtocolException e) {
213             // happy now
214
}
215     }
216
217
218     public void testLogicalHandlerHandlerFault() {
219
220         TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false);
221         TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false);
222         TestStreamHandler streamHandler = new TestStreamHandler(false);
223         addHandlersToChain((BindingProvider)handlerTest, handler1, handler2, streamHandler);
224         
225         try {
226             handlerTest.pingWithArgs("servant throw exception");
227             fail("did not get expected PingException");
228         } catch (PingException e) {
229             assertTrue(e.getMessage().contains("from servant"));
230         }
231
232         assertEquals(1, handler1.getHandleMessageInvoked());
233         assertEquals(1, handler2.getHandleMessageInvoked());
234         assertEquals(1, streamHandler.getHandleMessageInvoked());
235         assertEquals(1, handler1.getHandleFaultInvoked());
236         assertEquals(1, handler2.getHandleFaultInvoked());
237         assertEquals(1, streamHandler.getHandleFaultInvoked());
238     }
239
240
241     public void testLogicalHandlerOneWay() {
242         TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false);
243         TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false);
244         TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
245
246         addHandlersToChain((BindingProvider)handlerTest, handler1, handler2, soapHandler1);
247
248         handlerTest.pingOneWay();
249
250         assertEquals(1, handler1.getHandleMessageInvoked());
251         assertEquals(1, handler2.getHandleMessageInvoked());
252         assertEquals(1, soapHandler1.getHandleMessageInvoked());
253     }
254
255     void addHandlersToChain(BindingProvider bp, Handler...handlers) {
256         List JavaDoc<Handler> handlerChain = bp.getBinding().getHandlerChain();
257         assertNotNull(handlerChain);
258         for (Handler h : handlers) {
259             handlerChain.add(h);
260         }
261     }
262 }
263
Popular Tags