1   /*
2    * $Id: IdempotentReceiverTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.routing.inbound;
12  
13  import org.mule.impl.MuleEvent;
14  import org.mule.impl.MuleMessage;
15  import org.mule.routing.LoggingCatchAllStrategy;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.tck.MuleTestUtils;
18  import org.mule.tck.testmodels.fruit.Apple;
19  import org.mule.umo.UMOComponent;
20  import org.mule.umo.UMOEvent;
21  import org.mule.umo.UMOMessage;
22  import org.mule.umo.UMOSession;
23  import org.mule.umo.endpoint.UMOEndpoint;
24  import org.mule.umo.routing.UMOInboundRouter;
25  import org.mule.umo.routing.UMOInboundRouterCollection;
26  
27  import com.mockobjects.dynamic.C;
28  import com.mockobjects.dynamic.Mock;
29  
30  public class IdempotentReceiverTestCase extends AbstractMuleTestCase
31  {
32  
33      public void testIdempotentReceiverWithoutPersistence() throws Exception
34      {
35          IdempotentReceiver router = new IdempotentReceiver();
36          router.setDisablePersistence(true);
37          doTestIdempotentReceiver(router);
38      }
39  
40      public void testIdempotentReceiverWithPersistence() throws Exception
41      {
42          IdempotentReceiver router = new IdempotentReceiver();
43          doTestIdempotentReceiver(router);
44      }
45  
46      private void doTestIdempotentReceiver(UMOInboundRouter router) throws Exception
47      {
48          Mock session = MuleTestUtils.getMockSession();
49          UMOComponent testComponent = getTestComponent(getTestDescriptor("test", Apple.class.getName()));
50  
51          UMOInboundRouterCollection messageRouter = new InboundRouterCollection();
52  
53          messageRouter.addRouter(router);
54          messageRouter.setCatchAllStrategy(new LoggingCatchAllStrategy());
55  
56          UMOMessage message = new MuleMessage("test event");
57  
58          UMOEndpoint endpoint = getTestEndpoint("Test1Provider", UMOEndpoint.ENDPOINT_TYPE_SENDER);
59          UMOEvent event = new MuleEvent(message, endpoint, (UMOSession)session.proxy(), false);
60          // called by idempotent receiver as this is the fist event it will try
61          // and load the id store
62          session.expectAndReturn("getComponent", testComponent);
63  
64          assertTrue(router.isMatch(event));
65  
66          session.expect("dispatchEvent", C.eq(event));
67          // called by Inbound message router
68          session.expectAndReturn("getComponent", testComponent);
69  
70          // called by idempotent receiver
71          session.expectAndReturn("getComponent", testComponent);
72          messageRouter.route(event);
73  
74          session.verify();
75          message = new MuleMessage("test event");
76          event = new MuleEvent(message, endpoint, (UMOSession)session.proxy(), true);
77  
78          session.expectAndReturn("sendEvent", C.eq(event), message);
79          // called by idempotent receiver
80          session.expectAndReturn("getComponent", testComponent);
81          // called by Inbound message router
82          session.expectAndReturn("getComponent", testComponent);
83          UMOMessage result = messageRouter.route(event);
84          assertNotNull(result);
85          assertEquals(message, result);
86          session.verify();
87  
88          session.expect("toString");
89          // called by idempotent receiver
90          session.expectAndReturn("getComponent", testComponent);
91  
92          event = new MuleEvent(message, endpoint, (UMOSession)session.proxy(), false);
93          // we've already received this message
94          assertTrue(!router.isMatch(event));
95  
96          messageRouter.route(event);
97          session.verify();
98  
99      }
100 }