1 15 package hivemind.test.services; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.hivemind.service.ThreadCleanupListener; 19 import org.apache.hivemind.service.ThreadEventNotifier; 20 import org.apache.hivemind.service.impl.ThreadEventNotifierImpl; 21 22 import hivemind.test.FrameworkTestCase; 23 24 29 public class TestThreadEventNotifier extends FrameworkTestCase 30 { 31 static class Listener implements ThreadCleanupListener 32 { 33 boolean _cleanup; 34 35 public void threadDidCleanup() 36 { 37 _cleanup = true; 38 } 39 40 } 41 42 public void testAdd() 43 { 44 ThreadEventNotifier n = new ThreadEventNotifierImpl(); 45 Listener l = new Listener(); 46 47 n.addThreadCleanupListener(l); 48 n.fireThreadCleanup(); 49 50 assertEquals(true, l._cleanup); 51 } 52 53 public void testRemove() 54 { 55 ThreadEventNotifier n = new ThreadEventNotifierImpl(); 56 57 Listener l1 = new Listener(); 58 Listener l2 = new Listener(); 59 60 n.addThreadCleanupListener(l1); 61 n.addThreadCleanupListener(l2); 62 n.removeThreadCleanupListener(l1); 63 64 n.fireThreadCleanup(); 65 66 assertEquals(false, l1._cleanup); 67 assertEquals(true, l2._cleanup); 68 } 69 70 public void testNotifierClearsListenerListAfterFire() 71 { 72 ThreadEventNotifier n = new ThreadEventNotifierImpl(); 73 74 Listener l = new Listener(); 75 76 n.addThreadCleanupListener(l); 77 78 n.fireThreadCleanup(); 79 80 assertEquals(true, l._cleanup); 81 82 l._cleanup = false; 83 84 n.fireThreadCleanup(); 85 86 88 assertEquals(false, l._cleanup); 89 } 90 91 public void testListenerThrowsException() 92 { 93 Log log = (Log) newMock(Log.class); 94 95 final RuntimeException re = new RuntimeException ("Listener Failure"); 96 97 log.warn("Thread cleanup exception: Listener Failure", re); 98 99 replayControls(); 100 101 ThreadCleanupListener l = new ThreadCleanupListener() 102 { 103 public void threadDidCleanup() 104 { 105 throw re; 106 } 107 }; 108 109 ThreadEventNotifier n = new ThreadEventNotifierImpl(log); 110 111 n.addThreadCleanupListener(l); 112 113 n.fireThreadCleanup(); 114 115 verifyControls(); 116 } 117 } 118 | Popular Tags |