KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > services > TestThreadEventNotifier


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

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 /**
25  * Tests for {@link org.apache.hivemind.service.impl.ThreadEventNotifierImpl}.
26  *
27  * @author Howard Lewis Ship
28  */

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         // Don't expect a notification, because the notifier's list is gone
87

88         assertEquals(false, l._cleanup);
89     }
90
91     public void testListenerThrowsException()
92     {
93         Log log = (Log) newMock(Log.class);
94
95         final RuntimeException JavaDoc re = new RuntimeException JavaDoc("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