1   /*
2    * $Id: IdempotentReceiverTestCase.java 12111 2008-06-19 14:38:08Z dfeist $
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.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.MuleSession;
18  import org.mule.api.endpoint.ImmutableEndpoint;
19  import org.mule.api.routing.InboundRouterCollection;
20  import org.mule.api.service.Service;
21  import org.mule.routing.LoggingCatchAllStrategy;
22  import org.mule.tck.AbstractMuleTestCase;
23  import org.mule.tck.MuleTestUtils;
24  import org.mule.tck.testmodels.fruit.Apple;
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          Service testService = getTestService("test", Apple.class);
38  
39          InboundRouterCollection messageRouter = new DefaultInboundRouterCollection();
40  
41          messageRouter.addRouter(router);
42          messageRouter.setCatchAllStrategy(new LoggingCatchAllStrategy());
43  
44          MuleMessage message = new DefaultMuleMessage("test event");
45  
46          ImmutableEndpoint endpoint = getTestOutboundEndpoint("Test1Provider");
47          MuleEvent event = new DefaultMuleEvent(message, endpoint, (MuleSession) 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("getService", testService);
51  
52          assertTrue(router.isMatch(event));
53  
54          session.expect("dispatchEvent", C.eq(event));
55          // called by Inbound message router
56          session.expectAndReturn("getService", testService);
57  
58          // called by idempotent receiver
59          session.expectAndReturn("getService", testService);
60          messageRouter.route(event);
61  
62          session.verify();
63          message = new DefaultMuleMessage("test event");
64          event = new DefaultMuleEvent(message, endpoint, (MuleSession) session.proxy(), true);
65  
66          session.expectAndReturn("sendEvent", C.eq(event), message);
67          // called by idempotent receiver
68          session.expectAndReturn("getService", testService);
69          // called by Inbound message router
70          session.expectAndReturn("getService", testService);
71          MuleMessage 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("getService", testService);
79  
80          event = new DefaultMuleEvent(message, endpoint, (MuleSession) 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  }