KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.Test;
19 import junit.framework.TestSuite;
20 import junit.framework.Assert;
21
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Collection JavaDoc;
25
26 /**
27  * Extension of {@link AbstractTestMap} for exercising the
28  * {@link CompositeMap} implementation.
29  *
30  * @since Commons Collections 3.0
31  * @version $Revision: 1.3 $ $Date: 2004/02/18 01:20:37 $
32  *
33  * @author Brian McCallister
34  */

35 public class TestCompositeMap extends AbstractTestMap {
36     /** used as a flag in MapMutator tests */
37     private boolean pass = false;
38     
39     public TestCompositeMap(String JavaDoc testName) {
40         super(testName);
41     }
42     
43     public static Test suite() {
44         return new TestSuite(TestCompositeMap.class);
45     }
46     
47     public void setUp() throws Exception JavaDoc {
48         super.setUp();
49         this.pass = false;
50     }
51     
52     public static void main(String JavaDoc args[]) {
53         String JavaDoc[] testCaseName = {TestCompositeMap.class.getName()};
54         junit.textui.TestRunner.main(testCaseName);
55     }
56     
57     public Map JavaDoc makeEmptyMap() {
58         CompositeMap map = new CompositeMap();
59         map.addComposited(new HashMap JavaDoc());
60         map.setMutator(new CompositeMap.MapMutator() {
61             public void resolveCollision(CompositeMap composite,
62             Map JavaDoc existing,
63             Map JavaDoc added,
64             Collection JavaDoc intersect) {
65                 // Do nothing
66
}
67             
68             public Object JavaDoc put(CompositeMap map, Map JavaDoc[] composited, Object JavaDoc key, Object JavaDoc value) {
69                 return composited[0].put(key, value);
70             }
71             
72             public void putAll(CompositeMap map, Map JavaDoc[] composited, Map JavaDoc t) {
73                 composited[0].putAll(t);
74             }
75             
76         });
77         return map;
78     }
79     
80     private Map JavaDoc buildOne() {
81         HashMap JavaDoc map = new HashMap JavaDoc();
82         map.put("1", "one");
83         map.put("2", "two");
84         return map;
85     }
86     
87     public Map JavaDoc buildTwo() {
88         HashMap JavaDoc map = new HashMap JavaDoc();
89         map.put("3", "three");
90         map.put("4", "four");
91         return map;
92     }
93     
94     public void testGet() {
95         CompositeMap map = new CompositeMap(buildOne(), buildTwo());
96         Assert.assertEquals("one", map.get("1"));
97         Assert.assertEquals("four", map.get("4"));
98     }
99     
100     public void testAddComposited() {
101         CompositeMap map = new CompositeMap(buildOne(), buildTwo());
102         HashMap JavaDoc three = new HashMap JavaDoc();
103         three.put("5", "five");
104         map.addComposited(three);
105         assertTrue(map.containsKey("5"));
106         try {
107             map.addComposited(three);
108             fail("Expecting IllegalArgumentException.");
109         } catch (IllegalArgumentException JavaDoc ex) {
110             // expected
111
}
112     }
113     
114     public void testRemoveComposited() {
115         CompositeMap map = new CompositeMap(buildOne(), buildTwo());
116         HashMap JavaDoc three = new HashMap JavaDoc();
117         three.put("5", "five");
118         map.addComposited(three);
119         assertTrue(map.containsKey("5"));
120         
121         map.removeComposited(three);
122         assertFalse(map.containsKey("5"));
123         
124         map.removeComposited(buildOne());
125         assertFalse(map.containsKey("2"));
126         
127     }
128     
129     public void testRemoveFromUnderlying() {
130         CompositeMap map = new CompositeMap(buildOne(), buildTwo());
131         HashMap JavaDoc three = new HashMap JavaDoc();
132         three.put("5", "five");
133         map.addComposited(three);
134         assertTrue(map.containsKey("5"));
135         
136         //Now remove "5"
137
three.remove("5");
138         assertFalse(map.containsKey("5"));
139     }
140     
141     public void testRemoveFromComposited() {
142         CompositeMap map = new CompositeMap(buildOne(), buildTwo());
143         HashMap JavaDoc three = new HashMap JavaDoc();
144         three.put("5", "five");
145         map.addComposited(three);
146         assertTrue(map.containsKey("5"));
147         
148         //Now remove "5"
149
map.remove("5");
150         assertFalse(three.containsKey("5"));
151     }
152     
153     public void testResolveCollision() {
154         CompositeMap map = new CompositeMap(buildOne(), buildTwo(),
155             new CompositeMap.MapMutator() {
156             public void resolveCollision(CompositeMap composite,
157             Map JavaDoc existing,
158             Map JavaDoc added,
159             Collection JavaDoc intersect) {
160                 pass = true;
161             }
162             
163             public Object JavaDoc put(CompositeMap map, Map JavaDoc[] composited, Object JavaDoc key,
164                 Object JavaDoc value) {
165                 throw new UnsupportedOperationException JavaDoc();
166             }
167             
168             public void putAll(CompositeMap map, Map JavaDoc[] composited, Map JavaDoc t) {
169                 throw new UnsupportedOperationException JavaDoc();
170             }
171         });
172         
173         map.addComposited(buildOne());
174         assertTrue(pass);
175     }
176     
177     public void testPut() {
178         CompositeMap map = new CompositeMap(buildOne(), buildTwo(),
179             new CompositeMap.MapMutator() {
180             public void resolveCollision(CompositeMap composite,
181             Map JavaDoc existing,
182             Map JavaDoc added,
183             Collection JavaDoc intersect) {
184                 throw new UnsupportedOperationException JavaDoc();
185             }
186             
187             public Object JavaDoc put(CompositeMap map, Map JavaDoc[] composited, Object JavaDoc key,
188                 Object JavaDoc value) {
189                 pass = true;
190                 return "foo";
191             }
192             
193             public void putAll(CompositeMap map, Map JavaDoc[] composited, Map JavaDoc t) {
194                 throw new UnsupportedOperationException JavaDoc();
195             }
196         });
197         
198         map.put("willy", "wonka");
199         assertTrue(pass);
200     }
201     
202     public void testPutAll() {
203         CompositeMap map = new CompositeMap(buildOne(), buildTwo(),
204             new CompositeMap.MapMutator() {
205             public void resolveCollision(CompositeMap composite,
206             Map JavaDoc existing,
207             Map JavaDoc added,
208             Collection JavaDoc intersect) {
209                 throw new UnsupportedOperationException JavaDoc();
210             }
211             
212             public Object JavaDoc put(CompositeMap map, Map JavaDoc[] composited, Object JavaDoc key,
213                 Object JavaDoc value) {
214                 throw new UnsupportedOperationException JavaDoc();
215             }
216             
217             public void putAll(CompositeMap map, Map JavaDoc[] composited, Map JavaDoc t) {
218                 pass = true;
219             }
220         });
221         
222         map.putAll(null);
223         assertTrue(pass);
224     }
225 }
226
227
Popular Tags