View Javadoc

1   /*
2    * $Id: ServiceCompositeMessageSourceTestCase.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.service;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleException;
17  import org.mule.api.MuleMessage;
18  import org.mule.api.routing.filter.Filter;
19  import org.mule.processor.AbstractInterceptingMessageProcessor;
20  import org.mule.routing.MessageFilter;
21  import org.mule.source.StartableCompositeMessageSource;
22  import org.mule.source.StartableCompositeMessageSourceTestCase;
23  
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertNull;
29  
30  public class ServiceCompositeMessageSourceTestCase extends StartableCompositeMessageSourceTestCase
31  {
32  
33      protected StartableCompositeMessageSource getCompositeSource()
34      {
35          return new ServiceCompositeMessageSource();
36      }
37  
38      @Test
39      public void testInboundRouters() throws MuleException
40      {
41          ServiceCompositeMessageSource serviceCompositeMessageSource = (ServiceCompositeMessageSource) compositeSource;
42          serviceCompositeMessageSource.setListener(listener);
43          serviceCompositeMessageSource.addMessageProcessor(new AppendingInterceptingMessageProcessor("one"));
44          serviceCompositeMessageSource.addMessageProcessor(new AppendingInterceptingMessageProcessor("two"));
45  
46          serviceCompositeMessageSource.addSource(source);
47          serviceCompositeMessageSource.initialise();
48          serviceCompositeMessageSource.start();
49  
50          source.triggerSource();
51  
52          assertEquals(TEST_MESSAGE + "one" + "two", listener.event.getMessageAsString());
53      }
54  
55      @Test
56      public void testInboundRouterCatchAll() throws MuleException
57      {
58          ServiceCompositeMessageSource serviceCompositeMessageSource = (ServiceCompositeMessageSource) compositeSource;
59          serviceCompositeMessageSource.setListener(listener);
60          serviceCompositeMessageSource.setCatchAllStrategy(listener2);
61          serviceCompositeMessageSource.addMessageProcessor(new AppendingInterceptingMessageProcessor("one"));
62          serviceCompositeMessageSource.addMessageProcessor(new TestMessageFilter(false));
63  
64          serviceCompositeMessageSource.addSource(source);
65          serviceCompositeMessageSource.initialise();
66          serviceCompositeMessageSource.start();
67  
68          source.triggerSource();
69  
70          assertNull(listener.event);
71          assertNotNull(listener2.event);
72          assertEquals(TEST_MESSAGE + "one", listener2.event.getMessageAsString());
73      }
74  
75      class AppendingInterceptingMessageProcessor extends AbstractInterceptingMessageProcessor
76      {
77  
78          String appendString;
79  
80          public AppendingInterceptingMessageProcessor(String appendString)
81          {
82              this.appendString = appendString;
83          }
84  
85          public MuleEvent process(MuleEvent event) throws MuleException
86          {
87              return processNext(new DefaultMuleEvent(new DefaultMuleMessage(event.getMessage().getPayload()
88                                                                             + appendString, ServiceCompositeMessageSourceTestCase.muleContext),
89                  event));
90          }
91      }
92  
93      class TestMessageFilter extends MessageFilter
94      {
95          public TestMessageFilter(final boolean accept)
96          {
97              super(new Filter()
98              {
99                  public boolean accept(MuleMessage message)
100                 {
101                     return accept;
102                 }
103             });
104         }
105     }
106 
107 }