001: /*
002: * Copyright 2003-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.framework.TestSuite;
024:
025: import org.apache.commons.collections.TransformerUtils;
026: import org.apache.commons.collections.collection.TestTransformedCollection;
027:
028: /**
029: * Extension of {@link TestMap} for exercising the {@link TransformedMap}
030: * implementation.
031: *
032: * @since Commons Collections 3.0
033: * @version $Revision: 219347 $ $Date: 2005-07-16 18:54:00 +0100 (Sat, 16 Jul 2005) $
034: *
035: * @author Stephen Colebourne
036: */
037: public class TestTransformedMap extends AbstractTestMap {
038:
039: public TestTransformedMap(String testName) {
040: super (testName);
041: }
042:
043: public static Test suite() {
044: return new TestSuite(TestTransformedMap.class);
045: }
046:
047: public static void main(String args[]) {
048: String[] testCaseName = { TestTransformedMap.class.getName() };
049: junit.textui.TestRunner.main(testCaseName);
050: }
051:
052: //-----------------------------------------------------------------------
053: public Map makeEmptyMap() {
054: return TransformedMap.decorate(new HashMap(), TransformerUtils
055: .nopTransformer(), TransformerUtils.nopTransformer());
056: }
057:
058: //-----------------------------------------------------------------------
059: public void testTransformedMap() {
060: Object[] els = new Object[] { "1", "3", "5", "7", "2", "4", "6" };
061:
062: Map map = TransformedMap
063: .decorate(
064: new HashMap(),
065: TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER,
066: null);
067: assertEquals(0, map.size());
068: for (int i = 0; i < els.length; i++) {
069: map.put(els[i], els[i]);
070: assertEquals(i + 1, map.size());
071: assertEquals(true, map.containsKey(new Integer(
072: (String) els[i])));
073: assertEquals(false, map.containsKey(els[i]));
074: assertEquals(true, map.containsValue(els[i]));
075: assertEquals(els[i], map.get(new Integer((String) els[i])));
076: }
077:
078: assertEquals(null, map.remove(els[0]));
079: assertEquals(els[0], map.remove(new Integer((String) els[0])));
080:
081: map = TransformedMap
082: .decorate(
083: new HashMap(),
084: null,
085: TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
086: assertEquals(0, map.size());
087: for (int i = 0; i < els.length; i++) {
088: map.put(els[i], els[i]);
089: assertEquals(i + 1, map.size());
090: assertEquals(true, map.containsValue(new Integer(
091: (String) els[i])));
092: assertEquals(false, map.containsValue(els[i]));
093: assertEquals(true, map.containsKey(els[i]));
094: assertEquals(new Integer((String) els[i]), map.get(els[i]));
095: }
096:
097: assertEquals(new Integer((String) els[0]), map.remove(els[0]));
098:
099: Set entrySet = map.entrySet();
100: Map.Entry[] array = (Map.Entry[]) entrySet
101: .toArray(new Map.Entry[0]);
102: array[0].setValue("66");
103: assertEquals(new Integer(66), array[0].getValue());
104: assertEquals(new Integer(66), map.get(array[0].getKey()));
105:
106: Map.Entry entry = (Map.Entry) entrySet.iterator().next();
107: entry.setValue("88");
108: assertEquals(new Integer(88), entry.getValue());
109: assertEquals(new Integer(88), map.get(entry.getKey()));
110: }
111:
112: //-----------------------------------------------------------------------
113: public void testFactory_Decorate() {
114: Map base = new HashMap();
115: base.put("A", "1");
116: base.put("B", "2");
117: base.put("C", "3");
118:
119: Map trans = TransformedMap
120: .decorate(
121: base,
122: null,
123: TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
124: assertEquals(3, trans.size());
125: assertEquals("1", trans.get("A"));
126: assertEquals("2", trans.get("B"));
127: assertEquals("3", trans.get("C"));
128: trans.put("D", "4");
129: assertEquals(new Integer(4), trans.get("D"));
130: }
131:
132: public void testFactory_decorateTransform() {
133: Map base = new HashMap();
134: base.put("A", "1");
135: base.put("B", "2");
136: base.put("C", "3");
137:
138: Map trans = TransformedMap
139: .decorateTransform(
140: base,
141: null,
142: TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
143: assertEquals(3, trans.size());
144: assertEquals(new Integer(1), trans.get("A"));
145: assertEquals(new Integer(2), trans.get("B"));
146: assertEquals(new Integer(3), trans.get("C"));
147: trans.put("D", "4");
148: assertEquals(new Integer(4), trans.get("D"));
149: }
150:
151: //-----------------------------------------------------------------------
152: public String getCompatibilityVersion() {
153: return "3.1";
154: }
155:
156: // public void testCreate() throws Exception {
157: // resetEmpty();
158: // writeExternalFormToDisk(
159: // (java.io.Serializable) map,
160: // "D:/dev/collections/data/test/TransformedMap.emptyCollection.version3.1.obj");
161: // resetFull();
162: // writeExternalFormToDisk(
163: // (java.io.Serializable) map,
164: // "D:/dev/collections/data/test/TransformedMap.fullCollection.version3.1.obj");
165: // }
166: }
|