1
2
3
4
5
6
7
8
9
10
11 package org.mule.processor;
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.builder.InterceptingChainMessageProcessorBuilder;
23 import org.mule.tck.AbstractMuleTestCase;
24 import org.mule.util.ObjectUtils;
25
26 import org.junit.Test;
27
28 public class InterceptingChainMessageProcessorBuilderTestCase extends AbstractMuleTestCase
29 {
30
31 @Test
32 public void testMPChain() throws MuleException, Exception
33 {
34 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
35 builder.chain(new AppendingMP("1"), new AppendingMP("2"), new AppendingMP("3"));
36 assertEquals("0123", builder.build().process(getTestEvent("0")).getMessageAsString());
37 }
38
39 public void testMPChainWithBuilder() throws MuleException, Exception
40 {
41 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
42 builder.chain(new AppendingMP("1"));
43 builder.chain(new MessageProcessorBuilder()
44 {
45 public MessageProcessor build()
46 {
47 return new AppendingMP("2");
48 }
49 });
50 builder.chain(new AppendingMP("3"));
51 assertEquals("0123", builder.build().process(getTestEvent("0")).getMessageAsString());
52 }
53
54 @Test
55 public void testInterceptingMPChain() throws MuleException, Exception
56 {
57 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
58 builder.chain(new AppendingInterceptingMP("1"), new AppendingInterceptingMP("2"),
59 new AppendingInterceptingMP("3"));
60 assertEquals("0before1before2before3after3after2after1", builder.build()
61 .process(getTestEvent("0"))
62 .getMessageAsString());
63 }
64
65 @Test
66 public void testMixedMPChain() throws MuleException, Exception
67 {
68 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
69 builder.chain(new AppendingInterceptingMP("1"), new AppendingMP("2"), new AppendingMP("3"),
70 new AppendingInterceptingMP("4"), new AppendingMP("5"));
71 assertEquals("0before123before45after4after1", builder.build()
72 .process(getTestEvent("0"))
73 .getMessageAsString());
74 }
75
76 @Test
77 public void testNestedMPChain() throws MuleException, Exception
78 {
79 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
80 builder.chain(new AppendingMP("1"), new InterceptingChainMessageProcessorBuilder().chain(
81 new AppendingMP("a"), new AppendingMP("b")).build(), new AppendingMP("2"));
82 assertEquals("01ab2", builder.build().process(getTestEvent("0")).getMessageAsString());
83 }
84
85 @Test
86 public void testNestedInterceptingMPChain() throws MuleException, Exception
87 {
88 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
89 builder.chain(new AppendingInterceptingMP("1"), new InterceptingChainMessageProcessorBuilder().chain(
90 new AppendingInterceptingMP("a"), new AppendingInterceptingMP("b")).build(),
91 new AppendingInterceptingMP("2"));
92 assertEquals("0before1beforeabeforebafterbafterabefore2after2after1", builder.build().process(
93 getTestEvent("0")).getMessageAsString());
94 }
95
96 @Test
97 public void testNestedMixedMPChain() throws MuleException, Exception
98 {
99 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
100 builder.chain(new AppendingMP("1"), new InterceptingChainMessageProcessorBuilder().chain(
101 new AppendingInterceptingMP("a"), new AppendingMP("b")).build(), new AppendingInterceptingMP("2"));
102 assertEquals("01beforeabafterabefore2after2", builder.build()
103 .process(getTestEvent("0"))
104 .getMessageAsString());
105 }
106
107 @Test
108 public void testInterceptingMPChainStopFlow() throws MuleException, Exception
109 {
110 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
111 builder.chain(new AppendingInterceptingMP("1"), new AppendingInterceptingMP("2", true),
112 new AppendingInterceptingMP("3"));
113 assertEquals("0before1after1", builder.build().process(getTestEvent("0")).getMessageAsString());
114 }
115
116
117
118
119
120 @Test
121 public void testNestedInterceptingMPChainStopFlow() throws MuleException, Exception
122 {
123 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
124 builder.chain(new AppendingInterceptingMP("1"), new InterceptingChainMessageProcessorBuilder().chain(
125 new AppendingInterceptingMP("a", true), new AppendingInterceptingMP("b")).build(),
126 new AppendingInterceptingMP("3"));
127 assertEquals("0before1before3after3after1", builder.build()
128 .process(getTestEvent("0"))
129 .getMessageAsString());
130 }
131
132 @Test
133 public void testMPChainLifecycle() throws MuleException, Exception
134 {
135 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
136 AppendingMP mp1 = new AppendingInterceptingMP("1");
137 AppendingMP mp2 = new AppendingInterceptingMP("2");
138 MessageProcessor chain = builder.chain(mp1, mp2).build();
139 ((Lifecycle) chain).initialise();
140 ((Lifecycle) chain).start();
141 ((Lifecycle) chain).stop();
142 ((Lifecycle) chain).dispose();
143 assertLifecycle(mp1);
144 assertLifecycle(mp2);
145 }
146
147 @Test
148 public void testNestedMPChainLifecycle() throws MuleException, Exception
149 {
150 InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
151 InterceptingChainMessageProcessorBuilder nestedBuilder = new InterceptingChainMessageProcessorBuilder();
152 AppendingMP mp1 = new AppendingInterceptingMP("1");
153 AppendingMP mp2 = new AppendingInterceptingMP("2");
154 AppendingMP mpa = new AppendingInterceptingMP("a");
155 AppendingMP mpb = new AppendingInterceptingMP("b");
156 MessageProcessor chain = builder.chain(mp1, nestedBuilder.chain(mpa, mpb).build(), mp2).build();
157 ((Lifecycle) chain).initialise();
158 ((Lifecycle) chain).start();
159 ((Lifecycle) chain).stop();
160 ((Lifecycle) chain).dispose();
161 assertLifecycle(mp1);
162 assertLifecycle(mp2);
163 assertLifecycle(mpa);
164 assertLifecycle(mpb);
165 }
166
167 private void assertLifecycle(AppendingMP mp)
168 {
169 assertTrue(mp.initialised);
170 assertTrue(mp.started);
171 assertTrue(mp.stopped);
172 assertTrue(mp.disposed);
173 }
174
175 private static class AppendingMP implements MessageProcessor, Lifecycle
176 {
177 String appendString;
178 boolean initialised;
179 boolean started;
180 boolean stopped;
181 boolean disposed;
182
183 public AppendingMP(String append)
184 {
185 this.appendString = append;
186 }
187
188 public MuleEvent process(MuleEvent event) throws MuleException
189 {
190 return new DefaultMuleEvent(new DefaultMuleMessage(event.getMessageAsString() + appendString,
191 muleContext), event);
192 }
193
194 public void initialise() throws InitialisationException
195 {
196 initialised = true;
197 }
198
199 public void start() throws MuleException
200 {
201 started = true;
202 }
203
204 public void stop() throws MuleException
205 {
206 stopped = true;
207 }
208
209 public void dispose()
210 {
211 disposed = true;
212 }
213
214 @Override
215 public String toString()
216 {
217 return ObjectUtils.toString(this);
218 }
219 }
220
221 private static class AppendingInterceptingMP extends AppendingMP implements InterceptingMessageProcessor
222 {
223 private boolean stopProcessing;
224 private MessageProcessor next;
225
226 public AppendingInterceptingMP(String append)
227 {
228 this(append, false);
229 }
230
231 public AppendingInterceptingMP(String append, boolean stopProcessing)
232 {
233 super(append);
234 this.stopProcessing = stopProcessing;
235 }
236
237 public MuleEvent process(MuleEvent event) throws MuleException
238 {
239 if (stopProcessing)
240 {
241 return event;
242 }
243
244 MuleEvent intermediateEvent = new DefaultMuleEvent(new DefaultMuleMessage(
245 event.getMessageAsString() + "before" + appendString, muleContext), event);
246 if (next != null)
247 {
248 intermediateEvent = next.process(intermediateEvent);
249 }
250 return new DefaultMuleEvent(new DefaultMuleMessage(intermediateEvent.getMessageAsString()
251 + "after" + appendString, muleContext),
252 intermediateEvent);
253 }
254
255 public void setListener(MessageProcessor mp)
256 {
257 next = mp;
258 }
259
260 @Override
261 public String toString()
262 {
263 return ObjectUtils.toString(this);
264 }
265 }
266 }