View Javadoc

1   /*
2    * $Id: MuleTestUtils.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.MuleManager;
14  import org.mule.config.PoolingProfile;
15  import org.mule.impl.DefaultExceptionStrategy;
16  import org.mule.impl.MuleDescriptor;
17  import org.mule.impl.MuleEvent;
18  import org.mule.impl.MuleMessage;
19  import org.mule.impl.MuleSession;
20  import org.mule.impl.RequestContext;
21  import org.mule.impl.endpoint.MuleEndpoint;
22  import org.mule.impl.endpoint.MuleEndpointURI;
23  import org.mule.impl.model.seda.SedaComponent;
24  import org.mule.impl.model.seda.SedaModel;
25  import org.mule.providers.AbstractConnector;
26  import org.mule.tck.testmodels.mule.TestAgent;
27  import org.mule.tck.testmodels.mule.TestCompressionTransformer;
28  import org.mule.tck.testmodels.mule.TestConnector;
29  import org.mule.umo.UMOComponent;
30  import org.mule.umo.UMODescriptor;
31  import org.mule.umo.UMOEvent;
32  import org.mule.umo.UMOEventContext;
33  import org.mule.umo.UMOException;
34  import org.mule.umo.UMOSession;
35  import org.mule.umo.UMOTransaction;
36  import org.mule.umo.UMOTransactionFactory;
37  import org.mule.umo.endpoint.UMOEndpoint;
38  import org.mule.umo.endpoint.UMOEndpointURI;
39  import org.mule.umo.endpoint.UMOImmutableEndpoint;
40  import org.mule.umo.manager.UMOManager;
41  import org.mule.umo.model.UMOModel;
42  import org.mule.umo.provider.UMOConnector;
43  import org.mule.umo.provider.UMOMessageDispatcher;
44  import org.mule.umo.provider.UMOMessageDispatcherFactory;
45  import org.mule.umo.transformer.UMOTransformer;
46  import org.mule.util.ClassUtils;
47  import org.mule.util.StringUtils;
48  
49  import com.mockobjects.dynamic.Mock;
50  
51  import java.util.HashMap;
52  
53  /**
54   * Utilities for creating test and Mock Mule objects
55   */
56  public final class MuleTestUtils
57  {
58  
59      public static final String DEFAULT_MODEL_NAME = "main";
60  
61      /** Do not instanciate. */
62      private MuleTestUtils ()
63      {
64          // no-op
65      }
66  
67      public static UMOManager getManager(boolean disableAdminService) throws Exception
68      {
69          if (MuleManager.isInstanciated())
70          {
71              MuleManager.getInstance().dispose();
72          }
73  
74          UMOManager manager = MuleManager.getInstance();
75  
76          if (disableAdminService)
77          {
78              MuleManager.getConfiguration().setServerUrl(StringUtils.EMPTY);
79          }
80  
81          UMOModel model = new SedaModel();
82          model.setName(DEFAULT_MODEL_NAME);
83          manager.registerModel(model);
84          MuleManager.getConfiguration().setSynchronous(true);
85          MuleManager.getConfiguration().getPoolingProfile().setInitialisationPolicy(
86              PoolingProfile.INITIALISE_NONE);
87  
88          return manager;
89      }
90  
91      public static UMOModel getDefaultModel() throws UMOException
92      {
93          UMOModel m = MuleManager.getInstance().lookupModel(DEFAULT_MODEL_NAME);
94          if (m == null)
95          {
96              m = new SedaModel();
97              m.setName(DEFAULT_MODEL_NAME);
98              MuleManager.getInstance().registerModel(m);
99          }
100         return m;
101 
102     }
103 
104     public static UMOEndpoint getTestEndpoint(String name, String type) throws Exception
105     {
106         UMOEndpoint endpoint = new MuleEndpoint();
107         // need to build endpoint this way to avoid depenency to any endpoint jars
108         UMOConnector connector = null;
109         connector = (UMOConnector) ClassUtils.loadClass("org.mule.tck.testmodels.mule.TestConnector",
110             AbstractMuleTestCase.class).newInstance();
111 
112         connector.setName("testConnector");
113         endpoint.setConnector(connector);
114         endpoint.setEndpointURI(new MuleEndpointURI("test://test"));
115         endpoint.setName(name);
116         endpoint.setType(type);
117         return endpoint;
118     }
119     
120     public static UMOEndpoint getTestSchemeMetaInfoEndpoint(String name, String type, String protocol)
121         throws Exception
122     {
123         UMOEndpoint endpoint = new MuleEndpoint();
124         // need to build endpoint this way to avoid depenency to any endpoint jars
125         AbstractConnector connector = null;
126         connector = (AbstractConnector) ClassUtils.loadClass("org.mule.tck.testmodels.mule.TestConnector",
127             AbstractMuleTestCase.class).newInstance();
128 
129         connector.setName("testConnector");
130         connector.registerSupportedProtocol(protocol);
131         endpoint.setConnector(connector);
132         endpoint.setEndpointURI(new MuleEndpointURI("test:" + protocol + "://test"));
133         endpoint.setName(name);
134         endpoint.setType(type);
135         return endpoint;
136     }
137 
138     public static UMOEvent getTestEvent(Object data) throws Exception
139     {
140         UMOComponent component = getTestComponent(getTestDescriptor("string", String.class.getName()));
141         UMOSession session = getTestSession(component);
142         return new MuleEvent(new MuleMessage(data, new HashMap()), getTestEndpoint("test1",
143             UMOEndpoint.ENDPOINT_TYPE_SENDER), session, true);
144     }
145 
146     public static UMOEventContext getTestEventContext(Object data) throws Exception
147     {
148         try
149         {
150             UMOEvent event = getTestEvent(data);
151             RequestContext.setEvent(event);
152             return RequestContext.getEventContext();
153         }
154         finally
155         {
156             RequestContext.setEvent(null);
157         }
158     }
159 
160     public static UMOTransformer getTestTransformer()
161     {
162         return new TestCompressionTransformer();
163     }
164 
165     public static UMOEvent getTestEvent(Object data, MuleDescriptor descriptor) throws Exception
166     {
167         UMOComponent component = getTestComponent(descriptor);
168 
169         UMOSession session = getTestSession(component);
170 
171         UMOEndpoint endpoint = getTestEndpoint("test1", UMOEndpoint.ENDPOINT_TYPE_SENDER);
172 
173         return new MuleEvent(new MuleMessage(data, new HashMap()), endpoint, session, true);
174     }
175 
176     public static UMOEvent getTestEvent(Object data, UMOImmutableEndpoint endpoint) throws Exception
177     {
178         UMOSession session = getTestSession(getTestComponent(getTestDescriptor("string", String.class
179             .getName())));
180         return new MuleEvent(new MuleMessage(data, new HashMap()), endpoint, session, true);
181     }
182 
183     public static UMOEvent getTestEvent(Object data, MuleDescriptor descriptor, UMOImmutableEndpoint endpoint)
184         throws UMOException
185     {
186         UMOSession session = getTestSession(getTestComponent(descriptor));
187         UMOEvent event = new MuleEvent(new MuleMessage(data, new HashMap()), endpoint, session, true);
188         return event;
189     }
190 
191     public static UMOSession getTestSession(UMOComponent component)
192     {
193         return new MuleSession(component);
194     }
195 
196     public static UMOSession getTestSession()
197     {
198         return new MuleSession(null);
199     }
200 
201     public static TestConnector getTestConnector()
202     {
203         TestConnector testConnector = new TestConnector();
204         testConnector.setName("testConnector");
205         return testConnector;
206     }
207 
208     public static UMOComponent getTestComponent(MuleDescriptor descriptor)
209     {
210         return new SedaComponent(descriptor, new SedaModel());
211     }
212 
213     public static MuleDescriptor getTestDescriptor(String name, String implementation) throws Exception
214     {
215         MuleDescriptor descriptor = new MuleDescriptor();
216         descriptor.setExceptionListener(new DefaultExceptionStrategy());
217         descriptor.setName(name);
218         descriptor.setImplementation(implementation);
219         descriptor.setOutboundEndpoint(getTestEndpoint("test1", UMOEndpoint.ENDPOINT_TYPE_SENDER));
220         descriptor.initialise();
221 
222         //TODO descriptor.getPoolingProfile().setInitialisationPolicy(PoolingProfile.POOL_INITIALISE_NO_COMPONENTS);
223 
224         return descriptor;
225     }
226 
227 
228     public static TestAgent getTestAgent()
229     {
230         return new TestAgent();
231     }
232 
233     public static Mock getMockSession()
234     {
235         return new Mock(UMOSession.class, "umoSession");
236     }
237 
238     public static Mock getMockMessageDispatcher()
239     {
240         return new Mock(UMOMessageDispatcher.class, "umoMessageDispatcher");
241     }
242 
243     public static Mock getMockMessageDispatcherFactory()
244     {
245         return new Mock(UMOMessageDispatcherFactory.class, "umoMessageDispatcherFactory");
246     }
247 
248     public static Mock getMockConnector()
249     {
250         return new Mock(UMOConnector.class, "umoConnector");
251     }
252 
253     public static Mock getMockEvent()
254     {
255         return new Mock(UMOEvent.class, "umoEvent");
256     }
257 
258     public static Mock getMockManager()
259     {
260         return new Mock(MuleManager.class, "muleManager");
261     }
262 
263     public static Mock getMockEndpoint()
264     {
265         return new Mock(UMOEndpoint.class, "umoEndpoint");
266     }
267 
268     public static Mock getMockEndpointURI()
269     {
270         return new Mock(UMOEndpointURI.class, "umoEndpointUri");
271     }
272 
273     public static Mock getMockDescriptor()
274     {
275         return new Mock(UMODescriptor.class, "umoDescriptor");
276     }
277 
278     public static Mock getMockTransaction()
279     {
280         return new Mock(UMOTransaction.class, "umoTransaction");
281     }
282 
283     public static Mock getMockTransactionFactory()
284     {
285         return new Mock(UMOTransactionFactory.class, "umoTransactionFactory");
286     }
287 
288 }