001: package org.objectweb.celtix.bus.handlers;
002:
003: import java.io.InputStream;
004: import java.util.ArrayList;
005: import java.util.List;
006: import java.util.Map;
007:
008: import javax.xml.ws.ProtocolException;
009: import javax.xml.ws.handler.Handler;
010: import javax.xml.ws.handler.LogicalHandler;
011: import javax.xml.ws.handler.MessageContext;
012: import javax.xml.ws.handler.soap.SOAPMessageContext;
013:
014: import junit.framework.TestCase;
015:
016: import org.easymock.classextension.EasyMock;
017: import org.objectweb.celtix.bus.context.LogicalMessageContextImpl;
018: import org.objectweb.celtix.context.InputStreamMessageContext;
019: import org.objectweb.celtix.context.MessageContextWrapper;
020: import org.objectweb.celtix.context.ObjectMessageContext;
021: import org.objectweb.celtix.context.ObjectMessageContextImpl;
022: import org.objectweb.celtix.context.StreamMessageContext;
023: import org.objectweb.celtix.handlers.StreamHandler;
024:
025: public class HandlerChainInvokerTest extends TestCase {
026:
027: private static final int HANDLER_COUNT = 2;
028:
029: HandlerChainInvoker invoker;
030:
031: ObjectMessageContextImpl ctx = new ObjectMessageContextImpl();
032: SOAPMessageContext soapContext;
033:
034: TestLogicalHandler[] logicalHandlers = new TestLogicalHandler[HANDLER_COUNT];
035: TestProtocolHandler[] protocolHandlers = new TestProtocolHandler[HANDLER_COUNT];
036: TestStreamHandler[] streamHandlers = new TestStreamHandler[HANDLER_COUNT];
037:
038: public void setUp() {
039: AbstractHandlerBase.clear();
040:
041: List<Handler> handlers = new ArrayList<Handler>();
042: for (int i = 0; i < logicalHandlers.length; i++) {
043: logicalHandlers[i] = new TestLogicalHandler();
044: handlers.add(logicalHandlers[i]);
045: }
046: for (int i = 0; i < protocolHandlers.length; i++) {
047: protocolHandlers[i] = new TestProtocolHandler();
048: handlers.add(protocolHandlers[i]);
049: }
050: for (int i = 0; i < protocolHandlers.length; i++) {
051: streamHandlers[i] = new TestStreamHandler();
052: handlers.add(streamHandlers[i]);
053: }
054: invoker = new HandlerChainInvoker(handlers);
055:
056: soapContext = EasyMock.createNiceMock(SOAPMessageContext.class);
057: }
058:
059: public void testInvokeEmptyHandlerChain() {
060: invoker = new HandlerChainInvoker(new ArrayList<Handler>());
061: assertTrue(invoker.invokeLogicalHandlers(false, ctx));
062: assertTrue(doInvokeProtocolHandlers(false));
063: assertTrue(invoker.invokeStreamHandlers(EasyMock
064: .createMock(InputStreamMessageContext.class)));
065: }
066:
067: public void testHandlerPartitioning() {
068:
069: assertEquals(HANDLER_COUNT, invoker.getLogicalHandlers().size());
070: for (Handler h : invoker.getLogicalHandlers()) {
071: assertTrue(h instanceof LogicalHandler);
072: }
073:
074: assertEquals(HANDLER_COUNT, invoker.getStreamHandlers().size());
075: for (Handler h : invoker.getStreamHandlers()) {
076: assertTrue(h instanceof StreamHandler);
077: }
078:
079: assertEquals(HANDLER_COUNT, invoker.getProtocolHandlers()
080: .size());
081: for (Handler h : invoker.getProtocolHandlers()) {
082: assertTrue(!(h instanceof LogicalHandler));
083: }
084:
085: }
086:
087: public void testInvokeHandlersOutbound() {
088:
089: assertEquals(0, invoker.getInvokedHandlers().size());
090: assertTrue(invoker.isOutbound());
091:
092: checkLogicalHandlersInvoked(true, false);
093:
094: assertTrue(invoker.isOutbound());
095: assertEquals(2, invoker.getInvokedHandlers().size());
096: checkProtocolHandlersInvoked(true);
097: assertTrue(invoker.isOutbound());
098: assertEquals(4, invoker.getInvokedHandlers().size());
099: assertFalse(invoker.isClosed());
100:
101: assertTrue(logicalHandlers[0].getInvokedOrder() < logicalHandlers[1]
102: .getInvokedOrder());
103: assertTrue(logicalHandlers[1].getInvokedOrder() < protocolHandlers[0]
104: .getInvokedOrder());
105: assertTrue(protocolHandlers[0].getInvokedOrder() < protocolHandlers[1]
106: .getInvokedOrder());
107: }
108:
109: public void testInvokeHandlersInbound() {
110:
111: invoker.setInbound();
112: assertTrue(invoker.isInbound());
113: checkProtocolHandlersInvoked(false);
114:
115: assertEquals(2, invoker.getInvokedHandlers().size());
116: assertTrue(invoker.isInbound());
117:
118: checkLogicalHandlersInvoked(false, true);
119: assertEquals(4, invoker.getInvokedHandlers().size());
120: assertTrue(invoker.isInbound());
121:
122: checkStreamHandlersInvoked(false, true);
123:
124: assertFalse(invoker.isClosed());
125: assertTrue(logicalHandlers[0].getInvokedOrder() > logicalHandlers[1]
126: .getInvokedOrder());
127: assertTrue(logicalHandlers[1].getInvokedOrder() > protocolHandlers[0]
128: .getInvokedOrder());
129: assertTrue(protocolHandlers[0].getInvokedOrder() > protocolHandlers[1]
130: .getInvokedOrder());
131: }
132:
133: public void testLogicalHandlerOutboundProcessingStoppedResponseExpected() {
134:
135: assertEquals(0, logicalHandlers[0].getHandleMessageCount());
136: assertEquals(0, logicalHandlers[1].getHandleMessageCount());
137:
138: assertTrue(invoker.isOutbound());
139:
140: // invoke the handlers. when a handler returns false, processing
141: // of handlers is stopped and message direction is reversed.
142: logicalHandlers[0].setHandleMessageRet(false);
143: boolean ret = invoker.invokeLogicalHandlers(false, ctx);
144:
145: assertEquals(false, ret);
146: assertFalse(invoker.isClosed());
147: assertEquals(1, logicalHandlers[0].getHandleMessageCount());
148: assertEquals(0, logicalHandlers[1].getHandleMessageCount());
149: assertTrue(invoker.isInbound());
150:
151: // the next time invokeHandler is invoked, the 'next' handler is invoked.
152: // As message direction has been reversed this means the that the previous
153: // one on the list is actually invoked.
154: logicalHandlers[0].setHandleMessageRet(true);
155:
156: ret = invoker.invokeLogicalHandlers(false, ctx);
157: assertTrue(ret);
158: assertFalse(invoker.isClosed());
159: assertEquals(1, logicalHandlers[0].getHandleMessageCount());
160: assertEquals(0, logicalHandlers[1].getHandleMessageCount());
161: assertTrue(invoker.isInbound());
162: }
163:
164: public void testLogicalHandlerInboundProcessingStoppedResponseExpected() {
165:
166: assertEquals(0, logicalHandlers[0].getHandleMessageCount());
167: assertEquals(0, logicalHandlers[1].getHandleMessageCount());
168:
169: invoker.setInbound();
170:
171: logicalHandlers[1].setHandleMessageRet(false);
172: boolean ret = invoker.invokeLogicalHandlers(false, ctx);
173: assertFalse(invoker.isClosed());
174:
175: assertEquals(false, ret);
176: assertEquals(0, logicalHandlers[0].getHandleMessageCount());
177: assertEquals(1, logicalHandlers[1].getHandleMessageCount());
178: assertTrue(invoker.isOutbound());
179: }
180:
181: public void testHandleMessageThrowsProtocolException() {
182:
183: assertFalse(invoker.faultRaised(ctx));
184:
185: ProtocolException pe = new ProtocolException("banzai");
186: logicalHandlers[1].setException(pe);
187:
188: boolean continueProcessing = invoker.invokeLogicalHandlers(
189: false, ctx);
190: assertFalse(continueProcessing);
191: assertTrue(invoker.faultRaised(ctx));
192:
193: assertEquals(1, logicalHandlers[0].getHandleMessageCount());
194: assertEquals(1, logicalHandlers[1].getHandleMessageCount());
195: continueProcessing = invoker.invokeLogicalHandlers(false, ctx);
196: assertTrue(continueProcessing);
197: assertTrue(invoker.faultRaised(ctx));
198: assertFalse(invoker.isClosed());
199: assertSame(pe, ctx.getException());
200:
201: assertEquals(1, logicalHandlers[0].getHandleMessageCount());
202: assertEquals(1, logicalHandlers[1].getHandleMessageCount());
203: assertEquals(1, logicalHandlers[0].getHandleFaultCount());
204: assertEquals(0, logicalHandlers[1].getHandleFaultCount());
205:
206: assertTrue(logicalHandlers[1].getInvokedOrder() < logicalHandlers[0]
207: .getInvokedOrder());
208: }
209:
210: public void testHandleMessageThrowsRuntimeException() {
211:
212: assertFalse(invoker.faultRaised(ctx));
213:
214: RuntimeException re = new RuntimeException("banzai");
215: logicalHandlers[1].setException(re);
216:
217: boolean continueProcessing = invoker.invokeLogicalHandlers(
218: false, ctx);
219: assertFalse(continueProcessing);
220: assertFalse(invoker.faultRaised(ctx));
221: assertTrue(invoker.isClosed());
222:
223: assertEquals(1, logicalHandlers[0].getHandleMessageCount());
224: assertEquals(1, logicalHandlers[1].getHandleMessageCount());
225:
226: // should this throw exception???
227: continueProcessing = invoker.invokeLogicalHandlers(false, ctx);
228: assertFalse(continueProcessing);
229:
230: assertEquals(1, logicalHandlers[0].getHandleMessageCount());
231: assertEquals(1, logicalHandlers[1].getHandleMessageCount());
232: assertEquals(0, logicalHandlers[0].getHandleFaultCount());
233: assertEquals(0, logicalHandlers[1].getHandleFaultCount());
234: }
235:
236: public void testHandleFault() {
237:
238: // put invoker into fault state
239: ProtocolException pe = new ProtocolException("banzai");
240: invoker.setFault(ctx, pe);
241:
242: boolean continueProcessing = invoker.invokeLogicalHandlers(
243: false, ctx);
244: assertTrue(continueProcessing);
245: assertEquals(0, logicalHandlers[0].getHandleMessageCount());
246: assertEquals(0, logicalHandlers[1].getHandleMessageCount());
247: assertEquals(1, logicalHandlers[0].getHandleFaultCount());
248: assertEquals(1, logicalHandlers[1].getHandleFaultCount());
249:
250: assertTrue(logicalHandlers[0].getInvokedOrder() < logicalHandlers[1]
251: .getInvokedOrder());
252: }
253:
254: public void testFaultRaised() {
255:
256: assertFalse(invoker.faultRaised(ctx));
257:
258: invoker.setFault(ctx, new ProtocolException("test exception"));
259: assertTrue(invoker.faultRaised(ctx));
260:
261: // reset
262: invoker.setFault(ctx, null);
263: assertFalse(invoker.faultRaised(ctx));
264:
265: invoker.setFault(true);
266: assertTrue(invoker.faultRaised(ctx));
267:
268: // reset
269: invoker.setFault(false);
270: invoker.setFault(ctx, null);
271: assertFalse(invoker.faultRaised(ctx));
272:
273: invoker.setFault(true);
274: invoker.setFault(ctx, new ProtocolException("test exception"));
275: }
276:
277: public void testHandleFaultThrowsProtocolException() {
278:
279: doHandleFaultExceptionTest(new ProtocolException("banzai"));
280: }
281:
282: public void testHandleFaultThrowsRuntimeException() {
283:
284: doHandleFaultExceptionTest(new RuntimeException("banzai"));
285: }
286:
287: public void testMEPComplete() {
288:
289: invoker.invokeLogicalHandlers(false, ctx);
290: doInvokeProtocolHandlers(false);
291: TestInputStreamMessageContext istreamCtx = new TestInputStreamMessageContext(
292: ctx);
293: invoker.invokeStreamHandlers(istreamCtx);
294: assertEquals(6, invoker.getInvokedHandlers().size());
295:
296: invoker.mepComplete(ctx);
297:
298: assertTrue("close not invoked on logicalHandlers",
299: logicalHandlers[0].isCloseInvoked());
300: assertTrue("close not invoked on logicalHandlers",
301: logicalHandlers[1].isCloseInvoked());
302: assertTrue("close not invoked on protocolHandlers",
303: protocolHandlers[0].isCloseInvoked());
304: assertTrue("close not invoked on protocolHandlers",
305: protocolHandlers[1].isCloseInvoked());
306: assertTrue("close not invoked on streamHandlers",
307: streamHandlers[0].isCloseInvoked());
308: assertTrue("close not invoked on streamHandlers",
309: streamHandlers[1].isCloseInvoked());
310:
311: assertTrue(
312: "incorrect invocation order of close",
313: protocolHandlers[1].getInvokedOrder() < protocolHandlers[0]
314: .getInvokedOrder());
315: assertTrue(
316: "incorrect invocation order of close",
317: protocolHandlers[0].getInvokedOrder() < logicalHandlers[1]
318: .getInvokedOrder());
319: assertTrue(
320: "incorrect invocation order of close",
321: logicalHandlers[1].getInvokedOrder() < logicalHandlers[0]
322: .getInvokedOrder());
323: }
324:
325: public void testResponseExpectedDefault() {
326: assertTrue(invoker.isResponseExpected());
327: }
328:
329: /* test invoking logical handlers when processing has been aborted
330: * with both protocol and logical handlers in invokedHandlers list.
331: *
332: */
333: public void testInvokedAlreadyInvokedMixed() {
334:
335: // simulate an invocation being aborted by a logical handler
336: //
337: logicalHandlers[1].setHandleMessageRet(false);
338: invoker.setInbound();
339: //invoker.invokeProtocolHandlers(true, soapContext);
340: doInvokeProtocolHandlers(true);
341: invoker.invokeLogicalHandlers(true, ctx);
342:
343: assertEquals(2, invoker.getInvokedHandlers().size());
344: assertTrue(!invoker.getInvokedHandlers().contains(
345: logicalHandlers[1]));
346: assertTrue(invoker.getInvokedHandlers().contains(
347: protocolHandlers[0]));
348: assertTrue(invoker.getInvokedHandlers().contains(
349: protocolHandlers[1]));
350: assertEquals(0, logicalHandlers[0].getHandleMessageCount());
351: assertEquals(1, logicalHandlers[1].getHandleMessageCount());
352: assertEquals(1, protocolHandlers[0].getHandleMessageCount());
353:
354: assertEquals(1, protocolHandlers[1].getHandleMessageCount());
355:
356: // now, invoke handlers on outbound leg
357: invoker.invokeLogicalHandlers(true, ctx);
358:
359: assertEquals(1, logicalHandlers[1].getHandleMessageCount());
360: assertEquals(0, logicalHandlers[0].getHandleMessageCount());
361: assertEquals(1, protocolHandlers[0].getHandleMessageCount());
362: assertEquals(1, protocolHandlers[1].getHandleMessageCount());
363:
364: }
365:
366: protected void checkLogicalHandlersInvoked(
367: boolean outboundProperty, boolean requestorProperty) {
368:
369: invoker.invokeLogicalHandlers(requestorProperty, ctx);
370:
371: assertNotNull(ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY));
372: assertEquals(outboundProperty, ctx
373: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY));
374: assertNotNull(ctx
375: .get(ObjectMessageContext.REQUESTOR_ROLE_PROPERTY));
376: assertEquals(requestorProperty, ctx
377: .get(ObjectMessageContext.REQUESTOR_ROLE_PROPERTY));
378: assertTrue("handler not invoked", logicalHandlers[0]
379: .isHandleMessageInvoked());
380: assertTrue("handler not invoked", logicalHandlers[1]
381: .isHandleMessageInvoked());
382: assertTrue(invoker.getInvokedHandlers().contains(
383: logicalHandlers[0]));
384: assertTrue(invoker.getInvokedHandlers().contains(
385: logicalHandlers[1]));
386: }
387:
388: protected void checkProtocolHandlersInvoked(boolean outboundProperty) {
389:
390: soapContext.put(MessageContext.MESSAGE_OUTBOUND_PROPERTY,
391: invoker.isOutbound());
392: EasyMock.expectLastCall().andReturn(null);
393: EasyMock.replay(soapContext);
394:
395: invoker.invokeProtocolHandlers(false, soapContext);
396:
397: EasyMock.verify(soapContext);
398:
399: assertTrue("handler not invoked", protocolHandlers[0]
400: .isHandleMessageInvoked());
401: assertTrue("handler not invoked", protocolHandlers[1]
402: .isHandleMessageInvoked());
403:
404: assertTrue(invoker.getInvokedHandlers().contains(
405: protocolHandlers[0]));
406: assertTrue(invoker.getInvokedHandlers().contains(
407: protocolHandlers[1]));
408: }
409:
410: protected void checkStreamHandlersInvoked(boolean outboundProperty,
411: boolean requestorProperty) {
412:
413: InputStreamMessageContext istreamCtx = new TestInputStreamMessageContext(
414: ctx);
415: invoker.invokeStreamHandlers(istreamCtx);
416:
417: assertNotNull(ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY));
418: assertEquals(outboundProperty, ctx
419: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY));
420: assertNotNull(ctx
421: .get(ObjectMessageContext.REQUESTOR_ROLE_PROPERTY));
422: assertEquals(requestorProperty, ctx
423: .get(ObjectMessageContext.REQUESTOR_ROLE_PROPERTY));
424: assertTrue("handler not invoked", streamHandlers[0]
425: .isHandleMessageInvoked());
426: assertTrue("handler not invoked", streamHandlers[1]
427: .isHandleMessageInvoked());
428: assertTrue(invoker.getInvokedHandlers().contains(
429: streamHandlers[0]));
430: assertTrue(invoker.getInvokedHandlers().contains(
431: streamHandlers[1]));
432: }
433:
434: private void doHandleFaultExceptionTest(RuntimeException e) {
435:
436: // put invoker into fault state
437: ProtocolException pe = new ProtocolException("banzai");
438: invoker.setFault(ctx, pe);
439:
440: // throw exception during handleFault processing
441: logicalHandlers[0].setException(e);
442: boolean continueProcessing = invoker.invokeLogicalHandlers(
443: false, ctx);
444: assertFalse(continueProcessing);
445: assertTrue(invoker.isClosed());
446: assertEquals(1, logicalHandlers[0].getHandleFaultCount());
447: assertEquals(0, logicalHandlers[1].getHandleFaultCount());
448: }
449:
450: private boolean doInvokeProtocolHandlers(boolean requestor) {
451: soapContext.put(ObjectMessageContext.REQUESTOR_ROLE_PROPERTY,
452: requestor);
453: EasyMock.expectLastCall().andReturn(null);
454: EasyMock.replay(soapContext);
455: return invoker.invokeProtocolHandlers(requestor, soapContext);
456: }
457:
458: static class TestStreamHandler extends
459: AbstractHandlerBase<StreamMessageContext> implements
460: StreamHandler {
461:
462: }
463:
464: static class TestProtocolHandler extends
465: AbstractHandlerBase<SOAPMessageContext> {
466:
467: }
468:
469: static class TestLogicalHandler extends
470: AbstractHandlerBase<LogicalMessageContextImpl> implements
471: LogicalHandler<LogicalMessageContextImpl> {
472:
473: }
474:
475: static class AbstractHandlerBase<T extends MessageContext>
476: implements Handler<T> {
477:
478: private static int sinvokedOrder;
479: private static int sid;
480:
481: private int invokeOrder;
482: private final int id = ++sid;
483:
484: private int handleMessageInvoked;
485: private int handleFaultInvoked;
486: private boolean handleMessageRet = true;
487: private final boolean handleFaultRet = true;
488: private RuntimeException exception;
489:
490: private int closeInvoked;
491:
492: public void reset() {
493: handleMessageInvoked = 0;
494: handleFaultInvoked = 0;
495: handleMessageRet = true;
496: }
497:
498: public boolean handleMessage(T arg0) {
499: invokeOrder = ++sinvokedOrder;
500: handleMessageInvoked++;
501:
502: if (exception != null) {
503: RuntimeException e = exception;
504: exception = null;
505: throw e;
506: }
507:
508: return handleMessageRet;
509: }
510:
511: public boolean handleFault(T arg0) {
512: invokeOrder = ++sinvokedOrder;
513: handleFaultInvoked++;
514:
515: if (exception != null) {
516: throw exception;
517: }
518:
519: return handleFaultRet;
520: }
521:
522: public void close(MessageContext arg0) {
523: invokeOrder = ++sinvokedOrder;
524: closeInvoked++;
525: }
526:
527: public void init(Map<String, Object> arg0) {
528: // TODO Auto-generated method stub
529: }
530:
531: public void destroy() {
532: // TODO Auto-generated method stub
533: }
534:
535: public int getHandleMessageCount() {
536: return handleMessageInvoked;
537: }
538:
539: public int getHandleFaultCount() {
540: return handleFaultInvoked;
541: }
542:
543: public boolean isHandleMessageInvoked() {
544: return handleMessageInvoked > 0;
545: }
546:
547: public boolean isCloseInvoked() {
548: return closeInvoked > 0;
549: }
550:
551: public int getCloseCount() {
552: return closeInvoked;
553: }
554:
555: public int getInvokedOrder() {
556: return invokeOrder;
557: }
558:
559: public void setHandleMessageRet(boolean ret) {
560: handleMessageRet = ret;
561: }
562:
563: public void setHandleFaultRet(boolean ret) {
564: //handleFaultRet = ret;
565: }
566:
567: public String toString() {
568: return "[" + super .toString() + " id: " + id
569: + " invoke order: " + invokeOrder + "]";
570: }
571:
572: public void setException(RuntimeException rte) {
573: exception = rte;
574: }
575:
576: public static void clear() {
577: sinvokedOrder = 0;
578: sid = 0;
579: }
580: }
581:
582: class TestInputStreamMessageContext extends MessageContextWrapper
583: implements InputStreamMessageContext {
584:
585: TestInputStreamMessageContext(MessageContext wrapped) {
586: super (wrapped);
587: }
588:
589: public InputStream getInputStream() {
590: return null;
591: }
592:
593: public boolean isFault() {
594: return false;
595: }
596:
597: public void setFault(boolean isFault) {
598: }
599:
600: public void setInputStream(InputStream ins) {
601: }
602:
603: }
604: }
|