1 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 41 public class TimedProcessUTest extends TestCase 42 { 43 private static final Class THIS_CLASS = TimedProcessUTest.class; 46 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 47 48 public TimedProcessUTest( String name ) 49 { 50 super( name ); 51 } 52 53 54 55 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 71 { 72 Runnable r = new Runnable () 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 r = new Runnable () 90 { 91 public void run() 92 { 93 try 96 { 97 Thread.sleep( 2 * 1000 ); 98 } 99 catch (InterruptedException ie) 100 { 101 } 103 } 104 }; 105 try 106 { 107 TimedProcess.getInstance().runTimed( r, 1000 ); 108 fail( "Did not throw InterruptedException." ); 109 } 110 catch (InterruptedException ie) 111 { 112 } 114 } 115 116 117 private boolean stop = false; 118 public void testRunOwn1() 119 { 120 Runnable r = new Runnable () 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 r, Thread 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 ie) 143 { 144 } 146 } 147 148 149 150 151 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 [] args ) 163 { 164 String [] name = { THIS_CLASS.getName() }; 165 166 169 junit.textui.TestRunner.main( name ); 170 } 171 172 173 177 protected void setUp() throws Exception 178 { 179 super.setUp(); 180 181 } 183 184 185 189 protected void tearDown() throws Exception 190 { 191 193 194 super.tearDown(); 195 } 196 } 197 198 | Popular Tags |