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.HashMap;
019: import java.util.Map;
020:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023:
024: import org.apache.commons.collections.Factory;
025: import org.apache.commons.collections.FactoryUtils;
026: import org.apache.commons.collections.Transformer;
027: import org.apache.commons.collections.functors.ConstantFactory;
028:
029: /**
030: * Extension of {@link TestMap} for exercising the
031: * {@link DefaultedMap} implementation.
032: *
033: * @since Commons Collections 3.2
034: * @version $Revision: 155406 $ $Date: 2005-03-22 22:53:50 +0000 (Tue, 22 Mar 2005) $
035: *
036: * @author Stephen Colebourne
037: */
038: public class TestDefaultedMap extends AbstractTestMap {
039:
040: protected static final Factory nullFactory = FactoryUtils
041: .nullFactory();
042:
043: public TestDefaultedMap(String testName) {
044: super (testName);
045: }
046:
047: public static Test suite() {
048: return new TestSuite(TestDefaultedMap.class);
049: }
050:
051: public static void main(String args[]) {
052: String[] testCaseName = { TestDefaultedMap.class.getName() };
053: junit.textui.TestRunner.main(testCaseName);
054: }
055:
056: //-----------------------------------------------------------------------
057: public Map makeEmptyMap() {
058: return DefaultedMap.decorate(new HashMap(), nullFactory);
059: }
060:
061: //-----------------------------------------------------------------------
062: public void testMapGet() {
063: Map map = new DefaultedMap("NULL");
064:
065: assertEquals(0, map.size());
066: assertEquals(false, map.containsKey("NotInMap"));
067: assertEquals("NULL", map.get("NotInMap"));
068:
069: map.put("Key", "Value");
070: assertEquals(1, map.size());
071: assertEquals(true, map.containsKey("Key"));
072: assertEquals("Value", map.get("Key"));
073: assertEquals(false, map.containsKey("NotInMap"));
074: assertEquals("NULL", map.get("NotInMap"));
075: }
076:
077: public void testMapGet2() {
078: HashMap base = new HashMap();
079: Map map = DefaultedMap.decorate(base, "NULL");
080:
081: assertEquals(0, map.size());
082: assertEquals(0, base.size());
083: assertEquals(false, map.containsKey("NotInMap"));
084: assertEquals("NULL", map.get("NotInMap"));
085:
086: map.put("Key", "Value");
087: assertEquals(1, map.size());
088: assertEquals(1, base.size());
089: assertEquals(true, map.containsKey("Key"));
090: assertEquals("Value", map.get("Key"));
091: assertEquals(false, map.containsKey("NotInMap"));
092: assertEquals("NULL", map.get("NotInMap"));
093: }
094:
095: public void testMapGet3() {
096: HashMap base = new HashMap();
097: Map map = DefaultedMap.decorate(base, ConstantFactory
098: .getInstance("NULL"));
099:
100: assertEquals(0, map.size());
101: assertEquals(0, base.size());
102: assertEquals(false, map.containsKey("NotInMap"));
103: assertEquals("NULL", map.get("NotInMap"));
104:
105: map.put("Key", "Value");
106: assertEquals(1, map.size());
107: assertEquals(1, base.size());
108: assertEquals(true, map.containsKey("Key"));
109: assertEquals("Value", map.get("Key"));
110: assertEquals(false, map.containsKey("NotInMap"));
111: assertEquals("NULL", map.get("NotInMap"));
112: }
113:
114: public void testMapGet4() {
115: HashMap base = new HashMap();
116: Map map = DefaultedMap.decorate(base, new Transformer() {
117: public Object transform(Object input) {
118: if (input instanceof String) {
119: return "NULL";
120: }
121: return "NULL_OBJECT";
122: }
123: });
124:
125: assertEquals(0, map.size());
126: assertEquals(0, base.size());
127: assertEquals(false, map.containsKey("NotInMap"));
128: assertEquals("NULL", map.get("NotInMap"));
129: assertEquals("NULL_OBJECT", map.get(new Integer(0)));
130:
131: map.put("Key", "Value");
132: assertEquals(1, map.size());
133: assertEquals(1, base.size());
134: assertEquals(true, map.containsKey("Key"));
135: assertEquals("Value", map.get("Key"));
136: assertEquals(false, map.containsKey("NotInMap"));
137: assertEquals("NULL", map.get("NotInMap"));
138: assertEquals("NULL_OBJECT", map.get(new Integer(0)));
139: }
140:
141: public String getCompatibilityVersion() {
142: return "3.2";
143: }
144:
145: // public void testCreate() throws Exception {
146: // resetEmpty();
147: // writeExternalFormToDisk(
148: // (java.io.Serializable) map,
149: // "c:/commons/collections/data/test/DefaultedMap.emptyCollection.version3.2.obj");
150: // resetFull();
151: // writeExternalFormToDisk(
152: // (java.io.Serializable) map,
153: // "c:/commons/collections/data/test/DefaultedMap.fullCollection.version3.2.obj");
154: // }
155:
156: }
|