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