View Javadoc

1   /*
2    * $Id: CatchAllStrategiesTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.DefaultMuleEvent;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleSession;
16  import org.mule.api.endpoint.OutboundEndpoint;
17  import org.mule.api.routing.RoutingException;
18  import org.mule.api.transformer.TransformerException;
19  import org.mule.api.transport.MessageDispatcher;
20  import org.mule.endpoint.MuleEndpointURI;
21  import org.mule.routing.filters.PayloadTypeFilter;
22  import org.mule.routing.outbound.DefaultOutboundRouterCollection;
23  import org.mule.routing.outbound.FilteringOutboundRouter;
24  import org.mule.tck.AbstractMuleTestCase;
25  import org.mule.tck.MuleTestUtils;
26  import org.mule.transformer.AbstractTransformer;
27  import org.mule.util.CollectionUtils;
28  
29  import com.mockobjects.constraint.Constraint;
30  import com.mockobjects.dynamic.C;
31  import com.mockobjects.dynamic.Mock;
32  
33  import java.util.HashMap;
34  
35  public class CatchAllStrategiesTestCase extends AbstractMuleTestCase
36  {
37  
38      public CatchAllStrategiesTestCase()
39      {
40          setStartContext(true);
41      }
42  
43      public void testLoggingOnlyStrategy() throws Exception
44      {
45          // Just test it works without failure
46          MuleEvent event = getTestEvent("UncaughtEvent");
47          LoggingCatchAllStrategy strategy = new LoggingCatchAllStrategy();
48          strategy.process(event);
49      }
50  
51      public void testForwardingStrategy() throws Exception
52      {
53          ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
54          Mock endpoint = MuleTestUtils.getMockOutboundEndpoint();
55          Mock dispatcher = new Mock(MessageDispatcher.class);
56          Mock connector = MuleTestUtils.getMockConnector();
57          MuleEvent event = getTestEvent("UncaughtEvent");
58          strategy.setEndpoint((OutboundEndpoint) endpoint.proxy());
59  
60          endpoint.expectAndReturn("getProperties", new HashMap<Object, Object>());
61          endpoint.expectAndReturn("getProperties", new HashMap<Object, Object>());
62          endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy", muleContext));
63          endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy", muleContext));
64          endpoint.expect("process", C.isA(DefaultMuleEvent.class));
65  
66          strategy.process(event);
67  
68          endpoint.verify();
69          dispatcher.verify();
70          connector.verify();
71  
72          assertNotNull(strategy.getEndpoint());
73      }
74  
75      /**
76       * Test for MULE-3034
77       */
78      public void testForwardingStrategyNullEndpoint() throws Exception
79      {
80          ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
81          strategy.setEndpoint(null);
82          MuleEvent event = getTestEvent("UncaughtEvent");
83          MuleSession session = getTestSession(getTestService(), muleContext);
84  
85          try
86          {
87              strategy.process(event);
88              fail();
89          }
90          catch (RoutingException sre)
91          {
92              // we expected this exception
93          }
94      }
95  
96      private static class TestEventTransformer extends AbstractTransformer
97      {
98          public TestEventTransformer()
99          {
100             super();
101         }
102 
103         @Override
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.getMockOutboundEndpoint();
115         Mock dispatcher = new Mock(MessageDispatcher.class);
116         Mock connector = MuleTestUtils.getMockConnector();
117         MuleEvent event = getTestEvent("UncaughtEvent");
118         strategy.setEndpoint((OutboundEndpoint) endpoint.proxy());
119 
120         endpoint.expectAndReturn("getTransformers", CollectionUtils.singletonList(new TestEventTransformer()));
121         endpoint.expectAndReturn("getTransformers", CollectionUtils.singletonList(new TestEventTransformer()));
122         endpoint.expectAndReturn("getProperties", new HashMap<Object, Object>());
123         endpoint.expectAndReturn("getProperties", new HashMap<Object, Object>());
124         endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy", muleContext));
125         endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy", muleContext));
126         endpoint.expect("process", new Constraint()
127         {
128             public boolean eval(Object object)
129             {
130                 if (object instanceof MuleEvent)
131                 {
132                     return "Transformed Test Data".equals(((MuleEvent) object).getMessage().getPayload());
133                 }
134                 return false;
135             }
136         });
137 
138         strategy.process(event);
139 
140         endpoint.verify();
141         dispatcher.verify();
142         connector.verify();
143 
144         assertNotNull(strategy.getEndpoint());
145     }
146 
147     public void testFullRouter() throws Exception
148     {
149         final int[] count1 = new int[]{0};
150         final int[] count2 = new int[]{0};
151         final int[] catchAllCount = new int[]{0};
152 
153         DefaultOutboundRouterCollection messageRouter = new DefaultOutboundRouterCollection();
154 
155         FilteringOutboundRouter filterRouter1 = new FilteringOutboundRouter()
156         {
157             @Override
158             public MuleEvent route(MuleEvent event)
159             {
160                 count1[0]++;
161                 return event;
162             }
163         };
164 
165         FilteringOutboundRouter filterRouter2 = new FilteringOutboundRouter()
166         {
167             @Override
168             public MuleEvent route(MuleEvent event)
169             {
170                 count2[0]++;
171                 return event;
172             }
173         };
174 
175         filterRouter1.setFilter(new PayloadTypeFilter(Exception.class));
176         filterRouter2.setFilter(new PayloadTypeFilter(StringBuffer.class));
177         messageRouter.addRoute(filterRouter1);
178         messageRouter.addRoute(filterRouter2);
179 
180         AbstractCatchAllStrategy strategy = new AbstractCatchAllStrategy()
181         {
182             @Override
183             public MuleEvent doCatchMessage(MuleEvent event)
184             {
185                 catchAllCount[0]++;
186                 return null;
187             }
188         };
189         messageRouter.setCatchAllStrategy(strategy);
190 
191         MuleSession session = getTestSession(getTestService(), muleContext);
192 
193         messageRouter.process(getTestEvent("hello"));
194         assertEquals(1, catchAllCount[0]);
195         assertEquals(0, count1[0]);
196         assertEquals(0, count2[0]);
197 
198         messageRouter.process(getTestEvent(new StringBuffer()));
199         assertEquals(1, catchAllCount[0]);
200         assertEquals(0, count1[0]);
201         assertEquals(1, count2[0]);
202 
203         messageRouter.process(getTestEvent(new Exception()));
204         assertEquals(1, catchAllCount[0]);
205         assertEquals(1, count1[0]);
206         assertEquals(1, count2[0]);
207     }
208 
209 }