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 libcore.java.util;
19
20import java.io.Serializable;
21import java.text.CollationKey;
22import java.text.Collator;
23import java.util.Collection;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.Map;
27import java.util.NoSuchElementException;
28import java.util.Set;
29import java.util.SortedMap;
30import java.util.TreeMap;
31import tests.support.Support_MapTest2;
32
33public class OldTreeMapTest extends junit.framework.TestCase {
34
35    public static class ReversedComparator implements Comparator {
36        public int compare(Object o1, Object o2) {
37            return -(((Comparable) o1).compareTo(o2));
38        }
39
40        public boolean equals(Object o1, Object o2) {
41            return (((Comparable) o1).compareTo(o2)) == 0;
42        }
43    }
44
45    // Regression for Harmony-1026
46    public static class MockComparator<T extends Comparable<T>> implements
47            Comparator<T>, Serializable {
48
49        public int compare(T o1, T o2) {
50            if (o1 == o2) {
51                return 0;
52            }
53            if (null == o1 || null == o2) {
54                return -1;
55            }
56            T c1 = o1;
57            T c2 = o2;
58            return c1.compareTo(c2);
59        }
60    }
61
62    // Regression for Harmony-1161
63    class MockComparatorNullTolerable implements Comparator<String> {
64
65        public int compare(String o1, String o2) {
66            if (o1 == o2) {
67                return 0;
68            }
69            if (null == o1) {
70                return -1;
71            }
72            if (null == o2) {
73                return 1;
74            }
75            return o1.compareTo(o2);
76        }
77    }
78
79    TreeMap tm;
80
81    Object objArray[] = new Object[1000];
82
83    public void test_Constructor() {
84        // Test for method java.util.TreeMap()
85        new Support_MapTest2(new TreeMap()).runTest();
86
87        assertTrue("New treeMap non-empty", new TreeMap().isEmpty());
88    }
89
90    public void test_ConstructorLjava_util_Comparator() {
91        // Test for method java.util.TreeMap(java.util.Comparator)
92        Comparator comp = new ReversedComparator();
93        TreeMap reversedTreeMap = new TreeMap(comp);
94        assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
95                .comparator() == comp);
96        reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
97        reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
98        assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
99                reversedTreeMap.firstKey().equals(new Integer(2).toString()));
100        assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
101                reversedTreeMap.lastKey().equals(new Integer(1).toString()));
102
103    }
104
105    public void test_ConstructorLjava_util_Map() {
106        // Test for method java.util.TreeMap(java.util.Map)
107        TreeMap myTreeMap = new TreeMap(new HashMap(tm));
108        assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length);
109        for (Object element : objArray) {
110            assertTrue("Map has incorrect mappings", myTreeMap.get(
111                    element.toString()).equals(element));
112        }
113
114        HashMap hm = new HashMap();
115        hm.put(new Integer(1), "one");
116        hm.put("one", new Integer(1));
117
118        try {
119            new TreeMap(hm);
120            fail("ClassCastException expected");
121        } catch (ClassCastException e) {
122            //expected
123        }
124
125        try {
126            new TreeMap((Map)null);
127            fail("NullPointerException expected");
128        } catch (NullPointerException e) {
129            //expected
130        }
131    }
132
133    public void test_ConstructorLjava_util_SortedMap() {
134        // Test for method java.util.TreeMap(java.util.SortedMap)
135        Comparator comp = new ReversedComparator();
136        TreeMap reversedTreeMap = new TreeMap(comp);
137        reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
138        reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
139        TreeMap anotherTreeMap = new TreeMap(reversedTreeMap);
140        assertTrue("New tree map does not answer correct comparator",
141                anotherTreeMap.comparator() == comp);
142        assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
143                anotherTreeMap.firstKey().equals(new Integer(2).toString()));
144        assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
145                anotherTreeMap.lastKey().equals(new Integer(1).toString()));
146
147        try {
148            new TreeMap((SortedMap)null);
149            fail("NullPointerException expected");
150        } catch (NullPointerException e) {
151            //expected
152        }
153    }
154
155    public void test_containsKeyLjava_lang_Object() {
156        // Test for method boolean
157        // java.util.TreeMap.containsKey(java.lang.Object)
158        assertTrue("Returned false for valid key", tm.containsKey("95"));
159        assertTrue("Returned true for invalid key", !tm.containsKey("XXXXX"));
160
161        try {
162            tm.containsKey(new Double(3.14));
163            fail("ClassCastException expected");
164        } catch (ClassCastException e) {
165            //expected
166        }
167
168        try {
169            tm.containsKey(null);
170            fail("NullPointerException expected");
171        } catch (NullPointerException e) {
172            //expected
173        }
174    }
175
176    public void test_firstKey() {
177        // Test for method java.lang.Object java.util.TreeMap.firstKey()
178        assertEquals("Returned incorrect first key", "0", tm.firstKey());
179        tm = new TreeMap();
180        try {
181            tm.firstKey();
182            fail("NoSuchElementException expected");
183        } catch (NoSuchElementException e) {
184            //expected
185        }
186    }
187
188    public void test_getLjava_lang_Object() {
189        // Test for method java.lang.Object
190        // java.util.TreeMap.get(java.lang.Object)
191        Object o = new Object();
192        tm.put("Hello", o);
193        assertTrue("Failed to get mapping", tm.get("Hello") == o);
194
195        try {
196            tm.get(new Double(3.14));
197            fail("ClassCastException expected");
198        } catch (ClassCastException e) {
199            //expected
200        }
201
202        try {
203            tm.get(null);
204            fail("NullPointerException expected");
205        } catch (NullPointerException e) {
206            //expected
207        }
208    }
209
210    public void test_headMapLjava_lang_Object() {
211        // Test for method java.util.SortedMap
212        // java.util.TreeMap.headMap(java.lang.Object)
213        Map head = tm.headMap("100");
214        assertEquals("Returned map of incorrect size", 3, head.size());
215        assertTrue("Returned incorrect elements", head.containsKey("0")
216                && head.containsValue(new Integer("1"))
217                && head.containsKey("10"));
218        SortedMap sort = tm.headMap("100");
219        try {
220            sort.headMap("50");
221            fail("IllegalArgumentException expected");
222        } catch (IllegalArgumentException e) {
223            //expected
224        }
225
226        try {
227            tm.headMap(this);
228            fail("ClassCastException expected");
229        } catch (ClassCastException e) {
230            //expected
231        }
232
233        try {
234            tm.headMap(null);
235            fail("NullPointerException expected");
236        } catch (NullPointerException e) {
237            //expected
238        }
239
240        // Regression for Harmony-1026
241        TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(
242                new MockComparator());
243        map.put(1, 2.1);
244        map.put(2, 3.1);
245        map.put(3, 4.5);
246        map.put(7, 21.3);
247        map.put(null, null);
248
249        SortedMap<Integer, Double> smap = map.headMap(null);
250        assertEquals(0, smap.size());
251
252        Set<Integer> keySet = smap.keySet();
253        assertEquals(0, keySet.size());
254
255        Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet();
256        assertEquals(0, entrySet.size());
257
258        Collection<Double> valueCollection = smap.values();
259        assertEquals(0, valueCollection.size());
260
261        // Regression for Harmony-1066
262        assertTrue(head instanceof Serializable);
263
264        // Regression for ill-behaved collator
265        Collator c = new Collator() {
266            @Override
267            public int compare(String o1, String o2) {
268                if (o1 == null) {
269                    return 0;
270                }
271                return o1.compareTo(o2);
272            }
273
274            @Override
275            public CollationKey getCollationKey(String string) {
276                return null;
277            }
278
279            @Override
280            public int hashCode() {
281                return 0;
282            }
283        };
284
285        TreeMap<String, String> treemap = new TreeMap<String, String>(c);
286        assertEquals(0, treemap.headMap(null).size());
287    }
288
289    public void test_lastKey() {
290        // Test for method java.lang.Object java.util.TreeMap.lastKey()
291        assertTrue("Returned incorrect last key", tm.lastKey().equals(
292                objArray[objArray.length - 1].toString()));
293        tm = new TreeMap();
294        try {
295            tm.lastKey();
296            fail("NoSuchElementException expected");
297        } catch (NoSuchElementException e) {
298            //expected
299        }
300    }
301
302    public void test_putLjava_lang_ObjectLjava_lang_Object() {
303        // Test for method java.lang.Object
304        // java.util.TreeMap.put(java.lang.Object, java.lang.Object)
305        Object o = new Object();
306        tm.put("Hello", o);
307        assertTrue("Failed to put mapping", tm.get("Hello") == o);
308
309        try {
310            tm.put(null, "null");
311            fail("NullPointerException expected");
312        } catch (NullPointerException e) {
313            //expected
314        }
315
316        // regression for Harmony-780
317        tm = new TreeMap();
318        try {
319            assertNull(tm.put(new Object(), new Object()));
320            tm.put(new Integer(1), new Object());
321            fail("should throw ClassCastException");
322        } catch (ClassCastException e) {
323            // expected
324        }
325
326        tm = new TreeMap();
327        assertNull(tm.put(new Integer(1), new Object()));
328    }
329
330     /**
331      * Android's TreeMap requires every element to be comparable, even if
332      * there's no other element to compare it to. This is more strict than the
333      * RI. See Harmony-2474.
334      */
335    public void testRemoveNonComparableFromEmptyMap() {
336        tm = new TreeMap();
337        try {
338            tm.remove(new Object()); // succeeds on RI
339        } catch (ClassCastException expected) {
340            // expected on libcore only
341        }
342    }
343
344    public void test_putAllLjava_util_Map() {
345        // Test for method void java.util.TreeMap.putAll(java.util.Map)
346        TreeMap x = new TreeMap();
347        x.putAll(tm);
348        assertTrue("Map incorrect size after put", x.size() == tm.size());
349        for (Object element : objArray) {
350            assertTrue("Failed to put all elements", x.get(element.toString())
351                    .equals(element));
352        }
353        x = new TreeMap();
354        x.put(new Integer(1), "one");
355        x.put(new Integer(2), "two");
356
357        try {
358            tm.putAll(x);
359            fail("ClassCastException expected");
360        } catch (ClassCastException e) {
361            //expected
362        }
363
364        try {
365            tm.putAll(null);
366            fail("NullPointerException expected");
367        } catch (NullPointerException e) {
368            //expected
369        }
370    }
371
372    public void test_removeLjava_lang_Object() {
373        // Test for method java.lang.Object
374        // java.util.TreeMap.remove(java.lang.Object)
375        tm.remove("990");
376        assertTrue("Failed to remove mapping", !tm.containsKey("990"));
377
378        try {
379            tm.remove(new Double(3.14));
380            fail("ClassCastException expected");
381        } catch (ClassCastException e) {
382            //expected
383        }
384
385        try {
386            tm.remove(null);
387            fail("NullPointerException expected");
388        } catch (NullPointerException e) {
389            //expected
390        }
391    }
392
393    public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
394        // Test for method java.util.SortedMap
395        // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
396        SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
397                .toString());
398        assertEquals("subMap is of incorrect size", 9, subMap.size());
399        for (int counter = 100; counter < 109; counter++) {
400            assertTrue("SubMap contains incorrect elements", subMap.get(
401                    objArray[counter].toString()).equals(objArray[counter]));
402        }
403
404        try {
405            tm.subMap(objArray[9].toString(), objArray[1].toString());
406            fail("end key less than start key should throw IllegalArgumentException");
407        } catch (IllegalArgumentException e) {
408            // Expected
409        }
410
411        // Regression for Harmony-1161
412        TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
413                new MockComparatorNullTolerable());
414        treeMapWithNull.put("key1", "value1");
415        treeMapWithNull.put(null, "value2");
416        SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
417                "key1");
418        assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size());
419
420        // Regression test for typo in lastKey method
421        SortedMap<String, String> map = new TreeMap<String, String>();
422        map.put("1", "one");
423        map.put("2", "two");
424        map.put("3", "three");
425        assertEquals("3", map.lastKey());
426        SortedMap<String, String> sub = map.subMap("1", "3");
427        assertEquals("2", sub.lastKey());
428
429        try {
430            tm.subMap(this, this);
431            fail("ClassCastException expected");
432        } catch (ClassCastException e) {
433            //expected
434        }
435
436        try {
437            tm.subMap(objArray[9].toString(), null);
438            fail("NullPointerException expected");
439        } catch (NullPointerException e) {
440            //expected
441        }
442
443        try {
444            tm.subMap(null, objArray[9].toString());
445            fail("NullPointerException expected");
446        } catch (NullPointerException e) {
447            //expected
448        }
449    }
450
451    public void test_tailMapLjava_lang_Object() {
452        // Test for method java.util.SortedMap
453        // java.util.TreeMap.tailMap(java.lang.Object)
454        Map tail = tm.tailMap(objArray[900].toString());
455        assertTrue("Returned map of incorrect size : " + tail.size(), tail
456                .size() == (objArray.length - 900) + 9);
457        for (int i = 900; i < objArray.length; i++) {
458            assertTrue("Map contains incorrect entries", tail
459                    .containsValue(objArray[i]));
460        }
461
462        SortedMap sort = tm.tailMap("99");
463
464        try {
465            sort.tailMap("101");
466            fail("IllegalArgumentException expected");
467        } catch (IllegalArgumentException e) {
468            //expected
469        }
470
471        try {
472            tm.tailMap(this);
473            fail("ClassCastException expected");
474        } catch (ClassCastException e) {
475            //expected
476        }
477
478        try {
479            tm.tailMap(null);
480            fail("NullPointerException expected");
481        } catch (NullPointerException e) {
482            //expected
483        }
484
485        // Regression for Harmony-1066
486        assertTrue(tail instanceof Serializable);
487    }
488    /**
489     * Sets up the fixture, for example, open a network connection. This method
490     * is called before a test is executed.
491     */
492    @Override
493    protected void setUp() {
494        tm = new TreeMap();
495        for (int i = 0; i < objArray.length; i++) {
496            Object x = objArray[i] = new Integer(i);
497            tm.put(x.toString(), x);
498        }
499    }
500
501}
502