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