KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2003-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.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.commons.collections.Predicate;
26 import org.apache.commons.collections.PredicateUtils;
27
28 /**
29  * Extension of {@link TestMap} for exercising the
30  * {@link PredicatedMap} implementation.
31  *
32  * @since Commons Collections 3.0
33  * @version $Revision: 1.6 $ $Date: 2004/04/09 09:43:09 $
34  *
35  * @author Phil Steitz
36  */

37 public class TestPredicatedMap extends AbstractTestMap{
38     
39     protected static final Predicate truePredicate = PredicateUtils.truePredicate();
40     protected static final Predicate testPredicate = new Predicate() {
41         public boolean evaluate(Object JavaDoc o) {
42             return (o instanceof String JavaDoc);
43         }
44     };
45     
46     
47     public TestPredicatedMap(String JavaDoc testName) {
48         super(testName);
49     }
50     
51     public static Test suite() {
52         return new TestSuite(TestPredicatedMap.class);
53     }
54     
55     public static void main(String JavaDoc args[]) {
56         String JavaDoc[] testCaseName = { TestPredicatedMap.class.getName()};
57         junit.textui.TestRunner.main(testCaseName);
58     }
59
60     //-----------------------------------------------------------------------
61
protected Map JavaDoc decorateMap(Map JavaDoc map, Predicate keyPredicate,
62         Predicate valuePredicate) {
63         return PredicatedMap.decorate(map, keyPredicate, valuePredicate);
64     }
65     
66     public Map JavaDoc makeEmptyMap() {
67         return decorateMap(new HashMap JavaDoc(), truePredicate, truePredicate);
68     }
69     
70     public Map JavaDoc makeTestMap() {
71         return decorateMap(new HashMap JavaDoc(), testPredicate, testPredicate);
72     }
73
74     //-----------------------------------------------------------------------
75
public void testEntrySet() {
76         Map JavaDoc map = makeTestMap();
77         assertTrue("returned entryset should not be null",
78             map.entrySet() != null);
79         map = decorateMap(new HashMap JavaDoc(), null, null);
80         map.put("oneKey", "oneValue");
81         assertTrue("returned entryset should contain one entry",
82             map.entrySet().size() == 1);
83         map = decorateMap(map, null, null);
84     }
85     
86     public void testPut() {
87         Map JavaDoc map = makeTestMap();
88         try {
89             map.put("Hi", new Integer JavaDoc(3));
90             fail("Illegal value should raise IllegalArgument");
91         } catch (IllegalArgumentException JavaDoc e) {
92             // expected
93
}
94
95         try {
96             map.put(new Integer JavaDoc(3), "Hi");
97             fail("Illegal key should raise IllegalArgument");
98         } catch (IllegalArgumentException JavaDoc e) {
99             // expected
100
}
101
102         assertTrue(!map.containsKey(new Integer JavaDoc(3)));
103         assertTrue(!map.containsValue(new Integer JavaDoc(3)));
104
105         Map JavaDoc map2 = new HashMap JavaDoc();
106         map2.put("A", "a");
107         map2.put("B", "b");
108         map2.put("C", "c");
109         map2.put("c", new Integer JavaDoc(3));
110
111         try {
112             map.putAll(map2);
113             fail("Illegal value should raise IllegalArgument");
114         } catch (IllegalArgumentException JavaDoc e) {
115             // expected
116
}
117
118         map.put("E", "e");
119         Iterator JavaDoc iterator = map.entrySet().iterator();
120         try {
121             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
122             entry.setValue(new Integer JavaDoc(3));
123             fail("Illegal value should raise IllegalArgument");
124         } catch (IllegalArgumentException JavaDoc e) {
125             // expected
126
}
127         
128         map.put("F", "f");
129         iterator = map.entrySet().iterator();
130         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
131         entry.setValue("x");
132         
133     }
134
135     public String JavaDoc getCompatibilityVersion() {
136         return "3.1";
137     }
138
139 // public void testCreate() throws Exception {
140
// resetEmpty();
141
// writeExternalFormToDisk(
142
// (java.io.Serializable) map,
143
// "D:/dev/collections/data/test/PredicatedMap.emptyCollection.version3.1.obj");
144
// resetFull();
145
// writeExternalFormToDisk(
146
// (java.io.Serializable) map,
147
// "D:/dev/collections/data/test/PredicatedMap.fullCollection.version3.1.obj");
148
// }
149
}
Popular Tags