View Javadoc

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