AbstractMapTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
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 org.apache.harmony.luni.tests.java.util;
19
20import java.util.AbstractMap;
21import java.util.Collections;
22import java.util.Comparator;
23import java.util.HashMap;
24import java.util.HashSet;
25import java.util.IdentityHashMap;
26import java.util.LinkedHashMap;
27import java.util.Map;
28import java.util.Set;
29import java.util.TreeMap;
30import java.util.WeakHashMap;
31
32public class AbstractMapTest extends junit.framework.TestCase {
33
34    static final String specialKey = "specialKey".intern();
35
36    static final String specialValue = "specialValue".intern();
37
38    // The impl of MyMap is not realistic, but serves to create a type
39    // that uses the default remove behavior.
40    class MyMap extends AbstractMap {
41        final Set mySet = new HashSet(1);
42
43        MyMap() {
44            mySet.add(new Map.Entry() {
45                public Object getKey() {
46                    return specialKey;
47                }
48
49                public Object getValue() {
50                    return specialValue;
51                }
52
53                public Object setValue(Object object) {
54                    return null;
55                }
56            });
57        }
58
59        public Object put(Object key, Object value) {
60            return null;
61        }
62
63        public Set entrySet() {
64            return mySet;
65        }
66    }
67
68    /**
69     * @tests java.util.AbstractMap#keySet()
70     */
71    public void test_keySet() {
72        AbstractMap map1 = new HashMap(0);
73        assertSame("HashMap(0)", map1.keySet(), map1.keySet());
74
75        AbstractMap map2 = new HashMap(10);
76        assertSame("HashMap(10)", map2.keySet(), map2.keySet());
77
78        Map map3 = Collections.EMPTY_MAP;
79        assertSame("EMPTY_MAP", map3.keySet(), map3.keySet());
80
81        AbstractMap map4 = new IdentityHashMap(1);
82        assertSame("IdentityHashMap", map4.keySet(), map4.keySet());
83
84        AbstractMap map5 = new LinkedHashMap(122);
85        assertSame("LinkedHashMap", map5.keySet(), map5.keySet());
86
87        AbstractMap map6 = new TreeMap();
88        assertSame("TreeMap", map6.keySet(), map6.keySet());
89
90        AbstractMap map7 = new WeakHashMap();
91        assertSame("WeakHashMap", map7.keySet(), map7.keySet());
92    }
93
94    /**
95     * @tests java.util.AbstractMap#remove(java.lang.Object)
96     */
97    public void test_removeLjava_lang_Object() {
98        Object key = new Object();
99        Object value = new Object();
100
101        AbstractMap map1 = new HashMap(0);
102        map1.put("key", value);
103        assertSame("HashMap(0)", map1.remove("key"), value);
104
105        AbstractMap map4 = new IdentityHashMap(1);
106        map4.put(key, value);
107        assertSame("IdentityHashMap", map4.remove(key), value);
108
109        AbstractMap map5 = new LinkedHashMap(122);
110        map5.put(key, value);
111        assertSame("LinkedHashMap", map5.remove(key), value);
112
113        AbstractMap map6 = new TreeMap(new Comparator() {
114            // Bogus comparator
115            public int compare(Object object1, Object object2) {
116                return 0;
117            }
118        });
119        map6.put(key, value);
120        assertSame("TreeMap", map6.remove(key), value);
121
122        AbstractMap map7 = new WeakHashMap();
123        map7.put(key, value);
124        assertSame("WeakHashMap", map7.remove(key), value);
125
126        AbstractMap aSpecialMap = new MyMap();
127        aSpecialMap.put(specialKey, specialValue);
128        Object valueOut = aSpecialMap.remove(specialKey);
129        assertSame("MyMap", valueOut, specialValue);
130    }
131
132    /**
133     * @tests java.util.AbstractMap#values()
134     */
135    public void test_values() {
136        AbstractMap map1 = new HashMap(0);
137        assertSame("HashMap(0)", map1.values(), map1.values());
138
139        AbstractMap map2 = new HashMap(10);
140        assertSame("HashMap(10)", map2.values(), map2.values());
141
142        Map map3 = Collections.EMPTY_MAP;
143        assertSame("EMPTY_MAP", map3.values(), map3.values());
144
145        AbstractMap map4 = new IdentityHashMap(1);
146        assertSame("IdentityHashMap", map4.values(), map4.values());
147
148        AbstractMap map5 = new LinkedHashMap(122);
149        assertSame("IdentityHashMap", map5.values(), map5.values());
150
151        AbstractMap map6 = new TreeMap();
152        assertSame("TreeMap", map6.values(), map6.values());
153
154        AbstractMap map7 = new WeakHashMap();
155        assertSame("WeakHashMap", map7.values(), map7.values());
156    }
157
158    /**
159     * @tests java.util.AbstractMap#clone()
160     */
161    public void test_clone() {
162        class MyMap extends AbstractMap implements Cloneable {
163            private Map map = new HashMap();
164
165            public Set entrySet() {
166                return map.entrySet();
167            }
168
169            public Object put(Object key, Object value) {
170                return map.put(key, value);
171            }
172
173            public Map getMap() {
174                return map;
175            }
176
177            public Object clone() {
178                try {
179                    return super.clone();
180                } catch (CloneNotSupportedException e) {
181                    return null;
182                }
183            }
184        }
185        ;
186        MyMap map = new MyMap();
187        map.put("one", "1");
188        Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
189        assertTrue("entry not added", entry.getKey() == "one"
190                && entry.getValue() == "1");
191        MyMap mapClone = (MyMap) map.clone();
192        assertTrue("clone not shallow", map.getMap() == mapClone.getMap());
193    }
194
195    protected void setUp() {
196    }
197
198    protected void tearDown() {
199    }
200}
201