001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.io.Serializable;
021: import java.util.ArrayList;
022: import java.util.Arrays;
023: import java.util.Collection;
024: import java.util.Collections;
025: import java.util.Comparator;
026: import java.util.HashMap;
027: import java.util.HashSet;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.RandomAccess;
032: import java.util.Set;
033: import java.util.SortedMap;
034: import java.util.SortedSet;
035: import java.util.TreeMap;
036: import java.util.TreeSet;
037: import java.util.Vector;
038:
039: import org.apache.harmony.testframework.serialization.SerializationTest;
040: import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
041:
042: import junit.framework.TestCase;
043: import tests.util.SerializationTester;
044:
045: public class Collections2Test extends TestCase {
046:
047: private static final SerializableAssert comparator = new SerializableAssert() {
048: public void assertDeserialized(Serializable reference,
049: Serializable test) {
050: assertSame(reference, test);
051: }
052: };
053:
054: /**
055: * @tests java.util.Collections#binarySearch(java.util.List,
056: * java.lang.Object, java.util.Comparator)
057: */
058: public void test_binarySearchLjava_util_ListLjava_lang_ObjectLjava_util_Comparator() {
059: // Regression for HARMONY-94
060: LinkedList<Integer> lst = new LinkedList<Integer>();
061: lst.add(new Integer(30));
062: Collections.sort(lst, null);
063: int index = Collections.binarySearch(lst, new Integer(2), null);
064: assertEquals(-1, index);
065: }
066:
067: /**
068: * @tests java.util.Collections#binarySearch(java.util.List,
069: * java.lang.Object)
070: */
071: @SuppressWarnings("unchecked")
072: public void test_binarySearchLjava_util_ListLjava_lang_Object() {
073: // regression for Harmony-1367
074: List localList = new LinkedList();
075: assertEquals(-1, Collections.binarySearch(localList,
076: new Object()));
077: localList.add(new Object());
078: try {
079: Collections.binarySearch(localList, new Integer(1));
080: fail("Should throw ClassCastException");
081: } catch (ClassCastException e) {
082: // expected
083: }
084: }
085:
086: /**
087: * @tests java.util.Collections#rotate(java.util.List, int)
088: */
089: public void test_rotateLjava_util_ListI() {
090: // Regression for HARMONY-19 Rotate an *empty* list
091: Collections.rotate(new ArrayList<Object>(), 25);
092:
093: // Regression for HARMONY-20
094: List<String> list = new ArrayList<String>();
095: list.add(0, "zero");
096: list.add(1, "one");
097: list.add(2, "two");
098: list.add(3, "three");
099: list.add(4, "four");
100:
101: Collections.rotate(list, Integer.MIN_VALUE);
102: assertEquals("Rotated incorrectly at position 0, ", "three",
103: list.get(0));
104: assertEquals("Rotated incorrectly at position 1, ", "four",
105: list.get(1));
106: assertEquals("Rotated incorrectly at position 2, ", "zero",
107: list.get(2));
108: assertEquals("Rotated incorrectly at position 3, ", "one", list
109: .get(3));
110: assertEquals("Rotated incorrectly at position 4, ", "two", list
111: .get(4));
112: }
113:
114: /**
115: * @tests java.util.Collections#synchronizedCollection(java.util.Collection)
116: */
117: public void test_synchronizedCollectionLjava_util_Collection() {
118: try {
119: // Regression for HARMONY-93
120: Collections.synchronizedCollection(null);
121: fail("Assert 0: synchronizedCollection(null) must throw NPE");
122: } catch (NullPointerException e) {
123: // expected
124: }
125: }
126:
127: /**
128: * @tests java.util.Collections#synchronizedSortedMap(java.util.SortedMap)
129: */
130: public void test_synchronizedSortedMapLjava_util_SortedMap() {
131: try {
132: // Regression for HARMONY-93
133: Collections.synchronizedSortedMap(null);
134: fail("Assert 0: synchronizedSortedMap(null) must throw NPE");
135: } catch (NullPointerException e) {
136: // expected
137: }
138: }
139:
140: /**
141: * @tests java.util.Collections#synchronizedMap(java.util.Map)
142: */
143: public void test_synchronizedMapLjava_util_Map() {
144: try {
145: // Regression for HARMONY-93
146: Collections.synchronizedMap(null);
147: fail("Assert 0: synchronizedMap(map) must throw NPE");
148: } catch (NullPointerException e) {
149: // expected
150: }
151: }
152:
153: /**
154: * @tests java.util.Collections#synchronizedSet(java.util.Set)
155: */
156: public void test_synchronizedSetLjava_util_Set() {
157: try {
158: // Regression for HARMONY-93
159: Collections.synchronizedSet(null);
160: fail("Assert 0: synchronizedSet(set) must throw NPE");
161: } catch (NullPointerException e) {
162: // expected
163: }
164: }
165:
166: /**
167: * @tests java.util.Collections#synchronizedSortedSet(java.util.SortedSet)
168: */
169: public void test_synchronizedSortedSetLjava_util_SortedSet() {
170: try {
171: // Regression for HARMONY-93
172: Collections.synchronizedSortedSet(null);
173: fail("Assert 0: synchronizedSortedSet(null) must throw NPE");
174: } catch (NullPointerException e) {
175: // expected
176: }
177: }
178:
179: /**
180: * @tests java.util.Collections#unmodifiableCollection(java.util.Collection)
181: */
182: public void test_unmodifiableCollectionLjava_util_Collection() {
183: try {
184: // Regression for HARMONY-93
185: Collections.unmodifiableCollection(null);
186: fail("Assert 0: unmodifiableCollection(null) must throw NPE");
187: } catch (NullPointerException e) {
188: // expected
189: }
190: }
191:
192: /**
193: * @tests java.util.Collections#unmodifiableMap(java.util.Map)
194: */
195: public void test_unmodifiableMapLjava_util_Map() {
196: try {
197: // Regression for HARMONY-93
198: Collections.unmodifiableMap(null);
199: fail("Assert 0: unmodifiableMap(null) must throw NPE");
200: } catch (NullPointerException e) {
201: // expected
202: }
203: }
204:
205: /**
206: * @tests java.util.Collections#unmodifiableSet(java.util.Set)
207: */
208: public void test_unmodifiableSetLjava_util_Set() {
209: try {
210: // Regression for HARMONY-93
211: Collections.unmodifiableSet(null);
212: fail("Assert 0: unmodifiableSet(null) must throw NPE");
213: } catch (NullPointerException e) {
214: // expected
215: }
216: }
217:
218: /**
219: * @tests java.util.Collections#unmodifiableSortedMap(java.util.SortedMap)
220: */
221: public void test_unmodifiableSortedMapLjava_util_SortedMap() {
222: try {
223: // Regression for HARMONY-93
224: Collections.unmodifiableSortedMap(null);
225: fail("Assert 0: unmodifiableSortedMap(null) must throw NPE");
226: } catch (NullPointerException e) {
227: // expected
228: }
229: }
230:
231: /**
232: * @tests java.util.Collections#unmodifiableSortedSet(java.util.SortedSet)
233: */
234: public void test_unmodifiableSortedSetLjava_util_SortedSet() {
235: try {
236: // Regression for HARMONY-93
237: Collections.unmodifiableSortedSet(null);
238: fail("Assert 0: unmodifiableSortedSet(null) must throw NPE");
239: } catch (NullPointerException e) {
240: // expected
241: }
242: }
243:
244: /**
245: * @tests java.util.Collections#frequency(java.util.Collection,Object)
246: */
247: public void test_frequencyLjava_util_CollectionLint() {
248: try {
249: Collections.frequency(null, null);
250: fail("Assert 0: frequency(null,<any>) must throw NPE");
251: } catch (NullPointerException e) {
252: }
253:
254: List<String> strings = Arrays.asList(new String[] { "1", "2",
255: "3", "1", "1" });
256:
257: assertEquals("Assert 1: did not find three \"1\" strings", 3,
258: Collections.frequency(strings, "1"));
259:
260: assertEquals("Assert 2: did not find one \"2\" strings", 1,
261: Collections.frequency(strings, "2"));
262:
263: assertEquals("Assert 3: did not find three \"3\" strings", 1,
264: Collections.frequency(strings, "3"));
265:
266: assertEquals("Assert 4: matched on null when there are none",
267: 0, Collections.frequency(strings, null));
268:
269: List<Object> objects = Arrays.asList(new Object[] {
270: new Integer(1), null, null, new Long(1) });
271:
272: assertEquals("Assert 5: did not find one Integer(1)", 1,
273: Collections.frequency(objects, new Integer(1)));
274:
275: assertEquals("Assert 6: did not find one Long(1)", 1,
276: Collections.frequency(objects, new Long(1)));
277:
278: assertEquals("Assert 7: did not find two null references", 2,
279: Collections.frequency(objects, null));
280: }
281:
282: /**
283: * @tests java.util.Collections#reverseOrder()
284: */
285: public void test_reverseOrder() {
286: Comparator<String> roc = Collections.reverseOrder();
287: assertNotNull("Assert 0: comparator must not be null", roc);
288:
289: assertTrue("Assert 1: comparator must implement Serializable",
290: roc instanceof Serializable);
291:
292: String[] fixtureDesc = new String[] { "2", "1", "0" };
293: String[] numbers = new String[] { "0", "1", "2" };
294: Arrays.sort(numbers, roc);
295: assertTrue(
296: "Assert 2: the arrays are not equal, the sort failed",
297: Arrays.equals(fixtureDesc, numbers));
298: }
299:
300: /**
301: * @tests java.util.Collections#reverseOrder(java.util.Comparator)
302: */
303: public void test_reverseOrderLjava_util_Comparator() {
304: Comparator<String> roc = Collections
305: .reverseOrder(String.CASE_INSENSITIVE_ORDER);
306: assertNotNull("Assert 0: comparator must not be null", roc);
307:
308: assertTrue("Assert 1: comparator must implement Serializable",
309: roc instanceof Serializable);
310:
311: String[] fixtureDesc = new String[] { "2", "1", "0" };
312: String[] numbers = new String[] { "0", "1", "2" };
313: Arrays.sort(numbers, roc);
314: assertTrue(
315: "Assert 2: the arrays are not equal, the sort failed",
316: Arrays.equals(fixtureDesc, numbers));
317:
318: roc = Collections.reverseOrder(null);
319: assertNotNull("Assert 3: comparator must not be null", roc);
320:
321: assertTrue("Assert 4: comparator must implement Serializable",
322: roc instanceof Serializable);
323:
324: numbers = new String[] { "0", "1", "2" };
325: Arrays.sort(numbers, roc);
326: assertTrue(
327: "Assert 5: the arrays are not equal, the sort failed",
328: Arrays.equals(fixtureDesc, numbers));
329: }
330:
331: public void test_AddAll() {
332: List<Object> l = new ArrayList<Object>();
333: assertFalse(Collections.addAll(l, new Object[] {}));
334: assertTrue(l.isEmpty());
335: assertTrue(Collections.addAll(l, new Object[] { new Integer(1),
336: new Integer(2), new Integer(3) }));
337: assertFalse(l.isEmpty());
338: assertTrue(l.equals(Arrays.asList(new Object[] {
339: new Integer(1), new Integer(2), new Integer(3) })));
340: }
341:
342: public void test_Disjoint() {
343: Object[] arr1 = new Object[10];
344: for (int i = 0; i < arr1.length; i++) {
345: arr1[i] = new Integer(i);
346: }
347: Object[] arr2 = new Object[20];
348: for (int i = 0; i < arr2.length; i++) {
349: arr2[i] = new Integer(100 + i);
350: }
351: Collection<Object> c1 = new ArrayList<Object>();
352: Collection<Object> c2 = new ArrayList<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 LinkedList<Object>();
360: c2 = new LinkedList<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 TreeSet<Object>();
368: c2 = new TreeSet<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 HashSet<Object>();
376: c2 = new HashSet<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 LinkedList<Object>();
384: c2 = new TreeSet<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: c1 = new Vector<Object>();
392: c2 = new HashSet<Object>();
393: Collections.addAll(c1, arr1);
394: Collections.addAll(c2, arr2);
395: assertTrue(Collections.disjoint(c1, c2));
396: c1.add(arr2[10]);
397: assertFalse(Collections.disjoint(c1, c2));
398:
399: }
400:
401: /**
402: * @tests java.util.Collections.EmptyList#readResolve()
403: */
404: public void test_EmptyList_readResolve() throws Exception {
405: SerializationTest
406: .verifySelf(Collections.EMPTY_LIST, comparator);
407: }
408:
409: /**
410: * @tests java.util.Collections.EmptyMap#readResolve()
411: */
412: public void test_EmptyMap_readResolve() throws Exception {
413: SerializationTest.verifySelf(Collections.EMPTY_MAP, comparator);
414: }
415:
416: /**
417: * @tests java.util.Collections.EmptySet#readResolve()
418: */
419: public void test_EmptySet_readResolve() throws Exception {
420: SerializationTest.verifySelf(Collections.EMPTY_SET, comparator);
421: }
422:
423: public void test_checkedCollectionSerializationCompatability()
424: throws Exception {
425: Collection<String> c = Collections.emptySet();
426: c = Collections.checkedCollection(c, String.class);
427: SerializationTester
428: .assertCompabilityEquals(c,
429: "serialization/java/util/Collections_CheckedCollection.golden.ser");
430: }
431:
432: public void test_checkedListRandomAccessSerializationCompatability()
433: throws Exception {
434: List<String> c = new ArrayList<String>();
435: assertTrue(c instanceof RandomAccess);
436: c = Collections.checkedList(c, String.class);
437: SerializationTester
438: .assertCompabilityEquals(c,
439: "serialization/java/util/Collections_CheckedListRandomAccess.golden.ser");
440: }
441:
442: public void test_checkedListSerializationCompatability()
443: throws Exception {
444: List<String> c = new LinkedList<String>();
445: assertFalse(c instanceof RandomAccess);
446: c = Collections.checkedList(c, String.class);
447: SerializationTester
448: .assertCompabilityEquals(c,
449: "serialization/java/util/Collections_CheckedList.golden.ser");
450: }
451:
452: public void test_checkedSetSerializationCompatability()
453: throws Exception {
454: Set<String> c = new HashSet<String>();
455: assertFalse(c instanceof SortedSet);
456: c = Collections.checkedSet(c, String.class);
457: SerializationTester
458: .assertCompabilityEquals(c,
459: "serialization/java/util/Collections_CheckedSet.golden.ser");
460: }
461:
462: public void test_checkedMapSerializationCompatability()
463: throws Exception {
464: Map<String, String> c = new HashMap<String, String>();
465: assertFalse(c instanceof SortedMap);
466: c = Collections.checkedMap(c, String.class, String.class);
467: SerializationTester
468: .assertCompabilityEquals(c,
469: "serialization/java/util/Collections_CheckedMap.golden.ser");
470: }
471:
472: public void test_checkedSortedSetSerializationCompatability()
473: throws Exception {
474: SortedSet<String> c = new TreeSet<String>();
475: c = Collections.checkedSortedSet(c, String.class);
476: SerializationTester
477: .assertCompabilityEquals(c,
478: "serialization/java/util/Collections_CheckedSortedSet.golden.ser");
479: }
480:
481: public void test_checkedSortedMapSerializationCompatability()
482: throws Exception {
483: SortedMap<String, String> c = new TreeMap<String, String>();
484: c = Collections.checkedSortedMap(c, String.class, String.class);
485: SerializationTester
486: .assertCompabilityEquals(c,
487: "serialization/java/util/Collections_CheckedSortedMap.golden.ser");
488: }
489: }
|