001: /*
002: * Copyright 2004 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.HashMap;
019: import java.util.Map;
020: import java.util.Set;
021:
022: import junit.framework.Test;
023: import junit.textui.TestRunner;
024:
025: import org.apache.commons.collections.BulkTest;
026:
027: /**
028: * Tests for the {@link CaseInsensitiveMap} implementation.
029: *
030: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
031: *
032: * @author Commons-Collections team
033: */
034: public class TestCaseInsensitiveMap extends AbstractTestIterableMap {
035:
036: public TestCaseInsensitiveMap(String testName) {
037: super (testName);
038: }
039:
040: public static void main(String[] args) {
041: TestRunner.run(suite());
042: }
043:
044: public static Test suite() {
045: return BulkTest.makeSuite(TestCaseInsensitiveMap.class);
046: }
047:
048: public Map makeEmptyMap() {
049: return new CaseInsensitiveMap();
050: }
051:
052: public String getCompatibilityVersion() {
053: return "3";
054: }
055:
056: //-------------------------------------------------------------------------
057:
058: public void testCaseInsensitive() {
059: Map map = new CaseInsensitiveMap();
060: map.put("One", "One");
061: map.put("Two", "Two");
062: assertEquals("One", (String) map.get("one"));
063: assertEquals("One", (String) map.get("oNe"));
064: map.put("two", "Three");
065: assertEquals("Three", (String) map.get("Two"));
066: }
067:
068: public void testNullHandling() {
069: Map map = new CaseInsensitiveMap();
070: map.put("One", "One");
071: map.put("Two", "Two");
072: map.put(null, "Three");
073: assertEquals("Three", (String) map.get(null));
074: map.put(null, "Four");
075: assertEquals("Four", (String) map.get(null));
076: Set keys = map.keySet();
077: assertTrue(keys.contains("one"));
078: assertTrue(keys.contains("two"));
079: assertTrue(keys.contains(null));
080: assertTrue(keys.size() == 3);
081: }
082:
083: public void testPutAll() {
084: Map map = new HashMap();
085: map.put("One", "One");
086: map.put("Two", "Two");
087: map.put("one", "Three");
088: map.put(null, "Four");
089: map.put(new Integer(20), "Five");
090: Map caseInsensitiveMap = new CaseInsensitiveMap(map);
091: assertTrue(caseInsensitiveMap.size() == 4); // ones collapsed
092: Set keys = caseInsensitiveMap.keySet();
093: assertTrue(keys.contains("one"));
094: assertTrue(keys.contains("two"));
095: assertTrue(keys.contains(null));
096: assertTrue(keys.contains(Integer.toString(20)));
097: assertTrue(keys.size() == 4);
098: assertTrue(!caseInsensitiveMap.containsValue("One")
099: || !caseInsensitiveMap.containsValue("Three")); // ones collaped
100: assertEquals(caseInsensitiveMap.get(null), "Four");
101: }
102:
103: public void testClone() {
104: CaseInsensitiveMap map = new CaseInsensitiveMap(10);
105: map.put("1", "1");
106: Map cloned = (Map) map.clone();
107: assertEquals(map.size(), cloned.size());
108: assertSame(map.get("1"), cloned.get("1"));
109: }
110:
111: /*
112: public void testCreate() throws Exception {
113: resetEmpty();
114: writeExternalFormToDisk((java.io.Serializable) map, "/home/phil/jakarta-commons/collections/data/test/CaseInsensitiveMap.emptyCollection.version3.obj");
115: resetFull();
116: writeExternalFormToDisk((java.io.Serializable) map, "/home/phil/jakarta-commons/collections/data/test/CaseInsensitiveMap.fullCollection.version3.obj");
117: }
118: */
119: }
|