KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)MultiThreadedTestRunnerUTest.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 net.sourceforge.groboutils.autodoc.v1.AutoDoc;
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 import java.io.IOException JavaDoc;
35
36
37 /**
38  * Tests the MultiThreadedTestRunner 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: 2004/04/09 19:26:52 $
43  */

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

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

63     
64     public void testConstructor1()
65     {
66         DOC.getLog().debug("testConstructor1");
67         try
68         {
69             new MultiThreadedTestRunner( null );
70             fail("did not throw an IllegalArgumentException.");
71         }
72         catch (IllegalArgumentException JavaDoc e)
73         {
74             // check exception text?
75
}
76     }
77     
78     
79     public void testConstructor2()
80     {
81         DOC.getLog().debug("testConstructor2");
82         try
83         {
84             new MultiThreadedTestRunner( new TestRunnable[0] );
85             fail("did not throw an IllegalArgumentException.");
86         }
87         catch (IllegalArgumentException JavaDoc e)
88         {
89             // check exception text?
90
}
91     }
92     
93     
94     static class RunValue1
95     {
96         public int value;
97     }
98     
99     public synchronized void testRun1()
100             throws Throwable JavaDoc
101     {
102         DOC.getIT().testsIssue( 526710 );
103         DOC.getLog().debug("testRun1");
104         
105         final RunValue1 runCount = new RunValue1();
106         TestRunnable tr1 = new TestRunnable() {
107                 public void runTest() throws Throwable JavaDoc
108                 {
109                     DOC.getLog().debug("Running test testRun1");
110                     ++runCount.value;
111                 }
112         };
113         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
114             new TestRunnable[] { tr1 } );
115         mttr.runTestRunnables();
116         
117         assertEquals(
118             "Did not run the runTest method enough times.",
119             1,
120             runCount.value );
121     }
122     
123     
124     public synchronized void testRun2()
125             throws Throwable JavaDoc
126     {
127         DOC.getIT().testsIssue( 526710 );
128         DOC.getLog().debug("testRun2");
129         
130         final RunValue1 runCount = new RunValue1();
131         TestRunnable tr1 = new TestRunnable() {
132                 public void runTest() throws Throwable JavaDoc
133                 {
134                     ++runCount.value;
135                 }
136         };
137         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
138             new TestRunnable[] { tr1 } );
139         mttr.runTestRunnables( 1000 );
140         
141         assertEquals(
142             "Did not run the runTest method enough times.",
143             1,
144             runCount.value );
145     }
146     
147     
148     public synchronized void testRun3a()
149             throws Throwable JavaDoc
150     {
151         DOC.getIT().testsIssue( 526710 );
152         DOC.getLog().debug("testRun3a");
153         DOC.getLog().warn(
154         "****************\n"+
155         "This test may expose timing issues, where only one test is run.\n"+
156         "I've only seen this with JDK 1.1, but that may expose an underlying\n"+
157         "problem. This will require further investigation.\n"+
158         "****************" );
159         
160         final RunValue1 runCount = new RunValue1();
161         TestRunnable tr1 = new TestRunnable() {
162                 public synchronized void runTest() throws Throwable JavaDoc
163                 {
164                     ++runCount.value;
165                 }
166         };
167         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
168             new TestRunnable[] { tr1, tr1 } );
169         mttr.runTestRunnables();
170         
171         assertEquals(
172             "Did not run the runTest method enough times.",
173             2,
174             runCount.value );
175     }
176     
177     
178     public synchronized void testRun3b()
179             throws Throwable JavaDoc
180     {
181         DOC.getLog().debug("testRun3b");
182         final RunValue1 runCount = new RunValue1();
183         TestRunnable tr1 = new TestRunnable() {
184                 public void runTest() throws Throwable JavaDoc
185                 {
186                     ++runCount.value;
187                 }
188         };
189         int totalCount = 15;
190         TestRunnable trList[] = new TestRunnable[ totalCount ];
191         for (int i = 0; i < totalCount; ++i)
192         {
193             trList[ i ] = tr1;
194         }
195         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( trList );
196         mttr.runTestRunnables();
197         
198         assertEquals(
199             "Did not run the runTest method enough times.",
200             totalCount,
201             runCount.value );
202     }
203     
204     
205     public void testRun4()
206             throws Throwable JavaDoc
207     {
208         DOC.getLog().debug("testRun4");
209         TestRunnable tr1 = new TestRunnable() {
210                 public void runTest() throws Throwable JavaDoc
211                 {
212                     throw new IOException JavaDoc("My exception");
213                 }
214         };
215         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
216             new TestRunnable[] { tr1 } );
217         try
218         {
219             mttr.runTestRunnables();
220             fail("did not throw IOException.");
221         }
222         catch (IOException JavaDoc ioe)
223         {
224             // check message
225
assertEquals(
226                 "IOException not the one we threw.",
227                 "My exception",
228                 ioe.getMessage() );
229         }
230     }
231     
232     
233     public void testRun5()
234             throws Throwable JavaDoc
235     {
236         DOC.getLog().debug("testRun5");
237         TestRunnable tr1 = new TestRunnable() {
238                 public void runTest() throws Throwable JavaDoc
239                 {
240                     Object JavaDoc o = new Object JavaDoc();
241                     synchronized( o )
242                     {
243                         o.wait();
244                     }
245                 }
246         };
247         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
248             new TestRunnable[] { tr1 } );
249         boolean fail = false;
250         try
251         {
252             // wait for 10 ms to ensure the join loop is not entered
253
mttr.runTestRunnables( 10 );
254             fail = true;
255         }
256         catch (junit.framework.AssertionFailedError e)
257         {
258             // test failure?
259
}
260         if (fail)
261         {
262             fail("Did not throw an assertion failed error.");
263         }
264     }
265     
266     
267     public void testRun5a()
268             throws Throwable JavaDoc
269     {
270         DOC.getLog().debug("testRun5a");
271         TestRunnable tr1 = new TestRunnable() {
272                 public void runTest() throws Throwable JavaDoc
273                 {
274                     Object JavaDoc o = new Object JavaDoc();
275                     synchronized( o )
276                     {
277                         o.wait();
278                     }
279                 }
280         };
281         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
282             new TestRunnable[] { tr1 } );
283         boolean fail = false;
284         try
285         {
286             // wait for 1 second to ensure the join section is entered
287
mttr.runTestRunnables( 1000 );
288             fail = true;
289         }
290         catch (junit.framework.AssertionFailedError e)
291         {
292             // test failure?
293
}
294         if (fail)
295         {
296             fail("Did not throw an assertion failed error.");
297         }
298     }
299     
300     
301     private static final int MAX_LOOP_COUNT = 20000;
302     
303     static abstract class TestRunnable6 extends TestRunnable
304     {
305         public Thread JavaDoc runningThread = null;
306         public boolean wasInterrupted = false;
307         public void runTest() throws Throwable JavaDoc
308         {
309             this.runningThread = Thread.currentThread();
310             runMyTest();
311         }
312         
313         
314         public abstract void runMyTest() throws Throwable JavaDoc;
315     }
316     
317     
318     public void testRun6()
319             throws Throwable JavaDoc
320     {
321         DOC.getIT().testsIssue( 771000 );
322         DOC.getLog().debug("testRun6");
323         TestRunnable6 tr1 = new TestRunnable6() {
324                 public void runMyTest() throws Throwable JavaDoc
325                 {
326                     DOC.getLog().warn("runMyTest 6 running forever");
327                     // never exit!
328
for (int i = 0; i < MAX_LOOP_COUNT; ++i)
329                     {
330                         //Thread.yield();
331
try {
332                             delay( 100 );
333                         } catch (InterruptedException JavaDoc ie) {
334                             this.wasInterrupted = true;
335                             DOC.getLog().warn("runMyTest 6: interrupted!");
336                         }
337                         DOC.getLog().warn("runMyTest 6: Loop!");
338                         if (Thread.interrupted())
339                         {
340                             this.wasInterrupted = true;
341                             DOC.getLog().warn("runMyTest 6: inerrupted!");
342                         }
343                     }
344                     fail( "This line should NEVER be reached!" );
345                 }
346         };
347         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
348             new TestRunnable[] { tr1 } );
349         boolean fail = false;
350         try
351         {
352             mttr.runTestRunnables( 10 );
353             fail = true;
354         }
355         catch (junit.framework.AssertionFailedError e)
356         {
357             // test failure
358
if (e.getMessage() != null &&
359                 e.getMessage().indexOf( "This line should NEVER be reached!" )
360                 >= 0)
361             {
362                 fail("Did not kill the runnable." );
363             }
364         }
365         if (fail)
366         {
367             fail("Did not throw an assertion failed error.");
368         }
369         
370         assertTrue(
371             "Test was not interrupted.",
372             tr1.wasInterrupted );
373         assertFalse(
374             "Did not stop the test runnable.",
375             tr1.runningThread.isAlive() );
376     }
377     
378     
379     public void testRun7()
380             throws Throwable JavaDoc
381     {
382         DOC.getIT().testsIssue( 771001 );
383         DOC.getLog().debug("testRun7");
384         TestRunnable6 tr1 = new TestRunnable6() {
385                 public void runMyTest() throws Throwable JavaDoc
386                 {
387                     assertTrue(
388                         "Did not create the thread as daemon.",
389                         this.runningThread.isDaemon() );
390                 }
391         };
392         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
393             new TestRunnable[] { tr1 } );
394         mttr.runTestRunnables();
395     }
396     
397     
398     
399     static class TesterClass
400     {
401         public int value;
402     }
403     
404     public void testRun8()
405             throws Throwable JavaDoc
406     {
407         DOC.getIT().testsIssue( 771008 );
408         
409         // ensure the monitors catch an error
410

411         DOC.getLog().debug("testRun8");
412         final TesterClass tc = new TesterClass();
413         TestRunnable tr1 = new TestRunnable() {
414             public void runTest() throws Throwable JavaDoc
415             {
416                 for (int i = 0; i < 30; ++i)
417                 {
418                     tc.value = i;
419                     delay( 100 );
420                 }
421                 tc.value = -1;
422             }
423         };
424         TestMonitorRunnable tm1 = new TestMonitorRunnable() {
425             public void runMonitor() throws Throwable JavaDoc
426             {
427                 assertTrue(
428                     "Value is < 0.",
429                     tc.value >= 0 );
430             }
431         };
432         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
433             new TestRunnable[] { tr1 }, new TestRunnable[] { tm1 } );
434         boolean fail = false;
435         try
436         {
437             mttr.runTestRunnables();
438             fail = true;
439         }
440         catch (junit.framework.AssertionFailedError e)
441         {
442             assertTrue(
443                 "Did not throw correct exception from monitor.",
444                 e.getMessage().indexOf( "Value is < 0." ) >= 0 );
445         }
446         if (fail)
447         {
448             fail("Did not throw an assertion failed error.");
449         }
450     }
451     
452     
453     
454     static abstract class TestMonitorRunnable9 extends TestMonitorRunnable
455     {
456         public Thread JavaDoc runningThread = null;
457         public void runTest() throws Throwable JavaDoc
458         {
459             this.runningThread = Thread.currentThread();
460             super.runTest();
461         }
462     }
463     
464     
465     public void testRun9()
466             throws Throwable JavaDoc
467     {
468         DOC.getIT().testsIssue( 771008 );
469         
470         // ensure the monitors quit
471

472         DOC.getLog().debug("testRun9");
473         TestRunnable tr1 = new TestRunnable() {
474             public void runTest() throws Throwable JavaDoc
475             {
476                 delay( 100 );
477             }
478         };
479         TestMonitorRunnable9 tm1 = new TestMonitorRunnable9() {
480             public void runMonitor() throws Throwable JavaDoc
481             {
482                 // do nothing
483
}
484         };
485         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
486             new TestRunnable[] { tr1 }, new TestRunnable[] { tm1 } );
487         mttr.runTestRunnables();
488         assertFalse(
489             "Did not stop the test monitor.",
490             tm1.runningThread.isAlive() );
491     }
492     
493     
494     public void testRun10()
495             throws Throwable JavaDoc
496     {
497         DOC.getIT().testsIssue( 771008 );
498         
499         // ensure the monitors quit
500

501         DOC.getLog().debug("testRun10");
502         TestRunnable tr1 = new TestRunnable() {
503             public void runTest() throws Throwable JavaDoc
504             {
505                 delay( 100 );
506             }
507         };
508         TestRunnable6 tm1 = new TestRunnable6() {
509             public void runMyTest() throws Throwable JavaDoc
510             {
511                 DOC.getLog().warn("runMyTest 10 running forever");
512                 for (int i = 0; i < MAX_LOOP_COUNT; ++i)
513                 {
514                     //Thread.yield();
515
try {
516                         delay( 100 );
517                     } catch (InterruptedException JavaDoc ie) {
518                         this.wasInterrupted = true;
519                         DOC.getLog().warn("runMyTest 10: interrupted!");
520                     }
521                     DOC.getLog().warn("runMyTest 10: Loop!");
522                     if (Thread.interrupted())
523                     {
524                         this.wasInterrupted = true;
525                         DOC.getLog().warn("runMyTest 10: interrupted!");
526                     }
527                 }
528                 fail( "This line should NEVER be reached!" );
529             }
530         };
531         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
532             new TestRunnable[] { tr1 }, new TestRunnable[] { tm1 } );
533         
534         // monitor timeout should never cause an error.
535
mttr.runTestRunnables();
536         
537         assertTrue(
538             "monitor was not interrupted.",
539             tm1.wasInterrupted );
540         Thread.sleep( 100 );
541         assertFalse(
542             "Did not stop the test monitor.",
543             tm1.runningThread.isAlive() );
544     }
545     
546     public void testRun11()
547             throws Throwable JavaDoc
548     {
549         DOC.getIT().testsIssue( 771008 );
550         
551         // ensure the monitors catch an error (generated right away)
552

553         DOC.getLog().debug("testRun11");
554         final TesterClass tc = new TesterClass();
555         TestRunnable tr1 = new TestRunnable() {
556             public void runTest() throws Throwable JavaDoc
557             {
558                 tc.value = -1;
559                 for (int i = 0; i < 30; ++i)
560                 {
561                     delay( 100 );
562                     tc.value = i;
563                 }
564             }
565         };
566         TestMonitorRunnable tm1 = new TestMonitorRunnable() {
567             public void runMonitor() throws Throwable JavaDoc
568             {
569                 assertTrue(
570                     "Value is < 0.",
571                     tc.value >= 0 );
572             }
573         };
574         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(
575             new TestRunnable[] { tr1 }, new TestRunnable[] { tm1 } );
576         boolean fail = false;
577         try
578         {
579             mttr.runTestRunnables();
580             fail = true;
581         }
582         catch (junit.framework.AssertionFailedError e)
583         {
584             assertTrue(
585                 "Did not throw correct exception from monitor.",
586                 e.getMessage().indexOf( "Value is < 0." ) >= 0 );
587         }
588         if (fail)
589         {
590             fail("Did not throw an assertion failed error.");
591         }
592     }
593     
594     
595     //-------------------------------------------------------------------------
596
// Standard JUnit declarations
597

598     
599     public static Test suite()
600     {
601         TestSuite suite = new TestSuite( THIS_CLASS );
602         
603         return suite;
604     }
605     
606     public static void main( String JavaDoc[] args )
607     {
608         String JavaDoc[] name = { THIS_CLASS.getName() };
609         
610         // junit.textui.TestRunner.main( name );
611
// junit.swingui.TestRunner.main( name );
612

613         junit.textui.TestRunner.main( name );
614     }
615     
616     
617     /**
618      *
619      * @exception Exception thrown under any exceptional condition.
620      */

621     protected void setUp() throws Exception JavaDoc
622     {
623         super.setUp();
624         
625         // set ourself up
626
}
627     
628     
629     /**
630      *
631      * @exception Exception thrown under any exceptional condition.
632      */

633     protected void tearDown() throws Exception JavaDoc
634     {
635         // tear ourself down
636

637         
638         super.tearDown();
639     }
640 }
641
642
Popular Tags