View Javadoc

1   /*
2    * $Id: CatchAllStrategiesTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
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.routing.filters.PayloadTypeFilter;
21  import org.mule.routing.outbound.DefaultOutboundRouterCollection;
22  import org.mule.routing.outbound.FilteringOutboundRouter;
23  import org.mule.tck.MuleTestUtils;
24  import org.mule.tck.junit4.AbstractMuleContextTestCase;
25  import org.mule.transformer.AbstractTransformer;
26  import org.mule.util.CollectionUtils;
27  
28  import com.mockobjects.constraint.Constraint;
29  import com.mockobjects.dynamic.C;
30  import com.mockobjects.dynamic.Mock;
31  
32  import org.junit.Test;
33  
34  import static org.junit.Assert.assertEquals;
35  import static org.junit.Assert.assertNotNull;
36  import static org.junit.Assert.fail;
37  
38  public class CatchAllStrategiesTestCase extends AbstractMuleContextTestCase
39  {
40  
41      public CatchAllStrategiesTestCase()
42      {
43          setStartContext(true);
44      }
45  
46      @Test
47      public void testLoggingOnlyStrategy() throws Exception
48      {
49          // Just test it works without failure
50          MuleEvent event = getTestEvent("UncaughtEvent");
51          LoggingCatchAllStrategy strategy = new LoggingCatchAllStrategy();
52          strategy.process(event);
53      }
54  
55      @Test
56      public void testForwardingStrategy() throws Exception
57      {
58          ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
59          Mock endpoint = MuleTestUtils.getMockOutboundEndpoint();
60          Mock dispatcher = new Mock(MessageDispatcher.class);
61          Mock connector = MuleTestUtils.getMockConnector();
62          MuleEvent event = getTestEvent("UncaughtEvent");
63          strategy.setEndpoint((OutboundEndpoint) endpoint.proxy());
64  
65          endpoint.expect("process", C.isA(DefaultMuleEvent.class));
66  
67          strategy.process(event);
68  
69          endpoint.verify();
70          dispatcher.verify();
71          connector.verify();
72  
73          assertNotNull(strategy.getEndpoint());
74      }
75  
76      /**
77       * Test for MULE-3034
78       */
79      @Test
80      public void testForwardingStrategyNullEndpoint() throws Exception
81      {
82          ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
83          strategy.setEndpoint(null);
84          MuleEvent event = getTestEvent("UncaughtEvent");
85          MuleSession session = getTestSession(getTestService(), muleContext);
86  
87          try
88          {
89              strategy.process(event);
90              fail();
91          }
92          catch (RoutingException sre)
93          {
94              // we expected this exception
95          }
96      }
97  
98      private static class TestEventTransformer extends AbstractTransformer
99      {
100         public TestEventTransformer()
101         {
102             super();
103         }
104 
105         @Override
106         public Object doTransform(Object src, String encoding) throws TransformerException
107         {
108             return "Transformed Test Data";
109         }
110     }
111 
112     @Test
113     public void testForwardingStrategyWithTransform() throws Exception
114     {
115         ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
116         strategy.setSendTransformed(true);
117         Mock endpoint = MuleTestUtils.getMockOutboundEndpoint();
118         Mock dispatcher = new Mock(MessageDispatcher.class);
119         Mock connector = MuleTestUtils.getMockConnector();
120         MuleEvent event = getTestEvent("UncaughtEvent");
121         strategy.setEndpoint((OutboundEndpoint) endpoint.proxy());
122 
123         endpoint.expectAndReturn("getTransformers", CollectionUtils.singletonList(new TestEventTransformer()));
124         endpoint.expectAndReturn("getTransformers", CollectionUtils.singletonList(new TestEventTransformer()));
125         endpoint.expect("process", new Constraint()
126         {
127             public boolean eval(Object object)
128             {
129                 if (object instanceof MuleEvent)
130                 {
131                     return "Transformed Test Data".equals(((MuleEvent) object).getMessage().getPayload());
132                 }
133                 return false;
134             }
135         });
136 
137         strategy.process(event);
138 
139         endpoint.verify();
140         dispatcher.verify();
141         connector.verify();
142 
143         assertNotNull(strategy.getEndpoint());
144     }
145 
146     @Test
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 }