KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > notification > util > WeakHashSetTest


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21
22 package org.jacorb.test.notification.util;
23
24 import java.lang.ref.ReferenceQueue JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 import org.jacorb.notification.util.WeakHashSet;
36
37 /**
38  * @author Alphonse Bendt
39  * @version $Id: WeakHashSetTest.java,v 1.1 2005/02/14 00:17:38 alphonse.bendt Exp $
40  */

41 public class WeakHashSetTest extends TestCase
42 {
43     private Set JavaDoc objectUnderTest_;
44
45     /*
46      * @see TestCase#setUp()
47      */

48     protected void setUp() throws Exception JavaDoc
49     {
50         super.setUp();
51
52         objectUnderTest_ = new WeakHashSet();
53     }
54
55     /**
56      * Constructor for WeakHashSetTest.
57      *
58      * @param name
59      */

60     public WeakHashSetTest(String JavaDoc name)
61     {
62         super(name);
63     }
64
65     public void testSize()
66     {
67         assertEquals(0, objectUnderTest_.size());
68         Object JavaDoc o1 = new Object JavaDoc();
69         objectUnderTest_.add(o1);
70         assertEquals(1, objectUnderTest_.size());
71     }
72
73     public void testIsEmpty()
74     {
75         assertTrue(objectUnderTest_.isEmpty());
76         Object JavaDoc o1 = new Object JavaDoc();
77         objectUnderTest_.add(o1);
78         assertFalse(objectUnderTest_.isEmpty());
79     }
80
81     public void testContains()
82     {
83         Object JavaDoc o1 = new Object JavaDoc();
84         assertFalse(objectUnderTest_.contains(o1));
85         objectUnderTest_.add(o1);
86         assertTrue(objectUnderTest_.contains(o1));
87     }
88
89     public void testIterator()
90     {
91         Iterator JavaDoc i = objectUnderTest_.iterator();
92         assertFalse(i.hasNext());
93
94         Object JavaDoc o1 = new Object JavaDoc();
95         Object JavaDoc o2 = new Object JavaDoc();
96         objectUnderTest_.add(o1);
97         objectUnderTest_.add(o2);
98
99         i = objectUnderTest_.iterator();
100         assertTrue(i.hasNext());
101         assertNotNull(i.next());
102         assertNotNull(i.next());
103         assertFalse(i.hasNext());
104     }
105
106     /*
107      * Class under test for Object[] toArray()
108      */

109     public void testToArray()
110     {
111         Object JavaDoc[] array = objectUnderTest_.toArray();
112
113         assertEquals(0, array.length);
114
115         Object JavaDoc o1 = new Object JavaDoc();
116         objectUnderTest_.add(o1);
117
118         array = objectUnderTest_.toArray();
119
120         assertEquals(1, array.length);
121         assertEquals(o1, array[0]);
122     }
123
124     /*
125      * Class under test for Object[] toArray(Object[])
126      */

127     public void testToArrayObjectArray()
128     {
129         String JavaDoc s1 = "text";
130
131         objectUnderTest_.add(s1);
132
133         String JavaDoc[] array = (String JavaDoc[]) objectUnderTest_.toArray(new String JavaDoc[0]);
134         assertEquals(1, array.length);
135         assertEquals("text", array[0]);
136     }
137
138     public void testAdd()
139     {
140         Object JavaDoc o1 = new Object JavaDoc();
141
142         assertTrue(objectUnderTest_.add(o1));
143         assertFalse(objectUnderTest_.add(o1));
144     }
145
146     public void testRemove()
147     {
148         Object JavaDoc o1 = new Object JavaDoc();
149
150         objectUnderTest_.add(o1);
151         assertTrue(objectUnderTest_.contains(o1));
152         assertTrue(objectUnderTest_.remove(o1));
153         assertFalse(objectUnderTest_.remove(o1));
154         assertFalse(objectUnderTest_.contains(o1));
155     }
156
157     public void testContainsAll()
158     {
159         Object JavaDoc o1 = new Object JavaDoc();
160         Object JavaDoc o2 = new Object JavaDoc();
161
162         List JavaDoc all = Arrays.asList(new Object JavaDoc[] { o1, o2 });
163
164         assertFalse(objectUnderTest_.containsAll(all));
165
166         objectUnderTest_.add(o1);
167
168         assertFalse(objectUnderTest_.containsAll(all));
169
170         objectUnderTest_.add(o2);
171
172         assertTrue(objectUnderTest_.containsAll(all));
173     }
174
175     public void testAddAll()
176     {
177         Object JavaDoc o1 = new Object JavaDoc();
178         Object JavaDoc o2 = new Object JavaDoc();
179
180         List JavaDoc all = Arrays.asList(new Object JavaDoc[] { o1, o2 });
181
182         assertFalse(objectUnderTest_.contains(o1));
183         assertFalse(objectUnderTest_.contains(o2));
184
185         assertTrue(objectUnderTest_.addAll(all));
186         assertFalse(objectUnderTest_.addAll(all));
187
188         assertTrue(objectUnderTest_.contains(o1));
189         assertTrue(objectUnderTest_.contains(o2));
190     }
191
192     public void testRetainAll()
193     {
194         Object JavaDoc o1 = new Object JavaDoc();
195         Object JavaDoc o2 = new Object JavaDoc();
196
197         List JavaDoc all = Arrays.asList(new Object JavaDoc[] { o1 });
198
199         objectUnderTest_.add(o1);
200         objectUnderTest_.add(o2);
201
202         assertTrue(objectUnderTest_.contains(o1));
203         assertTrue(objectUnderTest_.contains(o2));
204
205         assertTrue(objectUnderTest_.retainAll(all));
206         assertFalse(objectUnderTest_.retainAll(all));
207
208         assertTrue(objectUnderTest_.contains(o1));
209         assertFalse(objectUnderTest_.contains(o2));
210     }
211
212     public void testRemoveAll()
213     {
214         Object JavaDoc o1 = new Object JavaDoc();
215         Object JavaDoc o2 = new Object JavaDoc();
216
217         List JavaDoc all = Arrays.asList(new Object JavaDoc[] { o1 });
218
219         objectUnderTest_.add(o1);
220         objectUnderTest_.add(o2);
221
222         assertTrue(objectUnderTest_.contains(o1));
223         assertTrue(objectUnderTest_.contains(o2));
224
225         assertTrue(objectUnderTest_.removeAll(all));
226         assertFalse(objectUnderTest_.removeAll(all));
227
228         assertFalse(objectUnderTest_.contains(o1));
229         assertTrue(objectUnderTest_.contains(o2));
230     }
231
232     public void testClear()
233     {
234         Object JavaDoc o1 = new Object JavaDoc();
235
236         assertTrue(objectUnderTest_.isEmpty());
237
238         objectUnderTest_.add(o1);
239
240         assertFalse(objectUnderTest_.isEmpty());
241
242         objectUnderTest_.clear();
243
244         assertTrue(objectUnderTest_.isEmpty());
245     }
246
247     public void testIsReallyWeak()
248     {
249         long now = System.currentTimeMillis();
250         
251         Object JavaDoc o1 = new Object JavaDoc();
252         ReferenceQueue JavaDoc queue = new ReferenceQueue JavaDoc();
253
254         new WeakReference JavaDoc(o1, queue);
255
256         objectUnderTest_.add(o1);
257
258         o1 = null;
259
260         while (queue.poll() == null && System.currentTimeMillis() < now + 5000)
261         {
262             System.gc();
263         }
264         
265         assertTrue(objectUnderTest_.isEmpty());
266     }
267
268     
269     public static Test suite()
270     {
271         return new TestSuite(WeakHashSetTest.class);
272     }
273 }
Popular Tags