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 18 package tests.support; 19 20 import java.util.HashSet; 21 import java.util.Iterator; 22 import java.util.Map; 23 import java.util.Set; 24 import junit.framework.TestCase; 25 26 public class Support_UnmodifiableMapTest extends TestCase { 27 28 Map<String, Integer> map; 29 30 // must be a map containing the string keys "0"-"99" paired with the Integer 31 // values Integer(0) to Integer(99) 32 33 public Support_UnmodifiableMapTest(String p1) { 34 super(p1); 35 } 36 37 public Support_UnmodifiableMapTest(String p1, Map<String, Integer> m) { 38 super(p1); 39 map = m; 40 } 41 42 @Override 43 public void runTest() { 44 // containsKey 45 assertTrue("UnmodifiableMapTest - Should contain the key \"0\"", map 46 .containsKey("0")); 47 assertTrue("UnmodifiableMapTest - Should contain the key \"50\"", map 48 .containsKey("50")); 49 assertTrue("UnmodifiableMapTest - Should not contain the key \"100\"", 50 !map.containsKey("100")); 51 52 // containsValue 53 assertTrue("UnmodifiableMapTest - Should contain the value 0", map 54 .containsValue(new Integer(0))); 55 assertTrue("UnmodifiableMapTest - Should contain the value 50", map 56 .containsValue(new Integer(50))); 57 assertTrue("UnmodifiableMapTest - Should not contain value 100", !map 58 .containsValue(new Integer(100))); 59 60 // entrySet 61 Set<?> entrySet = map.entrySet(); 62 Iterator<?> entrySetIterator = entrySet.iterator(); 63 int myCounter = 0; 64 while (entrySetIterator.hasNext()) { 65 Map.Entry<?, ?> me = (Map.Entry<?, ?>) entrySetIterator.next(); 66 assertTrue("UnmodifiableMapTest - Incorrect Map.Entry returned", 67 map.get(me.getKey()).equals(me.getValue())); 68 myCounter++; 69 } 70 assertEquals("UnmodifiableMapTest - Incorrect number of map entries returned", 71 100, myCounter); 72 73 // get 74 assertTrue("UnmodifiableMapTest - getting \"0\" didn't return 0", 75 map.get("0").intValue() == 0); 76 assertTrue("UnmodifiableMapTest - getting \"50\" didn't return 0", 77 map.get("0").intValue() == 0); 78 assertNull("UnmodifiableMapTest - getting \"100\" didn't return null", 79 map.get("100")); 80 81 // isEmpty 82 assertTrue( 83 "UnmodifiableMapTest - should have returned false to isEmpty", 84 !map.isEmpty()); 85 86 // keySet 87 Set<?> keySet = map.keySet(); 88 t_KeySet(keySet); 89 90 // size 91 assertTrue("Size should return 100, returned: " + map.size(), map 92 .size() == 100); 93 94 // values 95 new Support_UnmodifiableCollectionTest("Unmod--from map test", map 96 .values()); 97 98 } 99 100 void t_KeySet(Set<?> keySet) { 101 // keySet should be a set of the strings "0" to "99" 102 103 // contains 104 assertTrue("UnmodifiableMapTest - keySetTest - should contain \"0\"", 105 keySet.contains("0")); 106 assertTrue("UnmodifiableMapTest - keySetTest - should contain \"50\"", 107 keySet.contains("50")); 108 assertTrue( 109 "UnmodifiableMapTest - keySetTest - should not contain \"100\"", 110 !keySet.contains("100")); 111 112 // containsAll 113 HashSet<String> hs = new HashSet<String>(); 114 hs.add("0"); 115 hs.add("25"); 116 hs.add("99"); 117 assertTrue( 118 "UnmodifiableMapTest - keySetTest - should contain set of \"0\", \"25\", and \"99\"", 119 keySet.containsAll(hs)); 120 hs.add("100"); 121 assertTrue( 122 "UnmodifiableMapTest - keySetTest - should not contain set of \"0\", \"25\", \"99\" and \"100\"", 123 !keySet.containsAll(hs)); 124 125 // isEmpty 126 assertTrue("UnmodifiableMapTest - keySetTest - should not be empty", 127 !keySet.isEmpty()); 128 129 // iterator 130 Iterator<?> it = keySet.iterator(); 131 while (it.hasNext()) { 132 assertTrue( 133 "UnmodifiableMapTest - keySetTest - Iterator returned wrong values", 134 keySet.contains(it.next())); 135 } 136 137 // size 138 assertTrue( 139 "UnmodifiableMapTest - keySetTest - returned wrong size. Wanted 100, got: " 140 + keySet.size(), keySet.size() == 100); 141 142 // toArray 143 Object[] objArray; 144 objArray = keySet.toArray(); 145 for (int counter = 0; it.hasNext(); counter++) { 146 assertTrue( 147 "UnmodifiableMapTest - keySetTest - toArray returned incorrect array", 148 objArray[counter] == it.next()); 149 } 150 151 // toArray (Object[]) 152 objArray = new Object[100]; 153 keySet.toArray(objArray); 154 for (int counter = 0; it.hasNext(); counter++) { 155 assertTrue( 156 "UnmodifiableMapTest - keySetTest - toArray(Object) filled array incorrectly", 157 objArray[counter] == it.next()); 158 } 159 } 160 161 } 162