1   /*
2    * $Id: IdempotentReceiverTestCase.java 8942 2007-10-05 21:15:26Z holger $
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.UMOInboundRouterCollection;
25  
26  import com.mockobjects.dynamic.C;
27  import com.mockobjects.dynamic.Mock;
28  
29  public class IdempotentReceiverTestCase extends AbstractMuleTestCase
30  {
31  
32      public void testIdempotentReceiver() throws Exception
33      {
34          IdempotentReceiver router = new IdempotentReceiver();
35  
36          Mock session = MuleTestUtils.getMockSession();
37          UMOComponent testComponent = getTestComponent(getTestDescriptor("test", Apple.class.getName()));
38  
39          UMOInboundRouterCollection messageRouter = new InboundRouterCollection();
40  
41          messageRouter.addRouter(router);
42          messageRouter.setCatchAllStrategy(new LoggingCatchAllStrategy());
43  
44          UMOMessage message = new MuleMessage("test event");
45  
46          UMOEndpoint endpoint = getTestEndpoint("Test1Provider", UMOEndpoint.ENDPOINT_TYPE_SENDER);
47          UMOEvent event = new MuleEvent(message, endpoint, (UMOSession) session.proxy(), false);
48          // called by idempotent receiver as this is the fist event it will try
49          // and initialize the id store
50          session.expectAndReturn("getComponent", testComponent);
51  
52          assertTrue(router.isMatch(event));
53  
54          session.expect("dispatchEvent", C.eq(event));
55          // called by Inbound message router
56          session.expectAndReturn("getComponent", testComponent);
57  
58          // called by idempotent receiver
59          session.expectAndReturn("getComponent", testComponent);
60          messageRouter.route(event);
61  
62          session.verify();
63          message = new MuleMessage("test event");
64          event = new MuleEvent(message, endpoint, (UMOSession) session.proxy(), true);
65  
66          session.expectAndReturn("sendEvent", C.eq(event), message);
67          // called by idempotent receiver
68          session.expectAndReturn("getComponent", testComponent);
69          // called by Inbound message router
70          session.expectAndReturn("getComponent", testComponent);
71          UMOMessage result = messageRouter.route(event);
72          assertNotNull(result);
73          assertEquals(message, result);
74          session.verify();
75  
76          session.expect("toString");
77          // called by idempotent receiver
78          session.expectAndReturn("getComponent", testComponent);
79  
80          event = new MuleEvent(message, endpoint, (UMOSession) session.proxy(), false);
81          // we've already received this message
82          assertTrue(!router.isMatch(event));
83  
84          messageRouter.route(event);
85          session.verify();
86      }
87  
88  }