1   /*
2    * $Id: StaticRecipientListRouterTestCase.java 10524 2008-01-24 19:20:11Z akuzmin $
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.outbound;
12  
13  import org.mule.impl.MuleMessage;
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.tck.MuleTestUtils;
16  import org.mule.umo.UMOMessage;
17  import org.mule.umo.UMOSession;
18  import org.mule.umo.endpoint.UMOEndpoint;
19  import org.mule.umo.routing.RoutingException;
20  
21  import com.mockobjects.dynamic.C;
22  import com.mockobjects.dynamic.Mock;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  public class StaticRecipientListRouterTestCase extends AbstractMuleTestCase
28  {
29      public void testRecipientListRouter() throws Exception
30      {
31          Mock session = MuleTestUtils.getMockSession();
32          session.matchAndReturn("getComponent", getTestComponent(getTestDescriptor("TEST", "java.lang.Object")));
33          UMOEndpoint endpoint1 = getTestEndpoint("Test1Provider", UMOEndpoint.ENDPOINT_TYPE_SENDER);
34          assertNotNull(endpoint1);
35  
36          List recipients = new ArrayList();
37          recipients.add("test://recipient1");
38          recipients.add("test://recipient2");
39          StaticRecipientList router = new StaticRecipientList();
40          router.setRecipients(recipients);
41  
42          List endpoints = new ArrayList();
43          endpoints.add(endpoint1);
44          router.setEndpoints(endpoints);
45  
46          assertEquals(2, router.getRecipients().size());
47  
48          UMOMessage message = new MuleMessage("test event");
49          assertTrue(router.isMatch(message));
50          // note this router clones endpoints so that the endpointUri can be
51          // changed
52  
53          // The static recipient list router duplicates the message for each endpoint
54          // so we can't
55          // check for equality on the arguments passed to the dispatch / send methods
56          session.expect("dispatchEvent", C.args(C.isA(UMOMessage.class), C.isA(UMOEndpoint.class)));
57          session.expect("dispatchEvent", C.args(C.isA(UMOMessage.class), C.isA(UMOEndpoint.class)));
58          router.route(message, (UMOSession)session.proxy(), false);
59          session.verify();
60  
61          message = new MuleMessage("test event");
62          router.getRecipients().add("test://recipient3");
63          session.expectAndReturn("sendEvent", C.args(C.isA(UMOMessage.class), C.isA(UMOEndpoint.class)),
64              message);
65          session.expectAndReturn("sendEvent", C.args(C.isA(UMOMessage.class), C.isA(UMOEndpoint.class)),
66              message);
67          session.expectAndReturn("sendEvent", C.args(C.isA(UMOMessage.class), C.isA(UMOEndpoint.class)),
68              message);
69          UMOMessage result = router.route(message, (UMOSession)session.proxy(), true);
70          assertNotNull(result);
71          assertTrue(result.getPayload() instanceof List);
72          assertEquals(3, ((List)result.getPayload()).size());
73          session.verify();
74  
75      }
76  
77      public void testBadRecipientListRouter() throws Exception
78      {
79          Mock session = MuleTestUtils.getMockSession();
80  
81          UMOEndpoint endpoint1 = getTestEndpoint("Test1Provider", UMOEndpoint.ENDPOINT_TYPE_SENDER);
82          assertNotNull(endpoint1);
83  
84          List recipients = new ArrayList();
85          recipients.add("malformed-endpointUri-recipient1");
86          StaticRecipientList router = new StaticRecipientList();
87          router.setRecipients(recipients);
88  
89          List endpoints = new ArrayList();
90          endpoints.add(endpoint1);
91          router.setEndpoints(endpoints);
92  
93          assertEquals(1, router.getRecipients().size());
94  
95          UMOMessage message = new MuleMessage("test event");
96          assertTrue(router.isMatch(message));
97          try
98          {
99              router.route(message, (UMOSession)session.proxy(), false);
100             fail("Should not allow malformed endpointUri");
101         }
102         catch (RoutingException e)
103         {
104             // ignore
105         }
106         session.verify();
107     }
108 
109 }