KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > thread > v1 > TimedProcessUTest


1 /*
2  * @(#)TimedProcessUTest.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.util.thread.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
35 /**
36  * Tests the TimedProcess class.
37  *
38  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
39  * @version Jan 6, 2002
40  */

41 public class TimedProcessUTest extends TestCase
42 {
43     //-------------------------------------------------------------------------
44
// Standard JUnit Class-specific declarations
45
private static final Class JavaDoc THIS_CLASS = TimedProcessUTest.class;
46     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
47     
48     public TimedProcessUTest( String JavaDoc name )
49     {
50         super( name );
51     }
52
53
54
55     //-------------------------------------------------------------------------
56
// Tests
57

58     
59     
60     public void testConstruction1()
61     {
62         assertNotNull(
63             "GetInstance must never return null.",
64             TimedProcess.getInstance() );
65     }
66     
67     
68     private int runCount = 0;
69     
70     public void testRunRight1() throws InterruptedException JavaDoc
71     {
72         Runnable JavaDoc r = new Runnable JavaDoc()
73         {
74             public void run()
75             {
76                 ++runCount;
77             }
78         };
79         this.runCount = 0;
80         TimedProcess.getInstance().runTimed( r, 60 * 1000 );
81         assertEquals(
82             "did not run thread.",
83             1,
84             this.runCount );
85     }
86     
87     public void testRunWrong1()
88     {
89         Runnable JavaDoc r = new Runnable JavaDoc()
90         {
91             public void run()
92             {
93                 // sleep longer than the timer.
94
// give it a bit of leeway
95
try
96                 {
97                     Thread.sleep( 2 * 1000 );
98                 }
99                 catch (InterruptedException JavaDoc ie)
100                 {
101                     // ignore
102
}
103             }
104         };
105         try
106         {
107             TimedProcess.getInstance().runTimed( r, 1000 );
108             fail( "Did not throw InterruptedException." );
109         }
110         catch (InterruptedException JavaDoc ie)
111         {
112             // test exception?
113
}
114     }
115
116
117     private boolean stop = false;
118     public void testRunOwn1()
119     {
120         Runnable JavaDoc r = new Runnable JavaDoc()
121         {
122             public void run()
123             {
124                 while (stop == false)
125                 {
126                     Thread.yield();
127                 }
128             }
129         };
130         TimedProcess.RunnableKiller rk = new TimedProcess.RunnableKiller()
131         {
132             public void killRunnable( Runnable JavaDoc r, Thread JavaDoc t )
133             {
134                 stop = true;
135             }
136         };
137         try
138         {
139             TimedProcess.getInstance().runTimed( r, 1000, rk );
140             fail( "Did not throw InterruptedException." );
141         }
142         catch (InterruptedException JavaDoc ie)
143         {
144             // test exception?
145
}
146     }
147     
148     
149     
150     
151     //-------------------------------------------------------------------------
152
// Standard JUnit declarations
153

154     
155     public static Test suite()
156     {
157         TestSuite suite = new TestSuite( THIS_CLASS );
158         
159         return suite;
160     }
161     
162     public static void main( String JavaDoc[] args )
163     {
164         String JavaDoc[] name = { THIS_CLASS.getName() };
165         
166         // junit.textui.TestRunner.main( name );
167
// junit.swingui.TestRunner.main( name );
168

169         junit.textui.TestRunner.main( name );
170     }
171     
172     
173     /**
174      *
175      * @exception Exception thrown under any exceptional condition.
176      */

177     protected void setUp() throws Exception JavaDoc
178     {
179         super.setUp();
180         
181         // set ourself up
182
}
183     
184     
185     /**
186      *
187      * @exception Exception thrown under any exceptional condition.
188      */

189     protected void tearDown() throws Exception JavaDoc
190     {
191         // tear ourself down
192

193         
194         super.tearDown();
195     }
196 }
197
198
Popular Tags