1
2
3
4
5
6
7
8
9
10
11 package org.mule.tck;
12
13 import org.mule.DefaultMuleContext;
14 import org.mule.DefaultMuleEvent;
15 import org.mule.DefaultMuleMessage;
16 import org.mule.DefaultMuleSession;
17 import org.mule.RequestContext;
18 import org.mule.api.MuleContext;
19 import org.mule.api.MuleEvent;
20 import org.mule.api.MuleEventContext;
21 import org.mule.api.MuleException;
22 import org.mule.api.MuleSession;
23 import org.mule.api.endpoint.EndpointBuilder;
24 import org.mule.api.endpoint.EndpointURI;
25 import org.mule.api.endpoint.ImmutableEndpoint;
26 import org.mule.api.endpoint.InboundEndpoint;
27 import org.mule.api.endpoint.OutboundEndpoint;
28 import org.mule.api.object.ObjectFactory;
29 import org.mule.api.routing.OutboundRouter;
30 import org.mule.api.routing.filter.Filter;
31 import org.mule.api.service.Service;
32 import org.mule.api.transaction.Transaction;
33 import org.mule.api.transaction.TransactionFactory;
34 import org.mule.api.transformer.Transformer;
35 import org.mule.api.transport.Connector;
36 import org.mule.api.transport.MessageDispatcher;
37 import org.mule.api.transport.MessageDispatcherFactory;
38 import org.mule.component.DefaultJavaComponent;
39 import org.mule.endpoint.EndpointURIEndpointBuilder;
40 import org.mule.endpoint.MuleEndpointURI;
41 import org.mule.model.seda.SedaModel;
42 import org.mule.model.seda.SedaService;
43 import org.mule.object.SingletonObjectFactory;
44 import org.mule.routing.outbound.OutboundPassThroughRouter;
45 import org.mule.tck.testmodels.fruit.Apple;
46 import org.mule.tck.testmodels.mule.TestAgent;
47 import org.mule.tck.testmodels.mule.TestCompressionTransformer;
48 import org.mule.tck.testmodels.mule.TestConnector;
49 import org.mule.transport.AbstractConnector;
50 import org.mule.util.ClassUtils;
51
52 import com.mockobjects.dynamic.Mock;
53
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.Map;
57
58
59
60
61 public final class MuleTestUtils
62 {
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public static InboundEndpoint getTestInboundEndpoint(String name, final MuleContext context) throws Exception
99 {
100 return (InboundEndpoint) getTestEndpoint(name, null, null, null, null, context, new EndpointSource()
101 {
102 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
103 {
104 return context.getRegistry().lookupEndpointFactory().getInboundEndpoint(builder);
105 }
106 });
107 }
108
109 public static OutboundEndpoint getTestOutboundEndpoint(String name, final MuleContext context) throws Exception
110 {
111 return (OutboundEndpoint) getTestEndpoint(name, null, null, null, null, context, new EndpointSource()
112 {
113 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
114 {
115 return context.getRegistry().lookupEndpointFactory().getOutboundEndpoint(builder);
116 }
117 });
118 }
119
120 public static InboundEndpoint getTestInboundEndpoint(String name,
121 final MuleContext context,
122 String uri,
123 List transformers,
124 Filter filter,
125 Map properties) throws Exception
126 {
127 return (InboundEndpoint) getTestEndpoint(name, uri, transformers, filter, properties, context, new EndpointSource()
128 {
129 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
130 {
131 return context.getRegistry().lookupEndpointFactory().getInboundEndpoint(builder);
132 }
133 });
134 }
135
136 public static OutboundEndpoint getTestOutboundEndpoint(String name,
137 final MuleContext context,
138 String uri,
139 List transformers,
140 Filter filter,
141 Map properties) throws Exception
142 {
143 return (OutboundEndpoint) getTestEndpoint(name, uri, transformers, filter, properties, context, new EndpointSource()
144 {
145 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
146 {
147 return context.getRegistry().lookupEndpointFactory().getOutboundEndpoint(builder);
148 }
149 });
150 }
151
152
153 private static ImmutableEndpoint getTestEndpoint(String name,
154 String uri,
155 List transformers,
156 Filter filter,
157 Map properties,
158 MuleContext context,
159 EndpointSource source) throws Exception
160 {
161 Map props = new HashMap();
162 props.put("name", name);
163 props.put("endpointURI", new MuleEndpointURI("test://test"));
164 props.put("connector", "testConnector");
165
166 AbstractConnector connector = (AbstractConnector) ClassUtils.loadClass(
167 "org.mule.tck.testmodels.mule.TestConnector", AbstractMuleTestCase.class).newInstance();
168
169 connector.setName("testConnector");
170 connector.setMuleContext(context);
171 context.applyLifecycle(connector);
172
173 String endpoingUri = uri == null ? "test://test" : uri;
174 EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(endpoingUri, context);
175 endpointBuilder.setConnector(connector);
176 endpointBuilder.setName(name);
177 if (transformers != null)
178 {
179 endpointBuilder.setTransformers(transformers);
180 }
181
182 if (properties != null)
183 {
184 endpointBuilder.setProperties(properties);
185 }
186 endpointBuilder.setFilter(filter);
187 return source.getEndpoint(endpointBuilder);
188 }
189
190 private interface EndpointSource
191 {
192 ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException;
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226 public static ImmutableEndpoint getTestSchemeMetaInfoInboundEndpoint(String name, String protocol, final MuleContext context)
227 throws Exception
228 {
229 return getTestSchemeMetaInfoEndpoint(name, protocol, context, new EndpointSource()
230 {
231 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
232 {
233 return context.getRegistry().lookupEndpointFactory().getInboundEndpoint(builder);
234 }
235 });
236 }
237
238 public static ImmutableEndpoint getTestSchemeMetaInfoOutboundEndpoint(String name, String protocol, final MuleContext context)
239 throws Exception
240 {
241 return getTestSchemeMetaInfoEndpoint(name, protocol, context, new EndpointSource()
242 {
243 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
244 {
245 return context.getRegistry().lookupEndpointFactory().getOutboundEndpoint(builder);
246 }
247 });
248 }
249
250 private static ImmutableEndpoint getTestSchemeMetaInfoEndpoint(String name, String protocol, MuleContext context, EndpointSource source)
251 throws Exception
252 {
253
254 AbstractConnector connector =
255 (AbstractConnector) ClassUtils.loadClass("org.mule.tck.testmodels.mule.TestConnector",
256 AbstractMuleTestCase.class).newInstance();
257
258 connector.setName("testConnector");
259 connector.setMuleContext(context);
260 context.applyLifecycle(connector);
261 connector.registerSupportedProtocol(protocol);
262
263 EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder("test:" + protocol + "://test", context);
264 endpointBuilder.setConnector(connector);
265 endpointBuilder.setName(name);
266 return source.getEndpoint(endpointBuilder);
267 }
268
269
270 public static MuleEvent getTestEvent(Object data, MuleContext context) throws Exception
271 {
272 return getTestEvent(data, getTestService(context), context);
273 }
274
275 public static MuleEvent getTestInboundEvent(Object data, MuleContext context) throws Exception
276 {
277 return getTestInboundEvent(data, getTestService(context), context);
278 }
279
280
281 public static MuleEvent getTestEvent(Object data, Service service, MuleContext context) throws Exception
282 {
283 return getTestEvent(data, service, getTestOutboundEndpoint("test1", context), context);
284 }
285
286 public static MuleEvent getTestInboundEvent(Object data, Service service, MuleContext context) throws Exception
287 {
288 return getTestEvent(data, service, getTestInboundEndpoint("test1", context), context);
289 }
290
291
292 public static MuleEvent getTestEvent(Object data, ImmutableEndpoint endpoint, MuleContext context) throws Exception
293 {
294 return getTestEvent(data, getTestService(context), endpoint, context);
295 }
296
297 public static MuleEvent getTestEvent(Object data, Service service, ImmutableEndpoint endpoint, MuleContext context) throws Exception
298 {
299 MuleSession session = getTestSession(service, context);
300 return new DefaultMuleEvent(new DefaultMuleMessage(data, new HashMap()), endpoint, session, true);
301 }
302
303 public static MuleEventContext getTestEventContext(Object data, MuleContext context) throws Exception
304 {
305 try
306 {
307 MuleEvent event = getTestEvent(data, context);
308 RequestContext.setEvent(event);
309 return RequestContext.getEventContext();
310 }
311 finally
312 {
313 RequestContext.setEvent(null);
314 }
315 }
316
317 public static Transformer getTestTransformer() throws Exception
318 {
319 Transformer t = new TestCompressionTransformer();
320 t.initialise();
321 return t;
322 }
323
324 public static MuleSession getTestSession(Service service, MuleContext context)
325 {
326 return new DefaultMuleSession(service, context);
327 }
328
329 public static MuleSession getTestSession(MuleContext context)
330 {
331 return getTestSession(null, context);
332 }
333
334 public static TestConnector getTestConnector(MuleContext context) throws Exception
335 {
336 TestConnector testConnector = new TestConnector();
337 testConnector.setName("testConnector");
338 testConnector.setMuleContext(context);
339 context.applyLifecycle(testConnector);
340 return testConnector;
341 }
342
343 public static Service getTestService(MuleContext context) throws Exception
344 {
345 return getTestService("appleService", Apple.class, context);
346 }
347
348 public static Service getTestService(String name, Class clazz, MuleContext context) throws Exception
349 {
350 return getTestService(name, clazz, null, context);
351 }
352
353 public static Service getTestService(String name, Class clazz, Map props, MuleContext context) throws Exception
354 {
355 return getTestService(name, clazz, props, context, true);
356 }
357
358 public static Service getTestService(String name, Class clazz, Map props, MuleContext context, boolean initialize) throws Exception
359 {
360 SedaModel model = new SedaModel();
361 model.setMuleContext(context);
362 context.applyLifecycle(model);
363
364 Service c = new SedaService();
365 c.setName(name);
366 ObjectFactory of = new SingletonObjectFactory(clazz, props);
367 of.initialise();
368 c.setComponent(new DefaultJavaComponent(of));
369 c.setModel(model);
370 if (initialize)
371 {
372 context.getRegistry().registerService(c);
373
374 OutboundRouter router = new OutboundPassThroughRouter();
375 c.getOutboundRouter().addRouter(router);
376 }
377
378 return c;
379 }
380
381 public static TestAgent getTestAgent() throws Exception
382 {
383 TestAgent t = new TestAgent();
384 t.initialise();
385 return t;
386 }
387
388 public static Mock getMockSession()
389 {
390 return new Mock(MuleSession.class, "umoSession");
391 }
392
393 public static Mock getMockMessageDispatcher()
394 {
395 return new Mock(MessageDispatcher.class, "umoMessageDispatcher");
396 }
397
398 public static Mock getMockMessageDispatcherFactory()
399 {
400 return new Mock(MessageDispatcherFactory.class, "umoMessageDispatcherFactory");
401 }
402
403 public static Mock getMockConnector()
404 {
405 return new Mock(Connector.class, "umoConnector");
406 }
407
408 public static Mock getMockEvent()
409 {
410 return new Mock(MuleEvent.class, "umoEvent");
411 }
412
413 public static Mock getMockMuleContext()
414 {
415 return new Mock(DefaultMuleContext.class, "muleMuleContext");
416 }
417
418 public static Mock getMockInboundEndpoint()
419 {
420 return new Mock(InboundEndpoint.class, "umoEndpoint");
421 }
422
423 public static Mock getMockOutboundEndpoint()
424 {
425 return new Mock(OutboundEndpoint.class, "umoEndpoint");
426 }
427
428 public static Mock getMockEndpointURI()
429 {
430 return new Mock(EndpointURI.class, "umoEndpointUri");
431 }
432
433 public static Mock getMockTransaction()
434 {
435 return new Mock(Transaction.class, "umoTransaction");
436 }
437
438 public static Mock getMockTransactionFactory()
439 {
440 return new Mock(TransactionFactory.class, "umoTransactionFactory");
441 }
442
443
444 }