KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)TestRunnableUTest.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.junit.v1;
28
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33 import java.io.IOException JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35
36
37 /**
38  * Tests the TestRunnable class.
39  *
40  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
41  * @since March 1, 2002
42  * @version $Date: 2003/07/14 22:26:27 $
43  */

44 public class TestRunnableUTest extends TestCase
45 {
46     //-------------------------------------------------------------------------
47
// Standard JUnit Class-specific declarations
48

49     private static final Class JavaDoc THIS_CLASS = TestRunnableUTest.class;
50     
51     public TestRunnableUTest( String JavaDoc name )
52     {
53         super( name );
54     }
55
56     
57
58
59     //-------------------------------------------------------------------------
60
// Tests
61

62     private static class MyTestRunnable extends TestRunnable
63     {
64         Throwable JavaDoc t;
65         public MyTestRunnable( Throwable JavaDoc t )
66         {
67             this.t = t;
68         }
69         
70         public void runTest() throws Throwable JavaDoc
71         {
72             if (this.t != null)
73             {
74                 throw this.t;
75             }
76         }
77     }
78     
79     
80     public void testDelay1() throws InterruptedException JavaDoc
81     {
82         // JVMs do not have to delay exactly the amount listed, hence the
83
// error.
84
long delay = 100L;
85         long error = 10L;
86         long minDelay = delay - error;
87         TestRunnable tr = createTestRunnable( null );
88         long start = System.currentTimeMillis();
89         tr.delay( delay );
90         long end = System.currentTimeMillis();
91         assertTrue(
92             "Did not delay for long enough (delayed "+(end - start)+
93             " ms, should have delayed at least "+delay+" ms).",
94             (end - start) >= minDelay );
95     }
96     
97     
98     public void testRun1()
99     {
100         TestRunnable tr = createTestRunnable( null );
101         try
102         {
103             tr.run();
104             fail( "Did not throw IllegalStateException." );
105         }
106         catch (IllegalStateException JavaDoc e)
107         {
108             // test exception ???
109
}
110     }
111     
112     
113     public void testRun2() throws Throwable JavaDoc
114     {
115         TestRunnable tr = createTestRunnable( null );
116         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
117             new TestRunnable[] { tr } );
118         try
119         {
120             tr.run();
121             fail( "Did not throw IllegalStateException." );
122         }
123         catch (IllegalStateException JavaDoc e)
124         {
125             // test exception ???
126
}
127     }
128     
129     
130     public void testRun3() throws Throwable JavaDoc
131     {
132         TestRunnable tr = createTestRunnable( null );
133         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
134             new TestRunnable[] { tr } );
135         mttr.runTestRunnables( 1000 );
136     }
137     
138     
139     public void testRun4()
140     {
141         Throwable JavaDoc t = new Throwable JavaDoc( "Ignore" );
142         TestRunnable tr = createTestRunnable( t );
143         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
144             new TestRunnable[] { tr } );
145         try
146         {
147             mttr.runTestRunnables( 1000 );
148             fail( "Did not throw an exception." );
149         }
150         catch (Throwable JavaDoc actualT)
151         {
152             assertEquals(
153                 "Did not throw the intended exception.",
154                 actualT,
155                 t );
156         }
157     }
158     
159     
160     
161     
162     
163     //-------------------------------------------------------------------------
164
// Helpers
165

166     
167     
168     
169     protected TestRunnable createTestRunnable( Throwable JavaDoc throwThis )
170     {
171         return new MyTestRunnable( throwThis );
172     }
173     
174     
175     
176     //-------------------------------------------------------------------------
177
// Standard JUnit declarations
178

179     
180     public static Test suite()
181     {
182         TestSuite suite = new TestSuite( THIS_CLASS );
183         
184         return suite;
185     }
186     
187     public static void main( String JavaDoc[] args )
188     {
189         String JavaDoc[] name = { THIS_CLASS.getName() };
190         
191         // junit.textui.TestRunner.main( name );
192
// junit.swingui.TestRunner.main( name );
193

194         junit.textui.TestRunner.main( name );
195     }
196     
197     
198     /**
199      *
200      * @exception Exception thrown under any exceptional condition.
201      */

202     protected void setUp() throws Exception JavaDoc
203     {
204         super.setUp();
205         
206         // set ourself up
207
}
208     
209     
210     /**
211      *
212      * @exception Exception thrown under any exceptional condition.
213      */

214     protected void tearDown() throws Exception JavaDoc
215     {
216         // tear ourself down
217

218         
219         super.tearDown();
220     }
221 }
222
223
Popular Tags