1   /*
2    * $Id: EndpointSelectorTestCase.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.CouldNotRouteOutboundMessageException;
20  
21  import com.mockobjects.dynamic.C;
22  import com.mockobjects.dynamic.Mock;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  /**
30   * @author <a href="mailto:carlson@hotpop.com">Travis Carlson</a> Sample config:
31   *         <outbound-router> <router
32   *         className="org.mule.routing.outbound.EndpointSelector"> <endpoint
33   *         name="dest1" address="jms://queue1" /> <endpoint name="dest2"
34   *         address="jms://queue2" /> <endpoint name="dest3" address="jms://queue3" />
35   *         <properties> <property name="selector" value="endpoint" /> </properties>
36   *         </router> </outbound-router>
37   */
38  public class EndpointSelectorTestCase extends AbstractMuleTestCase
39  {
40      Mock session;
41      UMOEndpoint dest1;
42      UMOEndpoint dest2;
43      UMOEndpoint dest3;
44      EndpointSelector router;
45  
46      protected void doSetUp() throws Exception
47      {
48          super.doSetUp();
49          session = MuleTestUtils.getMockSession();
50          dest1 = getTestEndpoint("dest1", UMOEndpoint.ENDPOINT_TYPE_SENDER);
51          dest2 = getTestEndpoint("dest2", UMOEndpoint.ENDPOINT_TYPE_SENDER);
52          dest3 = getTestEndpoint("dest3", UMOEndpoint.ENDPOINT_TYPE_SENDER);
53  
54          List endpoints = new ArrayList();
55          endpoints.add(dest1);
56          endpoints.add(dest2);
57          endpoints.add(dest3);
58  
59          router = new EndpointSelector();
60          router.setEndpoints(endpoints);
61      }
62  
63      public void testSelectEndpointDefaultProperty() throws Exception
64      {
65          Map props = new HashMap();
66          props.put("apple", "red");
67          props.put(router.getSelectorProperty(), "dest3");
68          props.put("banana", "yellow");
69          UMOMessage message = new MuleMessage("test event", props);
70  
71          assertTrue(router.isMatch(message));
72          session.expect("dispatchEvent", C.eq(message, dest3));
73          router.route(message, (UMOSession)session.proxy(), false);
74          session.verify();
75      }
76  
77      public void testSelectEndpointCustomProperty() throws Exception
78      {
79          // The "wayOut" property will determine which endpoint the message gets sent
80          // to.
81          router.setSelectorProperty("wayOut");
82  
83          Map props = new HashMap();
84          props.put("apple", "red");
85          props.put("wayOut", "dest2");
86          props.put("banana", "yellow");
87          UMOMessage message = new MuleMessage("test event", props);
88  
89          assertTrue(router.isMatch(message));
90          session.expect("dispatchEvent", C.eq(message, dest2));
91          router.route(message, (UMOSession)session.proxy(), false);
92          session.verify();
93      }
94  
95      public void testSelectEndpointNoMatch() throws Exception
96      {
97          Map props = new HashMap();
98          props.put(router.getSelectorProperty(), "dest5");
99          UMOMessage message = new MuleMessage("test event", props);
100 
101         try
102         {
103             router.route(message, (UMOSession)session.proxy(), false);
104             fail("Router should have thrown an exception if endpoint was not found.");
105         }
106         catch (CouldNotRouteOutboundMessageException e)
107         {
108             // expected
109         }
110     }
111 
112     public void testSelectEndpointNoPropertySet() throws Exception
113     {
114         UMOMessage message = new MuleMessage("test event");
115 
116         try
117         {
118             router.route(message, (UMOSession)session.proxy(), false);
119             fail("Router should have thrown an exception if no selector property was set on the message.");
120         }
121         catch (IllegalArgumentException e)
122         {
123             // expected
124         }
125     }
126 }