1   /*
2    * $Id: StaticRecipientListRouterTestCase.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.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          UMOEndpoint endpoint1 = getTestEndpoint("Test1Provider", UMOEndpoint.ENDPOINT_TYPE_SENDER);
33          assertNotNull(endpoint1);
34  
35          List recipients = new ArrayList();
36          recipients.add("test://recipient1");
37          recipients.add("test://recipient2");
38          StaticRecipientList router = new StaticRecipientList();
39          router.setRecipients(recipients);
40  
41          List endpoints = new ArrayList();
42          endpoints.add(endpoint1);
43          router.setEndpoints(endpoints);
44  
45          assertEquals(2, router.getRecipients().size());
46  
47          UMOMessage message = new MuleMessage("test event");
48          assertTrue(router.isMatch(message));
49          // note this router clones endpoints so that the endpointUri can be
50          // changed
51  
52          // The static recipient list router duplicates the message for each endpoint
53          // so we can't
54          // check for equality on the arguments passed to the dispatch / send methods
55          session.expect("dispatchEvent", C.args(C.isA(UMOMessage.class), C.isA(UMOEndpoint.class)));
56          session.expect("dispatchEvent", C.args(C.isA(UMOMessage.class), C.isA(UMOEndpoint.class)));
57          router.route(message, (UMOSession)session.proxy(), false);
58          session.verify();
59  
60          message = new MuleMessage("test event");
61          router.getRecipients().add("test://recipient3");
62          session.expectAndReturn("sendEvent", C.args(C.isA(UMOMessage.class), C.isA(UMOEndpoint.class)),
63              message);
64          session.expectAndReturn("sendEvent", C.args(C.isA(UMOMessage.class), C.isA(UMOEndpoint.class)),
65              message);
66          session.expectAndReturn("sendEvent", C.args(C.isA(UMOMessage.class), C.isA(UMOEndpoint.class)),
67              message);
68          UMOMessage result = router.route(message, (UMOSession)session.proxy(), true);
69          assertNotNull(result);
70          assertTrue(result.getPayload() instanceof List);
71          assertEquals(3, ((List)result.getPayload()).size());
72          session.verify();
73  
74      }
75  
76      public void testBadRecipientListRouter() throws Exception
77      {
78          Mock session = MuleTestUtils.getMockSession();
79  
80          UMOEndpoint endpoint1 = getTestEndpoint("Test1Provider", UMOEndpoint.ENDPOINT_TYPE_SENDER);
81          assertNotNull(endpoint1);
82  
83          List recipients = new ArrayList();
84          recipients.add("malformed-endpointUri-recipient1");
85          StaticRecipientList router = new StaticRecipientList();
86          router.setRecipients(recipients);
87  
88          List endpoints = new ArrayList();
89          endpoints.add(endpoint1);
90          router.setEndpoints(endpoints);
91  
92          assertEquals(1, router.getRecipients().size());
93  
94          UMOMessage message = new MuleMessage("test event");
95          assertTrue(router.isMatch(message));
96          try
97          {
98              router.route(message, (UMOSession)session.proxy(), false);
99              fail("Should not allow malformed endpointUri");
100         }
101         catch (RoutingException e)
102         {
103             // ignore
104         }
105         session.verify();
106     }
107 
108 }