001: /*
002: * @(#)InterruptEUTest.java
003: *
004: * Copyright (C) 2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.junit.v1;
028:
029: //import net.sourceforge.groboutils.testing.junitlog.v1.*;
030: import junit.framework.Test;
031: import junit.framework.TestCase;
032: import junit.framework.TestSuite;
033:
034: import java.io.IOException;
035: import java.lang.reflect.Method;
036:
037: /**
038: * Tests the behavior of the Thread.interrupt() and InterruptedException
039: * in threaded and non-threaded environments.
040: *
041: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
042: * @since July 15, 2003
043: * @version $Date: 2004/04/09 19:26:52 $
044: */
045: public class InterruptEUTest extends TestCase {
046: //-------------------------------------------------------------------------
047: // Standard JUnit Class-specific declarations
048:
049: private static final Class THIS_CLASS = InterruptEUTest.class;
050:
051: public InterruptEUTest(String name) {
052: super (name);
053: }
054:
055: //-------------------------------------------------------------------------
056: // Tests
057:
058: public void testNonThreadedInterrupt1() {
059: Thread.currentThread().interrupt();
060: assertTrue("Current thread is interrupted.", Thread
061: .interrupted());
062: assertFalse("Interrupt was not cleared", Thread.interrupted());
063: }
064:
065: public void testNonThreadedInterrupt2() {
066: Thread.currentThread().interrupt();
067: try {
068: Thread.sleep(1);
069: fail("Did not throw interrupted exception.");
070: } catch (InterruptedException e) {
071: assertFalse("Interrupt was not cleared", Thread
072: .interrupted());
073: }
074: }
075:
076: private static class MyRunner implements Runnable {
077: boolean doRun = true;
078:
079: public void run() {
080: while (doRun) {
081: Thread.yield();
082: }
083: }
084: }
085:
086: public void testKillThread1() throws Exception {
087: MyRunner mr = new MyRunner();
088: Thread t = new Thread(mr);
089: t.setDaemon(true);
090: t.start();
091:
092: try {
093: t.stop(new RuntimeException("we stopped!"));
094: Thread.sleep(100);
095: assertFalse("Did not kill the thread", t.isAlive());
096: } finally {
097: mr.doRun = false;
098: }
099: }
100:
101: //-------------------------------------------------------------------------
102: // Standard JUnit declarations
103:
104: public static Test suite() {
105: TestSuite suite = new TestSuite(THIS_CLASS);
106:
107: return suite;
108: }
109:
110: public static void main(String[] args) {
111: String[] name = { THIS_CLASS.getName() };
112:
113: // junit.textui.TestRunner.main( name );
114: // junit.swingui.TestRunner.main( name );
115:
116: junit.textui.TestRunner.main(name);
117: }
118:
119: /**
120: *
121: * @exception Exception thrown under any exceptional condition.
122: */
123: protected void setUp() throws Exception {
124: super .setUp();
125:
126: // set ourself up
127: }
128:
129: /**
130: *
131: * @exception Exception thrown under any exceptional condition.
132: */
133: protected void tearDown() throws Exception {
134: // tear ourself down
135:
136: super.tearDown();
137: }
138: }
|