1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.tck; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.MuleConfiguration; |
15 | |
import org.mule.impl.MuleDescriptor; |
16 | |
import org.mule.tck.testmodels.mule.TestConnector; |
17 | |
import org.mule.umo.UMOComponent; |
18 | |
import org.mule.umo.UMOEvent; |
19 | |
import org.mule.umo.UMOEventContext; |
20 | |
import org.mule.umo.UMOException; |
21 | |
import org.mule.umo.UMOSession; |
22 | |
import org.mule.umo.endpoint.UMOEndpoint; |
23 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
24 | |
import org.mule.umo.manager.UMOManager; |
25 | |
import org.mule.umo.model.UMOModel; |
26 | |
import org.mule.umo.transformer.UMOTransformer; |
27 | |
import org.mule.util.FileUtils; |
28 | |
import org.mule.util.MuleUrlStreamHandlerFactory; |
29 | |
import org.mule.util.StringMessageUtils; |
30 | |
import org.mule.util.StringUtils; |
31 | |
import org.mule.util.SystemUtils; |
32 | |
|
33 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
34 | |
|
35 | |
import java.util.HashMap; |
36 | |
import java.util.Map; |
37 | |
|
38 | |
import junit.framework.TestCase; |
39 | |
import junit.framework.TestResult; |
40 | |
|
41 | |
import org.apache.commons.logging.Log; |
42 | |
import org.apache.commons.logging.LogFactory; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public abstract class AbstractMuleTestCase extends TestCase |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | |
private static final boolean verbose; |
54 | |
|
55 | 0 | protected final Log logger = LogFactory.getLog(this.getClass()); |
56 | |
|
57 | |
|
58 | 0 | private boolean offline = System.getProperty("org.mule.offline", "false").equalsIgnoreCase("true"); |
59 | |
|
60 | |
private static Map testCounters; |
61 | |
|
62 | |
private TestCaseWatchdog watchdog; |
63 | |
|
64 | |
static |
65 | |
{ |
66 | 0 | String muleOpts = SystemUtils.getenv("MULE_TEST_OPTS"); |
67 | 0 | if (StringUtils.isNotBlank(muleOpts)) |
68 | |
{ |
69 | 0 | Map parsedOpts = SystemUtils.parsePropertyDefinitions(muleOpts); |
70 | 0 | String optVerbose = (String)parsedOpts.get("mule.verbose"); |
71 | 0 | verbose = Boolean.valueOf(optVerbose).booleanValue(); |
72 | |
} |
73 | |
else |
74 | |
{ |
75 | |
|
76 | 0 | verbose = true; |
77 | |
} |
78 | |
|
79 | |
|
80 | 0 | MuleUrlStreamHandlerFactory.installUrlStreamHandlerFactory(); |
81 | 0 | } |
82 | |
|
83 | |
public AbstractMuleTestCase() |
84 | |
{ |
85 | 0 | super(); |
86 | 0 | if (testCounters == null) |
87 | |
{ |
88 | 0 | testCounters = new HashMap(); |
89 | |
} |
90 | 0 | addTest(); |
91 | 0 | } |
92 | |
|
93 | |
protected void addTest() |
94 | |
{ |
95 | 0 | TestInfo info = (TestInfo) testCounters.get(getClass().getName()); |
96 | 0 | if (info == null) |
97 | |
{ |
98 | 0 | info = new TestInfo(getClass().getName()); |
99 | 0 | testCounters.put(getClass().getName(), info); |
100 | |
} |
101 | 0 | info.incTestCount(); |
102 | 0 | } |
103 | |
|
104 | |
protected void setDisposeManagerPerSuite(boolean val) |
105 | |
{ |
106 | 0 | getTestInfo().setDisposeManagerPerSuite(val); |
107 | 0 | } |
108 | |
|
109 | |
protected TestInfo getTestInfo() |
110 | |
{ |
111 | 0 | TestInfo info = (TestInfo) testCounters.get(getClass().getName()); |
112 | 0 | if (info == null) |
113 | |
{ |
114 | 0 | info = new TestInfo(getClass().getName()); |
115 | 0 | testCounters.put(getClass().getName(), info); |
116 | |
} |
117 | 0 | return info; |
118 | |
} |
119 | |
|
120 | |
private void clearAllCounters() |
121 | |
{ |
122 | 0 | if (testCounters != null) |
123 | |
{ |
124 | 0 | testCounters.clear(); |
125 | |
} |
126 | 0 | } |
127 | |
|
128 | |
private void clearCounter() |
129 | |
{ |
130 | 0 | if (testCounters != null) |
131 | |
{ |
132 | 0 | testCounters.remove(getClass().getName()); |
133 | |
} |
134 | 0 | } |
135 | |
|
136 | |
public String getName() |
137 | |
{ |
138 | 0 | return super.getName().substring(4).replaceAll("([A-Z])", " $1").toLowerCase() + " "; |
139 | |
} |
140 | |
|
141 | |
public void run(TestResult result) |
142 | |
{ |
143 | 0 | if (this.isDisabledInThisEnvironment()) |
144 | |
{ |
145 | 0 | logger.info(this.getClass().getName() + " disabled"); |
146 | 0 | return; |
147 | |
} |
148 | |
|
149 | 0 | super.run(result); |
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
protected boolean isDisabledInThisEnvironment() |
158 | |
{ |
159 | 0 | return false; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public void runBare() throws Throwable |
169 | |
{ |
170 | |
|
171 | |
|
172 | 0 | if (this.isDisabledInThisEnvironment(super.getName())) |
173 | |
{ |
174 | 0 | logger.warn(this.getClass().getName() + "." + super.getName() + " disabled in this environment"); |
175 | 0 | return; |
176 | |
} |
177 | |
|
178 | |
|
179 | 0 | super.runBare(); |
180 | 0 | } |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
protected boolean isDisabledInThisEnvironment(String testMethodName) |
188 | |
{ |
189 | 0 | return false; |
190 | |
} |
191 | |
|
192 | |
public boolean isOffline(String method) |
193 | |
{ |
194 | 0 | if (offline) |
195 | |
{ |
196 | 0 | logger.warn(StringMessageUtils.getBoilerPlate( |
197 | |
"Working offline cannot run test: " + method, '=', 80)); |
198 | |
} |
199 | 0 | return offline; |
200 | |
} |
201 | |
|
202 | |
protected final void setUp() throws Exception |
203 | |
{ |
204 | |
|
205 | 0 | watchdog = new TestCaseWatchdog(30, TimeUnit.MINUTES); |
206 | 0 | watchdog.start(); |
207 | |
|
208 | 0 | if (verbose) |
209 | |
{ |
210 | 0 | System.out.println(StringMessageUtils.getBoilerPlate("Testing: " + toString(), '=', 80)); |
211 | |
} |
212 | 0 | MuleManager.getConfiguration().getDefaultThreadingProfile().setDoThreading(false); |
213 | 0 | MuleManager.getConfiguration().setServerUrl(StringUtils.EMPTY); |
214 | |
|
215 | |
try |
216 | |
{ |
217 | 0 | if (getTestInfo().getRunCount() == 0) |
218 | |
{ |
219 | 0 | if (getTestInfo().isDisposeManagerPerSuite()) |
220 | |
{ |
221 | |
|
222 | 0 | disposeManager(); |
223 | |
} |
224 | 0 | suitePreSetUp(); |
225 | |
} |
226 | 0 | if (!getTestInfo().isDisposeManagerPerSuite()) |
227 | |
{ |
228 | |
|
229 | 0 | disposeManager(); |
230 | |
} |
231 | 0 | doSetUp(); |
232 | 0 | if (getTestInfo().getRunCount() == 0) |
233 | |
{ |
234 | 0 | suitePostSetUp(); |
235 | |
} |
236 | |
} |
237 | 0 | catch (Exception e) |
238 | |
{ |
239 | 0 | getTestInfo().incRunCount(); |
240 | 0 | throw e; |
241 | 0 | } |
242 | 0 | } |
243 | |
|
244 | |
protected void suitePreSetUp() throws Exception |
245 | |
{ |
246 | |
|
247 | 0 | } |
248 | |
|
249 | |
protected void suitePostSetUp() throws Exception |
250 | |
{ |
251 | |
|
252 | 0 | } |
253 | |
|
254 | |
protected void suitePreTearDown() throws Exception |
255 | |
{ |
256 | |
|
257 | 0 | } |
258 | |
|
259 | |
protected void suitePostTearDown() throws Exception |
260 | |
{ |
261 | |
|
262 | 0 | } |
263 | |
|
264 | |
protected final void tearDown() throws Exception |
265 | |
{ |
266 | |
try |
267 | |
{ |
268 | 0 | if (getTestInfo().getRunCount() == getTestInfo().getTestCount()) |
269 | |
{ |
270 | 0 | suitePreTearDown(); |
271 | |
} |
272 | 0 | doTearDown(); |
273 | 0 | if (!getTestInfo().isDisposeManagerPerSuite()) |
274 | |
{ |
275 | 0 | disposeManager(); |
276 | |
} |
277 | 0 | } |
278 | |
finally |
279 | |
{ |
280 | 0 | try |
281 | |
{ |
282 | 0 | getTestInfo().incRunCount(); |
283 | 0 | if (getTestInfo().getRunCount() == getTestInfo().getTestCount()) |
284 | |
{ |
285 | |
try |
286 | |
{ |
287 | 0 | suitePostTearDown(); |
288 | |
} |
289 | |
finally |
290 | |
{ |
291 | 0 | clearCounter(); |
292 | 0 | disposeManager(); |
293 | 0 | } |
294 | |
} |
295 | |
} |
296 | |
finally |
297 | |
{ |
298 | |
|
299 | 0 | watchdog.cancel(); |
300 | 0 | } |
301 | 0 | } |
302 | 0 | } |
303 | |
|
304 | |
protected void disposeManager() throws UMOException |
305 | |
{ |
306 | 0 | if (MuleManager.isInstanciated()) |
307 | |
{ |
308 | 0 | MuleManager.getInstance().dispose(); |
309 | |
} |
310 | 0 | FileUtils.deleteTree(FileUtils.newFile(MuleManager.getConfiguration().getWorkingDirectory())); |
311 | 0 | FileUtils.deleteTree(FileUtils.newFile("./ActiveMQ")); |
312 | 0 | MuleManager.setConfiguration(new MuleConfiguration()); |
313 | 0 | } |
314 | |
|
315 | |
protected void doSetUp() throws Exception |
316 | |
{ |
317 | |
|
318 | 0 | } |
319 | |
|
320 | |
protected void doTearDown() throws Exception |
321 | |
{ |
322 | |
|
323 | 0 | } |
324 | |
|
325 | |
public static UMOManager getManager(boolean disableAdminAgent) throws Exception |
326 | |
{ |
327 | 0 | return MuleTestUtils.getManager(disableAdminAgent); |
328 | |
} |
329 | |
|
330 | |
public static UMOModel getDefaultModel() throws UMOException |
331 | |
{ |
332 | 0 | return MuleTestUtils.getDefaultModel(); |
333 | |
} |
334 | |
|
335 | |
public static UMOEndpoint getTestEndpoint(String name, String type) throws Exception |
336 | |
{ |
337 | 0 | return MuleTestUtils.getTestEndpoint(name, type); |
338 | |
} |
339 | |
|
340 | |
public static UMOEvent getTestEvent(Object data) throws Exception |
341 | |
{ |
342 | 0 | return MuleTestUtils.getTestEvent(data); |
343 | |
} |
344 | |
|
345 | |
public static UMOEventContext getTestEventContext(Object data) throws Exception |
346 | |
{ |
347 | 0 | return MuleTestUtils.getTestEventContext(data); |
348 | |
} |
349 | |
|
350 | |
public static UMOTransformer getTestTransformer() |
351 | |
{ |
352 | 0 | return MuleTestUtils.getTestTransformer(); |
353 | |
} |
354 | |
|
355 | |
public static UMOEvent getTestEvent(Object data, MuleDescriptor descriptor) throws Exception |
356 | |
{ |
357 | 0 | return MuleTestUtils.getTestEvent(data, descriptor); |
358 | |
} |
359 | |
|
360 | |
public static UMOEvent getTestEvent(Object data, UMOImmutableEndpoint endpoint) throws Exception |
361 | |
{ |
362 | 0 | return MuleTestUtils.getTestEvent(data, endpoint); |
363 | |
} |
364 | |
|
365 | |
public static UMOEvent getTestEvent(Object data, MuleDescriptor descriptor, UMOImmutableEndpoint endpoint) |
366 | |
throws UMOException |
367 | |
{ |
368 | 0 | return MuleTestUtils.getTestEvent(data, descriptor, endpoint); |
369 | |
} |
370 | |
|
371 | |
public static UMOSession getTestSession(UMOComponent component) |
372 | |
{ |
373 | 0 | return MuleTestUtils.getTestSession(component); |
374 | |
} |
375 | |
|
376 | |
public static TestConnector getTestConnector() |
377 | |
{ |
378 | 0 | return MuleTestUtils.getTestConnector(); |
379 | |
} |
380 | |
|
381 | |
public static UMOComponent getTestComponent(MuleDescriptor descriptor) |
382 | |
{ |
383 | 0 | return MuleTestUtils.getTestComponent(descriptor); |
384 | |
} |
385 | |
|
386 | |
public static MuleDescriptor getTestDescriptor(String name, String implementation) throws Exception |
387 | |
{ |
388 | 0 | return MuleTestUtils.getTestDescriptor(name, implementation); |
389 | |
} |
390 | |
|
391 | |
public static UMOManager getTestManager() throws Exception |
392 | |
{ |
393 | 0 | return MuleTestUtils.getManager(true); |
394 | |
} |
395 | |
|
396 | |
protected void finalize() throws Throwable |
397 | |
{ |
398 | |
try |
399 | |
{ |
400 | 0 | clearAllCounters(); |
401 | |
} |
402 | |
finally |
403 | |
{ |
404 | 0 | super.finalize(); |
405 | 0 | } |
406 | 0 | } |
407 | |
|
408 | |
protected class TestInfo |
409 | |
{ |
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | 0 | private boolean disposeManagerPerSuite = false; |
415 | 0 | private int testCount = 0; |
416 | 0 | private int runCount = 0; |
417 | |
private String name; |
418 | |
|
419 | |
public TestInfo(String name) |
420 | 0 | { |
421 | 0 | this.name = name; |
422 | 0 | } |
423 | |
|
424 | |
public void clearCounts() |
425 | |
{ |
426 | 0 | testCount = 0; |
427 | 0 | runCount = 0; |
428 | 0 | } |
429 | |
|
430 | |
public void incTestCount() |
431 | |
{ |
432 | 0 | testCount++; |
433 | 0 | } |
434 | |
|
435 | |
public void incRunCount() |
436 | |
{ |
437 | 0 | runCount++; |
438 | 0 | } |
439 | |
|
440 | |
public int getTestCount() |
441 | |
{ |
442 | 0 | return testCount; |
443 | |
} |
444 | |
|
445 | |
public int getRunCount() |
446 | |
{ |
447 | 0 | return runCount; |
448 | |
} |
449 | |
|
450 | |
public String getName() |
451 | |
{ |
452 | 0 | return name; |
453 | |
} |
454 | |
|
455 | |
public boolean isDisposeManagerPerSuite() |
456 | |
{ |
457 | 0 | return disposeManagerPerSuite; |
458 | |
} |
459 | |
|
460 | |
public void setDisposeManagerPerSuite(boolean disposeManagerPerSuite) |
461 | |
{ |
462 | 0 | this.disposeManagerPerSuite = disposeManagerPerSuite; |
463 | 0 | } |
464 | |
|
465 | |
public String toString() |
466 | |
{ |
467 | 0 | StringBuffer buf = new StringBuffer(); |
468 | 0 | return buf.append(name).append(", (").append(runCount).append(" / ").append(testCount).append( |
469 | |
") tests run, disposePerSuite=").append(disposeManagerPerSuite).toString(); |
470 | |
} |
471 | |
} |
472 | |
} |