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,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.AbstractMap;
021: import java.util.AbstractSet;
022: import java.util.Collections;
023: import java.util.Comparator;
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.Hashtable;
027: import java.util.IdentityHashMap;
028: import java.util.Iterator;
029: import java.util.LinkedHashMap;
030: import java.util.Map;
031: import java.util.Set;
032: import java.util.TreeMap;
033: import java.util.Vector;
034: import java.util.WeakHashMap;
035:
036: public class AbstractMapTest extends junit.framework.TestCase {
037:
038: static final String specialKey = "specialKey".intern();
039:
040: static final String specialValue = "specialValue".intern();
041:
042: // The impl of MyMap is not realistic, but serves to create a type
043: // that uses the default remove behavior.
044: class MyMap extends AbstractMap {
045: final Set mySet = new HashSet(1);
046:
047: MyMap() {
048: mySet.add(new Map.Entry() {
049: public Object getKey() {
050: return specialKey;
051: }
052:
053: public Object getValue() {
054: return specialValue;
055: }
056:
057: public Object setValue(Object object) {
058: return null;
059: }
060: });
061: }
062:
063: public Object put(Object key, Object value) {
064: return null;
065: }
066:
067: public Set entrySet() {
068: return mySet;
069: }
070: }
071:
072: /**
073: * @tests java.util.AbstractMap#keySet()
074: */
075: public void test_keySet() {
076: AbstractMap map1 = new HashMap(0);
077: assertSame("HashMap(0)", map1.keySet(), map1.keySet());
078:
079: AbstractMap map2 = new HashMap(10);
080: assertSame("HashMap(10)", map2.keySet(), map2.keySet());
081:
082: Map map3 = Collections.EMPTY_MAP;
083: assertSame("EMPTY_MAP", map3.keySet(), map3.keySet());
084:
085: AbstractMap map4 = new IdentityHashMap(1);
086: assertSame("IdentityHashMap", map4.keySet(), map4.keySet());
087:
088: AbstractMap map5 = new LinkedHashMap(122);
089: assertSame("LinkedHashMap", map5.keySet(), map5.keySet());
090:
091: AbstractMap map6 = new TreeMap();
092: assertSame("TreeMap", map6.keySet(), map6.keySet());
093:
094: AbstractMap map7 = new WeakHashMap();
095: assertSame("WeakHashMap", map7.keySet(), map7.keySet());
096: }
097:
098: /**
099: * @tests java.util.AbstractMap#remove(java.lang.Object)
100: */
101: public void test_removeLjava_lang_Object() {
102: Object key = new Object();
103: Object value = new Object();
104:
105: AbstractMap map1 = new HashMap(0);
106: map1.put("key", value);
107: assertSame("HashMap(0)", map1.remove("key"), value);
108:
109: AbstractMap map4 = new IdentityHashMap(1);
110: map4.put(key, value);
111: assertSame("IdentityHashMap", map4.remove(key), value);
112:
113: AbstractMap map5 = new LinkedHashMap(122);
114: map5.put(key, value);
115: assertSame("LinkedHashMap", map5.remove(key), value);
116:
117: AbstractMap map6 = new TreeMap(new Comparator() {
118: // Bogus comparator
119: public int compare(Object object1, Object object2) {
120: return 0;
121: }
122: });
123: map6.put(key, value);
124: assertSame("TreeMap", map6.remove(key), value);
125:
126: AbstractMap map7 = new WeakHashMap();
127: map7.put(key, value);
128: assertSame("WeakHashMap", map7.remove(key), value);
129:
130: AbstractMap aSpecialMap = new MyMap();
131: aSpecialMap.put(specialKey, specialValue);
132: Object valueOut = aSpecialMap.remove(specialKey);
133: assertSame("MyMap", valueOut, specialValue);
134: }
135:
136: /**
137: * @tests java.util.AbstractMap#values()
138: */
139: public void test_values() {
140: AbstractMap map1 = new HashMap(0);
141: assertSame("HashMap(0)", map1.values(), map1.values());
142:
143: AbstractMap map2 = new HashMap(10);
144: assertSame("HashMap(10)", map2.values(), map2.values());
145:
146: Map map3 = Collections.EMPTY_MAP;
147: assertSame("EMPTY_MAP", map3.values(), map3.values());
148:
149: AbstractMap map4 = new IdentityHashMap(1);
150: assertSame("IdentityHashMap", map4.values(), map4.values());
151:
152: AbstractMap map5 = new LinkedHashMap(122);
153: assertSame("IdentityHashMap", map5.values(), map5.values());
154:
155: AbstractMap map6 = new TreeMap();
156: assertSame("TreeMap", map6.values(), map6.values());
157:
158: AbstractMap map7 = new WeakHashMap();
159: assertSame("WeakHashMap", map7.values(), map7.values());
160: }
161:
162: /**
163: * @tests java.util.AbstractMap#clone()
164: */
165: public void test_clone() {
166: class MyMap extends AbstractMap implements Cloneable {
167: private Map map = new HashMap();
168:
169: public Set entrySet() {
170: return map.entrySet();
171: }
172:
173: public Object put(Object key, Object value) {
174: return map.put(key, value);
175: }
176:
177: public Map getMap() {
178: return map;
179: }
180:
181: public Object clone() {
182: try {
183: return super .clone();
184: } catch (CloneNotSupportedException e) {
185: return null;
186: }
187: }
188: }
189: ;
190: MyMap map = new MyMap();
191: map.put("one", "1");
192: Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
193: assertTrue("entry not added", entry.getKey() == "one"
194: && entry.getValue() == "1");
195: MyMap mapClone = (MyMap) map.clone();
196: assertTrue("clone not shallow", map.getMap() == mapClone
197: .getMap());
198: }
199:
200: public class AMT extends AbstractMap {
201:
202: // Very crude AbstractMap implementation
203: Vector values = new Vector();
204:
205: Vector keys = new Vector();
206:
207: public Set entrySet() {
208: return new AbstractSet() {
209: public Iterator iterator() {
210: return new Iterator() {
211: int index = 0;
212:
213: public boolean hasNext() {
214: return index < values.size();
215: }
216:
217: public Object next() {
218: if (index < values.size()) {
219: Map.Entry me = new Map.Entry() {
220: Object v = values.elementAt(index);
221:
222: Object k = keys.elementAt(index);
223:
224: public Object getKey() {
225: return k;
226: }
227:
228: public Object getValue() {
229: return v;
230: }
231:
232: public Object setValue(Object value) {
233: return null;
234: }
235: };
236: index++;
237: return me;
238: }
239: return null;
240: }
241:
242: public void remove() {
243: }
244: };
245: }
246:
247: public int size() {
248: return values.size();
249: }
250: };
251: }
252:
253: public Object put(Object k, Object v) {
254: keys.add(k);
255: values.add(v);
256: return v;
257: }
258: }
259:
260: /**
261: * @tests {@link java.util.AbstractMap#putAll(Map)}
262: */
263: public void test_putAllLMap() {
264: Hashtable ht = new Hashtable();
265: AMT amt = new AMT();
266: ht.put("this", "that");
267: amt.putAll(ht);
268: assertEquals("Should be equal", amt, ht);
269: }
270:
271: protected void setUp() {
272: }
273:
274: protected void tearDown() {
275: }
276: }
|