KickJava   Java API By Example, From Geeks To Geeks.

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


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.Comparator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.SortedMap JavaDoc;
21 import java.util.TreeMap JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.collections.Factory;
27 import org.apache.commons.collections.FactoryUtils;
28 import org.apache.commons.collections.Transformer;
29 import org.apache.commons.collections.TransformerUtils;
30
31 /**
32  * Extension of {@link TestLazyMap} for exercising the
33  * {@link LazySortedMap} implementation.
34  *
35  * @since Commons Collections 3.0
36  * @version $Revision: 1.6 $ $Date: 2004/04/09 09:39:47 $
37  *
38  * @author Phil Steitz
39  */

40 public class TestLazySortedMap extends AbstractTestSortedMap {
41     
42     protected static final Factory oneFactory = FactoryUtils.constantFactory("One");
43     protected static final Factory nullFactory = FactoryUtils.nullFactory();
44     
45     public TestLazySortedMap(String JavaDoc testName) {
46         super(testName);
47     }
48     
49     public static Test suite() {
50         return new TestSuite(TestLazySortedMap.class);
51     }
52     
53     public static void main(String JavaDoc args[]) {
54         String JavaDoc[] testCaseName = { TestLazySortedMap.class.getName()};
55         junit.textui.TestRunner.main(testCaseName);
56     }
57
58     //-----------------------------------------------------------------------
59
protected SortedMap JavaDoc decorateMap(SortedMap JavaDoc map, Factory factory) {
60         return LazySortedMap.decorate(map, factory);
61     }
62     
63     public Map JavaDoc makeEmptyMap() {
64         return decorateMap(new TreeMap JavaDoc(), nullFactory);
65     }
66     
67     protected SortedMap JavaDoc makeTestSortedMap(Factory factory) {
68         return decorateMap(new TreeMap JavaDoc(), factory);
69     }
70     
71     public boolean isSubMapViewsSerializable() {
72         // TreeMap sub map views have a bug in deserialization.
73
return false;
74     }
75
76     public boolean isAllowNullKey() {
77         return false;
78     }
79
80     // from TestLazyMap
81
//-----------------------------------------------------------------------
82
public void testMapGet() {
83         Map JavaDoc map = makeTestSortedMap(oneFactory);
84         assertEquals(0, map.size());
85         String JavaDoc s1 = (String JavaDoc) map.get("Five");
86         assertEquals("One", s1);
87         assertEquals(1, map.size());
88         String JavaDoc s2 = (String JavaDoc) map.get(new String JavaDoc(new char[] {'F','i','v','e'}));
89         assertEquals("One", s2);
90         assertEquals(1, map.size());
91         assertSame(s1, s2);
92         
93         map = makeTestSortedMap(nullFactory);
94         Object JavaDoc o = map.get("Five");
95         assertEquals(null,o);
96         assertEquals(1, map.size());
97         
98     }
99     
100     //-----------------------------------------------------------------------
101
public void testSortOrder() {
102         SortedMap JavaDoc map = makeTestSortedMap(oneFactory);
103         map.put("A", "a");
104         map.get("B"); // Entry with value "One" created
105
map.put("C", "c");
106         assertEquals("First key should be A", map.firstKey(), "A");
107         assertEquals("Last key should be C", map.lastKey(), "C");
108         assertEquals("First key in tail map should be B",
109             map.tailMap("B").firstKey(), "B");
110         assertEquals("Last key in head map should be B",
111             map.headMap("C").lastKey(), "B");
112         assertEquals("Last key in submap should be B",
113            map.subMap("A","C").lastKey(), "B");
114         
115         Comparator JavaDoc c = map.comparator();
116         assertTrue("natural order, so comparator should be null",
117             c == null);
118     }
119     
120     public void testTransformerDecorate() {
121         Transformer transformer = TransformerUtils.asTransformer(oneFactory);
122         SortedMap JavaDoc map = LazySortedMap.decorate(new TreeMap JavaDoc(), transformer);
123         assertTrue(map instanceof LazySortedMap);
124          try {
125             map = LazySortedMap.decorate(new TreeMap JavaDoc(), (Transformer) null);
126             fail("Expecting IllegalArgumentException for null transformer");
127         } catch (IllegalArgumentException JavaDoc e) {
128             // expected
129
}
130         try {
131             map = LazySortedMap.decorate(null, transformer);
132             fail("Expecting IllegalArgumentException for null map");
133         } catch (IllegalArgumentException JavaDoc e) {
134             // expected
135
}
136     }
137     
138     public String JavaDoc getCompatibilityVersion() {
139         return "3.1";
140     }
141
142 // public void testCreate() throws Exception {
143
// resetEmpty();
144
// writeExternalFormToDisk(
145
// (java.io.Serializable) map,
146
// "D:/dev/collections/data/test/LazySortedMap.emptyCollection.version3.1.obj");
147
// resetFull();
148
// writeExternalFormToDisk(
149
// (java.io.Serializable) map,
150
// "D:/dev/collections/data/test/LazySortedMap.fullCollection.version3.1.obj");
151
// }
152
}
Popular Tags