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.HashMap;
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.apache.commons.collections.Predicate;
026: import org.apache.commons.collections.PredicateUtils;
027:
028: /**
029: * Extension of {@link TestMap} for exercising the
030: * {@link PredicatedMap} implementation.
031: *
032: * @since Commons Collections 3.0
033: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
034: *
035: * @author Phil Steitz
036: */
037: public class TestPredicatedMap extends AbstractTestMap {
038:
039: protected static final Predicate truePredicate = PredicateUtils
040: .truePredicate();
041: protected static final Predicate testPredicate = new Predicate() {
042: public boolean evaluate(Object o) {
043: return (o instanceof String);
044: }
045: };
046:
047: public TestPredicatedMap(String testName) {
048: super (testName);
049: }
050:
051: public static Test suite() {
052: return new TestSuite(TestPredicatedMap.class);
053: }
054:
055: public static void main(String args[]) {
056: String[] testCaseName = { TestPredicatedMap.class.getName() };
057: junit.textui.TestRunner.main(testCaseName);
058: }
059:
060: //-----------------------------------------------------------------------
061: protected Map decorateMap(Map map, Predicate keyPredicate,
062: Predicate valuePredicate) {
063: return PredicatedMap
064: .decorate(map, keyPredicate, valuePredicate);
065: }
066:
067: public Map makeEmptyMap() {
068: return decorateMap(new HashMap(), truePredicate, truePredicate);
069: }
070:
071: public Map makeTestMap() {
072: return decorateMap(new HashMap(), testPredicate, testPredicate);
073: }
074:
075: //-----------------------------------------------------------------------
076: public void testEntrySet() {
077: Map map = makeTestMap();
078: assertTrue("returned entryset should not be null", map
079: .entrySet() != null);
080: map = decorateMap(new HashMap(), null, null);
081: map.put("oneKey", "oneValue");
082: assertTrue("returned entryset should contain one entry", map
083: .entrySet().size() == 1);
084: map = decorateMap(map, null, null);
085: }
086:
087: public void testPut() {
088: Map map = makeTestMap();
089: try {
090: map.put("Hi", new Integer(3));
091: fail("Illegal value should raise IllegalArgument");
092: } catch (IllegalArgumentException e) {
093: // expected
094: }
095:
096: try {
097: map.put(new Integer(3), "Hi");
098: fail("Illegal key should raise IllegalArgument");
099: } catch (IllegalArgumentException e) {
100: // expected
101: }
102:
103: assertTrue(!map.containsKey(new Integer(3)));
104: assertTrue(!map.containsValue(new Integer(3)));
105:
106: Map map2 = new HashMap();
107: map2.put("A", "a");
108: map2.put("B", "b");
109: map2.put("C", "c");
110: map2.put("c", new Integer(3));
111:
112: try {
113: map.putAll(map2);
114: fail("Illegal value should raise IllegalArgument");
115: } catch (IllegalArgumentException e) {
116: // expected
117: }
118:
119: map.put("E", "e");
120: Iterator iterator = map.entrySet().iterator();
121: try {
122: Map.Entry entry = (Map.Entry) iterator.next();
123: entry.setValue(new Integer(3));
124: fail("Illegal value should raise IllegalArgument");
125: } catch (IllegalArgumentException e) {
126: // expected
127: }
128:
129: map.put("F", "f");
130: iterator = map.entrySet().iterator();
131: Map.Entry entry = (Map.Entry) iterator.next();
132: entry.setValue("x");
133:
134: }
135:
136: public String getCompatibilityVersion() {
137: return "3.1";
138: }
139:
140: // public void testCreate() throws Exception {
141: // resetEmpty();
142: // writeExternalFormToDisk(
143: // (java.io.Serializable) map,
144: // "D:/dev/collections/data/test/PredicatedMap.emptyCollection.version3.1.obj");
145: // resetFull();
146: // writeExternalFormToDisk(
147: // (java.io.Serializable) map,
148: // "D:/dev/collections/data/test/PredicatedMap.fullCollection.version3.1.obj");
149: // }
150: }
|