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