01: /*
02: * @(#)TestMonitorRunnable.java
03: *
04: * The basics are taken from an article by Andy Schneider
05: * andrew.schneider@javaworld.com
06: * The article is "JUnit Best Practices"
07: * http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit_p.html
08: *
09: * Part of the GroboUtils package at:
10: * http://groboutils.sourceforge.net
11: *
12: * Permission is hereby granted, free of charge, to any person obtaining a
13: * copy of this software and associated documentation files (the "Software"),
14: * to deal in the Software without restriction, including without limitation
15: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16: * and/or sell copies of the Software, and to permit persons to whom the
17: * Software is furnished to do so, subject to the following conditions:
18: *
19: * The above copyright notice and this permission notice shall be included in
20: * all copies or substantial portions of the Software.
21: *
22: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28: * DEALINGS IN THE SOFTWARE.
29: */
30:
31: package net.sourceforge.groboutils.junit.v1;
32:
33: import org.apache.log4j.Logger;
34: import junit.framework.TestCase;
35: import junit.framework.TestResult;
36: import junit.framework.AssertionFailedError;
37: import junit.framework.Assert;
38:
39: /**
40: * A helper class to more easily create monitors. TestRunnable monitors
41: * do not have to extend this class, but it helps in becoming more
42: * conformant to the requirements of the superclass.
43: *
44: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
45: * @version $Date: 2003/09/29 21:09:40 $
46: * @since July 12, 2003
47: */
48: public abstract class TestMonitorRunnable extends TestRunnable {
49: public TestMonitorRunnable() {
50: super (true);
51: }
52:
53: /**
54: * Performs checks on the monitored object which is being subjected
55: * to parallel processing. This method should not perform looping
56: * over the check(s), since the <tt>runTest()</tt> method will
57: * perform these.
58: *
59: * @exception Throwable any exception may be thrown and will be
60: * reported as a test failure, except for
61: * <tt>InterruptedException</tt>s, which will be ignored.
62: */
63: public abstract void runMonitor() throws Throwable;
64:
65: /**
66: * Performs all the necessary looping, end-of-threads, and interrupt
67: * checking. The inner loop calls the <tt>runMonitor()</tt>
68: * method.
69: */
70: public void runTest() throws Throwable {
71: while (!isDone() && !Thread.interrupted()) {
72: runMonitor();
73: yieldProcessing();
74: }
75:
76: // perform one last pass, to ensure it's still valid
77: runMonitor();
78: }
79:
80: /**
81: * Instructs the thread to pause for a while. This method is called
82: * by the <tt>runTest()</tt> method's loop, immediately after
83: * each <tt>runMonitor()</tt> invocation. The default implementation
84: * performs a <tt>Thread.yield()</tt> call, but by putting it into
85: * this method, that behavior can be modified.
86: *
87: * @exception InterruptedException allows for overloading methods to
88: * perform a <tt>delay( long )</tt> call within their
89: * implementation.
90: */
91: protected void yieldProcessing() throws InterruptedException {
92: Thread.yield();
93: }
94: }
|