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
18package libcore.java.util;
19
20import java.io.Serializable;
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.Collection;
24import java.util.Collections;
25import java.util.Comparator;
26import java.util.HashMap;
27import java.util.HashSet;
28import java.util.Iterator;
29import java.util.LinkedList;
30import java.util.List;
31import java.util.Map;
32import java.util.RandomAccess;
33import java.util.Set;
34import java.util.SortedMap;
35import java.util.SortedSet;
36import java.util.TreeMap;
37import java.util.TreeSet;
38import java.util.Vector;
39import junit.framework.TestCase;
40import libcore.util.SerializationTester;
41import org.apache.harmony.testframework.serialization.SerializationTest;
42import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
43
44public class OldCollectionsTest extends TestCase {
45
46    private static final SerializableAssert comparator = new SerializableAssert() {
47        public void assertDeserialized(Serializable reference, Serializable test) {
48            assertSame(reference, test);
49        }
50    };
51
52    /**
53     * java.util.Collections#binarySearch(java.util.List,
54     *        java.lang.Object, java.util.Comparator)
55     */
56    public void test_binarySearchLjava_util_ListLjava_lang_ObjectLjava_util_Comparator() {
57        // Regression for HARMONY-94
58        LinkedList<Integer> lst = new LinkedList<Integer>();
59        lst.add(new Integer(30));
60        Collections.sort(lst, null);
61        int index = Collections.binarySearch(lst, new Integer(2), null);
62        assertEquals(-1, index);
63
64        LinkedList<String> lls = new LinkedList<String>();
65        lls.add("1");
66        lls.add("2");
67        lls.add("3");
68        lls.add("4");
69        lls.add("");
70        LinkedList<String> ll = lls;
71
72        try {
73            Collections.binarySearch(ll, new Integer(10), null);
74            fail("ClassCastException expected");
75        } catch (ClassCastException e) {
76            //expected
77        }
78    }
79
80    /**
81     * java.util.Collections#binarySearch(java.util.List,
82     *        java.lang.Object)
83     */
84    @SuppressWarnings("unchecked")
85    public void test_binarySearchLjava_util_ListLjava_lang_Object() {
86        // regression for Harmony-1367
87        List localList = new LinkedList();
88        assertEquals(-1, Collections.binarySearch(localList, new Object()));
89        localList.add(new Object());
90        try {
91            Collections.binarySearch(localList, new Integer(1));
92            fail("Should throw ClassCastException");
93        } catch (ClassCastException e) {
94            // expected
95        }
96
97        LinkedList<String> lls = new LinkedList<String>();
98        lls.add("1");
99        lls.add("2");
100        lls.add("3");
101        lls.add("4");
102        lls.add("");
103        LinkedList ll = lls;
104
105        try {
106            Collections.binarySearch(ll, new Integer(10));
107            fail("ClassCastException expected");
108        } catch (ClassCastException e) {
109            //expected
110        }
111    }
112
113    /**
114     * java.util.Collections#rotate(java.util.List, int)
115     */
116    public void test_rotateLjava_util_ListI() {
117        // Regression for HARMONY-19 Rotate an *empty* list
118        Collections.rotate(new ArrayList<Object>(), 25);
119
120        // Regression for HARMONY-20
121        List<String> list = new ArrayList<String>();
122        list.add(0, "zero");
123        list.add(1, "one");
124        list.add(2, "two");
125        list.add(3, "three");
126        list.add(4, "four");
127
128        Collections.rotate(list, Integer.MIN_VALUE);
129        assertEquals("Rotated incorrectly at position 0, ", "three",
130                list.get(0));
131        assertEquals("Rotated incorrectly at position 1, ", "four",
132                list.get(1));
133        assertEquals("Rotated incorrectly at position 2, ", "zero",
134                list.get(2));
135        assertEquals("Rotated incorrectly at position 3, ", "one",
136                list.get(3));
137        assertEquals("Rotated incorrectly at position 4, ", "two",
138                list.get(4));
139    }
140
141    /**
142     * java.util.Collections#synchronizedCollection(java.util.Collection)
143     */
144    public void test_synchronizedCollectionLjava_util_Collection() {
145        try {
146            // Regression for HARMONY-93
147            Collections.synchronizedCollection(null);
148            fail("Assert 0: synchronizedCollection(null) must throw NPE");
149        } catch (NullPointerException e) {
150            // expected
151        }
152    }
153
154    /**
155     * java.util.Collections#synchronizedSortedMap(java.util.SortedMap)
156     */
157    public void test_synchronizedSortedMapLjava_util_SortedMap() {
158        try {
159            // Regression for HARMONY-93
160            Collections.synchronizedSortedMap(null);
161            fail("Assert 0: synchronizedSortedMap(null) must throw NPE");
162        } catch (NullPointerException e) {
163            // expected
164        }
165    }
166
167    /**
168     * java.util.Collections#synchronizedMap(java.util.Map)
169     */
170    public void test_synchronizedMapLjava_util_Map() {
171        try {
172            // Regression for HARMONY-93
173            Collections.synchronizedMap(null);
174            fail("Assert 0: synchronizedMap(map) must throw NPE");
175        } catch (NullPointerException e) {
176            // expected
177        }
178    }
179
180    /**
181     * java.util.Collections#synchronizedSet(java.util.Set)
182     */
183    public void test_synchronizedSetLjava_util_Set() {
184        try {
185            // Regression for HARMONY-93
186            Collections.synchronizedSet(null);
187            fail("Assert 0: synchronizedSet(set) must throw NPE");
188        } catch (NullPointerException e) {
189            // expected
190        }
191    }
192
193    /**
194     * java.util.Collections#synchronizedSortedSet(java.util.SortedSet)
195     */
196    public void test_synchronizedSortedSetLjava_util_SortedSet() {
197        try {
198            // Regression for HARMONY-93
199            Collections.synchronizedSortedSet(null);
200            fail("Assert 0: synchronizedSortedSet(null) must throw NPE");
201        } catch (NullPointerException e) {
202            // expected
203        }
204    }
205
206    /**
207     * java.util.Collections#unmodifiableCollection(java.util.Collection)
208     */
209    public void test_unmodifiableCollectionLjava_util_Collection() {
210        try {
211            // Regression for HARMONY-93
212            Collections.unmodifiableCollection(null);
213            fail("Assert 0: unmodifiableCollection(null) must throw NPE");
214        } catch (NullPointerException e) {
215            // expected
216        }
217    }
218
219    /**
220     * java.util.Collections#unmodifiableMap(java.util.Map)
221     */
222    public void test_unmodifiableMapLjava_util_Map() {
223        try {
224            // Regression for HARMONY-93
225            Collections.unmodifiableMap(null);
226            fail("Assert 0: unmodifiableMap(null) must throw NPE");
227        } catch (NullPointerException e) {
228            // expected
229        }
230    }
231
232    /**
233     * java.util.Collections#unmodifiableSet(java.util.Set)
234     */
235    public void test_unmodifiableSetLjava_util_Set() {
236        try {
237            // Regression for HARMONY-93
238            Collections.unmodifiableSet(null);
239            fail("Assert 0: unmodifiableSet(null) must throw NPE");
240        } catch (NullPointerException e) {
241            // expected
242        }
243    }
244
245    /**
246     * java.util.Collections#unmodifiableSortedMap(java.util.SortedMap)
247     */
248    public void test_unmodifiableSortedMapLjava_util_SortedMap() {
249        try {
250            // Regression for HARMONY-93
251            Collections.unmodifiableSortedMap(null);
252            fail("Assert 0: unmodifiableSortedMap(null) must throw NPE");
253        } catch (NullPointerException e) {
254            // expected
255        }
256    }
257
258    /**
259     * java.util.Collections#unmodifiableSortedSet(java.util.SortedSet)
260     */
261    public void test_unmodifiableSortedSetLjava_util_SortedSet() {
262        try {
263            // Regression for HARMONY-93
264            Collections.unmodifiableSortedSet(null);
265            fail("Assert 0: unmodifiableSortedSet(null) must throw NPE");
266        } catch (NullPointerException e) {
267            // expected
268        }
269    }
270
271    /**
272     * java.util.Collections#frequency(java.util.Collection,Object)
273     */
274    public void test_frequencyLjava_util_CollectionLint() {
275        try {
276            Collections.frequency(null, null);
277            fail("Assert 0: frequency(null,<any>) must throw NPE");
278        } catch (NullPointerException e) {}
279
280        List<String> strings = Arrays.asList(new String[] { "1", "2", "3", "1", "1" });
281
282        assertEquals("Assert 1: did not find three \"1\" strings", 3,
283                Collections.frequency(strings, "1"));
284
285        assertEquals("Assert 2: did not find one \"2\" strings", 1, Collections
286                .frequency(strings, "2"));
287
288        assertEquals("Assert 3: did not find three \"3\" strings", 1,
289                Collections.frequency(strings, "3"));
290
291        assertEquals("Assert 4: matched on null when there are none", 0,
292                Collections.frequency(strings, null));
293
294        List<Object> objects = Arrays.asList(new Object[] { new Integer(1), null, null,
295                new Long(1) });
296
297        assertEquals("Assert 5: did not find one Integer(1)", 1, Collections
298                .frequency(objects, new Integer(1)));
299
300        assertEquals("Assert 6: did not find one Long(1)", 1, Collections
301                .frequency(objects, new Long(1)));
302
303        assertEquals("Assert 7: did not find two null references", 2,
304                Collections.frequency(objects, null));
305    }
306
307    /**
308     * java.util.Collections#reverseOrder()
309     */
310    public void test_reverseOrder() {
311        Comparator<String> roc = Collections.reverseOrder();
312        assertNotNull("Assert 0: comparator must not be null", roc);
313
314        assertTrue("Assert 1: comparator must implement Serializable",
315                roc instanceof Serializable);
316
317        String[] fixtureDesc = new String[] { "2", "1", "0" };
318        String[] numbers = new String[] { "0", "1", "2" };
319        Arrays.sort(numbers, roc);
320        assertTrue("Assert 2: the arrays are not equal, the sort failed",
321                Arrays.equals(fixtureDesc, numbers));
322    }
323
324    /**
325     * java.util.Collections#reverseOrder(java.util.Comparator)
326     */
327    public void test_reverseOrderLjava_util_Comparator() {
328        Comparator<String> roc = Collections
329                .reverseOrder(String.CASE_INSENSITIVE_ORDER);
330        assertNotNull("Assert 0: comparator must not be null", roc);
331
332        assertTrue("Assert 1: comparator must implement Serializable",
333                roc instanceof Serializable);
334
335        String[] fixtureDesc = new String[] { "2", "1", "0" };
336        String[] numbers = new String[] { "0", "1", "2" };
337        Arrays.sort(numbers, roc);
338        assertTrue("Assert 2: the arrays are not equal, the sort failed",
339                Arrays.equals(fixtureDesc, numbers));
340
341        roc = Collections.reverseOrder(null);
342        assertNotNull("Assert 3: comparator must not be null", roc);
343
344        assertTrue("Assert 4: comparator must implement Serializable",
345                roc instanceof Serializable);
346
347        numbers = new String[] { "0", "1", "2" };
348        Arrays.sort(numbers, roc);
349        assertTrue("Assert 5: the arrays are not equal, the sort failed",
350                Arrays.equals(fixtureDesc, numbers));
351    }
352
353    class Mock_Collection implements Collection {
354        public boolean add(Object o) {
355            throw new UnsupportedOperationException();
356        }
357
358        public boolean addAll(Collection c) {
359            return false;
360        }
361
362        public void clear() {
363        }
364
365        public boolean contains(Object o) {
366            return false;
367        }
368
369        public boolean containsAll(Collection c) {
370            return false;
371        }
372
373        public boolean isEmpty() {
374            return false;
375        }
376
377        public Iterator iterator() {
378            return null;
379        }
380
381        public boolean remove(Object o) {
382            return false;
383        }
384
385        public boolean removeAll(Collection c) {
386            return false;
387        }
388
389        public boolean retainAll(Collection c) {
390            return false;
391        }
392
393        public int size() {
394            return 0;
395        }
396
397        public Object[] toArray() {
398            return null;
399        }
400
401        public Object[] toArray(Object[] a) {
402            return null;
403        }
404    }
405
406    class Mock_WrongCollection implements Collection {
407        final String wrongElement = "Wrong element";
408        public boolean add(Object o) {
409            if (o.equals(wrongElement)) throw new IllegalArgumentException();
410            if (o == null) throw new NullPointerException();
411            return false;
412        }
413
414        public boolean addAll(Collection c) {
415            return false;
416        }
417
418        public void clear() {
419        }
420
421        public boolean contains(Object o) {
422            return false;
423        }
424
425        public boolean containsAll(Collection c) {
426            return false;
427        }
428
429        public boolean isEmpty() {
430            return false;
431        }
432
433        public Iterator iterator() {
434            return null;
435        }
436
437        public boolean remove(Object o) {
438            return false;
439        }
440
441        public boolean removeAll(Collection c) {
442            return false;
443        }
444
445        public boolean retainAll(Collection c) {
446            return false;
447        }
448
449        public int size() {
450            return 0;
451        }
452
453        public Object[] toArray() {
454            return null;
455        }
456
457        public Object[] toArray(Object[] a) {
458            return null;
459        }
460    }
461
462    public void test_AddAll() {
463        List<Object> l = new ArrayList<Object>();
464        assertFalse(Collections.addAll(l, new Object[] {}));
465        assertTrue(l.isEmpty());
466        assertTrue(Collections.addAll(l, new Object[] { new Integer(1),
467                new Integer(2), new Integer(3) }));
468        assertFalse(l.isEmpty());
469        assertTrue(l.equals(Arrays.asList(new Object[] { new Integer(1),
470                new Integer(2), new Integer(3) })));
471
472        try {
473            Collections.addAll(null,new Object[] { new Integer(1),
474                    new Integer(2), new Integer(3) });
475            fail("NullPointerException expected");
476        } catch (NullPointerException e) {
477            //fail
478        }
479
480        Collection c = new Mock_Collection();
481        try {
482            Collections.addAll(c, new Object[] { new Integer(1),
483                    new Integer(2), new Integer(3) });
484            fail("UnsupportedOperationException expected");
485        } catch (UnsupportedOperationException e) {
486            //expected
487        }
488
489        c = new Mock_WrongCollection ();
490
491        try {
492            Collections.addAll(c, new String[] { "String",
493                    "Correct element", null });
494            fail("NullPointerException expected");
495        } catch (NullPointerException e) {
496            //fail
497        }
498
499        try {
500            Collections.addAll(c, new String[] { "String",
501                    "Wrong element", "Correct element" });
502            fail("IllegalArgumentException expected");
503        } catch (IllegalArgumentException e) {
504            //fail
505        }
506
507        Collections.addAll(c, new String[] { "String",
508                "", "Correct element" });
509    }
510    public void test_Disjoint() {
511        Object[] arr1 = new Object[10];
512        for (int i = 0; i < arr1.length; i++) {
513            arr1[i] = new Integer(i);
514        }
515        Object[] arr2 = new Object[20];
516        for (int i = 0; i < arr2.length; i++) {
517            arr2[i] = new Integer(100 + i);
518        }
519        Collection<Object> c1 = new ArrayList<Object>();
520        Collection<Object> c2 = new ArrayList<Object>();
521        Collections.addAll(c1, arr1);
522        Collections.addAll(c2, arr2);
523        assertTrue(Collections.disjoint(c1, c2));
524        c1.add(arr2[10]);
525        assertFalse(Collections.disjoint(c1, c2));
526
527        c1 = new LinkedList<Object>();
528        c2 = new LinkedList<Object>();
529        Collections.addAll(c1, arr1);
530        Collections.addAll(c2, arr2);
531        assertTrue(Collections.disjoint(c1, c2));
532        c1.add(arr2[10]);
533        assertFalse(Collections.disjoint(c1, c2));
534
535        c1 = new TreeSet<Object>();
536        c2 = new TreeSet<Object>();
537        Collections.addAll(c1, arr1);
538        Collections.addAll(c2, arr2);
539        assertTrue(Collections.disjoint(c1, c2));
540        c1.add(arr2[10]);
541        assertFalse(Collections.disjoint(c1, c2));
542
543        c1 = new HashSet<Object>();
544        c2 = new HashSet<Object>();
545        Collections.addAll(c1, arr1);
546        Collections.addAll(c2, arr2);
547        assertTrue(Collections.disjoint(c1, c2));
548        c1.add(arr2[10]);
549        assertFalse(Collections.disjoint(c1, c2));
550
551        c1 = new LinkedList<Object>();
552        c2 = new TreeSet<Object>();
553        Collections.addAll(c1, arr1);
554        Collections.addAll(c2, arr2);
555        assertTrue(Collections.disjoint(c1, c2));
556        c1.add(arr2[10]);
557        assertFalse(Collections.disjoint(c1, c2));
558
559        c1 = new Vector<Object>();
560        c2 = new HashSet<Object>();
561        Collections.addAll(c1, arr1);
562        Collections.addAll(c2, arr2);
563        assertTrue(Collections.disjoint(c1, c2));
564        c1.add(arr2[10]);
565        assertFalse(Collections.disjoint(c1, c2));
566
567        try {
568            Collections.disjoint(c1, null);
569            fail("NullPointerException expected");
570        } catch (NullPointerException e) {
571            //expected
572        }
573
574        try {
575            Collections.disjoint(null, c2);
576            fail("NullPointerException expected");
577        } catch (NullPointerException e) {
578            //expected
579        }
580    }
581
582    /**
583     * java.util.Collections.EmptyList#readResolve()
584     */
585    public void test_EmptyList_readResolve() throws Exception {
586        SerializationTest.verifySelf(Collections.EMPTY_LIST, comparator);
587    }
588
589    /**
590     * java.util.Collections.EmptyMap#readResolve()
591     */
592    public void test_EmptyMap_readResolve() throws Exception {
593        SerializationTest.verifySelf(Collections.EMPTY_MAP, comparator);
594    }
595
596    /**
597     * java.util.Collections.EmptySet#readResolve()
598     */
599    public void test_EmptySet_readResolve() throws Exception {
600        SerializationTest.verifySelf(Collections.EMPTY_SET, comparator);
601    }
602
603    public void test_checkedCollectionSerializationCompatibility() throws Exception {
604        String s = "aced0005737200276a6176612e7574696c2e436f6c6c656374696f6e73244368"
605                + "65636b6564436f6c6c656374696f6e15e96dfd18e6cc6f0200034c00016374001"
606                + "64c6a6176612f7574696c2f436f6c6c656374696f6e3b4c000474797065740011"
607                + "4c6a6176612f6c616e672f436c6173733b5b00167a65726f4c656e677468456c6"
608                + "56d656e7441727261797400135b4c6a6176612f6c616e672f4f626a6563743b78"
609                + "707372001e6a6176612e7574696c2e436f6c6c656374696f6e7324456d7074795"
610                + "3657415f5721db403cb280200007870767200106a6176612e6c616e672e537472"
611                + "696e67a0f0a4387a3bb342020000787070";
612        assertSerialized(Collections.checkedCollection(
613                Collections.<String>emptySet(), String.class), s, false);
614    }
615    public void test_checkedListRandomAccessSerializationCompatibility() throws Exception {
616        String s = "aced00057372002d6a6176612e7574696c2e436f6c6c656374696f6e73244368"
617                + "65636b656452616e646f6d4163636573734c69737416bc0e55a2d7f2f10200007"
618                + "87200216a6176612e7574696c2e436f6c6c656374696f6e7324436865636b6564"
619                + "4c69737400e7ce7692c45f7c0200014c00046c6973747400104c6a6176612f757"
620                + "4696c2f4c6973743b787200276a6176612e7574696c2e436f6c6c656374696f6e"
621                + "7324436865636b6564436f6c6c656374696f6e15e96dfd18e6cc6f0200034c000"
622                + "1637400164c6a6176612f7574696c2f436f6c6c656374696f6e3b4c0004747970"
623                + "657400114c6a6176612f6c616e672f436c6173733b5b00167a65726f4c656e677"
624                + "468456c656d656e7441727261797400135b4c6a6176612f6c616e672f4f626a65"
625                + "63743b7870737200136a6176612e7574696c2e41727261794c6973747881d21d9"
626                + "9c7619d03000149000473697a6578700000000077040000000a78767200106a61"
627                + "76612e6c616e672e537472696e67a0f0a4387a3bb34202000078707071007e0009";
628        assertSerialized(Collections.checkedList(new ArrayList<String>(), String.class), s, true);
629    }
630    public void test_checkedListSerializationCompatibility() throws Exception {
631        String s = "aced0005737200216a6176612e7574696c2e436f6c6c656374696f6e73244368"
632                + "65636b65644c69737400e7ce7692c45f7c0200014c00046c6973747400104c6a6"
633                + "176612f7574696c2f4c6973743b787200276a6176612e7574696c2e436f6c6c65"
634                + "6374696f6e7324436865636b6564436f6c6c656374696f6e15e96dfd18e6cc6f0"
635                + "200034c0001637400164c6a6176612f7574696c2f436f6c6c656374696f6e3b4c"
636                + "0004747970657400114c6a6176612f6c616e672f436c6173733b5b00167a65726"
637                + "f4c656e677468456c656d656e7441727261797400135b4c6a6176612f6c616e67"
638                + "2f4f626a6563743b7870737200146a6176612e7574696c2e4c696e6b65644c697"
639                + "3740c29535d4a608822030000787077040000000078767200106a6176612e6c61"
640                + "6e672e537472696e67a0f0a4387a3bb34202000078707071007e0008";
641        assertSerialized(Collections.checkedList(new LinkedList<String>(), String.class), s, true);
642    }
643    public void test_checkedSetSerializationCompatibility() throws Exception {
644        String s = "aced0005737200206a6176612e7574696c2e436f6c6c656374696f6e73244368"
645                + "65636b656453657441249ba27ad9ffab020000787200276a6176612e7574696c2"
646                + "e436f6c6c656374696f6e7324436865636b6564436f6c6c656374696f6e15e96d"
647                + "fd18e6cc6f0200034c0001637400164c6a6176612f7574696c2f436f6c6c65637"
648                + "4696f6e3b4c0004747970657400114c6a6176612f6c616e672f436c6173733b5b"
649                + "00167a65726f4c656e677468456c656d656e7441727261797400135b4c6a61766"
650                + "12f6c616e672f4f626a6563743b7870737200116a6176612e7574696c2e486173"
651                + "68536574ba44859596b8b7340300007870770c000000103f40000000000000787"
652                + "67200106a6176612e6c616e672e537472696e67a0f0a4387a3bb3420200007870"
653                + "70";
654        assertSerialized(Collections.checkedSet(new HashSet<String>(), String.class), s, true);
655    }
656    public void test_checkedMapSerializationCompatibility() throws Exception {
657        String s = "aced0005737200206a6176612e7574696c2e436f6c6c656374696f6e73244368"
658                + "65636b65644d61704fb2bcdf0d1863680200054c00076b6579547970657400114"
659                + "c6a6176612f6c616e672f436c6173733b4c00016d74000f4c6a6176612f757469"
660                + "6c2f4d61703b4c000976616c75655479706571007e00015b00127a65726f4c656"
661                + "e6774684b657941727261797400135b4c6a6176612f6c616e672f4f626a656374"
662                + "3b5b00147a65726f4c656e67746856616c7565417272617971007e00037870767"
663                + "200106a6176612e6c616e672e537472696e67a0f0a4387a3bb342020000787073"
664                + "7200116a6176612e7574696c2e486173684d61700507dac1c31660d1030002460"
665                + "00a6c6f6164466163746f724900097468726573686f6c6478703f400000000000"
666                + "0c770800000010000000007871007e00067070";
667        assertSerialized(Collections.checkedMap(
668                new HashMap<String, String>(), String.class, String.class), s);
669    }
670    public void test_checkedSortedSetSerializationCompatibility() throws Exception {
671        String s = "aced0005737200266a6176612e7574696c2e436f6c6c656374696f6e73244368"
672                + "65636b6564536f72746564536574163406ba7362eb0f0200014c0002737374001"
673                + "54c6a6176612f7574696c2f536f727465645365743b787200206a6176612e7574"
674                + "696c2e436f6c6c656374696f6e7324436865636b656453657441249ba27ad9ffa"
675                + "b020000787200276a6176612e7574696c2e436f6c6c656374696f6e7324436865"
676                + "636b6564436f6c6c656374696f6e15e96dfd18e6cc6f0200034c0001637400164"
677                + "c6a6176612f7574696c2f436f6c6c656374696f6e3b4c0004747970657400114c"
678                + "6a6176612f6c616e672f436c6173733b5b00167a65726f4c656e677468456c656"
679                + "d656e7441727261797400135b4c6a6176612f6c616e672f4f626a6563743b7870"
680                + "737200116a6176612e7574696c2e54726565536574dd98509395ed875b0300007"
681                + "8707077040000000078767200106a6176612e6c616e672e537472696e67a0f0a4"
682                + "387a3bb34202000078707071007e0009";
683        assertSerialized(Collections.checkedSortedSet(new TreeSet<String>(), String.class), s, true);
684    }
685    public void test_checkedSortedMapSerializationCompatibility() throws Exception {
686        String s = "aced0005737200266a6176612e7574696c2e436f6c6c656374696f6e73244368"
687                + "65636b6564536f727465644d617016332c973afe036e0200014c0002736d74001"
688                + "54c6a6176612f7574696c2f536f727465644d61703b787200206a6176612e7574"
689                + "696c2e436f6c6c656374696f6e7324436865636b65644d61704fb2bcdf0d18636"
690                + "80200054c00076b6579547970657400114c6a6176612f6c616e672f436c617373"
691                + "3b4c00016d74000f4c6a6176612f7574696c2f4d61703b4c000976616c7565547"
692                + "9706571007e00035b00127a65726f4c656e6774684b657941727261797400135b"
693                + "4c6a6176612f6c616e672f4f626a6563743b5b00147a65726f4c656e677468566"
694                + "16c7565417272617971007e00057870767200106a6176612e6c616e672e537472"
695                + "696e67a0f0a4387a3bb3420200007870737200116a6176612e7574696c2e54726"
696                + "5654d61700cc1f63e2d256ae60300014c000a636f6d70617261746f727400164c"
697                + "6a6176612f7574696c2f436f6d70617261746f723b78707077040000000078710"
698                + "07e0008707071007e000b";
699        assertSerialized(Collections.checkedSortedMap(
700                new TreeMap<String, String>(), String.class, String.class), s);
701    }
702
703    private void assertSerialized(Collection<?> collection, String s, final boolean definesEquals) {
704        new SerializationTester<Collection<?>>(collection, s) {
705            @SuppressWarnings("unchecked")
706            @Override protected void verify(Collection<?> deserialized) throws Exception {
707                try {
708                    ((Collection) deserialized).add(Boolean.TRUE);
709                    fail();
710                } catch (ClassCastException expected) {
711                }
712            }
713            @Override protected boolean equals(Collection<?> a, Collection<?> b) {
714                boolean equal = definesEquals
715                        ? a.equals(b)
716                        : Arrays.equals(a.toArray(), b.toArray());
717                return equal
718                        && (a instanceof SortedSet == b instanceof SortedSet)
719                        && (a instanceof RandomAccess == b instanceof RandomAccess);
720            }
721        }.test();
722    }
723
724    private void assertSerialized(Map<?, ?> map, String s) {
725        new SerializationTester<Map<?, ?>>(map, s) {
726            @SuppressWarnings("unchecked")
727            @Override protected void verify(Map<?, ?> deserialized) throws Exception {
728                try {
729                    ((Map) deserialized).put(Boolean.TRUE, "a");
730                    fail();
731                } catch (ClassCastException expected) {
732                }
733                try {
734                    ((Map) deserialized).put("a", Boolean.TRUE);
735                    fail();
736                } catch (ClassCastException expected) {
737                }
738            }
739            @Override protected boolean equals(Map<?, ?> a, Map<?, ?> b) {
740                return super.equals(a, b)
741                        && (a instanceof SortedMap == b instanceof SortedMap);
742            }
743        }.test();
744    }
745
746    public void test_checkedCollectionLjava_util_CollectionLjava_lang_Class() {
747        ArrayList al = new ArrayList<Integer>();
748
749        Collection c = Collections.checkedCollection(al, Integer.class);
750
751        c.add(new Integer(1));
752
753        try {
754            c.add(new Double(3.14));
755            fail("ClassCastException expected");
756        } catch (ClassCastException e) {
757            //expected
758        }
759    }
760
761    public void test_checkedListLjava_util_ListLjava_lang_Class() {
762        ArrayList al = new ArrayList<Integer>();
763
764        List l = Collections.checkedList(al, Integer.class);
765
766        l.add(new Integer(1));
767
768        try {
769            l.add(new Double(3.14));
770            fail("ClassCastException expected");
771        } catch (ClassCastException e) {
772            //expected
773        }
774    }
775
776    public void test_checkedMapLjava_util_MapLjava_lang_ClassLjava_lang_Class() {
777        HashMap hm = new HashMap<Integer, String>();
778
779        Map m = Collections.checkedMap(hm, Integer.class, String.class);
780
781        m.put(1, "one");
782        m.put(2, "two");
783
784        try {
785            m.put("wron key", null);
786            fail("ClassCastException expected");
787        } catch (ClassCastException e) {
788            //expected
789        }
790
791        try {
792            m.put(3, new Double(3.14));
793            fail("ClassCastException expected");
794        } catch (ClassCastException e) {
795            //expected
796        }
797    }
798
799    public void test_checkedSetLjava_util_SetLjava_lang_Class() {
800        HashSet hs = new HashSet<Integer>();
801
802        Set s = Collections.checkedSet(hs, Integer.class);
803
804        s.add(new Integer(1));
805
806        try {
807            s.add(new Double(3.14));
808            fail("ClassCastException expected");
809        } catch (ClassCastException e) {
810            //expected
811        }
812    }
813
814    public void test_checkedSortedMapLjava_util_SortedMapLjava_lang_ClassLjava_lang_Class() {
815        TreeMap tm = new TreeMap<Integer, String>();
816
817        SortedMap sm = Collections.checkedSortedMap(tm, Integer.class, String.class);
818
819        sm.put(1, "one");
820        sm.put(2, "two");
821
822        try {
823            sm.put("wron key", null);
824            fail("ClassCastException expected");
825        } catch (ClassCastException e) {
826            //expected
827        }
828
829        try {
830            sm.put(3, new Double(3.14));
831            fail("ClassCastException expected");
832        } catch (ClassCastException e) {
833            //expected
834        }
835    }
836
837    public void test_checkedSortedSetLjava_util_SortedSetLjava_lang_Class() {
838        TreeSet ts = new TreeSet<Integer>();
839
840        SortedSet ss = Collections.checkedSortedSet(ts, Integer.class);
841
842        ss.add(new Integer(1));
843
844        try {
845            ss.add(new Double(3.14));
846            fail("ClassCastException expected");
847        } catch (ClassCastException e) {
848            //expected
849        }
850    }
851
852    public void test_emptyList() {
853        List<String> ls = Collections.emptyList();
854        List<Integer> li = Collections.emptyList();
855
856        assertTrue(ls.equals(li));
857        assertTrue(li.equals(Collections.EMPTY_LIST));
858    }
859
860    public void test_emptyMap() {
861        Map<Integer, String> mis = Collections.emptyMap();
862        Map<String, Integer> msi = Collections.emptyMap();
863
864        assertTrue(mis.equals(msi));
865        assertTrue(msi.equals(Collections.EMPTY_MAP));
866    }
867
868    public void test_emptySet() {
869        Set<String> ss = Collections.emptySet();
870        Set<Integer> si = Collections.emptySet();
871
872        assertTrue(ss.equals(si));
873        assertTrue(si.equals(Collections.EMPTY_SET));
874    }
875}
876