1   /*
2    * $Id: CatchAllStrategiesTestCase.java 11728 2008-05-13 07:31:11Z 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;
12  
13  import org.mule.impl.MuleEvent;
14  import org.mule.impl.MuleMessage;
15  import org.mule.impl.endpoint.MuleEndpointURI;
16  import org.mule.routing.filters.PayloadTypeFilter;
17  import org.mule.routing.outbound.FilteringOutboundRouter;
18  import org.mule.routing.outbound.OutboundRouterCollection;
19  import org.mule.tck.AbstractMuleTestCase;
20  import org.mule.tck.MuleTestUtils;
21  import org.mule.transformers.AbstractTransformer;
22  import org.mule.umo.UMOEvent;
23  import org.mule.umo.UMOMessage;
24  import org.mule.umo.UMOSession;
25  import org.mule.umo.endpoint.UMOEndpoint;
26  import org.mule.umo.provider.UMOMessageDispatcher;
27  import org.mule.umo.routing.ComponentRoutingException;
28  import org.mule.umo.transformer.TransformerException;
29  
30  import com.mockobjects.constraint.Constraint;
31  import com.mockobjects.dynamic.C;
32  import com.mockobjects.dynamic.Mock;
33  
34  import java.util.HashMap;
35  
36  public class CatchAllStrategiesTestCase extends AbstractMuleTestCase
37  {
38  
39      public void testLoggingOnlyStrategy() throws Exception
40      {
41          UMOEvent event = getTestEvent("UncaughtEvent");
42          LoggingCatchAllStrategy strategy = new LoggingCatchAllStrategy();
43          try
44          {
45              strategy.setEndpoint(getTestEndpoint("testProvider", UMOEndpoint.ENDPOINT_TYPE_SENDER));
46              fail("Illegal operation exception should have been thrown");
47          }
48          catch (Exception e)
49          {
50              // expected
51          }
52  
53          assertNull(strategy.getEndpoint());
54          strategy.catchMessage(event.getMessage(), null, false);
55      }
56  
57      public void testForwardingStrategy() throws Exception
58      {
59          ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
60          Mock endpoint = MuleTestUtils.getMockEndpoint();
61          Mock dispatcher = new Mock(UMOMessageDispatcher.class);
62          Mock connector = MuleTestUtils.getMockConnector();
63          UMOEvent event = getTestEvent("UncaughtEvent");
64          strategy.setEndpoint((UMOEndpoint)endpoint.proxy());
65  
66          endpoint.expectAndReturn("getProperties", new HashMap());
67          endpoint.expectAndReturn("getProperties", new HashMap());
68          endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy"));
69          endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy"));
70          endpoint.expect("dispatch", C.isA(MuleEvent.class));
71  
72          strategy.catchMessage(event.getMessage(), null, false);
73  
74          endpoint.verify();
75          dispatcher.verify();
76          connector.verify();
77  
78          assertNotNull(strategy.getEndpoint());
79      }
80      
81      /**
82       * Test for MULE-3034
83       */
84      public void testForwardingStrategyNullEndpoint() throws Exception
85      {
86          ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
87          strategy.setEndpoint(null);
88          UMOEvent event = getTestEvent("UncaughtEvent");
89          UMOSession session = getTestSession(getTestComponent(getTestDescriptor("test", "test")));
90  
91          try
92          {
93              strategy.catchMessage(event.getMessage(), session, false);
94              fail();
95          }
96          catch (ComponentRoutingException cre)
97          {
98              // we expected this exception
99          }
100     }
101 
102     private class TestEventTransformer extends AbstractTransformer
103     {
104         public Object doTransform(Object src, String encoding) throws TransformerException
105         {
106             return "Transformed Test Data";
107         }
108     }
109 
110     public void testForwardingStrategyWithTransform() throws Exception
111     {
112         ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
113         strategy.setSendTransformed(true);
114         Mock endpoint = MuleTestUtils.getMockEndpoint();
115         Mock dispatcher = new Mock(UMOMessageDispatcher.class);
116         Mock connector = MuleTestUtils.getMockConnector();
117         UMOEvent event = getTestEvent("UncaughtEvent");
118         strategy.setEndpoint((UMOEndpoint)endpoint.proxy());
119 
120         endpoint.expectAndReturn("getTransformer", new TestEventTransformer());
121         endpoint.expectAndReturn("getTransformer", new TestEventTransformer());
122         endpoint.expectAndReturn("getProperties", new HashMap());
123         endpoint.expectAndReturn("getProperties", new HashMap());
124 
125         endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy"));
126         endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy"));
127         endpoint.expect("send", new Constraint()
128         {
129             public boolean eval(Object arg0)
130             {
131                 if (arg0 instanceof UMOEvent)
132                 {
133                     return "Transformed Test Data".equals(((UMOEvent)arg0).getMessage().getPayload());
134                 }
135                 return false;
136             }
137         });
138 
139         strategy.catchMessage(event.getMessage(), null, true);
140 
141         endpoint.verify();
142         dispatcher.verify();
143         connector.verify();
144 
145         assertNotNull(strategy.getEndpoint());
146     }
147 
148     public void testFullRouter() throws Exception
149     {
150         final int[] count1 = new int[]{0};
151         final int[] count2 = new int[]{0};
152         final int[] catchAllCount = new int[]{0};
153 
154         OutboundRouterCollection messageRouter = new OutboundRouterCollection();
155 
156         FilteringOutboundRouter filterRouter1 = new FilteringOutboundRouter()
157         {
158             public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
159             {
160                 count1[0]++;
161                 return message;
162             }
163         };
164 
165         FilteringOutboundRouter filterRouter2 = new FilteringOutboundRouter()
166         {
167             public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
168             {
169                 count2[0]++;
170                 return message;
171             }
172         };
173 
174         filterRouter1.setFilter(new PayloadTypeFilter(Exception.class));
175         filterRouter2.setFilter(new PayloadTypeFilter(StringBuffer.class));
176         messageRouter.addRouter(filterRouter1);
177         messageRouter.addRouter(filterRouter2);
178 
179         LoggingCatchAllStrategy strategy = new LoggingCatchAllStrategy()
180         {
181             public UMOMessage catchMessage(UMOMessage message, UMOSession session, boolean synchronous)
182             {
183                 catchAllCount[0]++;
184                 return null;
185             }
186         };
187         messageRouter.setCatchAllStrategy(strategy);
188 
189         UMOSession session = getTestSession(getTestComponent(getTestDescriptor("test", "test")));
190 
191         messageRouter.route(new MuleMessage("hello"), session, true);
192         assertEquals(1, catchAllCount[0]);
193         assertEquals(0, count1[0]);
194         assertEquals(0, count2[0]);
195 
196         messageRouter.route(new MuleMessage(new StringBuffer()), session, true);
197         assertEquals(1, catchAllCount[0]);
198         assertEquals(0, count1[0]);
199         assertEquals(1, count2[0]);
200 
201         messageRouter.route(new MuleMessage(new Exception()), session, true);
202         assertEquals(1, catchAllCount[0]);
203         assertEquals(1, count1[0]);
204         assertEquals(1, count2[0]);
205     }
206 
207 }