1   /*
2    * $Id: CatchAllStrategiesTestCase.java 12111 2008-06-19 14:38:08Z dfeist $
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.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.MuleSession;
18  import org.mule.api.endpoint.OutboundEndpoint;
19  import org.mule.api.routing.ServiceRoutingException;
20  import org.mule.api.transformer.TransformerException;
21  import org.mule.api.transport.MessageDispatcher;
22  import org.mule.endpoint.MuleEndpointURI;
23  import org.mule.routing.filters.PayloadTypeFilter;
24  import org.mule.routing.outbound.DefaultOutboundRouterCollection;
25  import org.mule.routing.outbound.FilteringOutboundRouter;
26  import org.mule.tck.AbstractMuleTestCase;
27  import org.mule.tck.MuleTestUtils;
28  import org.mule.transformer.AbstractTransformer;
29  import org.mule.util.CollectionUtils;
30  
31  import com.mockobjects.constraint.Constraint;
32  import com.mockobjects.dynamic.C;
33  import com.mockobjects.dynamic.Mock;
34  
35  import java.util.HashMap;
36  
37  public class CatchAllStrategiesTestCase extends AbstractMuleTestCase
38  {
39  
40      public void testLoggingOnlyStrategy() throws Exception
41      {
42          MuleEvent event = getTestEvent("UncaughtEvent");
43          LoggingCatchAllStrategy strategy = new LoggingCatchAllStrategy();
44          try
45          {
46              strategy.setEndpoint(getTestOutboundEndpoint("testProvider"));
47              fail("Illegal operation exception should have been thrown");
48          }
49          catch (Exception e)
50          {
51              // expected
52          }
53  
54          assertNull(strategy.getEndpoint());
55          strategy.catchMessage(event.getMessage(), null, false);
56      }
57  
58      public void testForwardingStrategy() throws Exception
59      {
60          ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
61          Mock endpoint = MuleTestUtils.getMockOutboundEndpoint();
62          Mock dispatcher = new Mock(MessageDispatcher.class);
63          Mock connector = MuleTestUtils.getMockConnector();
64          MuleEvent event = getTestEvent("UncaughtEvent");
65          strategy.setEndpoint((OutboundEndpoint)endpoint.proxy());
66  
67          endpoint.expectAndReturn("getProperties", new HashMap());
68          endpoint.expectAndReturn("getProperties", new HashMap());
69          endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy"));
70          endpoint.expectAndReturn("getEndpointURI", new MuleEndpointURI("test://dummy"));
71          endpoint.expect("dispatch", C.isA(DefaultMuleEvent.class));
72  
73          strategy.catchMessage(event.getMessage(), null, false);
74  
75          endpoint.verify();
76          dispatcher.verify();
77          connector.verify();
78  
79          assertNotNull(strategy.getEndpoint());
80      }
81  
82      /**
83       * Test for MULE-3034
84       */
85      public void testForwardingStrategyNullEndpoint() throws Exception
86      {
87          ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
88          strategy.setEndpoint(null);
89          MuleEvent event = getTestEvent("UncaughtEvent");
90          MuleSession session = getTestSession(getTestService(), muleContext);
91       
92          try
93          {
94              strategy.catchMessage(event.getMessage(), session, false);
95              fail();
96          }
97          catch (ServiceRoutingException sre)
98          {
99              // we expected this exception
100         }
101     }
102     
103     private class TestEventTransformer extends AbstractTransformer
104     {
105         public Object doTransform(Object src, String encoding) throws TransformerException
106         {
107             return "Transformed Test Data";
108         }
109     }
110 
111     public void testForwardingStrategyWithTransform() throws Exception
112     {
113         ForwardingCatchAllStrategy strategy = new ForwardingCatchAllStrategy();
114         strategy.setSendTransformed(true);
115         Mock endpoint = MuleTestUtils.getMockOutboundEndpoint();
116         Mock dispatcher = new Mock(MessageDispatcher.class);
117         Mock connector = MuleTestUtils.getMockConnector();
118         MuleEvent event = getTestEvent("UncaughtEvent");
119         strategy.setEndpoint((OutboundEndpoint) endpoint.proxy());
120 
121         endpoint.expectAndReturn("getTransformers", CollectionUtils.singletonList(new TestEventTransformer()));
122         endpoint.expectAndReturn("getTransformers", CollectionUtils.singletonList(new TestEventTransformer()));
123         endpoint.expectAndReturn("getProperties", new HashMap());
124         endpoint.expectAndReturn("getProperties", new HashMap());
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 MuleEvent)
132                 {
133                     return "Transformed Test Data".equals(((MuleEvent)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         DefaultOutboundRouterCollection messageRouter = new DefaultOutboundRouterCollection();
155 
156         FilteringOutboundRouter filterRouter1 = new FilteringOutboundRouter()
157         {
158             public MuleMessage route(MuleMessage message, MuleSession session, boolean synchronous)
159             {
160                 count1[0]++;
161                 return message;
162             }
163         };
164 
165         FilteringOutboundRouter filterRouter2 = new FilteringOutboundRouter()
166         {
167             public MuleMessage route(MuleMessage message, MuleSession 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 MuleMessage catchMessage(MuleMessage message, MuleSession session, boolean synchronous)
182             {
183                 catchAllCount[0]++;
184                 return null;
185             }
186         };
187         messageRouter.setCatchAllStrategy(strategy);
188 
189         MuleSession session = getTestSession(getTestService(), muleContext);
190 
191         messageRouter.route(new DefaultMuleMessage("hello"), session, true);
192         assertEquals(1, catchAllCount[0]);
193         assertEquals(0, count1[0]);
194         assertEquals(0, count2[0]);
195 
196         messageRouter.route(new DefaultMuleMessage(new StringBuffer()), session, true);
197         assertEquals(1, catchAllCount[0]);
198         assertEquals(0, count1[0]);
199         assertEquals(1, count2[0]);
200 
201         messageRouter.route(new DefaultMuleMessage(new Exception()), session, true);
202         assertEquals(1, catchAllCount[0]);
203         assertEquals(1, count1[0]);
204         assertEquals(1, count2[0]);
205     }
206 
207 }