KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2005 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.Map JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 import org.apache.commons.collections.Factory;
25 import org.apache.commons.collections.FactoryUtils;
26 import org.apache.commons.collections.Transformer;
27 import org.apache.commons.collections.functors.ConstantFactory;
28
29 /**
30  * Extension of {@link TestMap} for exercising the
31  * {@link DefaultedMap} implementation.
32  *
33  * @since Commons Collections 3.2
34  * @version $Revision: 155406 $ $Date: 2005-03-22 22:53:50 +0000 (Tue, 22 Mar 2005) $
35  *
36  * @author Stephen Colebourne
37  */

38 public class TestDefaultedMap extends AbstractTestMap {
39
40     protected static final Factory nullFactory = FactoryUtils.nullFactory();
41
42     public TestDefaultedMap(String JavaDoc testName) {
43         super(testName);
44     }
45
46     public static Test suite() {
47         return new TestSuite(TestDefaultedMap.class);
48     }
49
50     public static void main(String JavaDoc args[]) {
51         String JavaDoc[] testCaseName = { TestDefaultedMap.class.getName()};
52         junit.textui.TestRunner.main(testCaseName);
53     }
54
55     //-----------------------------------------------------------------------
56
public Map JavaDoc makeEmptyMap() {
57         return DefaultedMap.decorate(new HashMap JavaDoc(), nullFactory);
58     }
59
60     //-----------------------------------------------------------------------
61
public void testMapGet() {
62         Map JavaDoc map = new DefaultedMap("NULL");
63         
64         assertEquals(0, map.size());
65         assertEquals(false, map.containsKey("NotInMap"));
66         assertEquals("NULL", map.get("NotInMap"));
67         
68         map.put("Key", "Value");
69         assertEquals(1, map.size());
70         assertEquals(true, map.containsKey("Key"));
71         assertEquals("Value", map.get("Key"));
72         assertEquals(false, map.containsKey("NotInMap"));
73         assertEquals("NULL", map.get("NotInMap"));
74     }
75
76     public void testMapGet2() {
77         HashMap JavaDoc base = new HashMap JavaDoc();
78         Map JavaDoc map = DefaultedMap.decorate(base, "NULL");
79         
80         assertEquals(0, map.size());
81         assertEquals(0, base.size());
82         assertEquals(false, map.containsKey("NotInMap"));
83         assertEquals("NULL", map.get("NotInMap"));
84         
85         map.put("Key", "Value");
86         assertEquals(1, map.size());
87         assertEquals(1, base.size());
88         assertEquals(true, map.containsKey("Key"));
89         assertEquals("Value", map.get("Key"));
90         assertEquals(false, map.containsKey("NotInMap"));
91         assertEquals("NULL", map.get("NotInMap"));
92     }
93
94     public void testMapGet3() {
95         HashMap JavaDoc base = new HashMap JavaDoc();
96         Map JavaDoc map = DefaultedMap.decorate(base, ConstantFactory.getInstance("NULL"));
97         
98         assertEquals(0, map.size());
99         assertEquals(0, base.size());
100         assertEquals(false, map.containsKey("NotInMap"));
101         assertEquals("NULL", map.get("NotInMap"));
102         
103         map.put("Key", "Value");
104         assertEquals(1, map.size());
105         assertEquals(1, base.size());
106         assertEquals(true, map.containsKey("Key"));
107         assertEquals("Value", map.get("Key"));
108         assertEquals(false, map.containsKey("NotInMap"));
109         assertEquals("NULL", map.get("NotInMap"));
110     }
111
112     public void testMapGet4() {
113         HashMap JavaDoc base = new HashMap JavaDoc();
114         Map JavaDoc map = DefaultedMap.decorate(base, new Transformer() {
115             public Object JavaDoc transform(Object JavaDoc input) {
116                 if (input instanceof String JavaDoc) {
117                     return "NULL";
118                 }
119                 return "NULL_OBJECT";
120             }
121         });
122         
123         assertEquals(0, map.size());
124         assertEquals(0, base.size());
125         assertEquals(false, map.containsKey("NotInMap"));
126         assertEquals("NULL", map.get("NotInMap"));
127         assertEquals("NULL_OBJECT", map.get(new Integer JavaDoc(0)));
128         
129         map.put("Key", "Value");
130         assertEquals(1, map.size());
131         assertEquals(1, base.size());
132         assertEquals(true, map.containsKey("Key"));
133         assertEquals("Value", map.get("Key"));
134         assertEquals(false, map.containsKey("NotInMap"));
135         assertEquals("NULL", map.get("NotInMap"));
136         assertEquals("NULL_OBJECT", map.get(new Integer JavaDoc(0)));
137     }
138
139     public String JavaDoc getCompatibilityVersion() {
140         return "3.2";
141     }
142
143 // public void testCreate() throws Exception {
144
// resetEmpty();
145
// writeExternalFormToDisk(
146
// (java.io.Serializable) map,
147
// "c:/commons/collections/data/test/DefaultedMap.emptyCollection.version3.2.obj");
148
// resetFull();
149
// writeExternalFormToDisk(
150
// (java.io.Serializable) map,
151
// "c:/commons/collections/data/test/DefaultedMap.fullCollection.version3.2.obj");
152
// }
153

154 }
155
Popular Tags