01: package abbot.util;
02:
03: import junit.extensions.abbot.*;
04: import junit.framework.TestCase;
05:
06: public class ThreadTerminatingSecurityManagerTest extends TestCase {
07:
08: public void testThreadsTerminated() {
09: ThreadGroup group = new ThreadGroup("AUT Group") {
10: public void uncaughtException(Thread t, Throwable thrown) {
11: }
12: };
13: // Normally, this thread would never exit
14: Thread t1 = new Thread(group, getName() + "1") {
15: public void run() {
16: while (true) {
17: currentThread().setContextClassLoader(null);
18: try {
19: sleep(1000);
20: } catch (InterruptedException ie) {
21: }
22: }
23: }
24: };
25: t1.start();
26: sm.terminateThreads(group);
27: Timer timer = new Timer();
28: while (t1.isAlive()) {
29: if (timer.elapsed() > 5000)
30: fail("Thread not terminated");
31: try {
32: Thread.sleep(100);
33: } catch (InterruptedException e) {
34: }
35: }
36: }
37:
38: private SecurityManager oldsm;
39: private ThreadTerminatingSecurityManager sm;
40:
41: protected void setUp() {
42: oldsm = System.getSecurityManager();
43: sm = new ThreadTerminatingSecurityManager() {
44: public void exitCalled(int status) {
45: }
46: };
47: System.setSecurityManager(sm);
48: }
49:
50: protected void tearDown() {
51: System.setSecurityManager(oldsm);
52: }
53:
54: public ThreadTerminatingSecurityManagerTest(String name) {
55: super (name);
56: }
57:
58: public static void main(String[] args) {
59: TestHelper.runTests(args,
60: ThreadTerminatingSecurityManagerTest.class);
61: }
62: }
|