KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)InterruptEUTest.java
3  *
4  * Copyright (C) 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.testing.junitlog.v1.*;
30
import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 import java.io.IOException JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36
37
38 /**
39  * Tests the behavior of the Thread.interrupt() and InterruptedException
40  * in threaded and non-threaded environments.
41  *
42  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
43  * @since July 15, 2003
44  * @version $Date: 2004/04/09 19:26:52 $
45  */

46 public class InterruptEUTest extends TestCase
47 {
48     //-------------------------------------------------------------------------
49
// Standard JUnit Class-specific declarations
50

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

64     
65     public void testNonThreadedInterrupt1()
66     {
67         Thread.currentThread().interrupt();
68         assertTrue(
69             "Current thread is interrupted.",
70             Thread.interrupted() );
71         assertFalse(
72             "Interrupt was not cleared",
73             Thread.interrupted() );
74     }
75     
76     
77     public void testNonThreadedInterrupt2()
78     {
79         Thread.currentThread().interrupt();
80         try
81         {
82             Thread.sleep(1);
83             fail("Did not throw interrupted exception.");
84         }
85         catch (InterruptedException JavaDoc e)
86         {
87             assertFalse(
88                 "Interrupt was not cleared",
89                 Thread.interrupted() );
90         }
91     }
92     
93     
94     private static class MyRunner implements Runnable JavaDoc
95     {
96         boolean doRun = true;
97         public void run()
98         {
99             while (doRun)
100             {
101                 Thread.yield();
102             }
103         }
104     }
105     public void testKillThread1() throws Exception JavaDoc
106     {
107         MyRunner mr = new MyRunner();
108         Thread JavaDoc t = new Thread JavaDoc( mr );
109         t.setDaemon( true );
110         t.start();
111         
112         try
113         {
114             t.stop( new RuntimeException JavaDoc( "we stopped!" ) );
115             Thread.sleep( 100 );
116             assertFalse( "Did not kill the thread",
117                 t.isAlive() );
118         }
119         finally
120         {
121             mr.doRun = false;
122         }
123     }
124     
125     
126     //-------------------------------------------------------------------------
127
// Standard JUnit declarations
128

129     
130     public static Test suite()
131     {
132         TestSuite suite = new TestSuite( THIS_CLASS );
133         
134         return suite;
135     }
136     
137     public static void main( String JavaDoc[] args )
138     {
139         String JavaDoc[] name = { THIS_CLASS.getName() };
140         
141         // junit.textui.TestRunner.main( name );
142
// junit.swingui.TestRunner.main( name );
143

144         junit.textui.TestRunner.main( name );
145     }
146     
147     
148     /**
149      *
150      * @exception Exception thrown under any exceptional condition.
151      */

152     protected void setUp() throws Exception JavaDoc
153     {
154         super.setUp();
155         
156         // set ourself up
157
}
158     
159     
160     /**
161      *
162      * @exception Exception thrown under any exceptional condition.
163      */

164     protected void tearDown() throws Exception JavaDoc
165     {
166         // tear ourself down
167

168         
169         super.tearDown();
170     }
171 }
172
173
Popular Tags