001: /*
002: * @(#)TimedProcessUTest.java
003: *
004: * Copyright (C) 2002-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.util.thread.v1;
028:
029: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
030: import junit.framework.Test;
031: import junit.framework.TestCase;
032: import junit.framework.TestSuite;
033:
034: /**
035: * Tests the TimedProcess class.
036: *
037: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
038: * @version Jan 6, 2002
039: */
040: public class TimedProcessUTest extends TestCase {
041: //-------------------------------------------------------------------------
042: // Standard JUnit Class-specific declarations
043: private static final Class THIS_CLASS = TimedProcessUTest.class;
044: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
045:
046: public TimedProcessUTest(String name) {
047: super (name);
048: }
049:
050: //-------------------------------------------------------------------------
051: // Tests
052:
053: public void testConstruction1() {
054: assertNotNull("GetInstance must never return null.",
055: TimedProcess.getInstance());
056: }
057:
058: private int runCount = 0;
059:
060: public void testRunRight1() throws InterruptedException {
061: Runnable r = new Runnable() {
062: public void run() {
063: ++runCount;
064: }
065: };
066: this .runCount = 0;
067: TimedProcess.getInstance().runTimed(r, 60 * 1000);
068: assertEquals("did not run thread.", 1, this .runCount);
069: }
070:
071: public void testRunWrong1() {
072: Runnable r = new Runnable() {
073: public void run() {
074: // sleep longer than the timer.
075: // give it a bit of leeway
076: try {
077: Thread.sleep(2 * 1000);
078: } catch (InterruptedException ie) {
079: // ignore
080: }
081: }
082: };
083: try {
084: TimedProcess.getInstance().runTimed(r, 1000);
085: fail("Did not throw InterruptedException.");
086: } catch (InterruptedException ie) {
087: // test exception?
088: }
089: }
090:
091: private boolean stop = false;
092:
093: public void testRunOwn1() {
094: Runnable r = new Runnable() {
095: public void run() {
096: while (stop == false) {
097: Thread.yield();
098: }
099: }
100: };
101: TimedProcess.RunnableKiller rk = new TimedProcess.RunnableKiller() {
102: public void killRunnable(Runnable r, Thread t) {
103: stop = true;
104: }
105: };
106: try {
107: TimedProcess.getInstance().runTimed(r, 1000, rk);
108: fail("Did not throw InterruptedException.");
109: } catch (InterruptedException ie) {
110: // test exception?
111: }
112: }
113:
114: //-------------------------------------------------------------------------
115: // Standard JUnit declarations
116:
117: public static Test suite() {
118: TestSuite suite = new TestSuite(THIS_CLASS);
119:
120: return suite;
121: }
122:
123: public static void main(String[] args) {
124: String[] name = { THIS_CLASS.getName() };
125:
126: // junit.textui.TestRunner.main( name );
127: // junit.swingui.TestRunner.main( name );
128:
129: junit.textui.TestRunner.main(name);
130: }
131:
132: /**
133: *
134: * @exception Exception thrown under any exceptional condition.
135: */
136: protected void setUp() throws Exception {
137: super .setUp();
138:
139: // set ourself up
140: }
141:
142: /**
143: *
144: * @exception Exception thrown under any exceptional condition.
145: */
146: protected void tearDown() throws Exception {
147: // tear ourself down
148:
149: super.tearDown();
150: }
151: }
|