001: /*
002: * Copyright 2001-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.ArrayList;
019: import java.util.Arrays;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.SortedMap;
024: import java.util.TreeMap;
025:
026: import org.apache.commons.collections.BulkTest;
027:
028: /**
029: * Abstract test class for {@link java.util.SortedMap} methods and contracts.
030: *
031: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
032: *
033: * @author Stephen Colebourne
034: */
035: public abstract class AbstractTestSortedMap extends AbstractTestMap {
036:
037: /**
038: * JUnit constructor.
039: *
040: * @param testName the test name
041: */
042: public AbstractTestSortedMap(String testName) {
043: super (testName);
044: }
045:
046: //-----------------------------------------------------------------------
047: /**
048: * Can't sort null keys.
049: *
050: * @return false
051: */
052: public boolean isAllowNullKey() {
053: return false;
054: }
055:
056: /**
057: * SortedMap uses TreeMap as its known comparison.
058: *
059: * @return a map that is known to be valid
060: */
061: public Map makeConfirmedMap() {
062: return new TreeMap();
063: }
064:
065: //-----------------------------------------------------------------------
066: public void testComparator() {
067: SortedMap sm = (SortedMap) makeFullMap();
068: // no tests I can think of
069: }
070:
071: public void testFirstKey() {
072: SortedMap sm = (SortedMap) makeFullMap();
073: assertSame(sm.keySet().iterator().next(), sm.firstKey());
074: }
075:
076: public void testLastKey() {
077: SortedMap sm = (SortedMap) makeFullMap();
078: Object obj = null;
079: for (Iterator it = sm.keySet().iterator(); it.hasNext();) {
080: obj = (Object) it.next();
081: }
082: assertSame(obj, sm.lastKey());
083: }
084:
085: //-----------------------------------------------------------------------
086: public BulkTest bulkTestHeadMap() {
087: return new TestHeadMap(this );
088: }
089:
090: public BulkTest bulkTestTailMap() {
091: return new TestTailMap(this );
092: }
093:
094: public BulkTest bulkTestSubMap() {
095: return new TestSubMap(this );
096: }
097:
098: public static abstract class TestViewMap extends
099: AbstractTestSortedMap {
100: protected final AbstractTestMap main;
101: protected final List subSortedKeys = new ArrayList();
102: protected final List subSortedValues = new ArrayList();
103: protected final List subSortedNewValues = new ArrayList();
104:
105: public TestViewMap(String name, AbstractTestMap main) {
106: super (name);
107: this .main = main;
108: }
109:
110: public void resetEmpty() {
111: // needed to init verify correctly
112: main.resetEmpty();
113: super .resetEmpty();
114: }
115:
116: public void resetFull() {
117: // needed to init verify correctly
118: main.resetFull();
119: super .resetFull();
120: }
121:
122: public void verify() {
123: // cross verify changes on view with changes on main map
124: super .verify();
125: main.verify();
126: }
127:
128: public BulkTest bulkTestHeadMap() {
129: return null; // block infinite recursion
130: }
131:
132: public BulkTest bulkTestTailMap() {
133: return null; // block infinite recursion
134: }
135:
136: public BulkTest bulkTestSubMap() {
137: return null; // block infinite recursion
138: }
139:
140: public Object[] getSampleKeys() {
141: return subSortedKeys.toArray();
142: }
143:
144: public Object[] getSampleValues() {
145: return subSortedValues.toArray();
146: }
147:
148: public Object[] getNewSampleValues() {
149: return subSortedNewValues.toArray();
150: }
151:
152: public boolean isAllowNullKey() {
153: return main.isAllowNullKey();
154: }
155:
156: public boolean isAllowNullValue() {
157: return main.isAllowNullValue();
158: }
159:
160: public boolean isPutAddSupported() {
161: return main.isPutAddSupported();
162: }
163:
164: public boolean isPutChangeSupported() {
165: return main.isPutChangeSupported();
166: }
167:
168: public boolean isRemoveSupported() {
169: return main.isRemoveSupported();
170: }
171:
172: public boolean isTestSerialization() {
173: return false;
174: }
175: // public void testSimpleSerialization() throws Exception {
176: // if (main.isSubMapViewsSerializable() == false) return;
177: // super.testSimpleSerialization();
178: // }
179: // public void testSerializeDeserializeThenCompare() throws Exception {
180: // if (main.isSubMapViewsSerializable() == false) return;
181: // super.testSerializeDeserializeThenCompare();
182: // }
183: // public void testEmptyMapCompatibility() throws Exception {
184: // if (main.isSubMapViewsSerializable() == false) return;
185: // super.testEmptyMapCompatibility();
186: // }
187: // public void testFullMapCompatibility() throws Exception {
188: // if (main.isSubMapViewsSerializable() == false) return;
189: // super.testFullMapCompatibility();
190: // }
191: }
192:
193: public static class TestHeadMap extends TestViewMap {
194: static final int SUBSIZE = 6;
195: final Object toKey;
196:
197: public TestHeadMap(AbstractTestMap main) {
198: super ("SortedMap.HeadMap", main);
199: SortedMap sm = (SortedMap) main.makeFullMap();
200: for (Iterator it = sm.entrySet().iterator(); it.hasNext();) {
201: Map.Entry entry = (Map.Entry) it.next();
202: this .subSortedKeys.add(entry.getKey());
203: this .subSortedValues.add(entry.getValue());
204: }
205: this .toKey = this .subSortedKeys.get(SUBSIZE);
206: this .subSortedKeys.subList(SUBSIZE,
207: this .subSortedKeys.size()).clear();
208: this .subSortedValues.subList(SUBSIZE,
209: this .subSortedValues.size()).clear();
210: this .subSortedNewValues.addAll(Arrays.asList(
211: main.getNewSampleValues()).subList(0, SUBSIZE));
212: }
213:
214: public Map makeEmptyMap() {
215: // done this way so toKey is correctly set in the returned map
216: return ((SortedMap) main.makeEmptyMap()).headMap(toKey);
217: }
218:
219: public Map makeFullMap() {
220: return ((SortedMap) main.makeFullMap()).headMap(toKey);
221: }
222:
223: public void testHeadMapOutOfRange() {
224: if (isPutAddSupported() == false)
225: return;
226: resetEmpty();
227: try {
228: ((SortedMap) map).put(toKey, subSortedValues.get(0));
229: fail();
230: } catch (IllegalArgumentException ex) {
231: }
232: verify();
233: }
234:
235: public String getCompatibilityVersion() {
236: return main.getCompatibilityVersion() + ".HeadMapView";
237: }
238:
239: // public void testCreate() throws Exception {
240: // Map map = makeEmptyMap();
241: // writeExternalFormToDisk(
242: // (java.io.Serializable) map,
243: // "D:/dev/collections/data/test/FixedSizeSortedMap.emptyCollection.version3.1.HeadMapView.obj");
244: // map = makeFullMap();
245: // writeExternalFormToDisk(
246: // (java.io.Serializable) map,
247: // "D:/dev/collections/data/test/FixedSizeSortedMap.fullCollection.version3.1.HeadMapView.obj");
248: // }
249: }
250:
251: public static class TestTailMap extends TestViewMap {
252: static final int SUBSIZE = 6;
253: final Object fromKey;
254: final Object invalidKey;
255:
256: public TestTailMap(AbstractTestMap main) {
257: super ("SortedMap.TailMap", main);
258: SortedMap sm = (SortedMap) main.makeFullMap();
259: for (Iterator it = sm.entrySet().iterator(); it.hasNext();) {
260: Map.Entry entry = (Map.Entry) it.next();
261: this .subSortedKeys.add(entry.getKey());
262: this .subSortedValues.add(entry.getValue());
263: }
264: this .fromKey = this .subSortedKeys.get(this .subSortedKeys
265: .size()
266: - SUBSIZE);
267: this .invalidKey = this .subSortedKeys.get(this .subSortedKeys
268: .size()
269: - SUBSIZE - 1);
270: this .subSortedKeys.subList(0,
271: this .subSortedKeys.size() - SUBSIZE).clear();
272: this .subSortedValues.subList(0,
273: this .subSortedValues.size() - SUBSIZE).clear();
274: this .subSortedNewValues.addAll(Arrays.asList(
275: main.getNewSampleValues()).subList(0, SUBSIZE));
276: }
277:
278: public Map makeEmptyMap() {
279: // done this way so toKey is correctly set in the returned map
280: return ((SortedMap) main.makeEmptyMap()).tailMap(fromKey);
281: }
282:
283: public Map makeFullMap() {
284: return ((SortedMap) main.makeFullMap()).tailMap(fromKey);
285: }
286:
287: public void testTailMapOutOfRange() {
288: if (isPutAddSupported() == false)
289: return;
290: resetEmpty();
291: try {
292: ((SortedMap) map).put(invalidKey, subSortedValues
293: .get(0));
294: fail();
295: } catch (IllegalArgumentException ex) {
296: }
297: verify();
298: }
299:
300: public String getCompatibilityVersion() {
301: return main.getCompatibilityVersion() + ".TailMapView";
302: }
303:
304: // public void testCreate() throws Exception {
305: // Map map = makeEmptyMap();
306: // writeExternalFormToDisk(
307: // (java.io.Serializable) map,
308: // "D:/dev/collections/data/test/FixedSizeSortedMap.emptyCollection.version3.1.TailMapView.obj");
309: // map = makeFullMap();
310: // writeExternalFormToDisk(
311: // (java.io.Serializable) map,
312: // "D:/dev/collections/data/test/FixedSizeSortedMap.fullCollection.version3.1.TailMapView.obj");
313: // }
314: }
315:
316: public static class TestSubMap extends TestViewMap {
317: static final int SUBSIZE = 3;
318: final Object fromKey;
319: final Object toKey;
320:
321: public TestSubMap(AbstractTestMap main) {
322: super ("SortedMap.SubMap", main);
323: SortedMap sm = (SortedMap) main.makeFullMap();
324: for (Iterator it = sm.entrySet().iterator(); it.hasNext();) {
325: Map.Entry entry = (Map.Entry) it.next();
326: this .subSortedKeys.add(entry.getKey());
327: this .subSortedValues.add(entry.getValue());
328: }
329: this .fromKey = this .subSortedKeys.get(SUBSIZE);
330: this .toKey = this .subSortedKeys.get(this .subSortedKeys
331: .size()
332: - SUBSIZE);
333:
334: this .subSortedKeys.subList(0, SUBSIZE).clear();
335: this .subSortedKeys.subList(
336: this .subSortedKeys.size() - SUBSIZE,
337: this .subSortedKeys.size()).clear();
338:
339: this .subSortedValues.subList(0, SUBSIZE).clear();
340: this .subSortedValues.subList(
341: this .subSortedValues.size() - SUBSIZE,
342: this .subSortedValues.size()).clear();
343:
344: this .subSortedNewValues.addAll(Arrays.asList(
345: main.getNewSampleValues()).subList(SUBSIZE,
346: this .main.getNewSampleValues().length - SUBSIZE));
347: }
348:
349: public Map makeEmptyMap() {
350: // done this way so toKey is correctly set in the returned map
351: return ((SortedMap) main.makeEmptyMap()).subMap(fromKey,
352: toKey);
353: }
354:
355: public Map makeFullMap() {
356: return ((SortedMap) main.makeFullMap()).subMap(fromKey,
357: toKey);
358: }
359:
360: public void testSubMapOutOfRange() {
361: if (isPutAddSupported() == false)
362: return;
363: resetEmpty();
364: try {
365: ((SortedMap) map).put(toKey, subSortedValues.get(0));
366: fail();
367: } catch (IllegalArgumentException ex) {
368: }
369: verify();
370: }
371:
372: public String getCompatibilityVersion() {
373: return main.getCompatibilityVersion() + ".SubMapView";
374: }
375:
376: // public void testCreate() throws Exception {
377: // Map map = makeEmptyMap();
378: // writeExternalFormToDisk(
379: // (java.io.Serializable) map,
380: // "D:/dev/collections/data/test/TransformedSortedMap.emptyCollection.version3.1.SubMapView.obj");
381: // map = makeFullMap();
382: // writeExternalFormToDisk(
383: // (java.io.Serializable) map,
384: // "D:/dev/collections/data/test/TransformedSortedMap.fullCollection.version3.1.SubMapView.obj");
385: // }
386: }
387:
388: }
|