Home | History | Annotate | Download | only in util
      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.luni.tests.java.util;
     19 
     20 import java.io.Serializable;
     21 import java.util.ArrayList;
     22 import java.util.Arrays;
     23 import java.util.Collection;
     24 import java.util.Collections;
     25 import java.util.Comparator;
     26 import java.util.HashMap;
     27 import java.util.HashSet;
     28 import java.util.LinkedList;
     29 import java.util.List;
     30 import java.util.Map;
     31 import java.util.RandomAccess;
     32 import java.util.Set;
     33 import java.util.SortedMap;
     34 import java.util.SortedSet;
     35 import java.util.TreeMap;
     36 import java.util.TreeSet;
     37 import java.util.Vector;
     38 
     39 import org.apache.harmony.testframework.serialization.SerializationTest;
     40 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
     41 
     42 import junit.framework.TestCase;
     43 import tests.util.SerializationTester;
     44 
     45 public class Collections2Test extends TestCase {
     46 
     47     private static final SerializableAssert comparator = new SerializableAssert() {
     48         public void assertDeserialized(Serializable reference, Serializable test) {
     49             assertSame(reference, test);
     50         }
     51     };
     52 
     53 	/**
     54 	 * @tests java.util.Collections#binarySearch(java.util.List,
     55 	 *        java.lang.Object, java.util.Comparator)
     56 	 */
     57 	public void test_binarySearchLjava_util_ListLjava_lang_ObjectLjava_util_Comparator() {
     58 		// Regression for HARMONY-94
     59 		LinkedList<Integer> lst = new LinkedList<Integer>();
     60 		lst.add(new Integer(30));
     61 		Collections.sort(lst, null);
     62 		int index = Collections.binarySearch(lst, new Integer(2), null);
     63 		assertEquals(-1, index);
     64 	}
     65 
     66 	/**
     67 	 * @tests java.util.Collections#binarySearch(java.util.List,
     68 	 *        java.lang.Object)
     69 	 */
     70 	@SuppressWarnings("unchecked")
     71     public void test_binarySearchLjava_util_ListLjava_lang_Object() {
     72 		// regression for Harmony-1367
     73 		List localList = new LinkedList();
     74 		assertEquals(-1, Collections.binarySearch(localList, new Object()));
     75 		localList.add(new Object());
     76 		try {
     77 			Collections.binarySearch(localList, new Integer(1));
     78 			fail("Should throw ClassCastException");
     79 		} catch (ClassCastException e) {
     80 			// expected
     81 		}
     82 	}
     83 
     84 	/**
     85 	 * @tests java.util.Collections#rotate(java.util.List, int)
     86 	 */
     87 	public void test_rotateLjava_util_ListI() {
     88 		// Regression for HARMONY-19 Rotate an *empty* list
     89 		Collections.rotate(new ArrayList<Object>(), 25);
     90 
     91 		// Regression for HARMONY-20
     92 		List<String> list = new ArrayList<String>();
     93 		list.add(0, "zero");
     94 		list.add(1, "one");
     95 		list.add(2, "two");
     96 		list.add(3, "three");
     97 		list.add(4, "four");
     98 
     99 		Collections.rotate(list, Integer.MIN_VALUE);
    100 		assertEquals("Rotated incorrectly at position 0, ", "three",
    101 				list.get(0));
    102 		assertEquals("Rotated incorrectly at position 1, ", "four",
    103 				list.get(1));
    104 		assertEquals("Rotated incorrectly at position 2, ", "zero",
    105 				list.get(2));
    106 		assertEquals("Rotated incorrectly at position 3, ", "one",
    107 				list.get(3));
    108 		assertEquals("Rotated incorrectly at position 4, ", "two",
    109 				list.get(4));
    110 	}
    111 
    112 	/**
    113 	 * @tests java.util.Collections#synchronizedCollection(java.util.Collection)
    114 	 */
    115 	public void test_synchronizedCollectionLjava_util_Collection() {
    116 		try {
    117 			// Regression for HARMONY-93
    118 			Collections.synchronizedCollection(null);
    119 			fail("Assert 0: synchronizedCollection(null) must throw NPE");
    120 		} catch (NullPointerException e) {
    121 			// expected
    122 		}
    123 	}
    124 
    125 	/**
    126 	 * @tests java.util.Collections#synchronizedSortedMap(java.util.SortedMap)
    127 	 */
    128 	public void test_synchronizedSortedMapLjava_util_SortedMap() {
    129 		try {
    130 			// Regression for HARMONY-93
    131 			Collections.synchronizedSortedMap(null);
    132 			fail("Assert 0: synchronizedSortedMap(null) must throw NPE");
    133 		} catch (NullPointerException e) {
    134 			// expected
    135 		}
    136 	}
    137 
    138 	/**
    139 	 * @tests java.util.Collections#synchronizedMap(java.util.Map)
    140 	 */
    141 	public void test_synchronizedMapLjava_util_Map() {
    142 		try {
    143 			// Regression for HARMONY-93
    144 			Collections.synchronizedMap(null);
    145 			fail("Assert 0: synchronizedMap(map) must throw NPE");
    146 		} catch (NullPointerException e) {
    147 			// expected
    148 		}
    149 	}
    150 
    151 	/**
    152 	 * @tests java.util.Collections#synchronizedSet(java.util.Set)
    153 	 */
    154 	public void test_synchronizedSetLjava_util_Set() {
    155 		try {
    156 			// Regression for HARMONY-93
    157 			Collections.synchronizedSet(null);
    158 			fail("Assert 0: synchronizedSet(set) must throw NPE");
    159 		} catch (NullPointerException e) {
    160 			// expected
    161 		}
    162 	}
    163 
    164 	/**
    165 	 * @tests java.util.Collections#synchronizedSortedSet(java.util.SortedSet)
    166 	 */
    167 	public void test_synchronizedSortedSetLjava_util_SortedSet() {
    168 		try {
    169 			// Regression for HARMONY-93
    170 			Collections.synchronizedSortedSet(null);
    171 			fail("Assert 0: synchronizedSortedSet(null) must throw NPE");
    172 		} catch (NullPointerException e) {
    173 			// expected
    174 		}
    175 	}
    176 
    177 	/**
    178 	 * @tests java.util.Collections#unmodifiableCollection(java.util.Collection)
    179 	 */
    180 	public void test_unmodifiableCollectionLjava_util_Collection() {
    181 		try {
    182 			// Regression for HARMONY-93
    183 			Collections.unmodifiableCollection(null);
    184 			fail("Assert 0: unmodifiableCollection(null) must throw NPE");
    185 		} catch (NullPointerException e) {
    186 			// expected
    187 		}
    188 	}
    189 
    190 	/**
    191 	 * @tests java.util.Collections#unmodifiableMap(java.util.Map)
    192 	 */
    193 	public void test_unmodifiableMapLjava_util_Map() {
    194 		try {
    195 			// Regression for HARMONY-93
    196 			Collections.unmodifiableMap(null);
    197 			fail("Assert 0: unmodifiableMap(null) must throw NPE");
    198 		} catch (NullPointerException e) {
    199 			// expected
    200 		}
    201 	}
    202 
    203 	/**
    204 	 * @tests java.util.Collections#unmodifiableSet(java.util.Set)
    205 	 */
    206 	public void test_unmodifiableSetLjava_util_Set() {
    207 		try {
    208 			// Regression for HARMONY-93
    209 			Collections.unmodifiableSet(null);
    210 			fail("Assert 0: unmodifiableSet(null) must throw NPE");
    211 		} catch (NullPointerException e) {
    212 			// expected
    213 		}
    214 	}
    215 
    216 	/**
    217 	 * @tests java.util.Collections#unmodifiableSortedMap(java.util.SortedMap)
    218 	 */
    219 	public void test_unmodifiableSortedMapLjava_util_SortedMap() {
    220 		try {
    221 			// Regression for HARMONY-93
    222 			Collections.unmodifiableSortedMap(null);
    223 			fail("Assert 0: unmodifiableSortedMap(null) must throw NPE");
    224 		} catch (NullPointerException e) {
    225 			// expected
    226 		}
    227 	}
    228 
    229 	/**
    230 	 * @tests java.util.Collections#unmodifiableSortedSet(java.util.SortedSet)
    231 	 */
    232 	public void test_unmodifiableSortedSetLjava_util_SortedSet() {
    233 		try {
    234 			// Regression for HARMONY-93
    235 			Collections.unmodifiableSortedSet(null);
    236 			fail("Assert 0: unmodifiableSortedSet(null) must throw NPE");
    237 		} catch (NullPointerException e) {
    238 			// expected
    239 		}
    240 	}
    241 
    242     /**
    243      * @tests java.util.Collections#frequency(java.util.Collection,Object)
    244      */
    245     public void test_frequencyLjava_util_CollectionLint() {
    246         try {
    247             Collections.frequency(null, null);
    248             fail("Assert 0: frequency(null,<any>) must throw NPE");
    249         } catch (NullPointerException e) {}
    250 
    251         List<String> strings = Arrays.asList(new String[] { "1", "2", "3", "1", "1" });
    252 
    253         assertEquals("Assert 1: did not find three \"1\" strings", 3,
    254                 Collections.frequency(strings, "1"));
    255 
    256         assertEquals("Assert 2: did not find one \"2\" strings", 1, Collections
    257                 .frequency(strings, "2"));
    258 
    259         assertEquals("Assert 3: did not find three \"3\" strings", 1,
    260                 Collections.frequency(strings, "3"));
    261 
    262         assertEquals("Assert 4: matched on null when there are none", 0,
    263                 Collections.frequency(strings, null));
    264 
    265         List<Object> objects = Arrays.asList(new Object[] { new Integer(1), null, null,
    266                 new Long(1) });
    267 
    268         assertEquals("Assert 5: did not find one Integer(1)", 1, Collections
    269                 .frequency(objects, new Integer(1)));
    270 
    271         assertEquals("Assert 6: did not find one Long(1)", 1, Collections
    272                 .frequency(objects, new Long(1)));
    273 
    274         assertEquals("Assert 7: did not find two null references", 2,
    275                 Collections.frequency(objects, null));
    276     }
    277 
    278     /**
    279      * @tests java.util.Collections#reverseOrder()
    280      */
    281     public void test_reverseOrder() {
    282         Comparator<String> roc = Collections.reverseOrder();
    283         assertNotNull("Assert 0: comparator must not be null", roc);
    284 
    285         assertTrue("Assert 1: comparator must implement Serializable",
    286                 roc instanceof Serializable);
    287 
    288         String[] fixtureDesc = new String[] { "2", "1", "0" };
    289         String[] numbers = new String[] { "0", "1", "2" };
    290         Arrays.sort(numbers, roc);
    291         assertTrue("Assert 2: the arrays are not equal, the sort failed",
    292                 Arrays.equals(fixtureDesc, numbers));
    293     }
    294 
    295     /**
    296      * @tests java.util.Collections#reverseOrder(java.util.Comparator)
    297      */
    298     public void test_reverseOrderLjava_util_Comparator() {
    299         Comparator<String> roc = Collections
    300                 .reverseOrder(String.CASE_INSENSITIVE_ORDER);
    301         assertNotNull("Assert 0: comparator must not be null", roc);
    302 
    303         assertTrue("Assert 1: comparator must implement Serializable",
    304                 roc instanceof Serializable);
    305 
    306         String[] fixtureDesc = new String[] { "2", "1", "0" };
    307         String[] numbers = new String[] { "0", "1", "2" };
    308         Arrays.sort(numbers, roc);
    309         assertTrue("Assert 2: the arrays are not equal, the sort failed",
    310                 Arrays.equals(fixtureDesc, numbers));
    311 
    312         roc = Collections.reverseOrder(null);
    313         assertNotNull("Assert 3: comparator must not be null", roc);
    314 
    315         assertTrue("Assert 4: comparator must implement Serializable",
    316                 roc instanceof Serializable);
    317 
    318         numbers = new String[] { "0", "1", "2" };
    319         Arrays.sort(numbers, roc);
    320         assertTrue("Assert 5: the arrays are not equal, the sort failed",
    321                 Arrays.equals(fixtureDesc, numbers));
    322     }
    323 
    324     public void test_AddAll() {
    325         List<Object> l = new ArrayList<Object>();
    326         assertFalse(Collections.addAll(l, new Object[] {}));
    327         assertTrue(l.isEmpty());
    328         assertTrue(Collections.addAll(l, new Object[] { new Integer(1),
    329                 new Integer(2), new Integer(3) }));
    330         assertFalse(l.isEmpty());
    331         assertTrue(l.equals(Arrays.asList(new Object[] { new Integer(1),
    332                 new Integer(2), new Integer(3) })));
    333     }
    334 
    335     public void test_Disjoint() {
    336         Object[] arr1 = new Object[10];
    337         for (int i = 0; i < arr1.length; i++) {
    338             arr1[i] = new Integer(i);
    339         }
    340         Object[] arr2 = new Object[20];
    341         for (int i = 0; i < arr2.length; i++) {
    342             arr2[i] = new Integer(100 + i);
    343         }
    344         Collection<Object> c1 = new ArrayList<Object>();
    345         Collection<Object> c2 = new ArrayList<Object>();
    346         Collections.addAll(c1, arr1);
    347         Collections.addAll(c2, arr2);
    348         assertTrue(Collections.disjoint(c1, c2));
    349         c1.add(arr2[10]);
    350         assertFalse(Collections.disjoint(c1, c2));
    351 
    352         c1 = new LinkedList<Object>();
    353         c2 = new LinkedList<Object>();
    354         Collections.addAll(c1, arr1);
    355         Collections.addAll(c2, arr2);
    356         assertTrue(Collections.disjoint(c1, c2));
    357         c1.add(arr2[10]);
    358         assertFalse(Collections.disjoint(c1, c2));
    359 
    360         c1 = new TreeSet<Object>();
    361         c2 = new TreeSet<Object>();
    362         Collections.addAll(c1, arr1);
    363         Collections.addAll(c2, arr2);
    364         assertTrue(Collections.disjoint(c1, c2));
    365         c1.add(arr2[10]);
    366         assertFalse(Collections.disjoint(c1, c2));
    367 
    368         c1 = new HashSet<Object>();
    369         c2 = new HashSet<Object>();
    370         Collections.addAll(c1, arr1);
    371         Collections.addAll(c2, arr2);
    372         assertTrue(Collections.disjoint(c1, c2));
    373         c1.add(arr2[10]);
    374         assertFalse(Collections.disjoint(c1, c2));
    375 
    376         c1 = new LinkedList<Object>();
    377         c2 = new TreeSet<Object>();
    378         Collections.addAll(c1, arr1);
    379         Collections.addAll(c2, arr2);
    380         assertTrue(Collections.disjoint(c1, c2));
    381         c1.add(arr2[10]);
    382         assertFalse(Collections.disjoint(c1, c2));
    383 
    384         c1 = new Vector<Object>();
    385         c2 = new HashSet<Object>();
    386         Collections.addAll(c1, arr1);
    387         Collections.addAll(c2, arr2);
    388         assertTrue(Collections.disjoint(c1, c2));
    389         c1.add(arr2[10]);
    390         assertFalse(Collections.disjoint(c1, c2));
    391 
    392     }
    393 
    394     /**
    395      * @tests java.util.Collections.EmptyList#readResolve()
    396      */
    397     public void test_EmptyList_readResolve() throws Exception {
    398         SerializationTest.verifySelf(Collections.EMPTY_LIST, comparator);
    399     }
    400 
    401     /**
    402      * @tests java.util.Collections.EmptyMap#readResolve()
    403      */
    404     public void test_EmptyMap_readResolve() throws Exception {
    405         SerializationTest.verifySelf(Collections.EMPTY_MAP, comparator);
    406     }
    407 
    408     /**
    409      * @tests java.util.Collections.EmptySet#readResolve()
    410      */
    411     public void test_EmptySet_readResolve() throws Exception {
    412         SerializationTest.verifySelf(Collections.EMPTY_SET, comparator);
    413     }
    414 
    415     public void test_checkedCollectionSerializationCompatability() throws Exception {
    416         Collection<String> c = Collections.emptySet();
    417         c = Collections.checkedCollection(c, String.class);
    418         SerializationTester.assertCompabilityEquals(c, "serialization/java/util/Collections_CheckedCollection.golden.ser");
    419     }
    420 
    421     public void test_checkedListRandomAccessSerializationCompatability() throws Exception {
    422         List<String> c = new ArrayList<String>();
    423         assertTrue(c instanceof RandomAccess);
    424         c = Collections.checkedList(c, String.class);
    425         SerializationTester.assertCompabilityEquals(c, "serialization/java/util/Collections_CheckedListRandomAccess.golden.ser");
    426     }
    427 
    428     public void test_checkedListSerializationCompatability() throws Exception {
    429         List<String> c = new LinkedList<String>();
    430         assertFalse(c instanceof RandomAccess);
    431         c = Collections.checkedList(c, String.class);
    432         SerializationTester.assertCompabilityEquals(c, "serialization/java/util/Collections_CheckedList.golden.ser");
    433     }
    434 
    435     public void test_checkedSetSerializationCompatability() throws Exception {
    436         Set<String> c = new HashSet<String>();
    437         assertFalse(c instanceof SortedSet);
    438         c = Collections.checkedSet(c, String.class);
    439         SerializationTester.assertCompabilityEquals(c, "serialization/java/util/Collections_CheckedSet.golden.ser");
    440     }
    441 
    442     public void test_checkedMapSerializationCompatability() throws Exception {
    443         Map<String, String> c = new HashMap<String, String>();
    444         assertFalse(c instanceof SortedMap);
    445         c = Collections.checkedMap(c, String.class, String.class);
    446         SerializationTester.assertCompabilityEquals(c, "serialization/java/util/Collections_CheckedMap.golden.ser");
    447     }
    448 
    449     public void test_checkedSortedSetSerializationCompatability() throws Exception {
    450         SortedSet<String> c = new TreeSet<String>();
    451         c = Collections.checkedSortedSet(c, String.class);
    452         SerializationTester.assertCompabilityEquals(c, "serialization/java/util/Collections_CheckedSortedSet.golden.ser");
    453     }
    454 
    455     public void test_checkedSortedMapSerializationCompatability() throws Exception {
    456         SortedMap<String, String> c = new TreeMap<String, String>();
    457         c = Collections.checkedSortedMap(c, String.class, String.class);
    458         SerializationTester.assertCompabilityEquals(c, "serialization/java/util/Collections_CheckedSortedMap.golden.ser");
    459     }
    460 
    461     public void test_emptyList(){
    462         List<Object> emptyList = Collections.emptyList();
    463         assertEquals(0, emptyList.size());
    464         assertTrue(emptyList instanceof RandomAccess);
    465     }
    466 
    467     // Regression test for http://issues.apache.org/jira/browse/HARMONY-6122
    468     public void test_Collections_swap_IndexOutOfBoundsException() {
    469         try {
    470             Collections.swap(new ArrayList<Object>(), -1, 3);
    471             fail("IOOBE expected");
    472         } catch (IndexOutOfBoundsException e) {
    473         }
    474 
    475         List<Object> list = new ArrayList<Object>();
    476         list.add("0");
    477         try {
    478             Collections.swap(list, 0, -1);
    479             fail("IOOBE expected");
    480         } catch (IndexOutOfBoundsException e) {
    481         }
    482 
    483         try {
    484             Collections.swap(list, 0, 3);
    485             fail("IOOBE expected");
    486         } catch (IndexOutOfBoundsException e) {
    487         }
    488 
    489         try {
    490             Collections.swap(new ArrayList<Object>(), 3, 3);
    491             fail("IOOBE expected");
    492         } catch (IndexOutOfBoundsException e) {
    493         }
    494     }
    495 
    496 }
    497