View Javadoc

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