TreeMapTest.java revision 3819a76e7c1f49253f0e077bd497f149340c02b8
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,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.util;
19
20import dalvik.annotation.KnownFailure;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestTargets;
23import dalvik.annotation.TestLevel;
24import dalvik.annotation.TestTargetClass;
25import dalvik.annotation.AndroidOnly;
26
27import java.io.Serializable;
28import java.text.CollationKey;
29import java.text.Collator;
30import java.util.AbstractMap;
31import java.util.Collection;
32import java.util.Comparator;
33import java.util.HashMap;
34import java.util.Iterator;
35import java.util.Map;
36import java.util.NoSuchElementException;
37import java.util.Set;
38import java.util.SortedMap;
39import java.util.TreeMap;
40
41import org.apache.harmony.testframework.serialization.SerializationTest;
42
43import tests.support.Support_MapTest2;
44import tests.support.Support_UnmodifiableCollectionTest;
45
46@TestTargetClass(TreeMap.class)
47public class TreeMapTest extends junit.framework.TestCase {
48
49    public static class ReversedComparator implements Comparator {
50        public int compare(Object o1, Object o2) {
51            return -(((Comparable) o1).compareTo(o2));
52        }
53
54        public boolean equals(Object o1, Object o2) {
55            return (((Comparable) o1).compareTo(o2)) == 0;
56        }
57    }
58
59    // Regression for Harmony-1026
60    public static class MockComparator<T extends Comparable<T>> implements
61            Comparator<T>, Serializable {
62
63        public int compare(T o1, T o2) {
64            if (o1 == o2) {
65                return 0;
66            }
67            if (null == o1 || null == o2) {
68                return -1;
69            }
70            T c1 = o1;
71            T c2 = o2;
72            return c1.compareTo(c2);
73        }
74    }
75
76    // Regression for Harmony-1161
77    class MockComparatorNullTolerable implements Comparator<String> {
78
79        public int compare(String o1, String o2) {
80            if (o1 == o2) {
81                return 0;
82            }
83            if (null == o1) {
84                return -1;
85            }
86            if (null == o2) {
87                return 1;
88            }
89            return o1.compareTo(o2);
90        }
91    }
92
93    TreeMap tm;
94
95    Object objArray[] = new Object[1000];
96
97    /**
98     * @tests java.util.TreeMap#TreeMap()
99     */
100    @TestTargetNew(
101        level = TestLevel.COMPLETE,
102        notes = "",
103        method = "TreeMap",
104        args = {}
105    )
106    public void test_Constructor() {
107        // Test for method java.util.TreeMap()
108        new Support_MapTest2(new TreeMap()).runTest();
109
110        assertTrue("New treeMap non-empty", new TreeMap().isEmpty());
111    }
112
113    /**
114     * @tests java.util.TreeMap#TreeMap(java.util.Comparator)
115     */
116    @TestTargetNew(
117        level = TestLevel.COMPLETE,
118        notes = "",
119        method = "TreeMap",
120        args = {java.util.Comparator.class}
121    )
122    public void test_ConstructorLjava_util_Comparator() {
123        // Test for method java.util.TreeMap(java.util.Comparator)
124        Comparator comp = new ReversedComparator();
125        TreeMap reversedTreeMap = new TreeMap(comp);
126        assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
127                .comparator() == comp);
128        reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
129        reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
130        assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
131                reversedTreeMap.firstKey().equals(new Integer(2).toString()));
132        assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
133                reversedTreeMap.lastKey().equals(new Integer(1).toString()));
134
135    }
136
137    /**
138     * @tests java.util.TreeMap#TreeMap(java.util.Map)
139     */
140    @TestTargetNew(
141        level = TestLevel.COMPLETE,
142        notes = "",
143        method = "TreeMap",
144        args = {java.util.Map.class}
145    )
146    public void test_ConstructorLjava_util_Map() {
147        // Test for method java.util.TreeMap(java.util.Map)
148        TreeMap myTreeMap = new TreeMap(new HashMap(tm));
149        assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length);
150        for (Object element : objArray) {
151            assertTrue("Map has incorrect mappings", myTreeMap.get(
152                    element.toString()).equals(element));
153        }
154
155        HashMap hm = new HashMap();
156        hm.put(new Integer(1), "one");
157        hm.put("one", new Integer(1));
158
159        try {
160            new TreeMap(hm);
161            fail("ClassCastException expected");
162        } catch (ClassCastException e) {
163            //expected
164        }
165
166        try {
167            new TreeMap((Map)null);
168            fail("NullPointerException expected");
169        } catch (NullPointerException e) {
170            //expected
171        }
172    }
173
174    /**
175     * @tests java.util.TreeMap#TreeMap(java.util.SortedMap)
176     */
177    @TestTargetNew(
178        level = TestLevel.COMPLETE,
179        notes = "",
180        method = "TreeMap",
181        args = {java.util.SortedMap.class}
182    )
183    public void test_ConstructorLjava_util_SortedMap() {
184        // Test for method java.util.TreeMap(java.util.SortedMap)
185        Comparator comp = new ReversedComparator();
186        TreeMap reversedTreeMap = new TreeMap(comp);
187        reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
188        reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
189        TreeMap anotherTreeMap = new TreeMap(reversedTreeMap);
190        assertTrue("New tree map does not answer correct comparator",
191                anotherTreeMap.comparator() == comp);
192        assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
193                anotherTreeMap.firstKey().equals(new Integer(2).toString()));
194        assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
195                anotherTreeMap.lastKey().equals(new Integer(1).toString()));
196
197        try {
198            new TreeMap((SortedMap)null);
199            fail("NullPointerException expected");
200        } catch (NullPointerException e) {
201            //expected
202        }
203    }
204
205    /**
206     * @tests java.util.TreeMap#clear()
207     */
208    @TestTargetNew(
209        level = TestLevel.COMPLETE,
210        notes = "",
211        method = "clear",
212        args = {}
213    )
214    public void test_clear() {
215        // Test for method void java.util.TreeMap.clear()
216        tm.clear();
217        assertEquals("Cleared map returned non-zero size", 0, tm.size());
218    }
219
220    /**
221     * @tests java.util.TreeMap#clone()
222     */
223    @TestTargetNew(
224        level = TestLevel.COMPLETE,
225        notes = "",
226        method = "clone",
227        args = {}
228    )
229    public void test_clone() {
230        // Test for method java.lang.Object java.util.TreeMap.clone()
231        TreeMap clonedMap = (TreeMap) tm.clone();
232        assertTrue("Cloned map does not equal the original map", clonedMap
233                .equals(tm));
234        assertTrue("Cloned map is the same reference as the original map",
235                clonedMap != tm);
236        for (Object element : objArray) {
237            assertTrue("Cloned map contains incorrect elements", clonedMap
238                    .get(element.toString()) == tm.get(element.toString()));
239        }
240
241        TreeMap map = new TreeMap();
242        map.put("key", "value");
243        // get the keySet() and values() on the original Map
244        Set keys = map.keySet();
245        Collection values = map.values();
246        assertEquals("values() does not work", "value", values.iterator()
247                .next());
248        assertEquals("keySet() does not work", "key", keys.iterator().next());
249        AbstractMap map2 = (AbstractMap) map.clone();
250        map2.put("key", "value2");
251        Collection values2 = map2.values();
252        assertTrue("values() is identical", values2 != values);
253        // values() and keySet() on the cloned() map should be different
254        assertEquals("values() was not cloned", "value2", values2.iterator()
255                .next());
256        map2.clear();
257        map2.put("key2", "value3");
258        Set key2 = map2.keySet();
259        assertTrue("keySet() is identical", key2 != keys);
260        assertEquals("keySet() was not cloned", "key2", key2.iterator().next());
261    }
262
263    /**
264     * @tests java.util.TreeMap#comparator()
265     */
266    @TestTargetNew(
267        level = TestLevel.COMPLETE,
268        notes = "",
269        method = "comparator",
270        args = {}
271    )
272    public void test_comparator() {
273        // Test for method java.util.Comparator java.util.TreeMap.comparator()\
274        Comparator comp = new ReversedComparator();
275        TreeMap reversedTreeMap = new TreeMap(comp);
276        assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
277                .comparator() == comp);
278        reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
279        reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
280        assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
281                reversedTreeMap.firstKey().equals(new Integer(2).toString()));
282        assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
283                reversedTreeMap.lastKey().equals(new Integer(1).toString()));
284    }
285
286    /**
287     * @tests java.util.TreeMap#containsKey(java.lang.Object)
288     */
289    @TestTargetNew(
290        level = TestLevel.COMPLETE,
291        notes = "",
292        method = "containsKey",
293        args = {java.lang.Object.class}
294    )
295    public void test_containsKeyLjava_lang_Object() {
296        // Test for method boolean
297        // java.util.TreeMap.containsKey(java.lang.Object)
298        assertTrue("Returned false for valid key", tm.containsKey("95"));
299        assertTrue("Returned true for invalid key", !tm.containsKey("XXXXX"));
300
301        try {
302            tm.containsKey(new Double(3.14));
303            fail("ClassCastException expected");
304        } catch (ClassCastException e) {
305            //expected
306        }
307
308        try {
309            tm.containsKey(null);
310            fail("NullPointerException expected");
311        } catch (NullPointerException e) {
312            //expected
313        }
314    }
315
316    /**
317     * @tests java.util.TreeMap#containsValue(java.lang.Object)
318     */
319    @TestTargetNew(
320        level = TestLevel.COMPLETE,
321        notes = "",
322        method = "containsValue",
323        args = {java.lang.Object.class}
324    )
325    public void test_containsValueLjava_lang_Object() {
326        // Test for method boolean
327        // java.util.TreeMap.containsValue(java.lang.Object)
328        assertTrue("Returned false for valid value", tm
329                .containsValue(objArray[986]));
330        assertTrue("Returned true for invalid value", !tm
331                .containsValue(new Object()));
332    }
333
334    /**
335     * @tests java.util.TreeMap#entrySet()
336     */
337    @TestTargetNew(
338        level = TestLevel.COMPLETE,
339        notes = "",
340        method = "entrySet",
341        args = {}
342    )
343    public void test_entrySet() {
344        // Test for method java.util.Set java.util.TreeMap.entrySet()
345        Set anEntrySet = tm.entrySet();
346        Iterator entrySetIterator = anEntrySet.iterator();
347        assertTrue("EntrySet is incorrect size",
348                anEntrySet.size() == objArray.length);
349        Map.Entry entry;
350        while (entrySetIterator.hasNext()) {
351            entry = (Map.Entry) entrySetIterator.next();
352            assertTrue("EntrySet does not contain correct mappings", tm
353                    .get(entry.getKey()) == entry.getValue());
354        }
355    }
356
357    /**
358     * @tests java.util.TreeMap#firstKey()
359     */
360    @TestTargetNew(
361        level = TestLevel.COMPLETE,
362        notes = "",
363        method = "firstKey",
364        args = {}
365    )
366    public void test_firstKey() {
367        // Test for method java.lang.Object java.util.TreeMap.firstKey()
368        assertEquals("Returned incorrect first key", "0", tm.firstKey());
369        tm = new TreeMap();
370        try {
371            tm.firstKey();
372            fail("NoSuchElementException expected");
373        } catch (NoSuchElementException e) {
374            //expected
375        }
376    }
377
378    /**
379     * @tests java.util.TreeMap#get(java.lang.Object)
380     */
381    @TestTargetNew(
382        level = TestLevel.COMPLETE,
383        notes = "",
384        method = "get",
385        args = {java.lang.Object.class}
386    )
387    public void test_getLjava_lang_Object() {
388        // Test for method java.lang.Object
389        // java.util.TreeMap.get(java.lang.Object)
390        Object o = new Object();
391        tm.put("Hello", o);
392        assertTrue("Failed to get mapping", tm.get("Hello") == o);
393
394        try {
395            tm.get(new Double(3.14));
396            fail("ClassCastException expected");
397        } catch (ClassCastException e) {
398            //expected
399        }
400
401        try {
402            tm.get(null);
403            fail("NullPointerException expected");
404        } catch (NullPointerException e) {
405            //expected
406        }
407    }
408
409    /**
410     * @tests java.util.TreeMap#headMap(java.lang.Object)
411     */
412    @TestTargetNew(
413        level = TestLevel.COMPLETE,
414        notes = "",
415        method = "headMap",
416        args = {java.lang.Object.class}
417    )
418    public void test_headMapLjava_lang_Object() {
419        // Test for method java.util.SortedMap
420        // java.util.TreeMap.headMap(java.lang.Object)
421        Map head = tm.headMap("100");
422        assertEquals("Returned map of incorrect size", 3, head.size());
423        assertTrue("Returned incorrect elements", head.containsKey("0")
424                && head.containsValue(new Integer("1"))
425                && head.containsKey("10"));
426        SortedMap sort = tm.headMap("100");
427        try {
428            sort.headMap("50");
429            fail("IllegalArgumentException expected");
430        } catch (IllegalArgumentException e) {
431            //expected
432        }
433
434        try {
435            tm.headMap(this);
436            fail("ClassCastException expected");
437        } catch (ClassCastException e) {
438            //expected
439        }
440
441        try {
442            tm.headMap(null);
443            fail("NullPointerException expected");
444        } catch (NullPointerException e) {
445            //expected
446        }
447
448        // Regression for Harmony-1026
449        TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(
450                new MockComparator());
451        map.put(1, 2.1);
452        map.put(2, 3.1);
453        map.put(3, 4.5);
454        map.put(7, 21.3);
455        map.put(null, null);
456
457        SortedMap<Integer, Double> smap = map.headMap(null);
458        assertEquals(0, smap.size());
459
460        Set<Integer> keySet = smap.keySet();
461        assertEquals(0, keySet.size());
462
463        Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet();
464        assertEquals(0, entrySet.size());
465
466        Collection<Double> valueCollection = smap.values();
467        assertEquals(0, valueCollection.size());
468
469        // Regression for Harmony-1066
470        assertTrue(head instanceof Serializable);
471
472        // Regression for ill-behaved collator
473        Collator c = new Collator() {
474            @Override
475            public int compare(String o1, String o2) {
476                if (o1 == null) {
477                    return 0;
478                }
479                return o1.compareTo(o2);
480            }
481
482            @Override
483            public CollationKey getCollationKey(String string) {
484                return null;
485            }
486
487            @Override
488            public int hashCode() {
489                return 0;
490            }
491        };
492
493        TreeMap<String, String> treemap = new TreeMap<String, String>(c);
494        assertEquals(0, treemap.headMap(null).size());
495    }
496
497    /**
498     * @tests java.util.TreeMap#keySet()
499     */
500    @TestTargetNew(
501        level = TestLevel.COMPLETE,
502        notes = "",
503        method = "keySet",
504        args = {}
505    )
506    public void test_keySet() {
507        // Test for method java.util.Set java.util.TreeMap.keySet()
508        Set ks = tm.keySet();
509        assertTrue("Returned set of incorrect size",
510                ks.size() == objArray.length);
511        for (int i = 0; i < tm.size(); i++) {
512            assertTrue("Returned set is missing keys", ks.contains(new Integer(
513                    i).toString()));
514        }
515    }
516
517    /**
518     * @tests java.util.TreeMap#lastKey()
519     */
520    @TestTargetNew(
521        level = TestLevel.COMPLETE,
522        notes = "",
523        method = "lastKey",
524        args = {}
525    )
526    public void test_lastKey() {
527        // Test for method java.lang.Object java.util.TreeMap.lastKey()
528        assertTrue("Returned incorrect last key", tm.lastKey().equals(
529                objArray[objArray.length - 1].toString()));
530        tm = new TreeMap();
531        try {
532            tm.lastKey();
533            fail("NoSuchElementException expected");
534        } catch (NoSuchElementException e) {
535            //expected
536        }
537    }
538
539    /**
540     * @tests java.util.TreeMap#put(java.lang.Object, java.lang.Object)
541     */
542    @TestTargetNew(
543        level = TestLevel.COMPLETE,
544        notes = "",
545        method = "put",
546        args = {java.lang.Object.class, java.lang.Object.class}
547    )
548    public void test_putLjava_lang_ObjectLjava_lang_Object() {
549        // Test for method java.lang.Object
550        // java.util.TreeMap.put(java.lang.Object, java.lang.Object)
551        Object o = new Object();
552        tm.put("Hello", o);
553        assertTrue("Failed to put mapping", tm.get("Hello") == o);
554
555        try {
556            tm.put(null, "null");
557            fail("NullPointerException expected");
558        } catch (NullPointerException e) {
559            //expected
560        }
561
562        // regression for Harmony-780
563        tm = new TreeMap();
564        assertNull(tm.put(new Object(), new Object()));
565        try {
566            tm.put(new Integer(1), new Object());
567            fail("should throw ClassCastException");
568        } catch (ClassCastException e) {
569            // expected
570        }
571
572        tm = new TreeMap();
573        assertNull(tm.put(new Integer(1), new Object()));
574
575        // regression for Harmony-2474
576        tm = new TreeMap();
577        tm.remove(o);
578    }
579
580    /**
581     * @tests java.util.TreeMap#putAll(java.util.Map)
582     */
583    @TestTargetNew(
584        level = TestLevel.COMPLETE,
585        notes = "",
586        method = "putAll",
587        args = {java.util.Map.class}
588    )
589    public void test_putAllLjava_util_Map() {
590        // Test for method void java.util.TreeMap.putAll(java.util.Map)
591        TreeMap x = new TreeMap();
592        x.putAll(tm);
593        assertTrue("Map incorrect size after put", x.size() == tm.size());
594        for (Object element : objArray) {
595            assertTrue("Failed to put all elements", x.get(element.toString())
596                    .equals(element));
597        }
598        x = new TreeMap();
599        x.put(new Integer(1), "one");
600        x.put(new Integer(2), "two");
601
602        try {
603            tm.putAll(x);
604            fail("ClassCastException expected");
605        } catch (ClassCastException e) {
606            //expected
607        }
608
609        try {
610            tm.putAll(null);
611            fail("NullPointerException expected");
612        } catch (NullPointerException e) {
613            //expected
614        }
615    }
616
617    /**
618     * @tests java.util.TreeMap#remove(java.lang.Object)
619     */
620    @TestTargetNew(
621        level = TestLevel.COMPLETE,
622        notes = "",
623        method = "remove",
624        args = {java.lang.Object.class}
625    )
626    public void test_removeLjava_lang_Object() {
627        // Test for method java.lang.Object
628        // java.util.TreeMap.remove(java.lang.Object)
629        tm.remove("990");
630        assertTrue("Failed to remove mapping", !tm.containsKey("990"));
631
632        try {
633            tm.remove(new Double(3.14));
634            fail("ClassCastException expected");
635        } catch (ClassCastException e) {
636            //expected
637        }
638
639        try {
640            tm.remove(null);
641            fail("NullPointerException expected");
642        } catch (NullPointerException e) {
643            //expected
644        }
645    }
646
647    /**
648     * @tests java.util.TreeMap#size()
649     */
650    @TestTargetNew(
651        level = TestLevel.COMPLETE,
652        notes = "",
653        method = "size",
654        args = {}
655    )
656    public void test_size() {
657        // Test for method int java.util.TreeMap.size()
658        assertEquals("Returned incorrect size", 1000, tm.size());
659    }
660
661    /**
662     * @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
663     */
664    @TestTargetNew(
665        level = TestLevel.COMPLETE,
666        notes = "",
667        method = "subMap",
668        args = {java.lang.Object.class, java.lang.Object.class}
669    )
670    public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
671        // Test for method java.util.SortedMap
672        // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
673        SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
674                .toString());
675        assertEquals("subMap is of incorrect size", 9, subMap.size());
676        for (int counter = 100; counter < 109; counter++) {
677            assertTrue("SubMap contains incorrect elements", subMap.get(
678                    objArray[counter].toString()).equals(objArray[counter]));
679        }
680
681        try {
682            tm.subMap(objArray[9].toString(), objArray[1].toString());
683            fail("end key less than start key should throw IllegalArgumentException");
684        } catch (IllegalArgumentException e) {
685            // Expected
686        }
687
688        // Regression for Harmony-1161
689        TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
690                new MockComparatorNullTolerable());
691        treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
692        treeMapWithNull.put(null, "value2"); //$NON-NLS-1$
693        SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
694                "key1"); //$NON-NLS-1$
695        assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$
696
697        // Regression test for typo in lastKey method
698        SortedMap<String, String> map = new TreeMap<String, String>();
699        map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$
700        map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$
701        map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$
702        assertEquals("3", map.lastKey());
703        SortedMap<String, String> sub = map.subMap("1", "3"); //$NON-NLS-1$ //$NON-NLS-2$
704        assertEquals("2", sub.lastKey()); //$NON-NLS-1$
705
706        try {
707            tm.subMap(this, this);
708            fail("ClassCastException expected");
709        } catch (ClassCastException e) {
710            //expected
711        }
712
713        try {
714            tm.subMap(objArray[9].toString(), null);
715            fail("NullPointerException expected");
716        } catch (NullPointerException e) {
717            //expected
718        }
719
720        try {
721            tm.subMap(null, objArray[9].toString());
722            fail("NullPointerException expected");
723        } catch (NullPointerException e) {
724            //expected
725        }
726    }
727
728    /**
729     * @tests java.util.TreeMap#tailMap(java.lang.Object)
730     */
731    @TestTargetNew(
732        level = TestLevel.COMPLETE,
733        notes = "",
734        method = "tailMap",
735        args = {java.lang.Object.class}
736    )
737    public void test_tailMapLjava_lang_Object() {
738        // Test for method java.util.SortedMap
739        // java.util.TreeMap.tailMap(java.lang.Object)
740        Map tail = tm.tailMap(objArray[900].toString());
741        assertTrue("Returned map of incorrect size : " + tail.size(), tail
742                .size() == (objArray.length - 900) + 9);
743        for (int i = 900; i < objArray.length; i++) {
744            assertTrue("Map contains incorrect entries", tail
745                    .containsValue(objArray[i]));
746        }
747
748        SortedMap sort = tm.tailMap("99");
749
750        try {
751            sort.tailMap("101");
752            fail("IllegalArgumentException expected");
753        } catch (IllegalArgumentException e) {
754            //expected
755        }
756
757        try {
758            tm.tailMap(this);
759            fail("ClassCastException expected");
760        } catch (ClassCastException e) {
761            //expected
762        }
763
764        try {
765            tm.tailMap(null);
766            fail("NullPointerException expected");
767        } catch (NullPointerException e) {
768            //expected
769        }
770
771        // Regression for Harmony-1066
772        assertTrue(tail instanceof Serializable);
773    }
774
775    /**
776     * @tests java.util.TreeMap#values()
777     */
778    @TestTargetNew(
779        level = TestLevel.COMPLETE,
780        notes = "",
781        method = "values",
782        args = {}
783    )
784    public void test_values() {
785        // Test for method java.util.Collection java.util.TreeMap.values()
786        Collection vals = tm.values();
787        vals.iterator();
788        assertTrue("Returned collection of incorrect size",
789                vals.size() == objArray.length);
790        for (Object element : objArray) {
791            assertTrue("Collection contains incorrect elements", vals
792                    .contains(element));
793        }
794
795        TreeMap myTreeMap = new TreeMap();
796        for (int i = 0; i < 100; i++) {
797            myTreeMap.put(objArray[i], objArray[i]);
798        }
799        Collection values = myTreeMap.values();
800        new Support_UnmodifiableCollectionTest(
801                "Test Returned Collection From TreeMap.values()", values)
802                .runTest();
803        values.remove(new Integer(0));
804        assertTrue(
805                "Removing from the values collection should remove from the original map",
806                !myTreeMap.containsValue(new Integer(0)));
807    }
808
809    /**
810     * @tests java.util.TreeMap#SerializationTest()
811     */
812    @TestTargetNew(
813        level = TestLevel.COMPLETE,
814        notes = "Verifies serialization/deserialization.",
815        method = "!SerializationSelf",
816        args = {}
817    )
818    @AndroidOnly("fail on RI. See comment below")
819    // Regression for Harmony-1066
820    public void test_SubMap_Serializable() throws Exception {
821        TreeMap<Integer, Double> map = new TreeMap<Integer, Double>();
822        map.put(1, 2.1);
823        map.put(2, 3.1);
824        map.put(3, 4.5);
825        map.put(7, 21.3);
826        SortedMap<Integer, Double> headMap = map.headMap(3);
827        assertTrue(headMap instanceof Serializable);
828        assertFalse(headMap instanceof TreeMap);
829        assertTrue(headMap instanceof SortedMap);
830
831        assertFalse(headMap.entrySet() instanceof Serializable);
832        assertFalse(headMap.keySet() instanceof Serializable);
833        assertFalse(headMap.values() instanceof Serializable);
834
835        // This assertion will fail on RI. This is a bug of RI.
836        SerializationTest.verifySelf(headMap);
837    }
838
839    /**
840     * Tests equals() method.
841     * Tests that no ClassCastException will be thrown in all cases.
842     * Regression test for HARMONY-1639.
843     */
844    @TestTargetNew(
845        level = TestLevel.COMPLETE,
846        notes = "",
847        method = "equals",
848        args = {java.lang.Object.class}
849    )
850    public void test_equals() throws Exception {
851        // comparing TreeMaps with different object types
852        Map m1 = new TreeMap();
853        Map m2 = new TreeMap();
854        m1.put("key1", "val1");
855        m1.put("key2", "val2");
856        m2.put(new Integer(1), "val1");
857        m2.put(new Integer(2), "val2");
858        assertFalse("Maps should not be equal 1", m1.equals(m2));
859        assertFalse("Maps should not be equal 2", m2.equals(m1));
860
861        // comparing TreeMap with HashMap
862        m1 = new TreeMap();
863        m2 = new HashMap();
864        m1.put("key", "val");
865        m2.put(new Object(), "val");
866        assertFalse("Maps should not be equal 3", m1.equals(m2));
867        assertFalse("Maps should not be equal 4", m2.equals(m1));
868
869        // comparing TreeMaps with not-comparable objects inside
870        m1 = new TreeMap();
871        m2 = new TreeMap();
872        m1.put(new Object(), "val1");
873        m2.put(new Object(), "val1");
874        assertFalse("Maps should not be equal 5", m1.equals(m2));
875        assertFalse("Maps should not be equal 6", m2.equals(m1));
876    }
877
878    /**
879     * Sets up the fixture, for example, open a network connection. This method
880     * is called before a test is executed.
881     */
882    @Override
883    protected void setUp() {
884        tm = new TreeMap();
885        for (int i = 0; i < objArray.length; i++) {
886            Object x = objArray[i] = new Integer(i);
887            tm.put(x.toString(), x);
888        }
889    }
890
891}
892