1 26 27 package net.sourceforge.groboutils.junit.v1; 28 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 33 import java.io.IOException ; 34 import java.lang.reflect.Method ; 35 36 37 44 public class TestRunnableUTest extends TestCase 45 { 46 49 private static final Class THIS_CLASS = TestRunnableUTest.class; 50 51 public TestRunnableUTest( String name ) 52 { 53 super( name ); 54 } 55 56 57 58 59 62 private static class MyTestRunnable extends TestRunnable 63 { 64 Throwable t; 65 public MyTestRunnable( Throwable t ) 66 { 67 this.t = t; 68 } 69 70 public void runTest() throws Throwable 71 { 72 if (this.t != null) 73 { 74 throw this.t; 75 } 76 } 77 } 78 79 80 public void testDelay1() throws InterruptedException 81 { 82 long delay = 100L; 85 long error = 10L; 86 long minDelay = delay - error; 87 TestRunnable tr = createTestRunnable( null ); 88 long start = System.currentTimeMillis(); 89 tr.delay( delay ); 90 long end = System.currentTimeMillis(); 91 assertTrue( 92 "Did not delay for long enough (delayed "+(end - start)+ 93 " ms, should have delayed at least "+delay+" ms).", 94 (end - start) >= minDelay ); 95 } 96 97 98 public void testRun1() 99 { 100 TestRunnable tr = createTestRunnable( null ); 101 try 102 { 103 tr.run(); 104 fail( "Did not throw IllegalStateException." ); 105 } 106 catch (IllegalStateException e) 107 { 108 } 110 } 111 112 113 public void testRun2() throws Throwable 114 { 115 TestRunnable tr = createTestRunnable( null ); 116 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 117 new TestRunnable[] { tr } ); 118 try 119 { 120 tr.run(); 121 fail( "Did not throw IllegalStateException." ); 122 } 123 catch (IllegalStateException e) 124 { 125 } 127 } 128 129 130 public void testRun3() throws Throwable 131 { 132 TestRunnable tr = createTestRunnable( null ); 133 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 134 new TestRunnable[] { tr } ); 135 mttr.runTestRunnables( 1000 ); 136 } 137 138 139 public void testRun4() 140 { 141 Throwable t = new Throwable ( "Ignore" ); 142 TestRunnable tr = createTestRunnable( t ); 143 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 144 new TestRunnable[] { tr } ); 145 try 146 { 147 mttr.runTestRunnables( 1000 ); 148 fail( "Did not throw an exception." ); 149 } 150 catch (Throwable actualT) 151 { 152 assertEquals( 153 "Did not throw the intended exception.", 154 actualT, 155 t ); 156 } 157 } 158 159 160 161 162 163 166 167 168 169 protected TestRunnable createTestRunnable( Throwable throwThis ) 170 { 171 return new MyTestRunnable( throwThis ); 172 } 173 174 175 176 179 180 public static Test suite() 181 { 182 TestSuite suite = new TestSuite( THIS_CLASS ); 183 184 return suite; 185 } 186 187 public static void main( String [] args ) 188 { 189 String [] name = { THIS_CLASS.getName() }; 190 191 194 junit.textui.TestRunner.main( name ); 195 } 196 197 198 202 protected void setUp() throws Exception 203 { 204 super.setUp(); 205 206 } 208 209 210 214 protected void tearDown() throws Exception 215 { 216 218 219 super.tearDown(); 220 } 221 } 222 223 | Popular Tags |