1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.map;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.TestMultiHashMap;
/**
* TestMultiValueMap.
*
* @author <a href="mailto:jcarman@apache.org">James Carman</a>
* @author Stephen Colebourne
* @since Commons Collections 3.2
*/
public class TestMultiValueMap extends TestCase {
public TestMultiValueMap(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestMultiHashMap.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestMultiHashMap.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
public void testNoMappingReturnsNull() {
final MultiValueMap map = createTestMap();
assertNull(map.get("whatever"));
}
public void testValueCollectionType() {
final MultiValueMap map = createTestMap(LinkedList.class);
assertTrue(map.get("one") instanceof LinkedList);
}
public void testMultipleValues() {
final MultiValueMap map = createTestMap(HashSet.class);
final HashSet expected = new HashSet();
expected.add("uno");
expected.add("un");
assertEquals(expected, map.get("one"));
}
public void testContainsValue() {
final MultiValueMap map = createTestMap(HashSet.class);
assertTrue(map.containsValue("uno"));
assertTrue(map.containsValue("un"));
assertTrue(map.containsValue("dos"));
assertTrue(map.containsValue("deux"));
assertTrue(map.containsValue("tres"));
assertTrue(map.containsValue("trois"));
assertFalse(map.containsValue("quatro"));
}
public void testKeyContainsValue() {
final MultiValueMap map = createTestMap(HashSet.class);
assertTrue(map.containsValue("one", "uno"));
assertTrue(map.containsValue("one", "un"));
assertTrue(map.containsValue("two", "dos"));
assertTrue(map.containsValue("two", "deux"));
assertTrue(map.containsValue("three", "tres"));
assertTrue(map.containsValue("three", "trois"));
assertFalse(map.containsValue("four", "quatro"));
}
public void testValues() {
final MultiValueMap map = createTestMap(HashSet.class);
final HashSet expected = new HashSet();
expected.add("uno");
expected.add("dos");
expected.add("tres");
expected.add("un");
expected.add("deux");
expected.add("trois");
final Collection c = map.values();
assertEquals(6, c.size());
assertEquals(expected, new HashSet(c));
}
private MultiValueMap createTestMap() {
return createTestMap(ArrayList.class);
}
private MultiValueMap createTestMap(Class collectionClass) {
final MultiValueMap map = MultiValueMap.decorate(new HashMap(), collectionClass);
map.put("one", "uno");
map.put("one", "un");
map.put("two", "dos");
map.put("two", "deux");
map.put("three", "tres");
map.put("three", "trois");
return map;
}
public void testKeyedIterator() {
final MultiValueMap map = createTestMap();
final ArrayList actual = new ArrayList(IteratorUtils.toList(map.iterator("one")));
final ArrayList expected = new ArrayList(Arrays.asList(new String[]{"uno", "un"}));
assertEquals(expected, actual);
}
public void testRemoveAllViaIterator() {
final MultiValueMap map = createTestMap();
for(Iterator i = map.values().iterator(); i.hasNext();) {
i.next();
i.remove();
}
assertNull(map.get("one"));
assertTrue(map.isEmpty());
}
public void testRemoveAllViaKeyedIterator() {
final MultiValueMap map = createTestMap();
for(Iterator i = map.iterator("one"); i.hasNext();) {
i.next();
i.remove();
}
assertNull(map.get("one"));
assertEquals(4, map.totalSize());
}
public void testTotalSizeA() {
assertEquals(6, createTestMap().totalSize());
}
//-----------------------------------------------------------------------
public void testMapEquals() {
MultiValueMap one = new MultiValueMap();
Integer value = new Integer(1);
one.put("One", value);
one.removeMapping("One", value);
MultiValueMap two = new MultiValueMap();
assertEquals(two, one);
}
//-----------------------------------------------------------------------
public void testGetCollection() {
MultiValueMap map = new MultiValueMap();
map.put("A", "AA");
assertSame(map.get("A"), map.getCollection("A"));
}
public void testTotalSize() {
MultiValueMap map = new MultiValueMap();
assertEquals(0, map.totalSize());
map.put("A", "AA");
assertEquals(1, map.totalSize());
map.put("B", "BA");
assertEquals(2, map.totalSize());
map.put("B", "BB");
assertEquals(3, map.totalSize());
map.put("B", "BC");
assertEquals(4, map.totalSize());
map.remove("A");
assertEquals(3, map.totalSize());
map.removeMapping("B", "BC");
assertEquals(2, map.totalSize());
}
public void testSize() {
MultiValueMap map = new MultiValueMap();
assertEquals(0, map.size());
map.put("A", "AA");
assertEquals(1, map.size());
map.put("B", "BA");
assertEquals(2, map.size());
map.put("B", "BB");
assertEquals(2, map.size());
map.put("B", "BC");
assertEquals(2, map.size());
map.remove("A");
assertEquals(2, map.size());
map.removeMapping("B", "BC");
assertEquals(2, map.size());
}
public void testSize_Key() {
MultiValueMap map = new MultiValueMap();
assertEquals(0, map.size("A"));
assertEquals(0, map.size("B"));
map.put("A", "AA");
assertEquals(1, map.size("A"));
assertEquals(0, map.size("B"));
map.put("B", "BA");
assertEquals(1, map.size("A"));
assertEquals(1, map.size("B"));
map.put("B", "BB");
assertEquals(1, map.size("A"));
assertEquals(2, map.size("B"));
map.put("B", "BC");
assertEquals(1, map.size("A"));
assertEquals(3, map.size("B"));
map.remove("A");
assertEquals(0, map.size("A"));
assertEquals(3, map.size("B"));
map.removeMapping("B", "BC");
assertEquals(0, map.size("A"));
assertEquals(2, map.size("B"));
}
public void testIterator_Key() {
MultiValueMap map = new MultiValueMap();
assertEquals(false, map.iterator("A").hasNext());
map.put("A", "AA");
Iterator it = map.iterator("A");
assertEquals(true, it.hasNext());
it.next();
assertEquals(false, it.hasNext());
}
public void testContainsValue_Key() {
MultiValueMap map = new MultiValueMap();
assertEquals(false, map.containsValue("A", "AA"));
assertEquals(false, map.containsValue("B", "BB"));
map.put("A", "AA");
assertEquals(true, map.containsValue("A", "AA"));
assertEquals(false, map.containsValue("A", "AB"));
}
public void testPut_ReturnValue() {
MultiValueMap test = new MultiValueMap();
assertNotNull(test.put("key", "object1"));
assertNotNull(test.put("key", "object2"));
List coll = Arrays.asList(new String[]{"uno", "un"});
assertTrue(test.putAll("key", coll));
assertFalse(test.putAll("key", new ArrayList()));
}
public void testPutAll_Map1() {
MultiMap original = new MultiValueMap();
original.put("key", "object1");
original.put("key", "object2");
MultiValueMap test = new MultiValueMap();
test.put("keyA", "objectA");
test.put("key", "object0");
test.putAll(original);
assertEquals(2, test.size());
assertEquals(4, test.totalSize());
assertEquals(1, test.getCollection("keyA").size());
assertEquals(3, test.getCollection("key").size());
assertEquals(true, test.containsValue("objectA"));
assertEquals(true, test.containsValue("object0"));
assertEquals(true, test.containsValue("object1"));
assertEquals(true, test.containsValue("object2"));
}
public void testPutAll_Map2() {
Map original = new HashMap();
original.put("keyX", "object1");
original.put("keyY", "object2");
MultiValueMap test = new MultiValueMap();
test.put("keyA", "objectA");
test.put("keyX", "object0");
test.putAll(original);
assertEquals(3, test.size());
assertEquals(4, test.totalSize());
assertEquals(1, test.getCollection("keyA").size());
assertEquals(2, test.getCollection("keyX").size());
assertEquals(1, test.getCollection("keyY").size());
assertEquals(true, test.containsValue("objectA"));
assertEquals(true, test.containsValue("object0"));
assertEquals(true, test.containsValue("object1"));
assertEquals(true, test.containsValue("object2"));
}
public void testPutAll_KeyCollection() {
MultiValueMap map = new MultiValueMap();
Collection coll = Arrays.asList(new Object[] {"X", "Y", "Z"});
assertEquals(true, map.putAll("A", coll));
assertEquals(3, map.size("A"));
assertEquals(true, map.containsValue("A", "X"));
assertEquals(true, map.containsValue("A", "Y"));
assertEquals(true, map.containsValue("A", "Z"));
assertEquals(false, map.putAll("A", null));
assertEquals(3, map.size("A"));
assertEquals(true, map.containsValue("A", "X"));
assertEquals(true, map.containsValue("A", "Y"));
assertEquals(true, map.containsValue("A", "Z"));
assertEquals(false, map.putAll("A", new ArrayList()));
assertEquals(3, map.size("A"));
assertEquals(true, map.containsValue("A", "X"));
assertEquals(true, map.containsValue("A", "Y"));
assertEquals(true, map.containsValue("A", "Z"));
coll = Arrays.asList(new Object[] {"M"});
assertEquals(true, map.putAll("A", coll));
assertEquals(4, map.size("A"));
assertEquals(true, map.containsValue("A", "X"));
assertEquals(true, map.containsValue("A", "Y"));
assertEquals(true, map.containsValue("A", "Z"));
assertEquals(true, map.containsValue("A", "M"));
}
public void testRemove_KeyItem() {
MultiValueMap map = new MultiValueMap();
map.put("A", "AA");
map.put("A", "AB");
map.put("A", "AC");
assertEquals(null, map.removeMapping("C", "CA"));
assertEquals(null, map.removeMapping("A", "AD"));
assertEquals("AC", map.removeMapping("A", "AC"));
assertEquals("AB", map.removeMapping("A", "AB"));
assertEquals("AA", map.removeMapping("A", "AA"));
assertEquals(new MultiValueMap(), map);
}
}
|