KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > tck > AbstractMuleTestCase


1 /*
2  * $Id: AbstractMuleTestCase.java 3937 2006-11-20 16:04:25Z lajos $
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 MuleSource MPL
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 java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import junit.framework.TestCase;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.mule.MuleManager;
22 import org.mule.config.MuleConfiguration;
23 import org.mule.impl.MuleDescriptor;
24 import org.mule.tck.testmodels.mule.TestConnector;
25 import org.mule.umo.UMOComponent;
26 import org.mule.umo.UMOEvent;
27 import org.mule.umo.UMOEventContext;
28 import org.mule.umo.UMOException;
29 import org.mule.umo.UMOSession;
30 import org.mule.umo.endpoint.UMOEndpoint;
31 import org.mule.umo.endpoint.UMOImmutableEndpoint;
32 import org.mule.umo.manager.UMOManager;
33 import org.mule.umo.transformer.UMOTransformer;
34 import org.mule.util.FileUtils;
35 import org.mule.util.StringMessageUtils;
36
37 /**
38  * <code>AbstractMuleTestCase</code> is a base class for Mule testcases. This
39  * implementation provides services to test code for creating mock and test objects.
40  */

41 public abstract class AbstractMuleTestCase extends TestCase
42 {
43     protected transient final Log logger = LogFactory.getLog(getClass());
44
45     // This should be set to a string message describing any prerequisites not met
46
protected String JavaDoc prereqs = null;
47     private boolean offline = System.getProperty("org.mule.offline", "false").equalsIgnoreCase("true");
48     private boolean testLogging = System.getProperty("org.mule.test.logging", "false").equalsIgnoreCase(
49         "true");
50
51     private static Map JavaDoc testCounters;
52
53     public AbstractMuleTestCase()
54     {
55         super();
56         if (testCounters == null)
57         {
58             testCounters = new HashMap JavaDoc();
59         }
60         addTest();
61     }
62
63     protected void addTest()
64     {
65         TestInfo info = (TestInfo)testCounters.get(getClass().getName());
66         if (info == null)
67         {
68             info = new TestInfo(getClass().getName());
69             testCounters.put(getClass().getName(), info);
70         }
71         info.incTestCount();
72     }
73
74     protected void setDisposeManagerPerSuite(boolean val)
75     {
76         getTestInfo().setDisposeManagerPerSuite(val);
77     }
78
79     protected TestInfo getTestInfo()
80     {
81         TestInfo info = (TestInfo)testCounters.get(getClass().getName());
82         if (info == null)
83         {
84             info = new TestInfo(getClass().getName());
85             testCounters.put(getClass().getName(), info);
86         }
87         return info;
88     }
89
90     private void clearAllCounters()
91     {
92         if (testCounters != null)
93         {
94             testCounters.clear();
95         }
96         log("Cleared all counters");
97     }
98
99     private void clearCounter()
100     {
101         if (testCounters != null)
102         {
103             testCounters.remove(getClass().getName());
104         }
105         log("Cleared counter: " + getClass().getName());
106     }
107
108     private void log(String JavaDoc s)
109     {
110         if (testLogging)
111         {
112             System.err.println(s);
113         }
114     }
115
116     public String JavaDoc getName()
117     {
118         return super.getName().substring(4).replaceAll("([A-Z])", " $1").toLowerCase() + " ";
119     }
120
121     /**
122      * Use this method to do any validation such as check for an installation of a
123      * required server If the current environment does not have the preReqs of the
124      * test return false and the test will be skipped.
125      *
126      */

127     protected String JavaDoc checkPreReqs()
128     {
129         return null;
130     }
131
132     public boolean isOffline(String JavaDoc method)
133     {
134         if (offline)
135         {
136             System.out.println(StringMessageUtils.getBoilerPlate(
137                 "Working offline cannot run test: " + method, '=', 80));
138         }
139         return offline;
140     }
141
142     public boolean isPrereqsMet(String JavaDoc method)
143     {
144         prereqs = checkPreReqs();
145         if (prereqs != null)
146         {
147             System.out.println(StringMessageUtils.getBoilerPlate(
148                 "WARNING\nPrerequisites for test: " + method + " were not met. skipping test: " + prereqs,
149                 '=', 80));
150         }
151         return prereqs == null;
152     }
153
154     protected final void setUp() throws Exception JavaDoc
155     {
156         System.out.println(StringMessageUtils.getBoilerPlate("Testing: " + toString(), '=', 80));
157         MuleManager.getConfiguration().getDefaultThreadingProfile().setDoThreading(false);
158         MuleManager.getConfiguration().setServerUrl(StringUtils.EMPTY);
159
160         try
161         {
162             if (getTestInfo().getRunCount() == 0)
163             {
164                 if (getTestInfo().isDisposeManagerPerSuite())
165                 {
166                     // We dispose here jut in case
167
disposeManager();
168                 }
169                 log("Pre suiteSetup for test: " + getTestInfo());
170                 suitePreSetUp();
171             }
172             if (!getTestInfo().isDisposeManagerPerSuite())
173             {
174                 // We dispose here jut in case
175
disposeManager();
176             }
177             if (!isPrereqsMet(getClass().getName() + ".setUp()"))
178             {
179                 return;
180             }
181             doSetUp();
182             if (getTestInfo().getRunCount() == 0)
183             {
184                 log("Post suiteSetup for test: " + getTestInfo());
185                 suitePostSetUp();
186             }
187         }
188         catch (Exception JavaDoc e)
189         {
190             getTestInfo().incRunCount();
191             throw e;
192         }
193     }
194
195     protected void suitePreSetUp() throws Exception JavaDoc
196     {
197         // nothing to do
198
}
199
200     protected void suitePostSetUp() throws Exception JavaDoc
201     {
202         // nothing to do
203
}
204
205     protected void suitePreTearDown() throws Exception JavaDoc
206     {
207         // nothing to do
208
}
209
210     protected void suitePostTearDown() throws Exception JavaDoc
211     {
212         // nothing to do
213
}
214
215     protected final void tearDown() throws Exception JavaDoc
216     {
217         try
218         {
219             if (getTestInfo().getRunCount() == getTestInfo().getTestCount())
220             {
221                 log("Pre suiteTearDown for test: " + getTestInfo());
222                 suitePreTearDown();
223             }
224             doTearDown();
225             if (!getTestInfo().isDisposeManagerPerSuite())
226             {
227                 disposeManager();
228             }
229         }
230         finally
231         {
232             getTestInfo().incRunCount();
233             if (getTestInfo().getRunCount() == getTestInfo().getTestCount())
234             {
235                 try
236                 {
237                     log("Post suiteTearDown for test: " + getTestInfo());
238                     suitePostTearDown();
239                 }
240                 finally
241                 {
242                     clearCounter();
243                     disposeManager();
244                 }
245             }
246         }
247     }
248
249     protected void disposeManager()
250     {
251         log("disposing manager. disposeManagerPerSuite=" + getTestInfo().isDisposeManagerPerSuite());
252         if (MuleManager.isInstanciated())
253         {
254             MuleManager.getInstance().dispose();
255         }
256         FileUtils.deleteTree(FileUtils.newFile(MuleManager.getConfiguration().getWorkingDirectory()));
257         FileUtils.deleteTree(FileUtils.newFile("./ActiveMQ"));
258         MuleManager.setConfiguration(new MuleConfiguration());
259     }
260
261     protected void doSetUp() throws Exception JavaDoc
262     {
263         // template method
264
}
265
266     protected void doTearDown() throws Exception JavaDoc
267     {
268         // template method
269
}
270
271     public static UMOManager getManager(boolean disableAdminAgent) throws Exception JavaDoc
272     {
273         return MuleTestUtils.getManager(disableAdminAgent);
274     }
275
276     public static UMOEndpoint getTestEndpoint(String JavaDoc name, String JavaDoc type) throws Exception JavaDoc
277     {
278         return MuleTestUtils.getTestEndpoint(name, type);
279     }
280
281     public static UMOEvent getTestEvent(Object JavaDoc data) throws Exception JavaDoc
282     {
283         return MuleTestUtils.getTestEvent(data);
284     }
285
286     public static UMOEventContext getTestEventContext(Object JavaDoc data) throws Exception JavaDoc
287     {
288         return MuleTestUtils.getTestEventContext(data);
289     }
290
291     public static UMOTransformer getTestTransformer()
292     {
293         return MuleTestUtils.getTestTransformer();
294     }
295
296     public static UMOEvent getTestEvent(Object JavaDoc data, MuleDescriptor descriptor) throws Exception JavaDoc
297     {
298         return MuleTestUtils.getTestEvent(data, descriptor);
299     }
300
301     public static UMOEvent getTestEvent(Object JavaDoc data, UMOImmutableEndpoint endpoint) throws Exception JavaDoc
302     {
303         return MuleTestUtils.getTestEvent(data, endpoint);
304     }
305
306     public static UMOEvent getTestEvent(Object JavaDoc data, MuleDescriptor descriptor, UMOImmutableEndpoint endpoint)
307         throws UMOException
308     {
309         return MuleTestUtils.getTestEvent(data, descriptor, endpoint);
310     }
311
312     public static UMOSession getTestSession(UMOComponent component)
313     {
314         return MuleTestUtils.getTestSession(component);
315     }
316
317     public static TestConnector getTestConnector()
318     {
319         return MuleTestUtils.getTestConnector();
320     }
321
322     public static UMOComponent getTestComponent(MuleDescriptor descriptor)
323     {
324         return MuleTestUtils.getTestComponent(descriptor);
325     }
326
327     public static MuleDescriptor getTestDescriptor(String JavaDoc name, String JavaDoc implementation) throws Exception JavaDoc
328     {
329         return MuleTestUtils.getTestDescriptor(name, implementation);
330     }
331
332     public static UMOManager getTestManager() throws UMOException
333     {
334         return MuleTestUtils.getTestManager();
335     }
336
337     protected void finalize() throws Throwable JavaDoc
338     {
339         try
340         {
341             clearAllCounters();
342         }
343         finally
344         {
345             super.finalize();
346         }
347     }
348
349     protected class TestInfo
350     {
351         /**
352          * Whether to dispose the manager after every method or once all tests for
353          * the class have run
354          */

355         private boolean disposeManagerPerSuite = false;
356         private int testCount = 0;
357         private int runCount = 0;
358         private String JavaDoc name;
359
360         public TestInfo(String JavaDoc name)
361         {
362             this.name = name;
363         }
364
365         public void clearCounts()
366         {
367             testCount = 0;
368             runCount = 0;
369             log("Cleared counts for: " + name);
370         }
371
372         public void incTestCount()
373         {
374             testCount++;
375             log("Added test: " + name + " " + testCount);
376         }
377
378         public void incRunCount()
379         {
380             runCount++;
381             log("Finished Run: " + toString());
382         }
383
384         public int getTestCount()
385         {
386             return testCount;
387         }
388
389         public int getRunCount()
390         {
391             return runCount;
392         }
393
394         public String JavaDoc getName()
395         {
396             return name;
397         }
398
399         public boolean isDisposeManagerPerSuite()
400         {
401             return disposeManagerPerSuite;
402         }
403
404         public void setDisposeManagerPerSuite(boolean disposeManagerPerSuite)
405         {
406             this.disposeManagerPerSuite = disposeManagerPerSuite;
407         }
408
409         public String JavaDoc toString()
410         {
411             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
412             return buf.append(name).append(", (").append(runCount).append(" / ").append(testCount).append(
413                 ") tests run, disposePerSuite=").append(disposeManagerPerSuite).toString();
414         }
415     }
416 }
417
Popular Tags