KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > TestMultiValueMap


1 /*
2  * Copyright 2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.map;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31 import org.apache.commons.collections.IteratorUtils;
32 import org.apache.commons.collections.MultiMap;
33 import org.apache.commons.collections.TestMultiHashMap;
34
35 /**
36  * TestMultiValueMap.
37  *
38  * @author <a HREF="mailto:jcarman@apache.org">James Carman</a>
39  * @author Stephen Colebourne
40  * @since Commons Collections 3.2
41  */

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