KickJava   Java API By Example, From Geeks To Geeks.

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


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

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