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 java.io.IOException; |
34 | |
import java.net.URI; |
35 | |
import java.net.URISyntaxException; |
36 | |
import java.net.URL; |
37 | |
import java.net.URLClassLoader; |
38 | |
import java.security.CodeSource; |
39 | |
import java.util.Collections; |
40 | |
import java.util.HashMap; |
41 | |
import java.util.Iterator; |
42 | |
import java.util.Map; |
43 | |
|
44 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
45 | |
import junit.framework.TestCase; |
46 | |
import junit.framework.TestResult; |
47 | |
import org.apache.commons.collections.IteratorUtils; |
48 | |
import org.apache.commons.collections.Predicate; |
49 | |
import org.apache.commons.logging.Log; |
50 | |
import org.apache.commons.logging.LogFactory; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public abstract class AbstractMuleTestCase extends TestCase implements TestCaseWatchdogTimeoutHandler |
57 | |
{ |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 2 | public static final String[] IGNORED_DOT_MULE_DIRS = new String[] {"transaction-log"}; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private static final boolean verbose; |
69 | |
|
70 | 848 | protected final Log logger = LogFactory.getLog(this.getClass()); |
71 | |
|
72 | |
|
73 | |
|
74 | 2 | private static final Map testInfos = Collections.synchronizedMap(new HashMap()); |
75 | |
|
76 | |
|
77 | 848 | private boolean offline = "true".equalsIgnoreCase(System.getProperty("org.mule.offline")); |
78 | |
|
79 | |
|
80 | |
private TestCaseWatchdog watchdog; |
81 | |
|
82 | |
static |
83 | |
{ |
84 | 2 | String muleOpts = SystemUtils.getenv("MULE_TEST_OPTS"); |
85 | 2 | if (StringUtils.isNotBlank(muleOpts)) |
86 | |
{ |
87 | 0 | Map parsedOpts = SystemUtils.parsePropertyDefinitions(muleOpts); |
88 | 0 | String optVerbose = (String)parsedOpts.get("mule.verbose"); |
89 | 0 | verbose = Boolean.valueOf(optVerbose).booleanValue(); |
90 | 0 | } |
91 | |
else |
92 | |
{ |
93 | |
|
94 | 2 | verbose = true; |
95 | |
} |
96 | |
|
97 | |
|
98 | 2 | MuleUrlStreamHandlerFactory.installUrlStreamHandlerFactory(); |
99 | 2 | } |
100 | |
|
101 | |
public AbstractMuleTestCase() |
102 | |
{ |
103 | 848 | super(); |
104 | |
|
105 | 848 | TestInfo info = (TestInfo) testInfos.get(getClass().getName()); |
106 | 848 | if (info == null) |
107 | |
{ |
108 | 216 | info = this.createTestInfo(); |
109 | 216 | testInfos.put(getClass().getName(), info); |
110 | |
} |
111 | |
|
112 | 848 | info.incTestCount(); |
113 | 848 | } |
114 | |
|
115 | |
protected TestInfo createTestInfo() |
116 | |
{ |
117 | 216 | return new TestInfo(this); |
118 | |
} |
119 | |
|
120 | |
protected TestInfo getTestInfo() |
121 | |
{ |
122 | 8656 | return (TestInfo) testInfos.get(this.getClass().getName()); |
123 | |
} |
124 | |
|
125 | |
private void clearInfo() |
126 | |
{ |
127 | 212 | testInfos.remove(this.getClass().getName()); |
128 | 212 | } |
129 | |
|
130 | |
public String getName() |
131 | |
{ |
132 | 2532 | return super.getName().substring(4).replaceAll("([A-Z])", " $1").toLowerCase() + " "; |
133 | |
} |
134 | |
|
135 | |
public void run(TestResult result) |
136 | |
{ |
137 | 848 | if (this.isExcluded()) |
138 | |
{ |
139 | 4 | if (verbose) |
140 | |
{ |
141 | 4 | logger.info(this.getClass().getName() + " excluded"); |
142 | |
} |
143 | 4 | return; |
144 | |
} |
145 | |
|
146 | 844 | if (this.isDisabledInThisEnvironment()) |
147 | |
{ |
148 | 0 | if (verbose) |
149 | |
{ |
150 | 0 | logger.info(this.getClass().getName() + " disabled"); |
151 | |
} |
152 | 0 | return; |
153 | |
} |
154 | |
|
155 | 844 | super.run(result); |
156 | 844 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public void runBare() throws Throwable |
165 | |
{ |
166 | |
|
167 | |
|
168 | 844 | if (this.isDisabledInThisEnvironment(super.getName())) |
169 | |
{ |
170 | 0 | logger.warn(this.getClass().getName() + "." + super.getName() + " disabled in this environment"); |
171 | 0 | return; |
172 | |
} |
173 | |
|
174 | |
|
175 | 844 | super.runBare(); |
176 | 844 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
protected boolean isDisabledInThisEnvironment() |
184 | |
{ |
185 | 844 | return false; |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
protected boolean isExcluded() |
195 | |
{ |
196 | 848 | return getTestInfo().isExcluded(); |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
protected boolean isDisabledInThisEnvironment(String testMethodName) |
205 | |
{ |
206 | 844 | return false; |
207 | |
} |
208 | |
|
209 | |
public boolean isOffline(String method) |
210 | |
{ |
211 | 0 | if (offline) |
212 | |
{ |
213 | 0 | logger.warn(StringMessageUtils.getBoilerPlate( |
214 | |
"Working offline cannot run test: " + method, '=', 80)); |
215 | |
} |
216 | 0 | return offline; |
217 | |
} |
218 | |
|
219 | |
protected boolean isDisposeManagerPerSuite() |
220 | |
{ |
221 | 0 | return getTestInfo().isDisposeManagerPerSuite(); |
222 | |
} |
223 | |
|
224 | |
protected void setDisposeManagerPerSuite(boolean val) |
225 | |
{ |
226 | 0 | getTestInfo().setDisposeManagerPerSuite(val); |
227 | 0 | } |
228 | |
|
229 | |
protected TestCaseWatchdog createWatchdog() |
230 | |
{ |
231 | 844 | return new TestCaseWatchdog(10, TimeUnit.MINUTES, this); |
232 | |
} |
233 | |
|
234 | |
public void handleTimeout(long timeout, TimeUnit unit) |
235 | |
{ |
236 | 0 | logger.fatal("Timeout of " + unit.toMillis(timeout) + "ms exceeded - exiting VM!"); |
237 | 0 | Runtime.getRuntime().halt(1); |
238 | 0 | } |
239 | |
|
240 | |
protected final void setUp() throws Exception |
241 | |
{ |
242 | |
|
243 | 844 | watchdog = createWatchdog(); |
244 | 844 | watchdog.start(); |
245 | |
|
246 | 844 | if (verbose) |
247 | |
{ |
248 | 844 | System.out.println(StringMessageUtils.getBoilerPlate("Testing: " + toString(), '=', 80)); |
249 | |
} |
250 | |
|
251 | 844 | MuleManager.getConfiguration().getDefaultThreadingProfile().setDoThreading(false); |
252 | 844 | MuleManager.getConfiguration().setServerUrl(StringUtils.EMPTY); |
253 | |
|
254 | |
try |
255 | |
{ |
256 | 844 | if (getTestInfo().getRunCount() == 0) |
257 | |
{ |
258 | 212 | if (getTestInfo().isDisposeManagerPerSuite()) |
259 | |
{ |
260 | |
|
261 | 0 | disposeManager(); |
262 | |
} |
263 | 212 | suitePreSetUp(); |
264 | |
} |
265 | 844 | if (!getTestInfo().isDisposeManagerPerSuite()) |
266 | |
{ |
267 | |
|
268 | 844 | disposeManager(); |
269 | |
} |
270 | 844 | doSetUp(); |
271 | 844 | if (getTestInfo().getRunCount() == 0) |
272 | |
{ |
273 | 212 | suitePostSetUp(); |
274 | |
} |
275 | |
} |
276 | 0 | catch (Exception e) |
277 | |
{ |
278 | 0 | getTestInfo().incRunCount(); |
279 | 0 | throw e; |
280 | 844 | } |
281 | 844 | } |
282 | |
|
283 | |
protected void suitePreSetUp() throws Exception |
284 | |
{ |
285 | |
|
286 | 212 | } |
287 | |
|
288 | |
protected void suitePostSetUp() throws Exception |
289 | |
{ |
290 | |
|
291 | 212 | } |
292 | |
|
293 | |
protected void suitePreTearDown() throws Exception |
294 | |
{ |
295 | |
|
296 | 0 | } |
297 | |
|
298 | |
protected void suitePostTearDown() throws Exception |
299 | |
{ |
300 | |
|
301 | 212 | } |
302 | |
|
303 | |
protected final void tearDown() throws Exception |
304 | |
{ |
305 | |
try |
306 | |
{ |
307 | 844 | if (getTestInfo().getRunCount() == getTestInfo().getTestCount()) |
308 | |
{ |
309 | 0 | suitePreTearDown(); |
310 | |
} |
311 | |
|
312 | 844 | doTearDown(); |
313 | |
|
314 | 844 | if (!getTestInfo().isDisposeManagerPerSuite()) |
315 | |
{ |
316 | 844 | disposeManager(); |
317 | |
} |
318 | |
} |
319 | |
finally |
320 | |
{ |
321 | 0 | try |
322 | |
{ |
323 | 844 | getTestInfo().incRunCount(); |
324 | 844 | if (getTestInfo().getRunCount() == getTestInfo().getTestCount()) |
325 | |
{ |
326 | |
try |
327 | |
{ |
328 | 212 | suitePostTearDown(); |
329 | |
} |
330 | |
finally |
331 | |
{ |
332 | 212 | clearInfo(); |
333 | 212 | disposeManager(); |
334 | 212 | } |
335 | |
} |
336 | |
} |
337 | |
finally |
338 | |
{ |
339 | |
|
340 | 844 | watchdog.cancel(); |
341 | 844 | } |
342 | 844 | } |
343 | 844 | } |
344 | |
|
345 | |
protected void disposeManager() throws UMOException |
346 | |
{ |
347 | 1900 | if (MuleManager.isInstanciated()) |
348 | |
{ |
349 | 392 | MuleManager.getInstance().dispose(); |
350 | |
} |
351 | |
|
352 | |
|
353 | 1900 | FileUtils.deleteTree(FileUtils.newFile(MuleManager.getConfiguration().getWorkingDirectory()), |
354 | |
IGNORED_DOT_MULE_DIRS); |
355 | 1900 | FileUtils.deleteTree(FileUtils.newFile("./ActiveMQ")); |
356 | 1900 | MuleManager.setConfiguration(new MuleConfiguration()); |
357 | 1900 | } |
358 | |
|
359 | |
protected void doSetUp() throws Exception |
360 | |
{ |
361 | |
|
362 | 552 | } |
363 | |
|
364 | |
protected void doTearDown() throws Exception |
365 | |
{ |
366 | |
|
367 | 592 | } |
368 | |
|
369 | |
public static UMOManager getManager(boolean disableAdminAgent) throws Exception |
370 | |
{ |
371 | 44 | return MuleTestUtils.getManager(disableAdminAgent); |
372 | |
} |
373 | |
|
374 | |
public static UMOModel getDefaultModel() throws UMOException |
375 | |
{ |
376 | 24 | return MuleTestUtils.getDefaultModel(); |
377 | |
} |
378 | |
|
379 | |
public static UMOEndpoint getTestEndpoint(String name, String type) throws Exception |
380 | |
{ |
381 | 124 | return MuleTestUtils.getTestEndpoint(name, type); |
382 | |
} |
383 | |
|
384 | |
public static UMOEvent getTestEvent(Object data) throws Exception |
385 | |
{ |
386 | 200 | return MuleTestUtils.getTestEvent(data); |
387 | |
} |
388 | |
|
389 | |
public static UMOEventContext getTestEventContext(Object data) throws Exception |
390 | |
{ |
391 | 8 | return MuleTestUtils.getTestEventContext(data); |
392 | |
} |
393 | |
|
394 | |
public static UMOTransformer getTestTransformer() |
395 | |
{ |
396 | 0 | return MuleTestUtils.getTestTransformer(); |
397 | |
} |
398 | |
|
399 | |
public static UMOEvent getTestEvent(Object data, MuleDescriptor descriptor) throws Exception |
400 | |
{ |
401 | 2 | return MuleTestUtils.getTestEvent(data, descriptor); |
402 | |
} |
403 | |
|
404 | |
public static UMOEvent getTestEvent(Object data, UMOImmutableEndpoint endpoint) throws Exception |
405 | |
{ |
406 | 4 | return MuleTestUtils.getTestEvent(data, endpoint); |
407 | |
} |
408 | |
|
409 | |
public static UMOEvent getTestEvent(Object data, MuleDescriptor descriptor, UMOImmutableEndpoint endpoint) |
410 | |
throws UMOException |
411 | |
{ |
412 | 6 | return MuleTestUtils.getTestEvent(data, descriptor, endpoint); |
413 | |
} |
414 | |
|
415 | |
public static UMOSession getTestSession(UMOComponent component) |
416 | |
{ |
417 | 16 | return MuleTestUtils.getTestSession(component); |
418 | |
} |
419 | |
|
420 | |
public static TestConnector getTestConnector() |
421 | |
{ |
422 | 6 | return MuleTestUtils.getTestConnector(); |
423 | |
} |
424 | |
|
425 | |
public static UMOComponent getTestComponent(MuleDescriptor descriptor) |
426 | |
{ |
427 | 68 | return MuleTestUtils.getTestComponent(descriptor); |
428 | |
} |
429 | |
|
430 | |
public static MuleDescriptor getTestDescriptor(String name, String implementation) throws Exception |
431 | |
{ |
432 | 146 | return MuleTestUtils.getTestDescriptor(name, implementation); |
433 | |
} |
434 | |
|
435 | |
public static UMOManager getTestManager() throws Exception |
436 | |
{ |
437 | 0 | return MuleTestUtils.getManager(true); |
438 | |
} |
439 | |
|
440 | 2150 | public static class TestInfo |
441 | |
{ |
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
private final String name; |
447 | 216 | private boolean disposeManagerPerSuite = false; |
448 | 216 | private boolean excluded = false; |
449 | 216 | private int testCount = 0; |
450 | 216 | private int runCount = 0; |
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
public static URL getClassPathRoot(Class clazz) |
456 | |
{ |
457 | 216 | CodeSource cs = clazz.getProtectionDomain().getCodeSource(); |
458 | 216 | return (cs != null ? cs.getLocation() : null); |
459 | |
} |
460 | |
|
461 | |
public TestInfo(TestCase test) |
462 | 216 | { |
463 | 216 | this.name = test.getClass().getName(); |
464 | |
|
465 | |
|
466 | |
try |
467 | |
{ |
468 | |
|
469 | |
|
470 | |
|
471 | 216 | URL[] urls = new URL[]{getClassPathRoot(test.getClass())}; |
472 | 216 | URL fileUrl = new URLClassLoader(urls).getResource("mule-test-exclusions.txt"); |
473 | |
|
474 | 216 | if (fileUrl != null) |
475 | |
{ |
476 | |
|
477 | 216 | URI fileUri = new URI(StringUtils.removeStart(fileUrl.toString(), "jar:")); |
478 | |
|
479 | |
|
480 | 216 | Iterator lines = FileUtils.lineIterator(FileUtils.newFile(fileUri)); |
481 | |
|
482 | |
|
483 | 216 | excluded = IteratorUtils.filteredIterator(lines, new Predicate() |
484 | |
{ |
485 | 216 | public boolean evaluate(Object object) |
486 | |
{ |
487 | 2150 | return StringUtils.equals(name, StringUtils.trimToEmpty((String) object)); |
488 | |
} |
489 | |
}).hasNext(); |
490 | |
} |
491 | |
} |
492 | 0 | catch (IOException ioex) |
493 | |
{ |
494 | |
|
495 | |
} |
496 | 0 | catch (URISyntaxException e) |
497 | |
{ |
498 | |
|
499 | 216 | } |
500 | 216 | } |
501 | |
|
502 | |
public int getTestCount() |
503 | |
{ |
504 | 1688 | return testCount; |
505 | |
} |
506 | |
|
507 | |
public void incTestCount() |
508 | |
{ |
509 | 848 | testCount++; |
510 | 848 | } |
511 | |
|
512 | |
public int getRunCount() |
513 | |
{ |
514 | 3376 | return runCount; |
515 | |
} |
516 | |
|
517 | |
public void incRunCount() |
518 | |
{ |
519 | 844 | runCount++; |
520 | 844 | } |
521 | |
|
522 | |
public String getName() |
523 | |
{ |
524 | 0 | return name; |
525 | |
} |
526 | |
|
527 | |
public boolean isDisposeManagerPerSuite() |
528 | |
{ |
529 | 1900 | return disposeManagerPerSuite; |
530 | |
} |
531 | |
|
532 | |
public void setDisposeManagerPerSuite(boolean disposeManagerPerSuite) |
533 | |
{ |
534 | 0 | this.disposeManagerPerSuite = disposeManagerPerSuite; |
535 | 0 | } |
536 | |
|
537 | |
public boolean isExcluded() |
538 | |
{ |
539 | 848 | return excluded; |
540 | |
} |
541 | |
|
542 | |
public String toString() |
543 | |
{ |
544 | 0 | StringBuffer buf = new StringBuffer(); |
545 | 0 | return buf.append(name).append(", (").append(runCount).append(" / ").append(testCount).append( |
546 | |
") tests run, disposePerSuite=").append(disposeManagerPerSuite).toString(); |
547 | |
} |
548 | |
} |
549 | |
} |