KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > junit > v1 > TestMonitorRunnable


1 /*
2  * @(#)TestMonitorRunnable.java
3  *
4  * The basics are taken from an article by Andy Schneider
5  * andrew.schneider@javaworld.com
6  * The article is "JUnit Best Practices"
7  * http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit_p.html
8  *
9  * 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 /**
41  * A helper class to more easily create monitors. TestRunnable monitors
42  * do not have to extend this class, but it helps in becoming more
43  * conformant to the requirements of the superclass.
44  *
45  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
46  * @version $Date: 2003/09/29 21:09:40 $
47  * @since July 12, 2003
48  */

49 public abstract class TestMonitorRunnable extends TestRunnable
50 {
51     public TestMonitorRunnable()
52     {
53         super( true );
54     }
55     
56     
57     
58     /**
59      * Performs checks on the monitored object which is being subjected
60      * to parallel processing. This method should not perform looping
61      * over the check(s), since the <tt>runTest()</tt> method will
62      * perform these.
63      *
64      * @exception Throwable any exception may be thrown and will be
65      * reported as a test failure, except for
66      * <tt>InterruptedException</tt>s, which will be ignored.
67      */

68     public abstract void runMonitor() throws Throwable JavaDoc;
69     
70     
71     /**
72      * Performs all the necessary looping, end-of-threads, and interrupt
73      * checking. The inner loop calls the <tt>runMonitor()</tt>
74      * method.
75      */

76     public void runTest() throws Throwable JavaDoc
77     {
78         while (!isDone() && !Thread.interrupted())
79         {
80             runMonitor();
81             yieldProcessing();
82         }
83         
84         // perform one last pass, to ensure it's still valid
85
runMonitor();
86     }
87     
88     
89     /**
90      * Instructs the thread to pause for a while. This method is called
91      * by the <tt>runTest()</tt> method's loop, immediately after
92      * each <tt>runMonitor()</tt> invocation. The default implementation
93      * performs a <tt>Thread.yield()</tt> call, but by putting it into
94      * this method, that behavior can be modified.
95      *
96      * @exception InterruptedException allows for overloading methods to
97      * perform a <tt>delay( long )</tt> call within their
98      * implementation.
99      */

100     protected void yieldProcessing() throws InterruptedException JavaDoc
101     {
102         Thread.yield();
103     }
104 }
105
106
Popular Tags