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.MessageExchangePattern;
16 import org.mule.RequestContext;
17 import org.mule.api.MuleContext;
18 import org.mule.api.MuleEvent;
19 import org.mule.api.MuleEventContext;
20 import org.mule.api.MuleException;
21 import org.mule.api.MuleMessage;
22 import org.mule.api.MuleSession;
23 import org.mule.api.component.JavaComponent;
24 import org.mule.api.construct.FlowConstruct;
25 import org.mule.api.context.MuleContextAware;
26 import org.mule.api.endpoint.EndpointBuilder;
27 import org.mule.api.endpoint.EndpointURI;
28 import org.mule.api.endpoint.ImmutableEndpoint;
29 import org.mule.api.endpoint.InboundEndpoint;
30 import org.mule.api.endpoint.OutboundEndpoint;
31 import org.mule.api.routing.filter.Filter;
32 import org.mule.api.service.Service;
33 import org.mule.api.transaction.Transaction;
34 import org.mule.api.transaction.TransactionConfig;
35 import org.mule.api.transaction.TransactionFactory;
36 import org.mule.api.transformer.Transformer;
37 import org.mule.api.transport.Connector;
38 import org.mule.api.transport.MessageDispatcher;
39 import org.mule.api.transport.MessageDispatcherFactory;
40 import org.mule.api.transport.MuleMessageFactory;
41 import org.mule.component.DefaultJavaComponent;
42 import org.mule.construct.Flow;
43 import org.mule.endpoint.EndpointURIEndpointBuilder;
44 import org.mule.endpoint.MuleEndpointURI;
45 import org.mule.model.seda.SedaModel;
46 import org.mule.model.seda.SedaService;
47 import org.mule.object.SingletonObjectFactory;
48 import org.mule.routing.MessageFilter;
49 import org.mule.session.DefaultMuleSession;
50 import org.mule.tck.testmodels.fruit.Apple;
51 import org.mule.tck.testmodels.mule.TestAgent;
52 import org.mule.tck.testmodels.mule.TestCompressionTransformer;
53 import org.mule.tck.testmodels.mule.TestConnector;
54 import org.mule.tck.testmodels.mule.TestTransactionFactory;
55 import org.mule.transaction.MuleTransactionConfig;
56 import org.mule.transport.AbstractConnector;
57 import org.mule.util.ClassUtils;
58
59 import com.mockobjects.dynamic.Mock;
60
61 import java.util.HashMap;
62 import java.util.List;
63 import java.util.Map;
64
65
66
67
68 public final class MuleTestUtils
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
99
100
101
102
103
104
105
106
107
108
109
110
111 public static InboundEndpoint getTestInboundEndpoint(String name, final MuleContext context)
112 throws Exception
113 {
114 return (InboundEndpoint) getTestEndpoint(name, null, null, null, null, context, new EndpointSource()
115 {
116 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
117 {
118 return context.getEndpointFactory().getInboundEndpoint(builder);
119 }
120 }, null);
121 }
122
123 public static OutboundEndpoint getTestOutboundEndpoint(String name, final MuleContext context)
124 throws Exception
125 {
126 return (OutboundEndpoint) getTestEndpoint(name, null, null, null, null, context, new EndpointSource()
127 {
128 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
129 {
130 return context.getEndpointFactory().getOutboundEndpoint(builder);
131 }
132 }, null);
133 }
134
135 public static InboundEndpoint getTestInboundEndpoint(String name,
136 final MuleContext context,
137 String uri,
138 List<Transformer> transformers,
139 Filter filter,
140 Map<Object, Object> properties,
141 Connector connector) throws Exception
142 {
143 return (InboundEndpoint) getTestEndpoint(name, uri, transformers, filter, properties, context,
144 new EndpointSource()
145 {
146 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
147 {
148 return context.getEndpointFactory().getInboundEndpoint(builder);
149 }
150 }, connector);
151 }
152
153 public static OutboundEndpoint getTestOutboundEndpoint(String name,
154 final MuleContext context,
155 String uri,
156 List<Transformer> transformers,
157 Filter filter,
158 Map<Object, Object> properties) throws Exception
159 {
160 return (OutboundEndpoint) getTestEndpoint(name, uri, transformers, filter, properties, context,
161 new EndpointSource()
162 {
163 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
164 {
165 return context.getEndpointFactory().getOutboundEndpoint(builder);
166 }
167 }, null);
168 }
169
170 public static InboundEndpoint getTestInboundEndpoint(String name,
171 final MuleContext context,
172 String uri,
173 List<Transformer> transformers,
174 Filter filter,
175 Map<Object, Object> properties) throws Exception
176 {
177 return (InboundEndpoint) getTestEndpoint(name, uri, transformers, filter, properties, context,
178 new EndpointSource()
179 {
180 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
181 {
182 return context.getEndpointFactory().getInboundEndpoint(builder);
183 }
184 }, null);
185 }
186
187 public static OutboundEndpoint getTestOutboundEndpoint(String name,
188 final MuleContext context,
189 String uri,
190 List<Transformer> transformers,
191 Filter filter,
192 Map<Object, Object> properties,
193 final Connector connector) throws Exception
194 {
195 return (OutboundEndpoint) getTestEndpoint(name, uri, transformers, filter, properties, context,
196 new EndpointSource()
197 {
198 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
199 {
200 builder.setConnector(connector);
201 return context.getEndpointFactory().getOutboundEndpoint(builder);
202 }
203 }, null);
204 }
205
206 public static OutboundEndpoint getTestOutboundEndpoint(final MessageExchangePattern mep,
207 final MuleContext context,
208 String uri,
209 final Connector connector) throws Exception
210 {
211 return (OutboundEndpoint) getTestEndpoint(null, uri, null, null, null, context, new EndpointSource()
212 {
213 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
214 {
215 builder.setConnector(connector);
216 builder.setExchangePattern(mep);
217 return context.getEndpointFactory().getOutboundEndpoint(builder);
218 }
219 }, null);
220 }
221
222 public static OutboundEndpoint getTestOutboundEndpoint(String name,
223 final MessageExchangePattern mep,
224 final MuleContext context) throws Exception
225 {
226 return (OutboundEndpoint) getTestEndpoint(name, null, null, null, null, context, new EndpointSource()
227 {
228 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
229 {
230 builder.setExchangePattern(mep);
231 return context.getEndpointFactory().getOutboundEndpoint(builder);
232 }
233 }, null);
234 }
235
236 public static OutboundEndpoint getTestOutboundEndpoint(final MessageExchangePattern mep,
237 final MuleContext context) throws Exception
238 {
239 return (OutboundEndpoint) getTestEndpoint(null, null, null, null, null, context, new EndpointSource()
240 {
241 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
242 {
243 builder.setExchangePattern(mep);
244 return context.getEndpointFactory().getOutboundEndpoint(builder);
245 }
246 }, null);
247 }
248
249 public static InboundEndpoint getTestInboundEndpoint(String name,
250 final MessageExchangePattern mep,
251 final MuleContext context,
252 final Connector connector) throws Exception
253 {
254 return (InboundEndpoint) getTestEndpoint(name, null, null, null, null, context, new EndpointSource()
255 {
256 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
257 {
258 builder.setExchangePattern(mep);
259 return context.getEndpointFactory().getInboundEndpoint(builder);
260 }
261 }, connector);
262 }
263
264 public static InboundEndpoint getTestInboundEndpoint(final MessageExchangePattern mep,
265 final MuleContext context) throws Exception
266 {
267 return (InboundEndpoint) getTestEndpoint(null, null, null, null, null, context, new EndpointSource()
268 {
269 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
270 {
271 builder.setExchangePattern(mep);
272 return context.getEndpointFactory().getInboundEndpoint(builder);
273 }
274 }, null);
275 }
276
277 public static InboundEndpoint getTestTransactedInboundEndpoint(final MessageExchangePattern mep,
278 final MuleContext context) throws Exception
279 {
280 return (InboundEndpoint) getTestEndpoint(null, null, null, null, null, context, new EndpointSource()
281 {
282 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
283 {
284 builder.setExchangePattern(mep);
285 TransactionConfig txConfig = new MuleTransactionConfig(TransactionConfig.ACTION_BEGIN_OR_JOIN);
286 txConfig.setFactory(new TestTransactionFactory());
287 builder.setTransactionConfig(txConfig);
288 return context.getEndpointFactory().getInboundEndpoint(builder);
289 }
290 }, null);
291 }
292
293 private static ImmutableEndpoint getTestEndpoint(String name,
294 String uri,
295 List<Transformer> transformers,
296 Filter filter,
297 Map<Object, Object> properties,
298 MuleContext context,
299 EndpointSource source,
300 Connector connector) throws Exception
301 {
302 final Map<String, Object> props = new HashMap<String, Object>();
303 props.put("name", name);
304 props.put("endpointURI", new MuleEndpointURI("test://test", context));
305 props.put("connector", "testConnector");
306 if (connector == null)
307 {
308
309
310 connector = (Connector) ClassUtils.loadClass("org.mule.tck.testmodels.mule.TestConnector",
311 AbstractMuleTestCase.class).getConstructor(MuleContext.class).newInstance(context);
312 }
313
314 connector.setName("testConnector");
315 context.getRegistry().applyLifecycle(connector);
316
317 final String endpoingUri = uri == null ? "test://test" : uri;
318 final EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(endpoingUri, context);
319 endpointBuilder.setConnector(connector);
320 endpointBuilder.setName(name);
321 if (transformers != null)
322 {
323 endpointBuilder.setTransformers(transformers);
324 }
325
326 if (properties != null)
327 {
328 endpointBuilder.setProperties(properties);
329 }
330 endpointBuilder.addMessageProcessor(new MessageFilter(filter));
331 return source.getEndpoint(endpointBuilder);
332 }
333
334 private interface EndpointSource
335 {
336 ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException;
337 }
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376 public static ImmutableEndpoint getTestSchemeMetaInfoInboundEndpoint(String name,
377 String protocol,
378 final MuleContext context)
379 throws Exception
380 {
381 return getTestSchemeMetaInfoEndpoint(name, protocol, context, new EndpointSource()
382 {
383 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
384 {
385 return context.getEndpointFactory().getInboundEndpoint(builder);
386 }
387 });
388 }
389
390 public static ImmutableEndpoint getTestSchemeMetaInfoOutboundEndpoint(String name,
391 String protocol,
392 final MuleContext context)
393 throws Exception
394 {
395 return getTestSchemeMetaInfoEndpoint(name, protocol, context, new EndpointSource()
396 {
397 public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
398 {
399 return context.getEndpointFactory().getOutboundEndpoint(builder);
400 }
401 });
402 }
403
404 private static ImmutableEndpoint getTestSchemeMetaInfoEndpoint(String name,
405 String protocol,
406 MuleContext context,
407 EndpointSource source) throws Exception
408 {
409
410 final AbstractConnector connector = (AbstractConnector) ClassUtils.loadClass(
411 "org.mule.tck.testmodels.mule.TestConnector", AbstractMuleTestCase.class).newInstance();
412
413 connector.setName("testConnector");
414 context.getRegistry().applyLifecycle(connector);
415 connector.registerSupportedProtocol(protocol);
416
417 final EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(
418 "test:" + protocol + "://test", context);
419 endpointBuilder.setConnector(connector);
420 endpointBuilder.setName(name);
421 return source.getEndpoint(endpointBuilder);
422 }
423
424
425 public static MuleEvent getTestEvent(Object data, MuleContext context) throws Exception
426 {
427 return getTestEvent(data, getTestService(context), MessageExchangePattern.REQUEST_RESPONSE, context);
428 }
429
430 public static MuleEvent getTestEvent(Object data, MessageExchangePattern mep, MuleContext context)
431 throws Exception
432 {
433 return getTestEvent(data, getTestService(context), mep, context);
434 }
435
436 public static MuleEvent getTestEventUsingFlow(Object data, MessageExchangePattern mep, MuleContext context)
437 throws Exception
438 {
439 return getTestEvent(data, getTestFlow(context), mep, context);
440 }
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455 public static MuleEvent getTestEvent(Object data, Service service, MuleContext context) throws Exception
456 {
457 return getTestEvent(data, service, getTestInboundEndpoint("test1",
458 MessageExchangePattern.REQUEST_RESPONSE, context, null), context);
459 }
460
461 public static MuleEvent getTestEvent(Object data,
462 FlowConstruct flowConstruct,
463 MessageExchangePattern mep,
464 MuleContext context) throws Exception
465 {
466 return getTestEvent(data, flowConstruct, getTestInboundEndpoint("test1", mep, context, null), context);
467 }
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485 public static MuleEvent getTestEvent(Object data, InboundEndpoint endpoint, MuleContext context)
486 throws Exception
487 {
488 return getTestEvent(data, getTestService(context), endpoint, context);
489 }
490
491 public static MuleEvent getTestEvent(Object data,
492 FlowConstruct flowConstruct,
493 InboundEndpoint endpoint,
494 MuleContext context) throws Exception
495 {
496 final MuleSession session = getTestSession(flowConstruct, context);
497
498 final MuleMessageFactory factory = endpoint.getConnector().createMuleMessageFactory();
499 final MuleMessage message = factory.create(data, endpoint.getEncoding());
500
501 return new DefaultMuleEvent(message, endpoint, session);
502 }
503
504 public static MuleEvent getTestEvent(Object data, MuleSession session, MuleContext context)
505 throws Exception
506 {
507 final InboundEndpoint endpoint = getTestInboundEndpoint("test1",
508 MessageExchangePattern.REQUEST_RESPONSE, context, null);
509 final MuleMessageFactory factory = endpoint.getConnector().createMuleMessageFactory();
510 final MuleMessage message = factory.create(data, endpoint.getEncoding());
511
512 return new DefaultMuleEvent(message, endpoint, session);
513 }
514
515 public static MuleEventContext getTestEventContext(Object data,
516 MessageExchangePattern mep,
517 MuleContext context) throws Exception
518 {
519 try
520 {
521 final MuleEvent event = getTestEvent(data, mep, context);
522 RequestContext.setEvent(event);
523 return RequestContext.getEventContext();
524 }
525 finally
526 {
527 RequestContext.setEvent(null);
528 }
529 }
530
531 public static Transformer getTestTransformer() throws Exception
532 {
533 final Transformer t = new TestCompressionTransformer();
534 t.initialise();
535 return t;
536 }
537
538 public static MuleSession getTestSession(FlowConstruct flowConstruct, MuleContext context)
539 {
540 return new DefaultMuleSession(flowConstruct, context);
541 }
542
543 public static MuleSession getTestSession(MuleContext context)
544 {
545 return getTestSession(null, context);
546 }
547
548 public static TestConnector getTestConnector(MuleContext context) throws Exception
549 {
550 final TestConnector testConnector = new TestConnector(context);
551 testConnector.setName("testConnector");
552 context.getRegistry().applyLifecycle(testConnector);
553 return testConnector;
554 }
555
556 public static Service getTestService(MuleContext context) throws Exception
557 {
558 return getTestService("appleService", Apple.class, context);
559 }
560
561 public static Flow getTestFlow(MuleContext context) throws Exception
562 {
563 return getTestFlow("appleFlow", context);
564 }
565
566 public static Service getTestService(String name, Class<?> clazz, MuleContext context) throws Exception
567 {
568 return getTestService(name, clazz, null, context);
569 }
570
571 public static Service getTestService(String name, Class<?> clazz, Map props, MuleContext context)
572 throws Exception
573 {
574 return getTestService(name, clazz, props, context, true);
575 }
576
577 public static Flow getTestFlow(String name, MuleContext context) throws Exception
578 {
579 return getTestFlow(name, context, true);
580 }
581
582 public static Service getTestService(String name,
583 Class<?> clazz,
584 Map props,
585 MuleContext context,
586 boolean initialize) throws Exception
587 {
588 final SedaModel model = new SedaModel();
589 model.setMuleContext(context);
590 context.getRegistry().applyLifecycle(model);
591
592 final Service service = new SedaService(context);
593 service.setName(name);
594 final SingletonObjectFactory of = new SingletonObjectFactory(clazz, props);
595 of.initialise();
596 final JavaComponent component = new DefaultJavaComponent(of);
597 ((MuleContextAware) component).setMuleContext(context);
598 service.setComponent(component);
599 service.setModel(model);
600 if (initialize)
601 {
602 context.getRegistry().registerService(service);
603 }
604
605 return service;
606 }
607
608 public static Flow getTestFlow(String name, MuleContext context, boolean initialize)
609 throws Exception
610 {
611 final Flow flow = new Flow(name, context);
612 if (initialize)
613 {
614 context.getRegistry().registerFlowConstruct(flow);
615 }
616
617 return flow;
618 }
619
620 public static TestAgent getTestAgent() throws Exception
621 {
622 final TestAgent t = new TestAgent();
623 t.initialise();
624 return t;
625 }
626
627 public static Mock getMockSession()
628 {
629 return new Mock(MuleSession.class, "umoSession");
630 }
631
632 public static Mock getMockMessageDispatcher()
633 {
634 return new Mock(MessageDispatcher.class, "umoMessageDispatcher");
635 }
636
637 public static Mock getMockMessageDispatcherFactory()
638 {
639 return new Mock(MessageDispatcherFactory.class, "umoMessageDispatcherFactory");
640 }
641
642 public static Mock getMockConnector()
643 {
644 return new Mock(Connector.class, "umoConnector");
645 }
646
647 public static Mock getMockEvent()
648 {
649 return new Mock(MuleEvent.class, "umoEvent");
650 }
651
652 public static Mock getMockMuleContext()
653 {
654 return new Mock(DefaultMuleContext.class, "muleMuleContext");
655 }
656
657 public static Mock getMockInboundEndpoint()
658 {
659 return new Mock(InboundEndpoint.class, "umoEndpoint");
660 }
661
662 public static Mock getMockOutboundEndpoint()
663 {
664 return new Mock(OutboundEndpoint.class, "umoEndpoint");
665 }
666
667 public static Mock getMockEndpointURI()
668 {
669 return new Mock(EndpointURI.class, "umoEndpointUri");
670 }
671
672 public static Mock getMockTransaction()
673 {
674 return new Mock(Transaction.class, "umoTransaction");
675 }
676
677 public static Mock getMockTransactionFactory()
678 {
679 return new Mock(TransactionFactory.class, "umoTransactionFactory");
680 }
681
682 }