KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > TestEventListenerList


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 org.apache.hivemind.util;
16
17 import hivemind.test.FrameworkTestCase;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.hivemind.util.EventListenerList;
24
25 /**
26  * Tests for {@link org.apache.hivemind.util.EventListenerList}.
27  *
28  * @author Howard Lewis Ship
29  */

30 public class TestEventListenerList extends FrameworkTestCase
31 {
32     private static class Trigger
33     {
34         private boolean _trigger;
35
36         public boolean isTrigger()
37         {
38             return _trigger;
39         }
40
41         public void setTrigger(boolean b)
42         {
43             _trigger = b;
44         }
45
46     }
47
48     private Trigger[] buildTriggers(int count)
49     {
50         Trigger[] result = new Trigger[count];
51
52         for (int i = 0; i < count; i++)
53         {
54             result[i] = new Trigger();
55         }
56
57         return result;
58     }
59
60     private void addAll(EventListenerList l, Trigger[] t)
61     {
62         for (int i = 0; i < t.length; i++)
63             l.addListener(t[i]);
64     }
65
66     private void checkAllTrue(Trigger[] t)
67     {
68         for (int i = 0; i < t.length; i++)
69             assertEquals(true, t[i].isTrigger());
70     }
71
72     public void testBasic()
73     {
74         EventListenerList l = new EventListenerList();
75
76         Trigger[] ta = buildTriggers(20);
77
78         addAll(l, ta);
79
80         Iterator JavaDoc i = l.getListeners();
81
82         while (i.hasNext())
83         {
84             Trigger t = (Trigger) i.next();
85
86             t.setTrigger(true);
87         }
88
89     }
90
91     public void testEmptyList()
92     {
93         EventListenerList l = new EventListenerList();
94
95         Iterator JavaDoc i = l.getListeners();
96
97         assertEquals(false, i.hasNext());
98     }
99
100     public void testLateAdd()
101     {
102         Trigger[] ta = buildTriggers(20);
103         EventListenerList l = new EventListenerList();
104
105         addAll(l, ta);
106
107         Iterator JavaDoc i = l.getListeners();
108
109         for (int j = 0; j < 5; j++)
110         {
111             Trigger t = (Trigger) i.next();
112             t.setTrigger(true);
113         }
114
115         Trigger tnew = new Trigger();
116         l.addListener(tnew);
117
118         while (i.hasNext())
119         {
120             Trigger t = (Trigger) i.next();
121             t.setTrigger(true);
122         }
123
124         assertEquals(false, tnew.isTrigger());
125
126         checkAllTrue(ta);
127     }
128
129     public void testLateRemove()
130     {
131         Trigger[] ta = buildTriggers(20);
132         EventListenerList l = new EventListenerList();
133
134         addAll(l, ta);
135
136         Iterator JavaDoc i = l.getListeners();
137
138         for (int j = 0; j < 5; j++)
139         {
140             Trigger t = (Trigger) i.next();
141             t.setTrigger(true);
142         }
143
144         Trigger tremoved = ta[15];
145         l.removeListener(tremoved);
146
147         while (i.hasNext())
148         {
149             Trigger t = (Trigger) i.next();
150             t.setTrigger(true);
151         }
152
153         checkAllTrue(ta);
154     }
155
156     public void testRemoveMissing()
157     {
158         Trigger[] ta = buildTriggers(20);
159         EventListenerList l = new EventListenerList();
160
161         addAll(l, ta);
162
163         Trigger tremove = new Trigger();
164
165         l.removeListener(tremove);
166
167         Iterator JavaDoc i = l.getListeners();
168         while (i.hasNext())
169         {
170             Trigger t = (Trigger) i.next();
171             t.setTrigger(true);
172         }
173
174         checkAllTrue(ta);
175     }
176
177     public void testIteratorRemoveFailure()
178     {
179         Trigger[] ta = buildTriggers(20);
180         EventListenerList l = new EventListenerList();
181
182         addAll(l, ta);
183
184         Iterator JavaDoc i = l.getListeners();
185
186         for (int j = 0; j < 5; j++)
187             i.next();
188
189         try
190         {
191             i.remove();
192             unreachable();
193         }
194         catch (UnsupportedOperationException JavaDoc ex)
195         {
196         }
197     }
198
199     public void testOutOfMemoryError()
200     {
201         Trigger ta = buildTriggers(1)[0];
202         EventListenerList l = new EventListenerList();
203         List JavaDoc iterators = new ArrayList JavaDoc();
204
205         try
206         {
207             for (int i = 0; i < 100; i++)
208             {
209                 l.addListener(ta);
210
211                 iterators.add(l.getListeners());
212
213                 l.removeListener(ta);
214
215             }
216
217         }
218         catch (OutOfMemoryError JavaDoc e)
219         {
220             fail("Ran out of memory!");
221         }
222         catch (Throwable JavaDoc e)
223         {
224             fail(e.getMessage());
225         }
226     }
227 }
228
Popular Tags