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.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.SortedMap;
023: import java.util.TreeMap;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: import org.apache.commons.collections.Predicate;
029: import org.apache.commons.collections.PredicateUtils;
030:
031: /**
032: * Extension of {@link TestPredicatedMap} for exercising the
033: * {@link PredicatedSortedMap} 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 TestPredicatedSortedMap extends AbstractTestSortedMap {
041:
042: protected static final Predicate truePredicate = PredicateUtils
043: .truePredicate();
044: protected static final Predicate testPredicate = new Predicate() {
045: public boolean evaluate(Object o) {
046: return (o instanceof String);
047: }
048: };
049:
050: public TestPredicatedSortedMap(String testName) {
051: super (testName);
052: }
053:
054: public static Test suite() {
055: return new TestSuite(TestPredicatedSortedMap.class);
056: }
057:
058: public static void main(String args[]) {
059: String[] testCaseName = { TestPredicatedSortedMap.class
060: .getName() };
061: junit.textui.TestRunner.main(testCaseName);
062: }
063:
064: //-----------------------------------------------------------------------
065: protected SortedMap decorateMap(SortedMap map,
066: Predicate keyPredicate, Predicate valuePredicate) {
067: return PredicatedSortedMap.decorate(map, keyPredicate,
068: valuePredicate);
069: }
070:
071: public Map makeEmptyMap() {
072: return decorateMap(new TreeMap(), truePredicate, truePredicate);
073: }
074:
075: public Map makeTestMap() {
076: return decorateMap(new TreeMap(), testPredicate, testPredicate);
077: }
078:
079: public SortedMap makeTestSortedMap() {
080: return decorateMap(new TreeMap(), testPredicate, testPredicate);
081: }
082:
083: public boolean isSubMapViewsSerializable() {
084: // TreeMap sub map views have a bug in deserialization.
085: return false;
086: }
087:
088: public boolean isAllowNullKey() {
089: return false;
090: }
091:
092: // from TestPredicatedMap
093: //-----------------------------------------------------------------------
094: public void testEntrySet() {
095: SortedMap map = makeTestSortedMap();
096: assertTrue("returned entryset should not be null", map
097: .entrySet() != null);
098: map = decorateMap(new TreeMap(), null, null);
099: map.put("oneKey", "oneValue");
100: assertTrue("returned entryset should contain one entry", map
101: .entrySet().size() == 1);
102: map = decorateMap(map, null, null);
103: }
104:
105: public void testPut() {
106: Map map = makeTestMap();
107: try {
108: map.put("Hi", new Integer(3));
109: fail("Illegal value should raise IllegalArgument");
110: } catch (IllegalArgumentException e) {
111: // expected
112: }
113:
114: try {
115: map.put(new Integer(3), "Hi");
116: fail("Illegal key should raise IllegalArgument");
117: } catch (IllegalArgumentException e) {
118: // expected
119: }
120:
121: assertTrue(!map.containsKey(new Integer(3)));
122: assertTrue(!map.containsValue(new Integer(3)));
123:
124: Map map2 = new HashMap();
125: map2.put("A", "a");
126: map2.put("B", "b");
127: map2.put("C", "c");
128: map2.put("c", new Integer(3));
129:
130: try {
131: map.putAll(map2);
132: fail("Illegal value should raise IllegalArgument");
133: } catch (IllegalArgumentException e) {
134: // expected
135: }
136:
137: map.put("E", "e");
138: Iterator iterator = map.entrySet().iterator();
139: try {
140: Map.Entry entry = (Map.Entry) iterator.next();
141: entry.setValue(new Integer(3));
142: fail("Illegal value should raise IllegalArgument");
143: } catch (IllegalArgumentException e) {
144: // expected
145: }
146:
147: map.put("F", "f");
148: iterator = map.entrySet().iterator();
149: Map.Entry entry = (Map.Entry) iterator.next();
150: entry.setValue("x");
151:
152: }
153:
154: //-----------------------------------------------------------------------
155: public void testSortOrder() {
156: SortedMap map = makeTestSortedMap();
157: map.put("A", "a");
158: map.put("B", "b");
159: try {
160: map.put(null, "c");
161: fail("Null key should raise IllegalArgument");
162: } catch (IllegalArgumentException e) {
163: // expected
164: }
165: map.put("C", "c");
166: try {
167: map.put("D", null);
168: fail("Null value should raise IllegalArgument");
169: } catch (IllegalArgumentException e) {
170: // expected
171: }
172: assertEquals("First key should be A", map.firstKey(), "A");
173: assertEquals("Last key should be C", map.lastKey(), "C");
174: assertEquals("First key in tail map should be B", map.tailMap(
175: "B").firstKey(), "B");
176: assertEquals("Last key in head map should be B", map.headMap(
177: "C").lastKey(), "B");
178: assertEquals("Last key in submap should be B", map.subMap("A",
179: "C").lastKey(), "B");
180:
181: Comparator c = map.comparator();
182: assertTrue("natural order, so comparator should be null",
183: c == null);
184: }
185:
186: public String getCompatibilityVersion() {
187: return "3.1";
188: }
189:
190: // public void testCreate() throws Exception {
191: // resetEmpty();
192: // writeExternalFormToDisk(
193: // (java.io.Serializable) map,
194: // "D:/dev/collections/data/test/PredicatedSortedMap.emptyCollection.version3.1.obj");
195: // resetFull();
196: // writeExternalFormToDisk(
197: // (java.io.Serializable) map,
198: // "D:/dev/collections/data/test/PredicatedSortedMap.fullCollection.version3.1.obj");
199: // }
200: }
|