View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.service;
8   
9   import org.mule.DefaultMuleEvent;
10  import org.mule.DefaultMuleMessage;
11  import org.mule.api.MuleEvent;
12  import org.mule.api.MuleException;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.routing.filter.Filter;
15  import org.mule.processor.AbstractInterceptingMessageProcessor;
16  import org.mule.routing.MessageFilter;
17  import org.mule.source.StartableCompositeMessageSource;
18  import org.mule.source.StartableCompositeMessageSourceTestCase;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  
26  public class ServiceCompositeMessageSourceTestCase extends StartableCompositeMessageSourceTestCase
27  {
28  
29      protected StartableCompositeMessageSource getCompositeSource()
30      {
31          return new ServiceCompositeMessageSource();
32      }
33  
34      @Test
35      public void testInboundRouters() throws MuleException
36      {
37          ServiceCompositeMessageSource serviceCompositeMessageSource = (ServiceCompositeMessageSource) compositeSource;
38          serviceCompositeMessageSource.setListener(listener);
39          serviceCompositeMessageSource.addMessageProcessor(new AppendingInterceptingMessageProcessor("one"));
40          serviceCompositeMessageSource.addMessageProcessor(new AppendingInterceptingMessageProcessor("two"));
41  
42          serviceCompositeMessageSource.addSource(source);
43          serviceCompositeMessageSource.initialise();
44          serviceCompositeMessageSource.start();
45  
46          source.triggerSource();
47  
48          assertEquals(TEST_MESSAGE + "one" + "two", listener.event.getMessageAsString());
49      }
50  
51      @Test
52      public void testInboundRouterCatchAll() throws MuleException
53      {
54          ServiceCompositeMessageSource serviceCompositeMessageSource = (ServiceCompositeMessageSource) compositeSource;
55          serviceCompositeMessageSource.setListener(listener);
56          serviceCompositeMessageSource.setCatchAllStrategy(listener2);
57          serviceCompositeMessageSource.addMessageProcessor(new AppendingInterceptingMessageProcessor("one"));
58          serviceCompositeMessageSource.addMessageProcessor(new TestMessageFilter(false));
59  
60          serviceCompositeMessageSource.addSource(source);
61          serviceCompositeMessageSource.initialise();
62          serviceCompositeMessageSource.start();
63  
64          source.triggerSource();
65  
66          assertNull(listener.event);
67          assertNotNull(listener2.event);
68          assertEquals(TEST_MESSAGE + "one", listener2.event.getMessageAsString());
69      }
70  
71      class AppendingInterceptingMessageProcessor extends AbstractInterceptingMessageProcessor
72      {
73  
74          String appendString;
75  
76          public AppendingInterceptingMessageProcessor(String appendString)
77          {
78              this.appendString = appendString;
79          }
80  
81          public MuleEvent process(MuleEvent event) throws MuleException
82          {
83              return processNext(new DefaultMuleEvent(new DefaultMuleMessage(event.getMessage().getPayload()
84                                                                             + appendString, ServiceCompositeMessageSourceTestCase.muleContext),
85                  event));
86          }
87      }
88  
89      class TestMessageFilter extends MessageFilter
90      {
91          public TestMessageFilter(final boolean accept)
92          {
93              super(new Filter()
94              {
95                  public boolean accept(MuleMessage message)
96                  {
97                      return accept;
98                  }
99              });
100         }
101     }
102 
103 }