001: /*
002: * Copyright 2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.map;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.Map;
026:
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030:
031: import org.apache.commons.collections.IteratorUtils;
032: import org.apache.commons.collections.MultiMap;
033: import org.apache.commons.collections.TestMultiHashMap;
034:
035: /**
036: * TestMultiValueMap.
037: *
038: * @author <a href="mailto:jcarman@apache.org">James Carman</a>
039: * @author Stephen Colebourne
040: * @since Commons Collections 3.2
041: */
042: public class TestMultiValueMap extends TestCase {
043:
044: public TestMultiValueMap(String testName) {
045: super (testName);
046: }
047:
048: public static Test suite() {
049: return new TestSuite(TestMultiHashMap.class);
050: }
051:
052: public static void main(String args[]) {
053: String[] testCaseName = { TestMultiHashMap.class.getName() };
054: junit.textui.TestRunner.main(testCaseName);
055: }
056:
057: public void testNoMappingReturnsNull() {
058: final MultiValueMap map = createTestMap();
059: assertNull(map.get("whatever"));
060: }
061:
062: public void testValueCollectionType() {
063: final MultiValueMap map = createTestMap(LinkedList.class);
064: assertTrue(map.get("one") instanceof LinkedList);
065: }
066:
067: public void testMultipleValues() {
068: final MultiValueMap map = createTestMap(HashSet.class);
069: final HashSet expected = new HashSet();
070: expected.add("uno");
071: expected.add("un");
072: assertEquals(expected, map.get("one"));
073: }
074:
075: public void testContainsValue() {
076: final MultiValueMap map = createTestMap(HashSet.class);
077: assertTrue(map.containsValue("uno"));
078: assertTrue(map.containsValue("un"));
079: assertTrue(map.containsValue("dos"));
080: assertTrue(map.containsValue("deux"));
081: assertTrue(map.containsValue("tres"));
082: assertTrue(map.containsValue("trois"));
083: assertFalse(map.containsValue("quatro"));
084: }
085:
086: public void testKeyContainsValue() {
087: final MultiValueMap map = createTestMap(HashSet.class);
088: assertTrue(map.containsValue("one", "uno"));
089: assertTrue(map.containsValue("one", "un"));
090: assertTrue(map.containsValue("two", "dos"));
091: assertTrue(map.containsValue("two", "deux"));
092: assertTrue(map.containsValue("three", "tres"));
093: assertTrue(map.containsValue("three", "trois"));
094: assertFalse(map.containsValue("four", "quatro"));
095: }
096:
097: public void testValues() {
098: final MultiValueMap map = createTestMap(HashSet.class);
099: final HashSet expected = new HashSet();
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 c = map.values();
107: assertEquals(6, c.size());
108: assertEquals(expected, new HashSet(c));
109: }
110:
111: private MultiValueMap createTestMap() {
112: return createTestMap(ArrayList.class);
113: }
114:
115: private MultiValueMap createTestMap(Class collectionClass) {
116: final MultiValueMap map = MultiValueMap.decorate(new HashMap(),
117: collectionClass);
118: map.put("one", "uno");
119: map.put("one", "un");
120: map.put("two", "dos");
121: map.put("two", "deux");
122: map.put("three", "tres");
123: map.put("three", "trois");
124: return map;
125: }
126:
127: public void testKeyedIterator() {
128: final MultiValueMap map = createTestMap();
129: final ArrayList actual = new ArrayList(IteratorUtils.toList(map
130: .iterator("one")));
131: final ArrayList expected = new ArrayList(Arrays
132: .asList(new String[] { "uno", "un" }));
133: assertEquals(expected, actual);
134: }
135:
136: public void testRemoveAllViaIterator() {
137: final MultiValueMap map = createTestMap();
138: for (Iterator i = map.values().iterator(); i.hasNext();) {
139: i.next();
140: i.remove();
141: }
142: assertNull(map.get("one"));
143: assertTrue(map.isEmpty());
144: }
145:
146: public void testRemoveAllViaKeyedIterator() {
147: final MultiValueMap map = createTestMap();
148: for (Iterator i = map.iterator("one"); i.hasNext();) {
149: i.next();
150: i.remove();
151: }
152: assertNull(map.get("one"));
153: assertEquals(4, map.totalSize());
154: }
155:
156: public void testTotalSizeA() {
157: assertEquals(6, createTestMap().totalSize());
158: }
159:
160: //-----------------------------------------------------------------------
161: public void testMapEquals() {
162: MultiValueMap one = new MultiValueMap();
163: Integer value = new Integer(1);
164: one.put("One", value);
165: one.remove("One", value);
166:
167: MultiValueMap two = new MultiValueMap();
168: assertEquals(two, one);
169: }
170:
171: //-----------------------------------------------------------------------
172: public void testGetCollection() {
173: MultiValueMap map = new MultiValueMap();
174: map.put("A", "AA");
175: assertSame(map.get("A"), map.getCollection("A"));
176: }
177:
178: public void testTotalSize() {
179: MultiValueMap map = new MultiValueMap();
180: assertEquals(0, map.totalSize());
181: map.put("A", "AA");
182: assertEquals(1, map.totalSize());
183: map.put("B", "BA");
184: assertEquals(2, map.totalSize());
185: map.put("B", "BB");
186: assertEquals(3, map.totalSize());
187: map.put("B", "BC");
188: assertEquals(4, map.totalSize());
189: map.remove("A");
190: assertEquals(3, map.totalSize());
191: map.remove("B", "BC");
192: assertEquals(2, map.totalSize());
193: }
194:
195: public void testSize() {
196: MultiValueMap map = new MultiValueMap();
197: assertEquals(0, map.size());
198: map.put("A", "AA");
199: assertEquals(1, map.size());
200: map.put("B", "BA");
201: assertEquals(2, map.size());
202: map.put("B", "BB");
203: assertEquals(2, map.size());
204: map.put("B", "BC");
205: assertEquals(2, map.size());
206: map.remove("A");
207: assertEquals(2, map.size());
208: map.remove("B", "BC");
209: assertEquals(2, map.size());
210: }
211:
212: public void testSize_Key() {
213: MultiValueMap map = new MultiValueMap();
214: assertEquals(0, map.size("A"));
215: assertEquals(0, map.size("B"));
216: map.put("A", "AA");
217: assertEquals(1, map.size("A"));
218: assertEquals(0, map.size("B"));
219: map.put("B", "BA");
220: assertEquals(1, map.size("A"));
221: assertEquals(1, map.size("B"));
222: map.put("B", "BB");
223: assertEquals(1, map.size("A"));
224: assertEquals(2, map.size("B"));
225: map.put("B", "BC");
226: assertEquals(1, map.size("A"));
227: assertEquals(3, map.size("B"));
228: map.remove("A");
229: assertEquals(0, map.size("A"));
230: assertEquals(3, map.size("B"));
231: map.remove("B", "BC");
232: assertEquals(0, map.size("A"));
233: assertEquals(2, map.size("B"));
234: }
235:
236: public void testIterator_Key() {
237: MultiValueMap map = new MultiValueMap();
238: assertEquals(false, map.iterator("A").hasNext());
239: map.put("A", "AA");
240: Iterator it = map.iterator("A");
241: assertEquals(true, it.hasNext());
242: it.next();
243: assertEquals(false, it.hasNext());
244: }
245:
246: public void testContainsValue_Key() {
247: MultiValueMap map = new MultiValueMap();
248: assertEquals(false, map.containsValue("A", "AA"));
249: assertEquals(false, map.containsValue("B", "BB"));
250: map.put("A", "AA");
251: assertEquals(true, map.containsValue("A", "AA"));
252: assertEquals(false, map.containsValue("A", "AB"));
253: }
254:
255: public void testPutAll_Map1() {
256: MultiMap original = new MultiValueMap();
257: original.put("key", "object1");
258: original.put("key", "object2");
259:
260: MultiValueMap test = new MultiValueMap();
261: test.put("keyA", "objectA");
262: test.put("key", "object0");
263: test.putAll(original);
264:
265: assertEquals(2, test.size());
266: assertEquals(4, test.totalSize());
267: assertEquals(1, test.getCollection("keyA").size());
268: assertEquals(3, test.getCollection("key").size());
269: assertEquals(true, test.containsValue("objectA"));
270: assertEquals(true, test.containsValue("object0"));
271: assertEquals(true, test.containsValue("object1"));
272: assertEquals(true, test.containsValue("object2"));
273: }
274:
275: public void testPutAll_Map2() {
276: Map original = new HashMap();
277: original.put("keyX", "object1");
278: original.put("keyY", "object2");
279:
280: MultiValueMap test = new MultiValueMap();
281: test.put("keyA", "objectA");
282: test.put("keyX", "object0");
283: test.putAll(original);
284:
285: assertEquals(3, test.size());
286: assertEquals(4, test.totalSize());
287: assertEquals(1, test.getCollection("keyA").size());
288: assertEquals(2, test.getCollection("keyX").size());
289: assertEquals(1, test.getCollection("keyY").size());
290: assertEquals(true, test.containsValue("objectA"));
291: assertEquals(true, test.containsValue("object0"));
292: assertEquals(true, test.containsValue("object1"));
293: assertEquals(true, test.containsValue("object2"));
294: }
295:
296: public void testPutAll_KeyCollection() {
297: MultiValueMap map = new MultiValueMap();
298: Collection coll = Arrays.asList(new Object[] { "X", "Y", "Z" });
299:
300: assertEquals(true, map.putAll("A", coll));
301: assertEquals(3, map.size("A"));
302: assertEquals(true, map.containsValue("A", "X"));
303: assertEquals(true, map.containsValue("A", "Y"));
304: assertEquals(true, map.containsValue("A", "Z"));
305:
306: assertEquals(false, map.putAll("A", null));
307: assertEquals(3, map.size("A"));
308: assertEquals(true, map.containsValue("A", "X"));
309: assertEquals(true, map.containsValue("A", "Y"));
310: assertEquals(true, map.containsValue("A", "Z"));
311:
312: assertEquals(false, map.putAll("A", new ArrayList()));
313: assertEquals(3, map.size("A"));
314: assertEquals(true, map.containsValue("A", "X"));
315: assertEquals(true, map.containsValue("A", "Y"));
316: assertEquals(true, map.containsValue("A", "Z"));
317:
318: coll = Arrays.asList(new Object[] { "M" });
319: assertEquals(true, map.putAll("A", coll));
320: assertEquals(4, map.size("A"));
321: assertEquals(true, map.containsValue("A", "X"));
322: assertEquals(true, map.containsValue("A", "Y"));
323: assertEquals(true, map.containsValue("A", "Z"));
324: assertEquals(true, map.containsValue("A", "M"));
325: }
326:
327: public void testRemove_KeyItem() {
328: MultiValueMap map = new MultiValueMap();
329: map.put("A", "AA");
330: map.put("A", "AB");
331: map.put("A", "AC");
332: assertEquals(null, map.remove("C", "CA"));
333: assertEquals(null, map.remove("A", "AD"));
334: assertEquals("AC", map.remove("A", "AC"));
335: assertEquals("AB", map.remove("A", "AB"));
336: assertEquals("AA", map.remove("A", "AA"));
337: assertEquals(new MultiValueMap(), map);
338: }
339:
340: }
|