View Javadoc

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