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 junit.framework.Test;
019: import junit.framework.TestSuite;
020: import junit.framework.Assert;
021:
022: import java.util.Map;
023: import java.util.HashMap;
024: import java.util.Collection;
025:
026: /**
027: * Extension of {@link AbstractTestMap} for exercising the
028: * {@link CompositeMap} implementation.
029: *
030: * @since Commons Collections 3.0
031: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
032: *
033: * @author Brian McCallister
034: */
035: public class TestCompositeMap extends AbstractTestMap {
036: /** used as a flag in MapMutator tests */
037: private boolean pass = false;
038:
039: public TestCompositeMap(String testName) {
040: super (testName);
041: }
042:
043: public static Test suite() {
044: return new TestSuite(TestCompositeMap.class);
045: }
046:
047: public void setUp() throws Exception {
048: super .setUp();
049: this .pass = false;
050: }
051:
052: public static void main(String args[]) {
053: String[] testCaseName = { TestCompositeMap.class.getName() };
054: junit.textui.TestRunner.main(testCaseName);
055: }
056:
057: public Map makeEmptyMap() {
058: CompositeMap map = new CompositeMap();
059: map.addComposited(new HashMap());
060: map.setMutator(new CompositeMap.MapMutator() {
061: public void resolveCollision(CompositeMap composite,
062: Map existing, Map added, Collection intersect) {
063: // Do nothing
064: }
065:
066: public Object put(CompositeMap map, Map[] composited,
067: Object key, Object value) {
068: return composited[0].put(key, value);
069: }
070:
071: public void putAll(CompositeMap map, Map[] composited, Map t) {
072: composited[0].putAll(t);
073: }
074:
075: });
076: return map;
077: }
078:
079: private Map buildOne() {
080: HashMap map = new HashMap();
081: map.put("1", "one");
082: map.put("2", "two");
083: return map;
084: }
085:
086: public Map buildTwo() {
087: HashMap map = new HashMap();
088: map.put("3", "three");
089: map.put("4", "four");
090: return map;
091: }
092:
093: public void testGet() {
094: CompositeMap map = new CompositeMap(buildOne(), buildTwo());
095: Assert.assertEquals("one", map.get("1"));
096: Assert.assertEquals("four", map.get("4"));
097: }
098:
099: public void testAddComposited() {
100: CompositeMap map = new CompositeMap(buildOne(), buildTwo());
101: HashMap three = new HashMap();
102: three.put("5", "five");
103: map.addComposited(three);
104: assertTrue(map.containsKey("5"));
105: try {
106: map.addComposited(three);
107: fail("Expecting IllegalArgumentException.");
108: } catch (IllegalArgumentException ex) {
109: // expected
110: }
111: }
112:
113: public void testRemoveComposited() {
114: CompositeMap map = new CompositeMap(buildOne(), buildTwo());
115: HashMap three = new HashMap();
116: three.put("5", "five");
117: map.addComposited(three);
118: assertTrue(map.containsKey("5"));
119:
120: map.removeComposited(three);
121: assertFalse(map.containsKey("5"));
122:
123: map.removeComposited(buildOne());
124: assertFalse(map.containsKey("2"));
125:
126: }
127:
128: public void testRemoveFromUnderlying() {
129: CompositeMap map = new CompositeMap(buildOne(), buildTwo());
130: HashMap three = new HashMap();
131: three.put("5", "five");
132: map.addComposited(three);
133: assertTrue(map.containsKey("5"));
134:
135: //Now remove "5"
136: three.remove("5");
137: assertFalse(map.containsKey("5"));
138: }
139:
140: public void testRemoveFromComposited() {
141: CompositeMap map = new CompositeMap(buildOne(), buildTwo());
142: HashMap three = new HashMap();
143: three.put("5", "five");
144: map.addComposited(three);
145: assertTrue(map.containsKey("5"));
146:
147: //Now remove "5"
148: map.remove("5");
149: assertFalse(three.containsKey("5"));
150: }
151:
152: public void testResolveCollision() {
153: CompositeMap map = new CompositeMap(buildOne(), buildTwo(),
154: new CompositeMap.MapMutator() {
155: public void resolveCollision(
156: CompositeMap composite, Map existing,
157: Map added, Collection intersect) {
158: pass = true;
159: }
160:
161: public Object put(CompositeMap map,
162: Map[] composited, Object key, Object value) {
163: throw new UnsupportedOperationException();
164: }
165:
166: public void putAll(CompositeMap map,
167: Map[] composited, Map t) {
168: throw new UnsupportedOperationException();
169: }
170: });
171:
172: map.addComposited(buildOne());
173: assertTrue(pass);
174: }
175:
176: public void testPut() {
177: CompositeMap map = new CompositeMap(buildOne(), buildTwo(),
178: new CompositeMap.MapMutator() {
179: public void resolveCollision(
180: CompositeMap composite, Map existing,
181: Map added, Collection intersect) {
182: throw new UnsupportedOperationException();
183: }
184:
185: public Object put(CompositeMap map,
186: Map[] composited, Object key, Object value) {
187: pass = true;
188: return "foo";
189: }
190:
191: public void putAll(CompositeMap map,
192: Map[] composited, Map t) {
193: throw new UnsupportedOperationException();
194: }
195: });
196:
197: map.put("willy", "wonka");
198: assertTrue(pass);
199: }
200:
201: public void testPutAll() {
202: CompositeMap map = new CompositeMap(buildOne(), buildTwo(),
203: new CompositeMap.MapMutator() {
204: public void resolveCollision(
205: CompositeMap composite, Map existing,
206: Map added, Collection intersect) {
207: throw new UnsupportedOperationException();
208: }
209:
210: public Object put(CompositeMap map,
211: Map[] composited, Object key, Object value) {
212: throw new UnsupportedOperationException();
213: }
214:
215: public void putAll(CompositeMap map,
216: Map[] composited, Map t) {
217: pass = true;
218: }
219: });
220:
221: map.putAll(null);
222: assertTrue(pass);
223: }
224: }
|