View Javadoc

1   /*
2    * $Id: FirstSuccessfulTestCase.java 20472 2010-12-06 16:28:51Z 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.DefaultMuleException;
16  import org.mule.api.MuleEvent;
17  import org.mule.api.MuleException;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.MuleSession;
20  import org.mule.api.processor.MessageProcessor;
21  import org.mule.api.routing.CouldNotRouteOutboundMessageException;
22  import org.mule.message.DefaultExceptionPayload;
23  import org.mule.tck.AbstractMuleTestCase;
24  import org.mule.transformer.simple.StringAppendTransformer;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  public class FirstSuccessfulTestCase extends AbstractMuleTestCase
30  {
31      private static final String EXCEPTION_SEEN = "EXCEPTION WAS SEEN";
32  
33      public FirstSuccessfulTestCase()
34      {
35          setStartContext(true);
36      }
37  
38      public void testFirstSuccessful() throws Exception
39      {
40          MuleSession session = getTestSession(getTestService(), muleContext);
41          FirstSuccessful fs = new FirstSuccessful();
42          fs.setMuleContext(muleContext);
43          List<MessageProcessor> routes = new ArrayList<MessageProcessor>();
44          routes.add(new TestProcessor("abc"));
45          routes.add(new TestProcessor("def"));
46          routes.add(new TestProcessor("ghi"));
47          fs.setMessageProcessors(routes);
48          fs.setMuleContext(muleContext);
49          fs.initialise();
50          assertEquals("No abc", getPayload(fs, session, ""));
51          assertEquals("No def", getPayload(fs, session, "abc"));
52          assertEquals("No ghi", getPayload(fs, session, "abcdef"));
53          assertEquals(EXCEPTION_SEEN, getPayload(fs, session, "abcdefghi"));
54          assertEquals("No def", getPayload(fs, session, "ABC"));
55          assertEquals("No ghi", getPayload(fs, session, "ABCDEF"));
56          assertEquals(EXCEPTION_SEEN, getPayload(fs, session, "ABCDEFGHI"));
57      }
58  
59      public void testFailureExpression() throws MuleException, Exception
60      {
61          FirstSuccessful fs = new FirstSuccessful();
62          fs.setFailureExpression("#[payload-type:java.lang.Integer]");
63          List<MessageProcessor> routes = new ArrayList<MessageProcessor>();
64          routes.add(new MessageProcessor()
65          {
66              public MuleEvent process(MuleEvent event) throws MuleException
67              {
68                  event.getMessage().setPayload(Integer.valueOf(1));
69                  return event;
70              }
71          });
72          routes.add(new StringAppendTransformer("abc"));
73          fs.setMessageProcessors(routes);
74          fs.setMuleContext(muleContext);
75          fs.initialise();
76          assertEquals("abc", fs.process(getTestEvent("")).getMessageAsString());
77      }
78  
79      public void testRouteReturnsNullEvent() throws MuleException, Exception
80      {
81          FirstSuccessful fs = new FirstSuccessful();
82          List<MessageProcessor> routes = new ArrayList<MessageProcessor>();
83          routes.add(new MessageProcessor()
84          {
85              public MuleEvent process(MuleEvent event) throws MuleException
86              {
87                  return null;
88              }
89          });
90          fs.setMessageProcessors(routes);
91          fs.setMuleContext(muleContext);
92          fs.initialise();
93          assertNull(fs.process(getTestEvent("")));
94      }
95  
96      public void testRouteReturnsNullMessage() throws MuleException, Exception
97      {
98          FirstSuccessful fs = new FirstSuccessful();
99          List<MessageProcessor> routes = new ArrayList<MessageProcessor>();
100         routes.add(new MessageProcessor()
101         {
102             public MuleEvent process(MuleEvent event) throws MuleException
103             {
104                 return new DefaultMuleEvent(null, event);
105             }
106         });
107         fs.setMessageProcessors(routes);
108         fs.setMuleContext(muleContext);
109         fs.initialise();
110         try
111         {
112             fs.process(getTestEvent(""));
113             fail("Exception expected");
114         }
115         catch (CouldNotRouteOutboundMessageException e)
116         {
117 
118         }
119     }
120 
121     private String getPayload(MessageProcessor mp, MuleSession session, String message) throws Exception
122     {
123         MuleMessage msg = new DefaultMuleMessage(message, muleContext);
124         try
125         {
126             MuleEvent event = mp.process(new DefaultMuleEvent(msg, null, session));
127             MuleMessage returnedMessage = event.getMessage();
128             if (returnedMessage.getExceptionPayload() != null)
129             {
130                 return EXCEPTION_SEEN;
131             }
132             else
133             {
134                 return returnedMessage.getPayloadAsString();
135             }
136         }
137         catch (Exception ex)
138         {
139             return EXCEPTION_SEEN;
140         }
141     }
142 
143     static class TestProcessor implements MessageProcessor
144     {
145         private String rejectIfMatches;
146 
147         TestProcessor(String rejectIfMatches)
148         {
149             this.rejectIfMatches = rejectIfMatches;
150         }
151 
152         public MuleEvent process(MuleEvent event) throws MuleException
153         {
154             try
155             {
156                 DefaultMuleMessage msg;
157                 String payload = event.getMessage().getPayloadAsString();
158                 if (payload.indexOf(rejectIfMatches) >= 0)
159                 {
160                     throw new DefaultMuleException("Saw " + rejectIfMatches);
161                 }
162                 else if (payload.toLowerCase().indexOf(rejectIfMatches) >= 0)
163                 {
164                     msg = new DefaultMuleMessage(null, muleContext);
165                     msg.setExceptionPayload(new DefaultExceptionPayload(new Exception()));
166                 }
167                 else
168                 {
169                     msg = new DefaultMuleMessage("No " + rejectIfMatches, muleContext);
170                 }
171                 return new DefaultMuleEvent(msg, null, event.getSession());
172             }
173             catch (Exception e)
174             {
175                 throw new DefaultMuleException(e);
176             }
177         }
178     }
179 }