KickJava   Java API By Example, From Geeks To Geeks.

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


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

40 public class TestPredicatedSortedMap extends AbstractTestSortedMap{
41     
42     protected static final Predicate truePredicate = PredicateUtils.truePredicate();
43     protected static final Predicate testPredicate = new Predicate() {
44         public boolean evaluate(Object JavaDoc o) {
45             return (o instanceof String JavaDoc);
46         }
47     };
48     
49     public TestPredicatedSortedMap(String JavaDoc testName) {
50         super(testName);
51     }
52     
53     public static Test suite() {
54         return new TestSuite(TestPredicatedSortedMap.class);
55     }
56     
57     public static void main(String JavaDoc args[]) {
58         String JavaDoc[] testCaseName = { TestPredicatedSortedMap.class.getName()};
59         junit.textui.TestRunner.main(testCaseName);
60     }
61
62     //-----------------------------------------------------------------------
63
protected SortedMap JavaDoc decorateMap(SortedMap JavaDoc map, Predicate keyPredicate,
64         Predicate valuePredicate) {
65         return PredicatedSortedMap.decorate(map, keyPredicate, valuePredicate);
66     }
67     
68     public Map JavaDoc makeEmptyMap() {
69         return decorateMap(new TreeMap JavaDoc(), truePredicate, truePredicate);
70     }
71    
72     public Map JavaDoc makeTestMap() {
73         return decorateMap(new TreeMap JavaDoc(), testPredicate, testPredicate);
74     }
75     
76     public SortedMap JavaDoc makeTestSortedMap() {
77         return decorateMap(new TreeMap JavaDoc(), testPredicate, testPredicate);
78     }
79     
80     public boolean isSubMapViewsSerializable() {
81         // TreeMap sub map views have a bug in deserialization.
82
return false;
83     }
84
85     public boolean isAllowNullKey() {
86         return false;
87     }
88
89     // from TestPredicatedMap
90
//-----------------------------------------------------------------------
91
public void testEntrySet() {
92         SortedMap JavaDoc map = makeTestSortedMap();
93         assertTrue("returned entryset should not be null",
94             map.entrySet() != null);
95         map = decorateMap(new TreeMap JavaDoc(), null, null);
96         map.put("oneKey", "oneValue");
97         assertTrue("returned entryset should contain one entry",
98             map.entrySet().size() == 1);
99         map = decorateMap(map, null, null);
100     }
101     
102     public void testPut() {
103         Map JavaDoc map = makeTestMap();
104         try {
105             map.put("Hi", new Integer JavaDoc(3));
106             fail("Illegal value should raise IllegalArgument");
107         } catch (IllegalArgumentException JavaDoc e) {
108             // expected
109
}
110
111         try {
112             map.put(new Integer JavaDoc(3), "Hi");
113             fail("Illegal key should raise IllegalArgument");
114         } catch (IllegalArgumentException JavaDoc e) {
115             // expected
116
}
117
118         assertTrue(!map.containsKey(new Integer JavaDoc(3)));
119         assertTrue(!map.containsValue(new Integer JavaDoc(3)));
120
121         Map JavaDoc map2 = new HashMap JavaDoc();
122         map2.put("A", "a");
123         map2.put("B", "b");
124         map2.put("C", "c");
125         map2.put("c", new Integer JavaDoc(3));
126
127         try {
128             map.putAll(map2);
129             fail("Illegal value should raise IllegalArgument");
130         } catch (IllegalArgumentException JavaDoc e) {
131             // expected
132
}
133
134         map.put("E", "e");
135         Iterator JavaDoc iterator = map.entrySet().iterator();
136         try {
137             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
138             entry.setValue(new Integer JavaDoc(3));
139             fail("Illegal value should raise IllegalArgument");
140         } catch (IllegalArgumentException JavaDoc e) {
141             // expected
142
}
143         
144         map.put("F", "f");
145         iterator = map.entrySet().iterator();
146         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
147         entry.setValue("x");
148         
149     }
150
151     //-----------------------------------------------------------------------
152
public void testSortOrder() {
153         SortedMap JavaDoc map = makeTestSortedMap();
154         map.put("A", "a");
155         map.put("B", "b");
156         try {
157             map.put(null, "c");
158             fail("Null key should raise IllegalArgument");
159         } catch (IllegalArgumentException JavaDoc e) {
160             // expected
161
}
162         map.put("C", "c");
163         try {
164             map.put("D", null);
165             fail("Null value should raise IllegalArgument");
166         } catch (IllegalArgumentException JavaDoc e) {
167             // expected
168
}
169         assertEquals("First key should be A", map.firstKey(), "A");
170         assertEquals("Last key should be C", map.lastKey(), "C");
171         assertEquals("First key in tail map should be B",
172             map.tailMap("B").firstKey(), "B");
173         assertEquals("Last key in head map should be B",
174             map.headMap("C").lastKey(), "B");
175         assertEquals("Last key in submap should be B",
176            map.subMap("A","C").lastKey(), "B");
177         
178         Comparator JavaDoc c = map.comparator();
179         assertTrue("natural order, so comparator should be null",
180             c == null);
181     }
182
183     public String JavaDoc getCompatibilityVersion() {
184         return "3.1";
185     }
186
187 // public void testCreate() throws Exception {
188
// resetEmpty();
189
// writeExternalFormToDisk(
190
// (java.io.Serializable) map,
191
// "D:/dev/collections/data/test/PredicatedSortedMap.emptyCollection.version3.1.obj");
192
// resetFull();
193
// writeExternalFormToDisk(
194
// (java.io.Serializable) map,
195
// "D:/dev/collections/data/test/PredicatedSortedMap.fullCollection.version3.1.obj");
196
// }
197
}
Popular Tags