1 26 27 package net.sourceforge.groboutils.junit.v1; 28 29 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 import java.io.IOException ; 35 import java.lang.reflect.Method ; 36 37 38 46 public class InterruptEUTest extends TestCase 47 { 48 51 private static final Class THIS_CLASS = InterruptEUTest.class; 52 53 public InterruptEUTest( String name ) 54 { 55 super( name ); 56 } 57 58 59 60 61 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 e) 86 { 87 assertFalse( 88 "Interrupt was not cleared", 89 Thread.interrupted() ); 90 } 91 } 92 93 94 private static class MyRunner implements Runnable 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 106 { 107 MyRunner mr = new MyRunner(); 108 Thread t = new Thread ( mr ); 109 t.setDaemon( true ); 110 t.start(); 111 112 try 113 { 114 t.stop( new RuntimeException ( "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 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 [] args ) 138 { 139 String [] name = { THIS_CLASS.getName() }; 140 141 144 junit.textui.TestRunner.main( name ); 145 } 146 147 148 152 protected void setUp() throws Exception 153 { 154 super.setUp(); 155 156 } 158 159 160 164 protected void tearDown() throws Exception 165 { 166 168 169 super.tearDown(); 170 } 171 } 172 173 | Popular Tags |