View Javadoc

1   /*
2    * $Id$
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.ExceptionPayload;
17  import org.mule.api.MuleEvent;
18  import org.mule.api.MuleException;
19  import org.mule.api.MuleMessage;
20  import org.mule.api.MuleSession;
21  import org.mule.api.processor.MessageProcessor;
22  import org.mule.message.DefaultExceptionPayload;
23  import org.mule.tck.AbstractMuleTestCase;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  public class FirstSuccessfulTestCase extends AbstractMuleTestCase
29  {
30      private static final String EXCEPTION_SEEN = "EXCEPTION WAS SEEN";
31  
32      public FirstSuccessfulTestCase()
33      {
34          setStartContext(true);
35      }
36  
37      public void testFirstSuccessful() throws Exception
38      {
39          MuleSession session = getTestSession(getTestService(), muleContext);
40          FirstSuccessful fs = new FirstSuccessful();
41          List<MessageProcessor> routes = new ArrayList<MessageProcessor>();
42          routes.add(new TestProcessor("abc"));
43          routes.add(new TestProcessor("def"));
44          routes.add(new TestProcessor("ghi"));
45          fs.setMessageProcessors(routes);
46          assertEquals("No abc", getPayload(fs, session, ""));
47          assertEquals("No def", getPayload(fs, session, "abc"));
48          assertEquals("No ghi", getPayload(fs, session, "abcdef"));
49          assertEquals(EXCEPTION_SEEN, getPayload(fs, session, "abcdefghi"));
50          assertEquals("No def", getPayload(fs, session, "ABC"));
51          assertEquals("No ghi", getPayload(fs, session, "ABCDEF"));
52          assertEquals(EXCEPTION_SEEN, getPayload(fs, session, "ABCDEFGHI"));
53      }
54  
55      private String getPayload(MessageProcessor mp, MuleSession session, String message) throws Exception
56      {
57          MuleMessage msg = new DefaultMuleMessage(message, muleContext);
58          try
59          {
60              MuleEvent event = mp.process(new DefaultMuleEvent(msg, null, session));
61              MuleMessage returnedMessage = event.getMessage();
62              if (returnedMessage.getExceptionPayload() != null)
63              {
64                  return EXCEPTION_SEEN;
65              }
66              else
67              {
68                  return returnedMessage.getPayloadAsString();
69              }
70          }
71          catch (Exception ex)
72          {
73              return EXCEPTION_SEEN;
74          }
75      }
76      static class TestProcessor implements MessageProcessor
77      {
78          private String rejectIfMatches;
79  
80          TestProcessor(String rejectIfMatches)
81          {
82              this.rejectIfMatches = rejectIfMatches;
83          }
84  
85          public MuleEvent process(MuleEvent event) throws MuleException
86          {
87              try
88              {
89                  DefaultMuleMessage msg;
90                  String payload = event.getMessage().getPayloadAsString();
91                  if (payload.indexOf(rejectIfMatches) >= 0)
92                  {
93                      throw new DefaultMuleException("Saw " + rejectIfMatches);
94                  }
95                  else if (payload.toLowerCase().indexOf(rejectIfMatches) >= 0)
96                  {
97                      msg = new DefaultMuleMessage(null, muleContext);
98                      msg.setExceptionPayload(new DefaultExceptionPayload(new Exception()));
99                  }
100                 else
101                 {
102                     msg = new DefaultMuleMessage("No " + rejectIfMatches, muleContext);
103                 }
104                 return new DefaultMuleEvent(msg, null, event.getSession());
105             }
106             catch (Exception e)
107             {
108                 throw new DefaultMuleException(e);
109             }
110         }
111     }
112 }