View Javadoc

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