Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
package org.openide.util; |
14 |
|
15 |
import java.util.*; |
16 |
import java.util.Enumeration; |
17 |
import java.util.Map; |
18 |
import java.util.Set; |
19 |
|
20 |
/** This is the base test for new and old enumerations. It contains |
21 |
* factory methods for various kinds of enumerations and set of tests |
22 |
* that use them. Factory methods are overriden in OldEnumerationsTest |
23 |
* |
24 |
* @author Jaroslav Tulach |
25 |
*/ |
26 |
public class EnumerationsTest extends org.netbeans.junit.NbTestCase { |
27 |
|
28 |
/** Creates a new instance of EnumerationsTest */ |
29 |
public EnumerationsTest (String testName) { |
30 |
super(testName); |
31 |
} |
32 |
|
33 |
public static void main(java.lang.String[] args) { |
34 |
junit.textui.TestRunner.run(new org.netbeans.junit.NbTestSuite(EnumerationsTest.class)); |
35 |
} |
36 |
|
37 |
// |
38 |
// Factory methods |
39 |
// |
40 |
|
41 |
protected Enumeration singleton (Object obj) { |
42 |
return Enumerations.singleton (obj); |
43 |
} |
44 |
protected Enumeration concat (Enumeration en1, Enumeration en2) { |
45 |
return Enumerations.concat (en1, en2); |
46 |
} |
47 |
protected Enumeration concat (Enumeration enumOfEnums) { |
48 |
return Enumerations.concat (enumOfEnums); |
49 |
} |
50 |
protected Enumeration removeDuplicates (Enumeration en) { |
51 |
return Enumerations.removeDuplicates (en); |
52 |
} |
53 |
protected Enumeration empty () { |
54 |
return Enumerations.EMPTY; |
55 |
} |
56 |
protected Enumeration array (Object[] arr) { |
57 |
return Enumerations.array (arr); |
58 |
} |
59 |
protected Enumeration convert (Enumeration en, Map map) { |
60 |
return Enumerations.convert (en, map); |
61 |
} |
62 |
protected Enumeration removeNulls (Enumeration en) { |
63 |
return Enumerations.removeNulls (en); |
64 |
} |
65 |
protected Enumeration filter (Enumeration en, Set filter) { |
66 |
return Enumerations.filter (en, filter); |
67 |
} |
68 |
protected Enumeration queue (List initial, final QueueProcessor processor) { |
69 |
return Enumerations.process ( |
70 |
java.util.Collections.enumeration (initial), new Enumerations.Processor() { |
71 |
public Object process (Object obj, List toAdd) { |
72 |
return processor.process (obj, toAdd); |
73 |
} |
74 |
} |
75 |
); |
76 |
} |
77 |
|
78 |
public static interface QueueProcessor { |
79 |
/** @param obj Object object on output |
80 |
* @param addItemsHere allows to add or addAll aditional items to the end of current enum |
81 |
* @return object to be returned |
82 |
*/ |
83 |
public Object process (Object obj, List addItemsHere); |
84 |
} |
85 |
|
86 |
// |
87 |
// The tests |
88 |
// |
89 |
|
90 |
public void testEmptyIsEmpty () { |
91 |
Enumeration e = empty (); |
92 |
assertFalse (e.hasMoreElements ()); |
93 |
try { |
94 |
e.nextElement (); |
95 |
fail ("No elements"); |
96 |
} catch (java.util.NoSuchElementException ex) { |
97 |
// ok |
98 |
} |
99 |
} |
100 |
|
101 |
public void testSingleIsSingle () { |
102 |
Enumeration e = singleton (this); |
103 |
assertTrue (e.hasMoreElements ()); |
104 |
assertEquals ("Returns me", this, e.nextElement ()); |
105 |
assertFalse ("Now it is empty", e.hasMoreElements ()); |
106 |
try { |
107 |
e.nextElement (); |
108 |
fail ("No elements"); |
109 |
} catch (java.util.NoSuchElementException ex) { |
110 |
// ok |
111 |
} |
112 |
} |
113 |
|
114 |
public void testConcatTwoAndArray () { |
115 |
Object[] one = { new Integer (1), new Integer (2), new Integer (3) }; |
116 |
Object[] two = { "1", "2", "3" }; |
117 |
|
118 |
ArrayList list = new ArrayList (Arrays.asList (one)); |
119 |
list.addAll (Arrays.asList (two)); |
120 |
|
121 |
assertEnums ( |
122 |
concat (array (one), array (two)), |
123 |
Collections.enumeration (list) |
124 |
); |
125 |
} |
126 |
|
127 |
public void testConcatTwoAndArrayAndTakeOnlyStrings () { |
128 |
Object[] one = { new Integer (1), new Integer (2), new Integer (3) }; |
129 |
Object[] two = { "1", "2", "3" }; |
130 |
Object[] three = { new Long (1) }; |
131 |
Object[] four = { "Kuk" }; |
132 |
|
133 |
ArrayList list = new ArrayList (Arrays.asList (two)); |
134 |
list.addAll (Arrays.asList (four)); |
135 |
|
136 |
Enumeration[] alls = { |
137 |
array (one), array (two), array (three), array (four) |
138 |
}; |
139 |
|
140 |
assertEnums ( |
141 |
filter (concat (array (alls)), new OnlyStrings()), |
142 |
Collections.enumeration (list) |
143 |
); |
144 |
} |
145 |
|
146 |
public void testRemoveDuplicates () { |
147 |
Object[] one = { new Integer (1), new Integer (2), new Integer (3) }; |
148 |
Object[] two = { "1", "2", "3" }; |
149 |
Object[] three = { new Integer (1) }; |
150 |
Object[] four = { "2", "3", "4" }; |
151 |
|
152 |
Enumeration[] alls = { |
153 |
array (one), array (two), array (three), array (four) |
154 |
}; |
155 |
|
156 |
assertEnums ( |
157 |
removeDuplicates (concat (array (alls))), |
158 |
array (new Object[] { new Integer (1), new Integer (2), new Integer (3), "1", "2", "3", "4" }) |
159 |
); |
160 |
|
161 |
} |
162 |
|
163 |
|
164 |
public void testConvertIntegersToStringRemoveNulls () { |
165 |
Object[] garbage = { new Integer (1), "kuk", "hle", new Integer (5) }; |
166 |
|
167 |
assertEnums ( |
168 |
convert (array (garbage), new MapIntegers ()), |
169 |
array (new Object[] { "1", null, null, "5" }) |
170 |
); |
171 |
|
172 |
assertEnums ( |
173 |
removeNulls (convert (array (garbage), new MapIntegers ())), |
174 |
array (new Object[] { "1", "5" }) |
175 |
); |
176 |
} |
177 |
|
178 |
public void testQueueEnum () { |
179 |
Object obj = new Integer (5); |
180 |
class AddOneTill10 implements QueueProcessor { |
181 |
public Object process (Object obj, List toAddTo) { |
182 |
Integer i = (Integer)obj; |
183 |
if (i.intValue () < 10) { |
184 |
toAddTo.add (new Integer (i.intValue () + 1)); |
185 |
} |
186 |
return i; |
187 |
} |
188 |
} |
189 |
Enumeration en = queue (Collections.nCopies (1, obj), new AddOneTill10 ()); |
190 |
|
191 |
assertEnums (en, array (new Object[] { |
192 |
new Integer (5), new Integer (6), new Integer (7), new Integer (8), new Integer (9), new Integer (10) |
193 |
})); |
194 |
|
195 |
} |
196 |
|
197 |
public void testQueueEnumDoesWideSearchFirst () { |
198 |
Object[] obj = { |
199 |
new Integer (1), |
200 |
new Object[] { |
201 |
new Integer (4), |
202 |
new Object[] { |
203 |
new Integer (7), |
204 |
new Integer (8), |
205 |
}, |
206 |
new Integer (5), |
207 |
}, |
208 |
new Integer (2), |
209 |
new Object[] { new Integer (6) }, |
210 |
new Integer (3), |
211 |
}; |
212 |
class AddArray implements QueueProcessor { |
213 |
public Object process (Object obj, List toAddTo) { |
214 |
if (obj instanceof Object[]) { |
215 |
toAddTo.addAll (Arrays.asList ((Object[])obj)); |
216 |
return null; |
217 |
} else { |
218 |
return obj; |
219 |
} |
220 |
} |
221 |
} |
222 |
|
223 |
|
224 |
Enumeration en = removeNulls ( |
225 |
queue (Collections.nCopies (1, obj), new AddArray ()) |
226 |
); |
227 |
|
228 |
for (int i = 1; i < 9; i++) { |
229 |
Integer number = (Integer)en.nextElement (); |
230 |
assertEquals (i, number.intValue ()); |
231 |
} |
232 |
|
233 |
} |
234 |
|
235 |
private static void assertEnums (Enumeration e1, Enumeration e2) { |
236 |
int indx = 0; |
237 |
while (e1.hasMoreElements () && e2.hasMoreElements ()) { |
238 |
assertEquals (indx++ + "th: ", e1.nextElement (), e2.nextElement ()); |
239 |
} |
240 |
|
241 |
if (e1.hasMoreElements ()) { |
242 |
fail ("first one contains another element: " + e1.nextElement ()); |
243 |
} |
244 |
if (e2.hasMoreElements ()) { |
245 |
fail ("second one contains another element: " + e2.nextElement ()); |
246 |
} |
247 |
|
248 |
try { |
249 |
e1.nextElement (); |
250 |
fail ("First one should throw exception, but nothing happend"); |
251 |
} catch (java.util.NoSuchElementException ex) { |
252 |
// ok |
253 |
} |
254 |
|
255 |
try { |
256 |
e2.nextElement (); |
257 |
fail ("Second one should throw exception, but nothing happend"); |
258 |
} catch (java.util.NoSuchElementException ex) { |
259 |
// ok |
260 |
} |
261 |
} |
262 |
|
263 |
/** Filters only strings. |
264 |
*/ |
265 |
private static final class OnlyStrings implements java.util.Set { |
266 |
public boolean add (Object o) { |
267 |
fail ("Should not be every called"); |
268 |
return false; |
269 |
} |
270 |
|
271 |
public boolean addAll (Collection c) { |
272 |
fail ("Should not be every called"); |
273 |
return false; |
274 |
} |
275 |
|
276 |
public void clear () { |
277 |
fail ("Should not be every called"); |
278 |
} |
279 |
|
280 |
public boolean contains (Object o) { |
281 |
return o instanceof String; |
282 |
} |
283 |
|
284 |
public boolean containsAll (Collection c) { |
285 |
fail ("Should not be every called"); |
286 |
return false; |
287 |
} |
288 |
|
289 |
public boolean isEmpty () { |
290 |
fail ("Should not be every called"); |
291 |
return false; |
292 |
} |
293 |
|
294 |
public Iterator iterator () { |
295 |
fail ("Should not be every called"); |
296 |
return null; |
297 |
} |
298 |
|
299 |
public boolean remove (Object o) { |
300 |
fail ("Should not be every called"); |
301 |
return false; |
302 |
} |
303 |
|
304 |
public boolean removeAll (Collection c) { |
305 |
fail ("Should not be every called"); |
306 |
return false; |
307 |
} |
308 |
|
309 |
public boolean retainAll (Collection c) { |
310 |
fail ("Should not be every called"); |
311 |
return false; |
312 |
} |
313 |
|
314 |
public int size () { |
315 |
fail ("Should not be every called"); |
316 |
return 1; |
317 |
} |
318 |
|
319 |
public Object[] toArray () { |
320 |
fail ("Should not be every called"); |
321 |
return null; |
322 |
} |
323 |
|
324 |
public Object[] toArray (Object[] a) { |
325 |
fail ("Should not be every called"); |
326 |
return null; |
327 |
} |
328 |
} |
329 |
|
330 |
/** Filters only strings. |
331 |
*/ |
332 |
private static final class MapIntegers implements java.util.Map { |
333 |
public boolean containsKey (Object key) { |
334 |
fail ("Should not be every called"); |
335 |
return false; |
336 |
} |
337 |
|
338 |
public boolean containsValue (Object value) { |
339 |
fail ("Should not be every called"); |
340 |
return false; |
341 |
} |
342 |
|
343 |
public Set entrySet () { |
344 |
fail ("Should not be every called"); |
345 |
return null; |
346 |
} |
347 |
|
348 |
public Object get (Object key) { |
349 |
if (key instanceof Integer) { |
350 |
return key.toString (); |
351 |
} |
352 |
return null; |
353 |
} |
354 |
|
355 |
public Set keySet () { |
356 |
fail ("Should not be every called"); |
357 |
return null; |
358 |
} |
359 |
|
360 |
public Object put (Object key, Object value) { |
361 |
fail ("Should not be every called"); |
362 |
return null; |
363 |
} |
364 |
|
365 |
public void putAll (Map t) { |
366 |
fail ("Should not be every called"); |
367 |
} |
368 |
|
369 |
public Collection values () { |
370 |
fail ("Should not be every called"); |
371 |
return null; |
372 |
} |
373 |
|
374 |
public void clear () { |
375 |
fail ("Should not be every called"); |
376 |
} |
377 |
|
378 |
public boolean isEmpty () { |
379 |
fail ("Should not be every called"); |
380 |
return false; |
381 |
} |
382 |
|
383 |
public Object remove (Object key) { |
384 |
fail ("Should not be every called"); |
385 |
return null; |
386 |
} |
387 |
|
388 |
public int size () { |
389 |
fail ("Should not be every called"); |
390 |
return 1; |
391 |
} |
392 |
|
393 |
} |
394 |
} |