View Javadoc

1   /*
2    * $Id: DefaultMessageProcessorChainTestCase.java 20320 2010-11-24 15:03:31Z 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.processor.chain;
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.lifecycle.InitialisationException;
18  import org.mule.api.lifecycle.Lifecycle;
19  import org.mule.api.processor.InterceptingMessageProcessor;
20  import org.mule.api.processor.MessageProcessor;
21  import org.mule.api.processor.MessageProcessorBuilder;
22  import org.mule.processor.AbstractInterceptingMessageProcessor;
23  import org.mule.processor.chain.DefaultMessageProcessorChainBuilder;
24  import org.mule.tck.AbstractMuleTestCase;
25  import org.mule.transformer.simple.StringAppendTransformer;
26  import org.mule.util.ObjectUtils;
27  
28  import org.junit.Test;
29  
30  public class DefaultMessageProcessorChainTestCase extends AbstractMuleTestCase
31  {
32  
33      @Test
34      public void testMPChain() throws MuleException, Exception
35      {
36          DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
37          builder.chain(new AppendingMP("1"), new AppendingMP("2"), new AppendingMP("3"));
38          assertEquals("0123", builder.build().process(getTestEvent("0")).getMessageAsString());
39      }
40  
41      public void testMPChainWithBuilder() throws MuleException, Exception
42      {
43          DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
44          builder.chain(new AppendingMP("1"));
45          builder.chain(new MessageProcessorBuilder()
46          {
47              public MessageProcessor build()
48              {
49                  return new AppendingMP("2");
50              }
51          });
52          builder.chain(new AppendingMP("3"));
53          assertEquals("0123", builder.build().process(getTestEvent("0")).getMessageAsString());
54      }
55  
56      @Test
57      public void testInterceptingMPChain() throws MuleException, Exception
58      {
59          DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
60          builder.chain(new AppendingInterceptingMP("1"), new AppendingInterceptingMP("2"),
61              new AppendingInterceptingMP("3"));
62          assertEquals("0before1before2before3after3after2after1", builder.build()
63              .process(getTestEvent("0"))
64              .getMessageAsString());
65      }
66  
67      @Test
68      public void testMixedMPChain() throws MuleException, Exception
69      {
70          DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
71          builder.chain(new AppendingInterceptingMP("1"), new AppendingMP("2"), new AppendingMP("3"),
72              new AppendingInterceptingMP("4"), new AppendingMP("5"));
73          assertEquals("0before123before45after4after1", builder.build()
74              .process(getTestEvent("0"))
75              .getMessageAsString());
76      }
77  
78      @Test
79      public void testNestedMPChain() throws MuleException, Exception
80      {
81          DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
82          builder.chain(new AppendingMP("1"), new DefaultMessageProcessorChainBuilder().chain(
83              new AppendingMP("a"), new AppendingMP("b")).build(), new AppendingMP("2"));
84          assertEquals("01ab2", builder.build().process(getTestEvent("0")).getMessageAsString());
85      }
86  
87      @Test
88      public void testNestedInterceptingMPChain() throws MuleException, Exception
89      {
90          DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
91          builder.chain(new AppendingInterceptingMP("1"), new DefaultMessageProcessorChainBuilder().chain(
92              new AppendingInterceptingMP("a"), new AppendingInterceptingMP("b")).build(),
93              new AppendingInterceptingMP("2"));
94          assertEquals("0before1beforeabeforebafterbafterabefore2after2after1", builder.build().process(
95              getTestEvent("0")).getMessageAsString());
96      }
97  
98      @Test
99      public void testNestedMixedMPChain() throws MuleException, Exception
100     {
101         DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
102         builder.chain(new AppendingMP("1"), new DefaultMessageProcessorChainBuilder().chain(
103             new AppendingInterceptingMP("a"), new AppendingMP("b")).build(), new AppendingInterceptingMP("2"));
104         assertEquals("01beforeabafterabefore2after2", builder.build()
105             .process(getTestEvent("0"))
106             .getMessageAsString());
107     }
108 
109     @Test
110     public void testInterceptingMPChainStopFlow() throws MuleException, Exception
111     {
112         DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
113         builder.chain(new AppendingInterceptingMP("1"), new AppendingInterceptingMP("2", true),
114             new AppendingInterceptingMP("3"));
115         assertEquals("0before1after1", builder.build().process(getTestEvent("0")).getMessageAsString());
116     }
117 
118     /**
119      * Note: Stopping the flow of a nested chain causes the nested chain to return early, but does not stop
120      * the flow of the parent chain.
121      */
122     @Test
123     public void testNestedInterceptingMPChainStopFlow() throws MuleException, Exception
124     {
125         DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
126         builder.chain(new AppendingInterceptingMP("1"), new DefaultMessageProcessorChainBuilder().chain(
127             new AppendingInterceptingMP("a", true), new AppendingInterceptingMP("b")).build(),
128             new AppendingInterceptingMP("3"));
129         assertEquals("0before1before3after3after1", builder.build()
130             .process(getTestEvent("0"))
131             .getMessageAsString());
132     }
133 
134     @Test
135     public void testMPChainLifecycle() throws MuleException, Exception
136     {
137         DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
138         AppendingMP mp1 = new AppendingInterceptingMP("1");
139         AppendingMP mp2 = new AppendingInterceptingMP("2");
140         MessageProcessor chain = builder.chain(mp1, mp2).build();
141         ((Lifecycle) chain).initialise();
142         ((Lifecycle) chain).start();
143         ((Lifecycle) chain).stop();
144         ((Lifecycle) chain).dispose();
145         assertLifecycle(mp1);
146         assertLifecycle(mp2);
147     }
148 
149     @Test
150     public void testNestedMPChainLifecycle() throws MuleException, Exception
151     {
152         DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
153         DefaultMessageProcessorChainBuilder nestedBuilder = new DefaultMessageProcessorChainBuilder();
154         AppendingMP mp1 = new AppendingInterceptingMP("1");
155         AppendingMP mp2 = new AppendingInterceptingMP("2");
156         AppendingMP mpa = new AppendingInterceptingMP("a");
157         AppendingMP mpb = new AppendingInterceptingMP("b");
158         MessageProcessor chain = builder.chain(mp1, nestedBuilder.chain(mpa, mpb).build(), mp2).build();
159         ((Lifecycle) chain).initialise();
160         ((Lifecycle) chain).start();
161         ((Lifecycle) chain).stop();
162         ((Lifecycle) chain).dispose();
163         assertLifecycle(mp1);
164         assertLifecycle(mp2);
165         assertLifecycle(mpa);
166         assertLifecycle(mpb);
167     }
168 
169     public void testNoneIntercepting() throws Exception
170     {
171         DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
172         builder.chain(new TestNonIntercepting(), new TestNonIntercepting(), new TestNonIntercepting());
173         MuleEvent restul = builder.build().process(getTestEvent(""));
174         assertEquals("MessageProcessorMessageProcessorMessageProcessor", restul.getMessageAsString());
175     }
176 
177     public void testAllIntercepting() throws Exception
178     {
179         DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
180         builder.chain(new TestIntercepting(), new TestIntercepting(), new TestIntercepting());
181         MuleEvent restul = builder.build().process(getTestEvent(""));
182         assertEquals("InterceptingMessageProcessorInterceptingMessageProcessorInterceptingMessageProcessor",
183             restul.getMessageAsString());
184     }
185 
186     public void testMix() throws Exception
187     {
188         DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
189         builder.chain(new TestIntercepting(), new TestNonIntercepting(), new TestNonIntercepting(),
190             new TestIntercepting(), new TestNonIntercepting(), new TestNonIntercepting());
191         MuleEvent restul = builder.build().process(getTestEvent(""));
192         assertEquals(
193             "InterceptingMessageProcessorMessageProcessorMessageProcessorInterceptingMessageProcessorMessageProcessorMessageProcessor",
194             restul.getMessageAsString());
195 
196     }
197 
198     public void testMix2() throws Exception
199     {
200         DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
201         builder.chain(new TestNonIntercepting(), new TestIntercepting(), new TestNonIntercepting(),
202             new TestNonIntercepting(), new TestNonIntercepting(), new TestIntercepting());
203         MuleEvent restul = builder.build().process(getTestEvent(""));
204         assertEquals(
205             "MessageProcessorInterceptingMessageProcessorMessageProcessorMessageProcessorMessageProcessorInterceptingMessageProcessor",
206             restul.getMessageAsString());
207 
208     }
209 
210     static class TestNonIntercepting implements MessageProcessor
211     {
212         public MuleEvent process(MuleEvent event) throws MuleException
213         {
214             return new StringAppendTransformer("MessageProcessor").process(event);
215         }
216     }
217 
218     static class TestIntercepting extends AbstractInterceptingMessageProcessor
219     {
220         public MuleEvent process(MuleEvent event) throws MuleException
221         {
222             return processNext(new StringAppendTransformer("InterceptingMessageProcessor").process(event));
223         }
224     }
225 
226     private void assertLifecycle(AppendingMP mp)
227     {
228         assertTrue(mp.initialised);
229         assertTrue(mp.started);
230         assertTrue(mp.stopped);
231         assertTrue(mp.disposed);
232     }
233 
234     private static class AppendingMP implements MessageProcessor, Lifecycle
235     {
236         String appendString;
237         boolean initialised;
238         boolean started;
239         boolean stopped;
240         boolean disposed;
241 
242         public AppendingMP(String append)
243         {
244             this.appendString = append;
245         }
246 
247         public MuleEvent process(MuleEvent event) throws MuleException
248         {
249             return new DefaultMuleEvent(new DefaultMuleMessage(event.getMessageAsString() + appendString,
250                 muleContext), event);
251         }
252 
253         public void initialise() throws InitialisationException
254         {
255             initialised = true;
256         }
257 
258         public void start() throws MuleException
259         {
260             started = true;
261         }
262 
263         public void stop() throws MuleException
264         {
265             stopped = true;
266         }
267 
268         public void dispose()
269         {
270             disposed = true;
271         }
272 
273         @Override
274         public String toString()
275         {
276             return ObjectUtils.toString(this);
277         }
278     }
279 
280     private static class AppendingInterceptingMP extends AppendingMP implements InterceptingMessageProcessor
281     {
282         private boolean stopProcessing;
283         private MessageProcessor next;
284 
285         public AppendingInterceptingMP(String append)
286         {
287             this(append, false);
288         }
289 
290         public AppendingInterceptingMP(String append, boolean stopProcessing)
291         {
292             super(append);
293             this.stopProcessing = stopProcessing;
294         }
295 
296         public MuleEvent process(MuleEvent event) throws MuleException
297         {
298             if (stopProcessing)
299             {
300                 return event;
301             }
302 
303             MuleEvent intermediateEvent = new DefaultMuleEvent(new DefaultMuleMessage(
304                 event.getMessageAsString() + "before" + appendString, muleContext), event);
305             if (next != null)
306             {
307                 intermediateEvent = next.process(intermediateEvent);
308             }
309             return new DefaultMuleEvent(new DefaultMuleMessage(intermediateEvent.getMessageAsString()
310                                                                + "after" + appendString, muleContext),
311                 intermediateEvent);
312         }
313 
314         public void setListener(MessageProcessor mp)
315         {
316             next = mp;
317         }
318 
319         @Override
320         public String toString()
321         {
322             return ObjectUtils.toString(this);
323         }
324     }
325 }