KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > handlers > HandlerChainInvokerTest


1 package org.objectweb.celtix.bus.handlers;
2
3
4
5 import java.io.InputStream JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.xml.ws.ProtocolException;
11 import javax.xml.ws.handler.Handler;
12 import javax.xml.ws.handler.LogicalHandler;
13 import javax.xml.ws.handler.MessageContext;
14 import javax.xml.ws.handler.soap.SOAPMessageContext;
15
16 import junit.framework.TestCase;
17
18 import org.easymock.classextension.EasyMock;
19 import org.objectweb.celtix.bus.context.LogicalMessageContextImpl;
20 import org.objectweb.celtix.context.InputStreamMessageContext;
21 import org.objectweb.celtix.context.MessageContextWrapper;
22 import org.objectweb.celtix.context.ObjectMessageContext;
23 import org.objectweb.celtix.context.ObjectMessageContextImpl;
24 import org.objectweb.celtix.context.StreamMessageContext;
25 import org.objectweb.celtix.handlers.StreamHandler;
26
27 public class HandlerChainInvokerTest extends TestCase {
28     
29     private static final int HANDLER_COUNT = 2;
30     
31     HandlerChainInvoker invoker;
32     
33     ObjectMessageContextImpl ctx = new ObjectMessageContextImpl();
34     SOAPMessageContext soapContext;
35     
36     TestLogicalHandler[] logicalHandlers = new TestLogicalHandler[HANDLER_COUNT];
37     TestProtocolHandler[] protocolHandlers = new TestProtocolHandler[HANDLER_COUNT];
38     TestStreamHandler[] streamHandlers = new TestStreamHandler[HANDLER_COUNT];
39
40     public void setUp() {
41         AbstractHandlerBase.clear();
42
43         List JavaDoc<Handler> handlers = new ArrayList JavaDoc<Handler>();
44         for (int i = 0; i < logicalHandlers.length; i++) {
45             logicalHandlers[i] = new TestLogicalHandler();
46             handlers.add(logicalHandlers[i]);
47         }
48         for (int i = 0; i < protocolHandlers.length; i++) {
49             protocolHandlers[i] = new TestProtocolHandler();
50             handlers.add(protocolHandlers[i]);
51         }
52         for (int i = 0; i < protocolHandlers.length; i++) {
53             streamHandlers[i] = new TestStreamHandler();
54             handlers.add(streamHandlers[i]);
55         }
56         invoker = new HandlerChainInvoker(handlers);
57         
58         soapContext = EasyMock.createNiceMock(SOAPMessageContext.class);
59     }
60     
61     public void testInvokeEmptyHandlerChain() {
62         invoker = new HandlerChainInvoker(new ArrayList JavaDoc<Handler>());
63         assertTrue(invoker.invokeLogicalHandlers(false, ctx));
64         assertTrue(doInvokeProtocolHandlers(false));
65         assertTrue(invoker.invokeStreamHandlers(EasyMock.createMock(InputStreamMessageContext.class)));
66     }
67
68     
69     public void testHandlerPartitioning() {
70         
71         assertEquals(HANDLER_COUNT, invoker.getLogicalHandlers().size());
72         for (Handler h : invoker.getLogicalHandlers()) {
73             assertTrue(h instanceof LogicalHandler);
74         }
75
76         assertEquals(HANDLER_COUNT, invoker.getStreamHandlers().size());
77         for (Handler h : invoker.getStreamHandlers()) {
78             assertTrue(h instanceof StreamHandler);
79         }
80
81         assertEquals(HANDLER_COUNT, invoker.getProtocolHandlers().size());
82         for (Handler h : invoker.getProtocolHandlers()) {
83             assertTrue(!(h instanceof LogicalHandler));
84         }
85
86     }
87
88     
89     public void testInvokeHandlersOutbound() {
90
91         assertEquals(0, invoker.getInvokedHandlers().size());
92         assertTrue(invoker.isOutbound());
93
94         checkLogicalHandlersInvoked(true, false);
95         
96         assertTrue(invoker.isOutbound());
97         assertEquals(2, invoker.getInvokedHandlers().size());
98         checkProtocolHandlersInvoked(true);
99         assertTrue(invoker.isOutbound());
100         assertEquals(4, invoker.getInvokedHandlers().size());
101         assertFalse(invoker.isClosed());
102
103         assertTrue(logicalHandlers[0].getInvokedOrder() < logicalHandlers[1].getInvokedOrder());
104         assertTrue(logicalHandlers[1].getInvokedOrder() < protocolHandlers[0].getInvokedOrder());
105         assertTrue(protocolHandlers[0].getInvokedOrder() < protocolHandlers[1].getInvokedOrder());
106     }
107
108     public void testInvokeHandlersInbound() {
109
110         invoker.setInbound();
111         assertTrue(invoker.isInbound());
112         checkProtocolHandlersInvoked(false);
113
114         assertEquals(2, invoker.getInvokedHandlers().size());
115         assertTrue(invoker.isInbound());
116
117         checkLogicalHandlersInvoked(false, true);
118         assertEquals(4, invoker.getInvokedHandlers().size());
119         assertTrue(invoker.isInbound());
120
121         checkStreamHandlersInvoked(false, true);
122
123         assertFalse(invoker.isClosed());
124         assertTrue(logicalHandlers[0].getInvokedOrder() > logicalHandlers[1].getInvokedOrder());
125         assertTrue(logicalHandlers[1].getInvokedOrder() > protocolHandlers[0].getInvokedOrder());
126         assertTrue(protocolHandlers[0].getInvokedOrder() > protocolHandlers[1].getInvokedOrder());
127     }
128         
129     public void testLogicalHandlerOutboundProcessingStoppedResponseExpected() {
130
131         assertEquals(0, logicalHandlers[0].getHandleMessageCount());
132         assertEquals(0, logicalHandlers[1].getHandleMessageCount());
133
134         assertTrue(invoker.isOutbound());
135          
136         // invoke the handlers. when a handler returns false, processing
137
// of handlers is stopped and message direction is reversed.
138
logicalHandlers[0].setHandleMessageRet(false);
139         boolean ret = invoker.invokeLogicalHandlers(false, ctx);
140                 
141         assertEquals(false, ret);
142         assertFalse(invoker.isClosed());
143         assertEquals(1, logicalHandlers[0].getHandleMessageCount());
144         assertEquals(0, logicalHandlers[1].getHandleMessageCount());
145         assertTrue(invoker.isInbound());
146         
147         // the next time invokeHandler is invoked, the 'next' handler is invoked.
148
// As message direction has been reversed this means the that the previous
149
// one on the list is actually invoked.
150
logicalHandlers[0].setHandleMessageRet(true);
151         
152         ret = invoker.invokeLogicalHandlers(false, ctx);
153         assertTrue(ret);
154         assertFalse(invoker.isClosed());
155         assertEquals(1, logicalHandlers[0].getHandleMessageCount());
156         assertEquals(0, logicalHandlers[1].getHandleMessageCount());
157         assertTrue(invoker.isInbound());
158     }
159     
160     public void testLogicalHandlerInboundProcessingStoppedResponseExpected() {
161
162         assertEquals(0, logicalHandlers[0].getHandleMessageCount());
163         assertEquals(0, logicalHandlers[1].getHandleMessageCount());
164
165         invoker.setInbound();
166          
167         logicalHandlers[1].setHandleMessageRet(false);
168         boolean ret = invoker.invokeLogicalHandlers(false, ctx);
169         assertFalse(invoker.isClosed());
170                 
171         assertEquals(false, ret);
172         assertEquals(0, logicalHandlers[0].getHandleMessageCount());
173         assertEquals(1, logicalHandlers[1].getHandleMessageCount());
174         assertTrue(invoker.isOutbound());
175     }
176
177
178     public void testHandleMessageThrowsProtocolException() {
179
180         assertFalse(invoker.faultRaised(ctx));
181         
182         ProtocolException pe = new ProtocolException("banzai");
183         logicalHandlers[1].setException(pe);
184
185         boolean continueProcessing = invoker.invokeLogicalHandlers(false, ctx);
186         assertFalse(continueProcessing);
187         assertTrue(invoker.faultRaised(ctx));
188
189         assertEquals(1, logicalHandlers[0].getHandleMessageCount());
190         assertEquals(1, logicalHandlers[1].getHandleMessageCount());
191         continueProcessing = invoker.invokeLogicalHandlers(false, ctx);
192         assertTrue(continueProcessing);
193         assertTrue(invoker.faultRaised(ctx));
194         assertFalse(invoker.isClosed());
195         assertSame(pe, ctx.getException());
196
197         assertEquals(1, logicalHandlers[0].getHandleMessageCount());
198         assertEquals(1, logicalHandlers[1].getHandleMessageCount());
199         assertEquals(1, logicalHandlers[0].getHandleFaultCount());
200         assertEquals(0, logicalHandlers[1].getHandleFaultCount());
201
202         assertTrue(logicalHandlers[1].getInvokedOrder()
203                    < logicalHandlers[0].getInvokedOrder());
204     }
205
206
207     public void testHandleMessageThrowsRuntimeException() {
208
209         assertFalse(invoker.faultRaised(ctx));
210         
211         RuntimeException JavaDoc re = new RuntimeException JavaDoc("banzai");
212         logicalHandlers[1].setException(re);
213
214         boolean continueProcessing = invoker.invokeLogicalHandlers(false, ctx);
215         assertFalse(continueProcessing);
216         assertFalse(invoker.faultRaised(ctx));
217         assertTrue(invoker.isClosed());
218
219         assertEquals(1, logicalHandlers[0].getHandleMessageCount());
220         assertEquals(1, logicalHandlers[1].getHandleMessageCount());
221
222         // should this throw exception???
223
continueProcessing = invoker.invokeLogicalHandlers(false, ctx);
224         assertFalse(continueProcessing);
225
226         assertEquals(1, logicalHandlers[0].getHandleMessageCount());
227         assertEquals(1, logicalHandlers[1].getHandleMessageCount());
228         assertEquals(0, logicalHandlers[0].getHandleFaultCount());
229         assertEquals(0, logicalHandlers[1].getHandleFaultCount());
230     }
231     
232
233     public void testHandleFault() {
234
235         // put invoker into fault state
236
ProtocolException pe = new ProtocolException("banzai");
237         invoker.setFault(ctx, pe);
238
239         boolean continueProcessing = invoker.invokeLogicalHandlers(false, ctx);
240         assertTrue(continueProcessing);
241         assertEquals(0, logicalHandlers[0].getHandleMessageCount());
242         assertEquals(0, logicalHandlers[1].getHandleMessageCount());
243         assertEquals(1, logicalHandlers[0].getHandleFaultCount());
244         assertEquals(1, logicalHandlers[1].getHandleFaultCount());
245
246         assertTrue(logicalHandlers[0].getInvokedOrder() < logicalHandlers[1].getInvokedOrder());
247     }
248
249
250     public void testFaultRaised() {
251
252         assertFalse(invoker.faultRaised(ctx));
253
254         invoker.setFault(ctx, new ProtocolException("test exception"));
255         assertTrue(invoker.faultRaised(ctx));
256
257         // reset
258
invoker.setFault(ctx, null);
259         assertFalse(invoker.faultRaised(ctx));
260
261         invoker.setFault(true);
262         assertTrue(invoker.faultRaised(ctx));
263
264         // reset
265
invoker.setFault(false);
266         invoker.setFault(ctx, null);
267         assertFalse(invoker.faultRaised(ctx));
268
269         invoker.setFault(true);
270         invoker.setFault(ctx, new ProtocolException("test exception"));
271     }
272
273
274
275     public void testHandleFaultThrowsProtocolException() {
276
277         doHandleFaultExceptionTest(new ProtocolException("banzai"));
278     }
279
280     public void testHandleFaultThrowsRuntimeException() {
281
282         doHandleFaultExceptionTest(new RuntimeException JavaDoc("banzai"));
283     }
284
285
286     public void testMEPComplete() {
287
288         invoker.invokeLogicalHandlers(false, ctx);
289         doInvokeProtocolHandlers(false);
290         TestInputStreamMessageContext istreamCtx = new TestInputStreamMessageContext(ctx);
291         invoker.invokeStreamHandlers(istreamCtx);
292         assertEquals(6, invoker.getInvokedHandlers().size());
293
294         invoker.mepComplete(ctx);
295
296         assertTrue("close not invoked on logicalHandlers", logicalHandlers[0].isCloseInvoked());
297         assertTrue("close not invoked on logicalHandlers", logicalHandlers[1].isCloseInvoked());
298         assertTrue("close not invoked on protocolHandlers", protocolHandlers[0].isCloseInvoked());
299         assertTrue("close not invoked on protocolHandlers", protocolHandlers[1].isCloseInvoked());
300         assertTrue("close not invoked on streamHandlers", streamHandlers[0].isCloseInvoked());
301         assertTrue("close not invoked on streamHandlers", streamHandlers[1].isCloseInvoked());
302
303         assertTrue("incorrect invocation order of close", protocolHandlers[1].getInvokedOrder()
304                    < protocolHandlers[0].getInvokedOrder());
305         assertTrue("incorrect invocation order of close", protocolHandlers[0].getInvokedOrder()
306                    < logicalHandlers[1].getInvokedOrder());
307         assertTrue("incorrect invocation order of close", logicalHandlers[1].getInvokedOrder()
308                    < logicalHandlers[0].getInvokedOrder());
309     }
310
311
312     public void testResponseExpectedDefault() {
313         assertTrue(invoker.isResponseExpected());
314     }
315     
316     /* test invoking logical handlers when processing has been aborted
317      * with both protocol and logical handlers in invokedHandlers list.
318      *
319      */

320     public void testInvokedAlreadyInvokedMixed() {
321
322         // simulate an invocation being aborted by a logical handler
323
//
324
logicalHandlers[1].setHandleMessageRet(false);
325         invoker.setInbound();
326         //invoker.invokeProtocolHandlers(true, soapContext);
327
doInvokeProtocolHandlers(true);
328         invoker.invokeLogicalHandlers(true, ctx);
329
330         assertEquals(2, invoker.getInvokedHandlers().size());
331         assertTrue(!invoker.getInvokedHandlers().contains(logicalHandlers[1]));
332         assertTrue(invoker.getInvokedHandlers().contains(protocolHandlers[0]));
333         assertTrue(invoker.getInvokedHandlers().contains(protocolHandlers[1]));
334         assertEquals(0, logicalHandlers[0].getHandleMessageCount());
335         assertEquals(1, logicalHandlers[1].getHandleMessageCount());
336         assertEquals(1, protocolHandlers[0].getHandleMessageCount());
337
338         assertEquals(1, protocolHandlers[1].getHandleMessageCount());
339
340         // now, invoke handlers on outbound leg
341
invoker.invokeLogicalHandlers(true, ctx);
342
343         assertEquals(1, logicalHandlers[1].getHandleMessageCount());
344         assertEquals(0, logicalHandlers[0].getHandleMessageCount());
345         assertEquals(1, protocolHandlers[0].getHandleMessageCount());
346         assertEquals(1, protocolHandlers[1].getHandleMessageCount());
347
348     }
349     
350     protected void checkLogicalHandlersInvoked(boolean outboundProperty, boolean requestorProperty) {
351
352         invoker.invokeLogicalHandlers(requestorProperty, ctx);
353
354         assertNotNull(ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY));
355         assertEquals(outboundProperty, ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY));
356         assertNotNull(ctx.get(ObjectMessageContext.REQUESTOR_ROLE_PROPERTY));
357         assertEquals(requestorProperty, ctx.get(ObjectMessageContext.REQUESTOR_ROLE_PROPERTY));
358         assertTrue("handler not invoked", logicalHandlers[0].isHandleMessageInvoked());
359         assertTrue("handler not invoked", logicalHandlers[1].isHandleMessageInvoked());
360         assertTrue(invoker.getInvokedHandlers().contains(logicalHandlers[0]));
361         assertTrue(invoker.getInvokedHandlers().contains(logicalHandlers[1]));
362     }
363     
364     protected void checkProtocolHandlersInvoked(boolean outboundProperty) {
365
366         soapContext.put(MessageContext.MESSAGE_OUTBOUND_PROPERTY, invoker.isOutbound());
367         EasyMock.expectLastCall().andReturn(null);
368         EasyMock.replay(soapContext);
369
370         invoker.invokeProtocolHandlers(false, soapContext);
371
372         EasyMock.verify(soapContext);
373
374         assertTrue("handler not invoked", protocolHandlers[0].isHandleMessageInvoked());
375         assertTrue("handler not invoked", protocolHandlers[1].isHandleMessageInvoked());
376
377         assertTrue(invoker.getInvokedHandlers().contains(protocolHandlers[0]));
378         assertTrue(invoker.getInvokedHandlers().contains(protocolHandlers[1]));
379     }
380     
381     protected void checkStreamHandlersInvoked(boolean outboundProperty, boolean requestorProperty) {
382         
383         InputStreamMessageContext istreamCtx = new TestInputStreamMessageContext(ctx);
384         invoker.invokeStreamHandlers(istreamCtx);
385                  
386         assertNotNull(ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY));
387         assertEquals(outboundProperty, ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY));
388         assertNotNull(ctx.get(ObjectMessageContext.REQUESTOR_ROLE_PROPERTY));
389         assertEquals(requestorProperty, ctx.get(ObjectMessageContext.REQUESTOR_ROLE_PROPERTY));
390         assertTrue("handler not invoked", streamHandlers[0].isHandleMessageInvoked());
391         assertTrue("handler not invoked", streamHandlers[1].isHandleMessageInvoked());
392         assertTrue(invoker.getInvokedHandlers().contains(streamHandlers[0]));
393         assertTrue(invoker.getInvokedHandlers().contains(streamHandlers[1]));
394     }
395     
396     private void doHandleFaultExceptionTest(RuntimeException JavaDoc e) {
397
398         // put invoker into fault state
399
ProtocolException pe = new ProtocolException("banzai");
400         invoker.setFault(ctx, pe);
401
402         // throw exception during handleFault processing
403
logicalHandlers[0].setException(e);
404         boolean continueProcessing = invoker.invokeLogicalHandlers(false, ctx);
405         assertFalse(continueProcessing);
406         assertTrue(invoker.isClosed());
407         assertEquals(1, logicalHandlers[0].getHandleFaultCount());
408         assertEquals(0, logicalHandlers[1].getHandleFaultCount());
409     }
410  
411     private boolean doInvokeProtocolHandlers(boolean requestor) {
412         soapContext.put(ObjectMessageContext.REQUESTOR_ROLE_PROPERTY, requestor);
413         EasyMock.expectLastCall().andReturn(null);
414         EasyMock.replay(soapContext);
415         return invoker.invokeProtocolHandlers(requestor, soapContext);
416     }
417     static class TestStreamHandler extends AbstractHandlerBase<StreamMessageContext>
418         implements StreamHandler {
419
420     }
421
422
423     static class TestProtocolHandler extends AbstractHandlerBase<SOAPMessageContext> {
424         
425     }
426     
427     
428
429     static class TestLogicalHandler extends AbstractHandlerBase<LogicalMessageContextImpl>
430         implements LogicalHandler<LogicalMessageContextImpl> {
431         
432     }
433     
434     static class AbstractHandlerBase<T extends MessageContext> implements Handler<T> {
435         
436         private static int sinvokedOrder;
437         private static int sid;
438        
439         private int invokeOrder;
440         private final int id = ++sid;
441         
442         private int handleMessageInvoked;
443         private int handleFaultInvoked;
444         private boolean handleMessageRet = true;
445         private final boolean handleFaultRet = true;
446         private RuntimeException JavaDoc exception;
447
448         private int closeInvoked;
449
450         public void reset() {
451             handleMessageInvoked = 0;
452             handleFaultInvoked = 0;
453             handleMessageRet = true;
454         }
455         
456         public boolean handleMessage(T arg0) {
457             invokeOrder = ++sinvokedOrder;
458             handleMessageInvoked++;
459
460             if (exception != null) {
461                 RuntimeException JavaDoc e = exception;
462                 exception = null;
463                 throw e;
464             }
465
466             return handleMessageRet;
467         }
468
469         public boolean handleFault(T arg0) {
470             invokeOrder = ++sinvokedOrder;
471             handleFaultInvoked++;
472
473             if (exception != null) {
474                 throw exception;
475             }
476
477             return handleFaultRet;
478         }
479
480         public void close(MessageContext arg0) {
481             invokeOrder = ++sinvokedOrder;
482             closeInvoked++;
483         }
484
485         
486         public void init(Map JavaDoc<String JavaDoc, Object JavaDoc> arg0) {
487             // TODO Auto-generated method stub
488
}
489
490         
491         public void destroy() {
492             // TODO Auto-generated method stub
493
}
494
495         public int getHandleMessageCount() {
496             return handleMessageInvoked;
497         }
498         
499         public int getHandleFaultCount() {
500             return handleFaultInvoked;
501         }
502
503         public boolean isHandleMessageInvoked() {
504             return handleMessageInvoked > 0;
505         }
506
507         public boolean isCloseInvoked() {
508             return closeInvoked > 0;
509         }
510
511         public int getCloseCount() {
512             return closeInvoked;
513         }
514
515         public int getInvokedOrder() {
516             return invokeOrder;
517         }
518         
519         public void setHandleMessageRet(boolean ret) {
520             handleMessageRet = ret;
521         }
522         
523         public void setHandleFaultRet(boolean ret) {
524             //handleFaultRet = ret;
525
}
526
527         public String JavaDoc toString() {
528             return "[" + super.toString() + " id: " + id + " invoke order: " + invokeOrder + "]";
529         }
530
531         
532         public void setException(RuntimeException JavaDoc rte) {
533             exception = rte;
534         }
535
536         public static void clear() {
537             sinvokedOrder = 0;
538             sid = 0;
539         }
540     }
541     
542     class TestInputStreamMessageContext extends MessageContextWrapper implements InputStreamMessageContext {
543
544         TestInputStreamMessageContext(MessageContext wrapped) {
545             super(wrapped);
546         }
547
548         public InputStream JavaDoc getInputStream() {
549             return null;
550         }
551
552         public boolean isFault() {
553             return false;
554         }
555
556         public void setFault(boolean isFault) {
557         }
558
559         public void setInputStream(InputStream JavaDoc ins) {
560         }
561
562     }
563 }
564
Popular Tags