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