• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 
18 package org.apache.harmony.tests.java.util;
19 
20 import junit.framework.TestCase;
21 import org.apache.harmony.testframework.serialization.SerializationTest;
22 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
23 import tests.util.SerializationTester;
24 import java.io.Serializable;
25 import java.util.ArrayDeque;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.Deque;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Queue;
38 import java.util.RandomAccess;
39 import java.util.Set;
40 import java.util.SortedMap;
41 import java.util.SortedSet;
42 import java.util.TreeMap;
43 import java.util.TreeSet;
44 import java.util.Vector;
45 
46 public class Collections2Test extends TestCase {
47 
48     private static final SerializableAssert comparator = new SerializableAssert() {
49         public void assertDeserialized(Serializable reference, Serializable test) {
50             assertSame(reference, test);
51         }
52     };
53 
54     /**
55      * java.util.Collections#binarySearch(java.util.List,
56      *java.lang.Object, java.util.Comparator)
57      */
test_binarySearchLjava_util_ListLjava_lang_ObjectLjava_util_Comparator()58     public void test_binarySearchLjava_util_ListLjava_lang_ObjectLjava_util_Comparator() {
59         // Regression for HARMONY-94
60         LinkedList<Integer> lst = new LinkedList<Integer>();
61         lst.add(new Integer(30));
62         Collections.sort(lst, null);
63         int index = Collections.binarySearch(lst, new Integer(2), null);
64         assertEquals(-1, index);
65     }
66 
67     /**
68      * java.util.Collections#binarySearch(java.util.List,
69      *java.lang.Object)
70      */
71     @SuppressWarnings("unchecked")
test_binarySearchLjava_util_ListLjava_lang_Object()72     public void test_binarySearchLjava_util_ListLjava_lang_Object() {
73         // regression for Harmony-1367
74         List localList = new LinkedList();
75         assertEquals(-1, Collections.binarySearch(localList, new Object()));
76         localList.add(new Object());
77         try {
78             Collections.binarySearch(localList, new Integer(1));
79             fail("Should throw ClassCastException");
80         } catch (ClassCastException e) {
81             // expected
82         }
83     }
84 
85     /**
86      * java.util.Collections#rotate(java.util.List, int)
87      */
test_rotateLjava_util_ListI()88     public void test_rotateLjava_util_ListI() {
89         // Regression for HARMONY-19 Rotate an *empty* list
90         Collections.rotate(new ArrayList<Object>(), 25);
91 
92         // Regression for HARMONY-20
93         List<String> list = new ArrayList<String>();
94         list.add(0, "zero");
95         list.add(1, "one");
96         list.add(2, "two");
97         list.add(3, "three");
98         list.add(4, "four");
99 
100         Collections.rotate(list, Integer.MIN_VALUE);
101         assertEquals("Rotated incorrectly at position 0, ", "three",
102                 list.get(0));
103         assertEquals("Rotated incorrectly at position 1, ", "four",
104                 list.get(1));
105         assertEquals("Rotated incorrectly at position 2, ", "zero",
106                 list.get(2));
107         assertEquals("Rotated incorrectly at position 3, ", "one",
108                 list.get(3));
109         assertEquals("Rotated incorrectly at position 4, ", "two",
110                 list.get(4));
111     }
112 
113     /**
114      * java.util.Collections#synchronizedCollection(java.util.Collection)
115      */
test_synchronizedCollectionLjava_util_Collection()116     public void test_synchronizedCollectionLjava_util_Collection() {
117         try {
118             // Regression for HARMONY-93
119             Collections.synchronizedCollection(null);
120             fail("Assert 0: synchronizedCollection(null) must throw NPE");
121         } catch (NullPointerException e) {
122             // expected
123         }
124     }
125 
126     /**
127      * java.util.Collections#synchronizedSortedMap(java.util.SortedMap)
128      */
test_synchronizedSortedMapLjava_util_SortedMap()129     public void test_synchronizedSortedMapLjava_util_SortedMap() {
130         try {
131             // Regression for HARMONY-93
132             Collections.synchronizedSortedMap(null);
133             fail("Assert 0: synchronizedSortedMap(null) must throw NPE");
134         } catch (NullPointerException e) {
135             // expected
136         }
137     }
138 
139     /**
140      * java.util.Collections#synchronizedMap(java.util.Map)
141      */
test_synchronizedMapLjava_util_Map()142     public void test_synchronizedMapLjava_util_Map() {
143         try {
144             // Regression for HARMONY-93
145             Collections.synchronizedMap(null);
146             fail("Assert 0: synchronizedMap(map) must throw NPE");
147         } catch (NullPointerException e) {
148             // expected
149         }
150     }
151 
152     /**
153      * java.util.Collections#synchronizedSet(java.util.Set)
154      */
test_synchronizedSetLjava_util_Set()155     public void test_synchronizedSetLjava_util_Set() {
156         try {
157             // Regression for HARMONY-93
158             Collections.synchronizedSet(null);
159             fail("Assert 0: synchronizedSet(set) must throw NPE");
160         } catch (NullPointerException e) {
161             // expected
162         }
163     }
164 
165     /**
166      * java.util.Collections#synchronizedSortedSet(java.util.SortedSet)
167      */
test_synchronizedSortedSetLjava_util_SortedSet()168     public void test_synchronizedSortedSetLjava_util_SortedSet() {
169         try {
170             // Regression for HARMONY-93
171             Collections.synchronizedSortedSet(null);
172             fail("Assert 0: synchronizedSortedSet(null) must throw NPE");
173         } catch (NullPointerException e) {
174             // expected
175         }
176     }
177 
178     /**
179      * java.util.Collections#unmodifiableCollection(java.util.Collection)
180      */
test_unmodifiableCollectionLjava_util_Collection()181     public void test_unmodifiableCollectionLjava_util_Collection() {
182         try {
183             // Regression for HARMONY-93
184             Collections.unmodifiableCollection(null);
185             fail("Assert 0: unmodifiableCollection(null) must throw NPE");
186         } catch (NullPointerException e) {
187             // expected
188         }
189     }
190 
191     /**
192      * java.util.Collections#unmodifiableMap(java.util.Map)
193      */
test_unmodifiableMapLjava_util_Map()194     public void test_unmodifiableMapLjava_util_Map() {
195         try {
196             // Regression for HARMONY-93
197             Collections.unmodifiableMap(null);
198             fail("Assert 0: unmodifiableMap(null) must throw NPE");
199         } catch (NullPointerException e) {
200             // expected
201         }
202     }
203 
204     /**
205      * java.util.Collections#unmodifiableSet(java.util.Set)
206      */
test_unmodifiableSetLjava_util_Set()207     public void test_unmodifiableSetLjava_util_Set() {
208         try {
209             // Regression for HARMONY-93
210             Collections.unmodifiableSet(null);
211             fail("Assert 0: unmodifiableSet(null) must throw NPE");
212         } catch (NullPointerException e) {
213             // expected
214         }
215     }
216 
217     /**
218      * java.util.Collections#unmodifiableSortedMap(java.util.SortedMap)
219      */
test_unmodifiableSortedMapLjava_util_SortedMap()220     public void test_unmodifiableSortedMapLjava_util_SortedMap() {
221         try {
222             // Regression for HARMONY-93
223             Collections.unmodifiableSortedMap(null);
224             fail("Assert 0: unmodifiableSortedMap(null) must throw NPE");
225         } catch (NullPointerException e) {
226             // expected
227         }
228     }
229 
230     /**
231      * java.util.Collections#unmodifiableSortedSet(java.util.SortedSet)
232      */
test_unmodifiableSortedSetLjava_util_SortedSet()233     public void test_unmodifiableSortedSetLjava_util_SortedSet() {
234         try {
235             // Regression for HARMONY-93
236             Collections.unmodifiableSortedSet(null);
237             fail("Assert 0: unmodifiableSortedSet(null) must throw NPE");
238         } catch (NullPointerException e) {
239             // expected
240         }
241     }
242 
243     /**
244      * java.util.Collections#frequency(java.util.Collection, Object)
245      */
test_frequencyLjava_util_CollectionLint()246     public void test_frequencyLjava_util_CollectionLint() {
247         try {
248             Collections.frequency(null, null);
249             fail("Assert 0: frequency(null,<any>) must throw NPE");
250         } catch (NullPointerException e) {
251         }
252 
253         List<String> strings = Arrays.asList(new String[] { "1", "2", "3", "1", "1" });
254 
255         assertEquals("Assert 1: did not find three \"1\" strings", 3,
256                 Collections.frequency(strings, "1"));
257 
258         assertEquals("Assert 2: did not find one \"2\" strings", 1, Collections
259                 .frequency(strings, "2"));
260 
261         assertEquals("Assert 3: did not find three \"3\" strings", 1,
262                 Collections.frequency(strings, "3"));
263 
264         assertEquals("Assert 4: matched on null when there are none", 0,
265                 Collections.frequency(strings, null));
266 
267         List<Object> objects = Arrays.asList(new Object[] { new Integer(1), null, null,
268                 new Long(1) });
269 
270         assertEquals("Assert 5: did not find one Integer(1)", 1, Collections
271                 .frequency(objects, new Integer(1)));
272 
273         assertEquals("Assert 6: did not find one Long(1)", 1, Collections
274                 .frequency(objects, new Long(1)));
275 
276         assertEquals("Assert 7: did not find two null references", 2,
277                 Collections.frequency(objects, null));
278     }
279 
280     /**
281      * java.util.Collections#reverseOrder()
282      */
test_reverseOrder()283     public void test_reverseOrder() {
284         Comparator<String> roc = Collections.reverseOrder();
285         assertNotNull("Assert 0: comparator must not be null", roc);
286 
287         assertTrue("Assert 1: comparator must implement Serializable",
288                 roc instanceof Serializable);
289 
290         String[] fixtureDesc = new String[] { "2", "1", "0" };
291         String[] numbers = new String[] { "0", "1", "2" };
292         Arrays.sort(numbers, roc);
293         assertTrue("Assert 2: the arrays are not equal, the sort failed",
294                 Arrays.equals(fixtureDesc, numbers));
295     }
296 
297     /**
298      * java.util.Collections#reverseOrder(java.util.Comparator)
299      */
test_reverseOrderLjava_util_Comparator()300     public void test_reverseOrderLjava_util_Comparator() {
301         Comparator<String> roc = Collections
302                 .reverseOrder(String.CASE_INSENSITIVE_ORDER);
303         assertNotNull("Assert 0: comparator must not be null", roc);
304 
305         assertTrue("Assert 1: comparator must implement Serializable",
306                 roc instanceof Serializable);
307 
308         String[] fixtureDesc = new String[] { "2", "1", "0" };
309         String[] numbers = new String[] { "0", "1", "2" };
310         Arrays.sort(numbers, roc);
311         assertTrue("Assert 2: the arrays are not equal, the sort failed",
312                 Arrays.equals(fixtureDesc, numbers));
313 
314         roc = Collections.reverseOrder(null);
315         assertNotNull("Assert 3: comparator must not be null", roc);
316 
317         assertTrue("Assert 4: comparator must implement Serializable",
318                 roc instanceof Serializable);
319 
320         numbers = new String[] { "0", "1", "2" };
321         Arrays.sort(numbers, roc);
322         assertTrue("Assert 5: the arrays are not equal, the sort failed",
323                 Arrays.equals(fixtureDesc, numbers));
324     }
325 
test_AddAll()326     public void test_AddAll() {
327         List<Object> l = new ArrayList<Object>();
328         assertFalse(Collections.addAll(l, new Object[] { }));
329         assertTrue(l.isEmpty());
330         assertTrue(Collections.addAll(l, new Object[] { new Integer(1),
331                 new Integer(2), new Integer(3) }));
332         assertFalse(l.isEmpty());
333         assertTrue(l.equals(Arrays.asList(new Object[] { new Integer(1),
334                 new Integer(2), new Integer(3) })));
335     }
336 
test_Disjoint()337     public void test_Disjoint() {
338         Object[] arr1 = new Object[10];
339         for (int i = 0; i < arr1.length; i++) {
340             arr1[i] = new Integer(i);
341         }
342         Object[] arr2 = new Object[20];
343         for (int i = 0; i < arr2.length; i++) {
344             arr2[i] = new Integer(100 + i);
345         }
346         Collection<Object> c1 = new ArrayList<Object>();
347         Collection<Object> c2 = new ArrayList<Object>();
348         Collections.addAll(c1, arr1);
349         Collections.addAll(c2, arr2);
350         assertTrue(Collections.disjoint(c1, c2));
351         c1.add(arr2[10]);
352         assertFalse(Collections.disjoint(c1, c2));
353 
354         c1 = new LinkedList<Object>();
355         c2 = new LinkedList<Object>();
356         Collections.addAll(c1, arr1);
357         Collections.addAll(c2, arr2);
358         assertTrue(Collections.disjoint(c1, c2));
359         c1.add(arr2[10]);
360         assertFalse(Collections.disjoint(c1, c2));
361 
362         c1 = new TreeSet<Object>();
363         c2 = new TreeSet<Object>();
364         Collections.addAll(c1, arr1);
365         Collections.addAll(c2, arr2);
366         assertTrue(Collections.disjoint(c1, c2));
367         c1.add(arr2[10]);
368         assertFalse(Collections.disjoint(c1, c2));
369 
370         c1 = new HashSet<Object>();
371         c2 = new HashSet<Object>();
372         Collections.addAll(c1, arr1);
373         Collections.addAll(c2, arr2);
374         assertTrue(Collections.disjoint(c1, c2));
375         c1.add(arr2[10]);
376         assertFalse(Collections.disjoint(c1, c2));
377 
378         c1 = new LinkedList<Object>();
379         c2 = new TreeSet<Object>();
380         Collections.addAll(c1, arr1);
381         Collections.addAll(c2, arr2);
382         assertTrue(Collections.disjoint(c1, c2));
383         c1.add(arr2[10]);
384         assertFalse(Collections.disjoint(c1, c2));
385 
386         c1 = new Vector<Object>();
387         c2 = new HashSet<Object>();
388         Collections.addAll(c1, arr1);
389         Collections.addAll(c2, arr2);
390         assertTrue(Collections.disjoint(c1, c2));
391         c1.add(arr2[10]);
392         assertFalse(Collections.disjoint(c1, c2));
393 
394     }
395 
396     /**
397      * java.util.Collections.EmptyList#readResolve()
398      */
test_EmptyList_readResolve()399     public void test_EmptyList_readResolve() throws Exception {
400         SerializationTest.verifySelf(Collections.EMPTY_LIST, comparator);
401     }
402 
403     /**
404      * java.util.Collections.EmptyMap#readResolve()
405      */
test_EmptyMap_readResolve()406     public void test_EmptyMap_readResolve() throws Exception {
407         SerializationTest.verifySelf(Collections.EMPTY_MAP, comparator);
408     }
409 
410     /**
411      * java.util.Collections.EmptySet#readResolve()
412      */
test_EmptySet_readResolve()413     public void test_EmptySet_readResolve() throws Exception {
414         SerializationTest.verifySelf(Collections.EMPTY_SET, comparator);
415     }
416 
test_checkedCollectionSerializationCompatability()417     public void test_checkedCollectionSerializationCompatability() throws Exception {
418         Collection<String> c = Collections.emptySet();
419         c = Collections.checkedCollection(c, String.class);
420         SerializationTester.assertCompabilityEquals(c, "serialization/org/apache/harmony/tests/java/util/Collections_CheckedCollection.golden.ser");
421     }
422 
test_checkedListRandomAccessSerializationCompatability()423     public void test_checkedListRandomAccessSerializationCompatability() throws Exception {
424         List<String> c = new ArrayList<String>();
425         assertTrue(c instanceof RandomAccess);
426         c = Collections.checkedList(c, String.class);
427         SerializationTester.assertCompabilityEquals(c, "serialization/org/apache/harmony/tests/java/util/Collections_CheckedListRandomAccess.golden.ser");
428     }
429 
test_checkedListSerializationCompatability()430     public void test_checkedListSerializationCompatability() throws Exception {
431         List<String> c = new LinkedList<String>();
432         assertFalse(c instanceof RandomAccess);
433         c = Collections.checkedList(c, String.class);
434         SerializationTester.assertCompabilityEquals(c, "serialization/org/apache/harmony/tests/java/util/Collections_CheckedList.golden.ser");
435     }
436 
test_checkedSetSerializationCompatability()437     public void test_checkedSetSerializationCompatability() throws Exception {
438         Set<String> c = new HashSet<String>();
439         assertFalse(c instanceof SortedSet);
440         c = Collections.checkedSet(c, String.class);
441         SerializationTester.assertCompabilityEquals(c, "serialization/org/apache/harmony/tests/java/util/Collections_CheckedSet.golden.ser");
442     }
443 
test_checkedMapSerializationCompatability()444     public void test_checkedMapSerializationCompatability() throws Exception {
445         Map<String, String> c = new HashMap<String, String>();
446         assertFalse(c instanceof SortedMap);
447         c = Collections.checkedMap(c, String.class, String.class);
448         SerializationTester.assertCompabilityEquals(c, "serialization/org/apache/harmony/tests/java/util/Collections_CheckedMap.golden.ser");
449     }
450 
test_checkedSortedSetSerializationCompatability()451     public void test_checkedSortedSetSerializationCompatability() throws Exception {
452         SortedSet<String> c = new TreeSet<String>();
453         c = Collections.checkedSortedSet(c, String.class);
454         SerializationTester.assertCompabilityEquals(c, "serialization/org/apache/harmony/tests/java/util/Collections_CheckedSortedSet.golden.ser");
455     }
456 
test_checkedSortedMapSerializationCompatability()457     public void test_checkedSortedMapSerializationCompatability() throws Exception {
458         SortedMap<String, String> c = new TreeMap<String, String>();
459         c = Collections.checkedSortedMap(c, String.class, String.class);
460         SerializationTester.assertCompabilityEquals(c, "serialization/org/apache/harmony/tests/java/util/Collections_CheckedSortedMap.golden.ser");
461     }
462 
test_emptyList()463     public void test_emptyList() {
464         List<Object> emptyList = Collections.emptyList();
465         assertEquals(0, emptyList.size());
466         assertTrue(emptyList instanceof RandomAccess);
467     }
468 
469     // Regression test for http://issues.apache.org/jira/browse/HARMONY-6122
test_Collections_swap_IndexOutOfBoundsException()470     public void test_Collections_swap_IndexOutOfBoundsException() {
471         try {
472             Collections.swap(new ArrayList<Object>(), -1, 3);
473             fail("IOOBE expected");
474         } catch (IndexOutOfBoundsException e) {
475         }
476 
477         List<Object> list = new ArrayList<Object>();
478         list.add("0");
479         try {
480             Collections.swap(list, 0, -1);
481             fail("IOOBE expected");
482         } catch (IndexOutOfBoundsException e) {
483         }
484 
485         try {
486             Collections.swap(list, 0, 3);
487             fail("IOOBE expected");
488         } catch (IndexOutOfBoundsException e) {
489         }
490 
491         try {
492             Collections.swap(new ArrayList<Object>(), 3, 3);
493             fail("IOOBE expected");
494         } catch (IndexOutOfBoundsException e) {
495         }
496     }
497 
testCollectionForEach(Collection<Integer> collection)498     void testCollectionForEach(Collection<Integer> collection) {
499         ArrayList<Integer> output = new ArrayList<Integer>();
500         collection.forEach(k -> output.add(k));
501 
502         assertEquals(new ArrayList<>(collection), output);
503     }
504 
test_Collection_forEach()505     public void test_Collection_forEach() {
506         ArrayList<Integer> list = new ArrayList<Integer>();
507         list.add(0);
508         list.add(1);
509         list.add(2);
510         testCollectionForEach(Collections.unmodifiableCollection(list));
511         testCollectionForEach(Collections.synchronizedCollection(list));
512         testCollectionForEach(Collections.checkedCollection(list, Integer.class));
513         testCollectionForEach(Collections.singletonList(new Integer(0)));
514     }
515 
testMapForEach(Map<String,String> map)516     void testMapForEach(Map<String,String> map) {
517         HashMap<String, String> output = new HashMap<String, String>();
518         map.forEach((k, v) -> output.put(k, v));
519         assertEquals(map, output);
520 
521         output.clear();
522         map.entrySet().forEach(entry -> output.put(entry.getKey(), entry.getValue()));
523         assertEquals(map, output);
524 
525         HashSet<String> setOutput = new HashSet<>();
526         map.values().forEach(value -> setOutput.add(value));
527         assertEquals(new HashSet<>(map.values()), setOutput);
528 
529         setOutput.clear();
530         map.keySet().forEach((k) -> setOutput.add(k));
531         assertEquals(map.keySet(), setOutput);
532     }
533 
test_Map_forEach()534     public void test_Map_forEach() {
535         HashMap<String, String> map = new HashMap<String, String>();
536         map.put("one", "1");
537         map.put("two", "2");
538         map.put("three", "3");
539         testMapForEach(Collections.unmodifiableMap(map));
540         testMapForEach(Collections.synchronizedMap(map));
541         testMapForEach(Collections.checkedMap(map, String.class, String.class));
542         testMapForEach(Collections.singletonMap("one", "1"));
543     }
544 
testSetForEach(Set<Integer> set)545     void testSetForEach(Set<Integer> set) {
546         HashSet<Integer> output = new HashSet<Integer>();
547         set.forEach(k -> output.add(k));
548 
549         assertEquals(set.size(), output.size());
550         for (Integer key : set) {
551             assertTrue(output.contains(key));
552         }
553     }
554 
test_Set_forEach()555     public void test_Set_forEach() {
556         HashSet<Integer> set = new HashSet<Integer>();
557         set.add(1);
558         set.add(2);
559         set.add(3);
560         testSetForEach(Collections.unmodifiableSet(set));
561         testSetForEach(Collections.synchronizedSet(set));
562         testSetForEach(Collections.checkedSet(set, Integer.class));
563         testSetForEach(Collections.singleton(1));
564 
565         Set<Integer> fromMap = Collections.newSetFromMap(new HashMap<Integer, Boolean>());
566         fromMap.add(1);
567         fromMap.add(2);
568         fromMap.add(3);
569         testSetForEach(fromMap);
570     }
571 
572 
test_Queue_forEach()573     public void test_Queue_forEach() {
574         Deque<Integer> deque = new ArrayDeque<Integer>();
575         deque.addFirst(2);
576         deque.addFirst(1);
577         deque.addFirst(0);
578 
579         Queue<Integer> queue = Collections.asLifoQueue(deque);
580         ArrayList<Integer> output = new ArrayList<Integer>();
581         queue.forEach(v -> output.add(v));
582 
583         assertEquals(3, output.size());
584         assertEquals(0, (int)output.get(0));
585         assertEquals(1, (int)output.get(1));
586         assertEquals(2, (int)output.get(2));
587     }
588 
589 }
590