001: /*
002: * Copyright 2003-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.Comparator;
019: import java.util.Map;
020: import java.util.SortedMap;
021: import java.util.TreeMap;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.collections.Factory;
027: import org.apache.commons.collections.FactoryUtils;
028: import org.apache.commons.collections.Transformer;
029: import org.apache.commons.collections.TransformerUtils;
030:
031: /**
032: * Extension of {@link TestLazyMap} for exercising the
033: * {@link LazySortedMap} implementation.
034: *
035: * @since Commons Collections 3.0
036: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
037: *
038: * @author Phil Steitz
039: */
040: public class TestLazySortedMap extends AbstractTestSortedMap {
041:
042: protected static final Factory oneFactory = FactoryUtils
043: .constantFactory("One");
044: protected static final Factory nullFactory = FactoryUtils
045: .nullFactory();
046:
047: public TestLazySortedMap(String testName) {
048: super (testName);
049: }
050:
051: public static Test suite() {
052: return new TestSuite(TestLazySortedMap.class);
053: }
054:
055: public static void main(String args[]) {
056: String[] testCaseName = { TestLazySortedMap.class.getName() };
057: junit.textui.TestRunner.main(testCaseName);
058: }
059:
060: //-----------------------------------------------------------------------
061: protected SortedMap decorateMap(SortedMap map, Factory factory) {
062: return LazySortedMap.decorate(map, factory);
063: }
064:
065: public Map makeEmptyMap() {
066: return decorateMap(new TreeMap(), nullFactory);
067: }
068:
069: protected SortedMap makeTestSortedMap(Factory factory) {
070: return decorateMap(new TreeMap(), factory);
071: }
072:
073: public boolean isSubMapViewsSerializable() {
074: // TreeMap sub map views have a bug in deserialization.
075: return false;
076: }
077:
078: public boolean isAllowNullKey() {
079: return false;
080: }
081:
082: // from TestLazyMap
083: //-----------------------------------------------------------------------
084: public void testMapGet() {
085: Map map = makeTestSortedMap(oneFactory);
086: assertEquals(0, map.size());
087: String s1 = (String) map.get("Five");
088: assertEquals("One", s1);
089: assertEquals(1, map.size());
090: String s2 = (String) map.get(new String(new char[] { 'F', 'i',
091: 'v', 'e' }));
092: assertEquals("One", s2);
093: assertEquals(1, map.size());
094: assertSame(s1, s2);
095:
096: map = makeTestSortedMap(nullFactory);
097: Object o = map.get("Five");
098: assertEquals(null, o);
099: assertEquals(1, map.size());
100:
101: }
102:
103: //-----------------------------------------------------------------------
104: public void testSortOrder() {
105: SortedMap map = makeTestSortedMap(oneFactory);
106: map.put("A", "a");
107: map.get("B"); // Entry with value "One" created
108: map.put("C", "c");
109: assertEquals("First key should be A", map.firstKey(), "A");
110: assertEquals("Last key should be C", map.lastKey(), "C");
111: assertEquals("First key in tail map should be B", map.tailMap(
112: "B").firstKey(), "B");
113: assertEquals("Last key in head map should be B", map.headMap(
114: "C").lastKey(), "B");
115: assertEquals("Last key in submap should be B", map.subMap("A",
116: "C").lastKey(), "B");
117:
118: Comparator c = map.comparator();
119: assertTrue("natural order, so comparator should be null",
120: c == null);
121: }
122:
123: public void testTransformerDecorate() {
124: Transformer transformer = TransformerUtils
125: .asTransformer(oneFactory);
126: SortedMap map = LazySortedMap.decorate(new TreeMap(),
127: transformer);
128: assertTrue(map instanceof LazySortedMap);
129: try {
130: map = LazySortedMap.decorate(new TreeMap(),
131: (Transformer) null);
132: fail("Expecting IllegalArgumentException for null transformer");
133: } catch (IllegalArgumentException e) {
134: // expected
135: }
136: try {
137: map = LazySortedMap.decorate(null, transformer);
138: fail("Expecting IllegalArgumentException for null map");
139: } catch (IllegalArgumentException e) {
140: // expected
141: }
142: }
143:
144: public String getCompatibilityVersion() {
145: return "3.1";
146: }
147:
148: // public void testCreate() throws Exception {
149: // resetEmpty();
150: // writeExternalFormToDisk(
151: // (java.io.Serializable) map,
152: // "D:/dev/collections/data/test/LazySortedMap.emptyCollection.version3.1.obj");
153: // resetFull();
154: // writeExternalFormToDisk(
155: // (java.io.Serializable) map,
156: // "D:/dev/collections/data/test/LazySortedMap.fullCollection.version3.1.obj");
157: // }
158: }
|