KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2003-2004 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.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.commons.collections.TransformerUtils;
26 import org.apache.commons.collections.collection.TestTransformedCollection;
27
28 /**
29  * Extension of {@link TestMap} for exercising the {@link TransformedMap}
30  * implementation.
31  *
32  * @since Commons Collections 3.0
33  * @version $Revision: 1.7 $ $Date: 2004/04/10 22:22:56 $
34  *
35  * @author Stephen Colebourne
36  */

37 public class TestTransformedMap extends AbstractTestMap {
38     
39     public TestTransformedMap(String JavaDoc testName) {
40         super(testName);
41     }
42
43     public static Test suite() {
44         return new TestSuite(TestTransformedMap.class);
45     }
46
47     public static void main(String JavaDoc args[]) {
48         String JavaDoc[] testCaseName = { TestTransformedMap.class.getName()};
49         junit.textui.TestRunner.main(testCaseName);
50     }
51
52     //-----------------------------------------------------------------------
53
public Map JavaDoc makeEmptyMap() {
54         return TransformedMap.decorate(new HashMap JavaDoc(), TransformerUtils.nopTransformer(), TransformerUtils.nopTransformer());
55     }
56
57     //-----------------------------------------------------------------------
58
public void testTransformedMap() {
59         Object JavaDoc[] els = new Object JavaDoc[] {"1", "3", "5", "7", "2", "4", "6"};
60
61         Map JavaDoc map = TransformedMap.decorate(new HashMap JavaDoc(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER, null);
62         assertEquals(0, map.size());
63         for (int i = 0; i < els.length; i++) {
64             map.put(els[i], els[i]);
65             assertEquals(i + 1, map.size());
66             assertEquals(true, map.containsKey(new Integer JavaDoc((String JavaDoc) els[i])));
67             assertEquals(false, map.containsKey(els[i]));
68             assertEquals(true, map.containsValue(els[i]));
69             assertEquals(els[i], map.get(new Integer JavaDoc((String JavaDoc) els[i])));
70         }
71         
72         assertEquals(null, map.remove(els[0]));
73         assertEquals(els[0], map.remove(new Integer JavaDoc((String JavaDoc) els[0])));
74         
75         map = TransformedMap.decorate(new HashMap JavaDoc(), null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
76         assertEquals(0, map.size());
77         for (int i = 0; i < els.length; i++) {
78             map.put(els[i], els[i]);
79             assertEquals(i + 1, map.size());
80             assertEquals(true, map.containsValue(new Integer JavaDoc((String JavaDoc) els[i])));
81             assertEquals(false, map.containsValue(els[i]));
82             assertEquals(true, map.containsKey(els[i]));
83             assertEquals(new Integer JavaDoc((String JavaDoc) els[i]), map.get(els[i]));
84         }
85
86         assertEquals(new Integer JavaDoc((String JavaDoc) els[0]), map.remove(els[0]));
87         
88         Set JavaDoc entrySet = map.entrySet();
89         Map.Entry JavaDoc[] array = (Map.Entry JavaDoc[]) entrySet.toArray(new Map.Entry JavaDoc[0]);
90         array[0].setValue("66");
91         assertEquals(new Integer JavaDoc(66), array[0].getValue());
92         assertEquals(new Integer JavaDoc(66), map.get(array[0].getKey()));
93         
94         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entrySet.iterator().next();
95         entry.setValue("88");
96         assertEquals(new Integer JavaDoc(88), entry.getValue());
97         assertEquals(new Integer JavaDoc(88), map.get(entry.getKey()));
98     }
99
100     public String JavaDoc getCompatibilityVersion() {
101         return "3.1";
102     }
103
104 // public void testCreate() throws Exception {
105
// resetEmpty();
106
// writeExternalFormToDisk(
107
// (java.io.Serializable) map,
108
// "D:/dev/collections/data/test/TransformedMap.emptyCollection.version3.1.obj");
109
// resetFull();
110
// writeExternalFormToDisk(
111
// (java.io.Serializable) map,
112
// "D:/dev/collections/data/test/TransformedMap.fullCollection.version3.1.obj");
113
// }
114
}
115
Popular Tags