KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.SortedMap JavaDoc;
24 import java.util.TreeMap JavaDoc;
25
26 import org.apache.commons.collections.BulkTest;
27
28 /**
29  * Abstract test class for {@link java.util.SortedMap} methods and contracts.
30  *
31  * @version $Revision: 1.9 $ $Date: 2004/05/31 22:39:20 $
32  *
33  * @author Stephen Colebourne
34  */

35 public abstract class AbstractTestSortedMap extends AbstractTestMap {
36
37     /**
38      * JUnit constructor.
39      *
40      * @param testName the test name
41      */

42     public AbstractTestSortedMap(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     //-----------------------------------------------------------------------
47
/**
48      * Can't sort null keys.
49      *
50      * @return false
51      */

52     public boolean isAllowNullKey() {
53         return false;
54     }
55
56     /**
57      * SortedMap uses TreeMap as its known comparison.
58      *
59      * @return a map that is known to be valid
60      */

61     public Map JavaDoc makeConfirmedMap() {
62         return new TreeMap JavaDoc();
63     }
64
65     //-----------------------------------------------------------------------
66
public void testComparator() {
67         SortedMap JavaDoc sm = (SortedMap JavaDoc) makeFullMap();
68         // no tests I can think of
69
}
70     
71     public void testFirstKey() {
72         SortedMap JavaDoc sm = (SortedMap JavaDoc) makeFullMap();
73         assertSame(sm.keySet().iterator().next(), sm.firstKey());
74     }
75     
76     public void testLastKey() {
77         SortedMap JavaDoc sm = (SortedMap JavaDoc) makeFullMap();
78         Object JavaDoc obj = null;
79         for (Iterator JavaDoc it = sm.keySet().iterator(); it.hasNext();) {
80             obj = (Object JavaDoc) it.next();
81         }
82         assertSame(obj, sm.lastKey());
83     }
84     
85     //-----------------------------------------------------------------------
86
public BulkTest bulkTestHeadMap() {
87         return new TestHeadMap(this);
88     }
89
90     public BulkTest bulkTestTailMap() {
91         return new TestTailMap(this);
92     }
93
94     public BulkTest bulkTestSubMap() {
95         return new TestSubMap(this);
96     }
97
98     public static abstract class TestViewMap extends AbstractTestSortedMap {
99         protected final AbstractTestMap main;
100         protected final List JavaDoc subSortedKeys = new ArrayList JavaDoc();
101         protected final List JavaDoc subSortedValues = new ArrayList JavaDoc();
102         protected final List JavaDoc subSortedNewValues = new ArrayList JavaDoc();
103         
104         public TestViewMap(String JavaDoc name, AbstractTestMap main) {
105             super(name);
106             this.main = main;
107         }
108         public void resetEmpty() {
109             // needed to init verify correctly
110
main.resetEmpty();
111             super.resetEmpty();
112         }
113         public void resetFull() {
114             // needed to init verify correctly
115
main.resetFull();
116             super.resetFull();
117         }
118         public void verify() {
119             // cross verify changes on view with changes on main map
120
super.verify();
121             main.verify();
122         }
123         public BulkTest bulkTestHeadMap() {
124             return null; // block infinite recursion
125
}
126         public BulkTest bulkTestTailMap() {
127             return null; // block infinite recursion
128
}
129         public BulkTest bulkTestSubMap() {
130             return null; // block infinite recursion
131
}
132         
133         public Object JavaDoc[] getSampleKeys() {
134             return subSortedKeys.toArray();
135         }
136         public Object JavaDoc[] getSampleValues() {
137             return subSortedValues.toArray();
138         }
139         public Object JavaDoc[] getNewSampleValues() {
140             return subSortedNewValues.toArray();
141         }
142         
143         public boolean isAllowNullKey() {
144             return main.isAllowNullKey();
145         }
146         public boolean isAllowNullValue() {
147             return main.isAllowNullValue();
148         }
149         public boolean isPutAddSupported() {
150             return main.isPutAddSupported();
151         }
152         public boolean isPutChangeSupported() {
153             return main.isPutChangeSupported();
154         }
155         public boolean isRemoveSupported() {
156             return main.isRemoveSupported();
157         }
158         public boolean isTestSerialization() {
159             return false;
160         }
161 // public void testSimpleSerialization() throws Exception {
162
// if (main.isSubMapViewsSerializable() == false) return;
163
// super.testSimpleSerialization();
164
// }
165
// public void testSerializeDeserializeThenCompare() throws Exception {
166
// if (main.isSubMapViewsSerializable() == false) return;
167
// super.testSerializeDeserializeThenCompare();
168
// }
169
// public void testEmptyMapCompatibility() throws Exception {
170
// if (main.isSubMapViewsSerializable() == false) return;
171
// super.testEmptyMapCompatibility();
172
// }
173
// public void testFullMapCompatibility() throws Exception {
174
// if (main.isSubMapViewsSerializable() == false) return;
175
// super.testFullMapCompatibility();
176
// }
177
}
178     
179     public static class TestHeadMap extends TestViewMap {
180         static final int SUBSIZE = 6;
181         final Object JavaDoc toKey;
182         
183         public TestHeadMap(AbstractTestMap main) {
184             super("SortedMap.HeadMap", main);
185             SortedMap JavaDoc sm = (SortedMap JavaDoc) main.makeFullMap();
186             for (Iterator JavaDoc it = sm.entrySet().iterator(); it.hasNext();) {
187                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
188                 this.subSortedKeys.add(entry.getKey());
189                 this.subSortedValues.add(entry.getValue());
190             }
191             this.toKey = this.subSortedKeys.get(SUBSIZE);
192             this.subSortedKeys.subList(SUBSIZE, this.subSortedKeys.size()).clear();
193             this.subSortedValues.subList(SUBSIZE, this.subSortedValues.size()).clear();
194             this.subSortedNewValues.addAll(Arrays.asList(main.getNewSampleValues()).subList(0, SUBSIZE));
195         }
196         public Map JavaDoc makeEmptyMap() {
197             // done this way so toKey is correctly set in the returned map
198
return ((SortedMap JavaDoc) main.makeEmptyMap()).headMap(toKey);
199         }
200         public Map JavaDoc makeFullMap() {
201             return ((SortedMap JavaDoc) main.makeFullMap()).headMap(toKey);
202         }
203         public void testHeadMapOutOfRange() {
204             if (isPutAddSupported() == false) return;
205             resetEmpty();
206             try {
207                 ((SortedMap JavaDoc) map).put(toKey, subSortedValues.get(0));
208                 fail();
209             } catch (IllegalArgumentException JavaDoc ex) {}
210             verify();
211         }
212         public String JavaDoc getCompatibilityVersion() {
213             return main.getCompatibilityVersion() + ".HeadMapView";
214         }
215
216 // public void testCreate() throws Exception {
217
// Map map = makeEmptyMap();
218
// writeExternalFormToDisk(
219
// (java.io.Serializable) map,
220
// "D:/dev/collections/data/test/FixedSizeSortedMap.emptyCollection.version3.1.HeadMapView.obj");
221
// map = makeFullMap();
222
// writeExternalFormToDisk(
223
// (java.io.Serializable) map,
224
// "D:/dev/collections/data/test/FixedSizeSortedMap.fullCollection.version3.1.HeadMapView.obj");
225
// }
226
}
227     
228     public static class TestTailMap extends TestViewMap {
229         static final int SUBSIZE = 6;
230         final Object JavaDoc fromKey;
231         final Object JavaDoc invalidKey;
232         
233         public TestTailMap(AbstractTestMap main) {
234             super("SortedMap.TailMap", main);
235             SortedMap JavaDoc sm = (SortedMap JavaDoc) main.makeFullMap();
236             for (Iterator JavaDoc it = sm.entrySet().iterator(); it.hasNext();) {
237                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
238                 this.subSortedKeys.add(entry.getKey());
239                 this.subSortedValues.add(entry.getValue());
240             }
241             this.fromKey = this.subSortedKeys.get(this.subSortedKeys.size() - SUBSIZE);
242             this.invalidKey = this.subSortedKeys.get(this.subSortedKeys.size() - SUBSIZE - 1);
243             this.subSortedKeys.subList(0, this.subSortedKeys.size() - SUBSIZE).clear();
244             this.subSortedValues.subList(0, this.subSortedValues.size() - SUBSIZE).clear();
245             this.subSortedNewValues.addAll(Arrays.asList(main.getNewSampleValues()).subList(0, SUBSIZE));
246         }
247         public Map JavaDoc makeEmptyMap() {
248             // done this way so toKey is correctly set in the returned map
249
return ((SortedMap JavaDoc) main.makeEmptyMap()).tailMap(fromKey);
250         }
251         public Map JavaDoc makeFullMap() {
252             return ((SortedMap JavaDoc) main.makeFullMap()).tailMap(fromKey);
253         }
254         public void testTailMapOutOfRange() {
255             if (isPutAddSupported() == false) return;
256             resetEmpty();
257             try {
258                 ((SortedMap JavaDoc) map).put(invalidKey, subSortedValues.get(0));
259                 fail();
260             } catch (IllegalArgumentException JavaDoc ex) {}
261             verify();
262         }
263         public String JavaDoc getCompatibilityVersion() {
264             return main.getCompatibilityVersion() + ".TailMapView";
265         }
266
267 // public void testCreate() throws Exception {
268
// Map map = makeEmptyMap();
269
// writeExternalFormToDisk(
270
// (java.io.Serializable) map,
271
// "D:/dev/collections/data/test/FixedSizeSortedMap.emptyCollection.version3.1.TailMapView.obj");
272
// map = makeFullMap();
273
// writeExternalFormToDisk(
274
// (java.io.Serializable) map,
275
// "D:/dev/collections/data/test/FixedSizeSortedMap.fullCollection.version3.1.TailMapView.obj");
276
// }
277
}
278     
279     public static class TestSubMap extends TestViewMap {
280         static final int SUBSIZE = 3;
281         final Object JavaDoc fromKey;
282         final Object JavaDoc toKey;
283         
284         public TestSubMap(AbstractTestMap main) {
285             super("SortedMap.SubMap", main);
286             SortedMap JavaDoc sm = (SortedMap JavaDoc) main.makeFullMap();
287             for (Iterator JavaDoc it = sm.entrySet().iterator(); it.hasNext();) {
288                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
289                 this.subSortedKeys.add(entry.getKey());
290                 this.subSortedValues.add(entry.getValue());
291             }
292             this.fromKey = this.subSortedKeys.get(SUBSIZE);
293             this.toKey = this.subSortedKeys.get(this.subSortedKeys.size() - SUBSIZE);
294             
295             this.subSortedKeys.subList(0, SUBSIZE).clear();
296             this.subSortedKeys.subList(this.subSortedKeys.size() - SUBSIZE, this.subSortedKeys.size()).clear();
297             
298             this.subSortedValues.subList(0, SUBSIZE).clear();
299             this.subSortedValues.subList(this.subSortedValues.size() - SUBSIZE, this.subSortedValues.size()).clear();
300             
301             this.subSortedNewValues.addAll(Arrays.asList(main.getNewSampleValues()).subList(
302                 SUBSIZE, this.main.getNewSampleValues().length - SUBSIZE));
303         }
304         
305         public Map JavaDoc makeEmptyMap() {
306             // done this way so toKey is correctly set in the returned map
307
return ((SortedMap JavaDoc) main.makeEmptyMap()).subMap(fromKey, toKey);
308         }
309         public Map JavaDoc makeFullMap() {
310             return ((SortedMap JavaDoc) main.makeFullMap()).subMap(fromKey, toKey);
311         }
312         public void testSubMapOutOfRange() {
313             if (isPutAddSupported() == false) return;
314             resetEmpty();
315             try {
316                 ((SortedMap JavaDoc) map).put(toKey, subSortedValues.get(0));
317                 fail();
318             } catch (IllegalArgumentException JavaDoc ex) {}
319             verify();
320         }
321         public String JavaDoc getCompatibilityVersion() {
322             return main.getCompatibilityVersion() + ".SubMapView";
323         }
324
325 // public void testCreate() throws Exception {
326
// Map map = makeEmptyMap();
327
// writeExternalFormToDisk(
328
// (java.io.Serializable) map,
329
// "D:/dev/collections/data/test/TransformedSortedMap.emptyCollection.version3.1.SubMapView.obj");
330
// map = makeFullMap();
331
// writeExternalFormToDisk(
332
// (java.io.Serializable) map,
333
// "D:/dev/collections/data/test/TransformedSortedMap.fullCollection.version3.1.SubMapView.obj");
334
// }
335
}
336     
337 }
338
Popular Tags