KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestBeanMap


1 /*
2  * Copyright 2001-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;
17
18 import java.io.Serializable JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import junit.framework.Test;
23 import junit.textui.TestRunner;
24
25 import org.apache.commons.collections.map.AbstractTestMap;
26
27 /**
28  * Test cases for BeanMap
29  *
30  * @version $Revision: 1.18 $ $Date: 2004/02/18 01:20:35 $
31  *
32  * @author Morgan Delagrange
33  * @author Stephen Colebourne
34  */

35 public class TestBeanMap extends AbstractTestMap {
36
37     public TestBeanMap(String JavaDoc testName) {
38         super(testName);
39     }
40     
41     public static void main(String JavaDoc[] args) {
42         TestRunner.run(suite());
43     }
44
45     public static Test suite() {
46         return BulkTest.makeSuite(TestBeanMap.class);
47     }
48
49 /*
50   note to self. The getter and setter methods were generated by copying the
51   field declarations and using the following regular expression search and
52   replace:
53
54   From:
55         private \(.*\) some\(.*\);
56   To:
57         public \1 getSome\2Value() {
58             return some\2;
59         }
60         public void setSome\2Value(\1 value) {
61             some\2 = value;
62         }
63
64   Also note: The sample keys and mappings were generated manually.
65 */

66
67
68     public static class BeanWithProperties implements Serializable JavaDoc {
69         private int someInt;
70         private long someLong;
71         private double someDouble;
72         private float someFloat;
73         private short someShort;
74         private byte someByte;
75         private char someChar;
76         private Integer JavaDoc someInteger;
77         private String JavaDoc someString;
78         private Object JavaDoc someObject;
79
80         public int getSomeIntValue() {
81             return someInt;
82         }
83         public void setSomeIntValue(int value) {
84             someInt = value;
85         }
86
87         public long getSomeLongValue() {
88             return someLong;
89         }
90         public void setSomeLongValue(long value) {
91             someLong = value;
92         }
93
94         public double getSomeDoubleValue() {
95             return someDouble;
96         }
97         public void setSomeDoubleValue(double value) {
98             someDouble = value;
99         }
100
101         public float getSomeFloatValue() {
102             return someFloat;
103         }
104         public void setSomeFloatValue(float value) {
105             someFloat = value;
106         }
107
108         public short getSomeShortValue() {
109             return someShort;
110         }
111         public void setSomeShortValue(short value) {
112             someShort = value;
113         }
114
115         public byte getSomeByteValue() {
116             return someByte;
117         }
118         public void setSomeByteValue(byte value) {
119             someByte = value;
120         }
121
122         public char getSomeCharValue() {
123             return someChar;
124         }
125         public void setSomeCharValue(char value) {
126             someChar = value;
127         }
128
129         public String JavaDoc getSomeStringValue() {
130             return someString;
131         }
132         public void setSomeStringValue(String JavaDoc value) {
133             someString = value;
134         }
135
136         public Integer JavaDoc getSomeIntegerValue() {
137             return someInteger;
138         }
139         public void setSomeIntegerValue(Integer JavaDoc value) {
140             someInteger = value;
141         }
142
143         public Object JavaDoc getSomeObjectValue() {
144             return someObject;
145         }
146         public void setSomeObjectValue(Object JavaDoc value) {
147             someObject = value;
148         }
149     }
150     
151     // note to self. The Sample keys were generated by copying the field
152
// declarations and using the following regular expression search and replace:
153
//
154
// From:
155
// private \(.*\) some\(.*\);
156
// To:
157
// "some\2Value",
158
//
159
// Then, I manually added the "class" key, which is a property that exists for
160
// all beans (and all objects for that matter.
161
public Object JavaDoc[] getSampleKeys() {
162         Object JavaDoc[] keys = new Object JavaDoc[] {
163             "someIntValue",
164             "someLongValue",
165             "someDoubleValue",
166             "someFloatValue",
167             "someShortValue",
168             "someByteValue",
169             "someCharValue",
170             "someIntegerValue",
171             "someStringValue",
172             "someObjectValue",
173             "class",
174         };
175         return keys;
176     }
177
178     /**
179      * An object value that will be stored in the bean map as a value. Need
180      * to save this externally so that we can make sure the object instances
181      * are equivalent since getSampleValues() would otherwise construct a new
182      * and different Object each time.
183      **/

184     private Object JavaDoc objectInFullMap = new Object JavaDoc();
185
186     // note to self: the sample values were created manually
187
public Object JavaDoc[] getSampleValues() {
188         Object JavaDoc[] values = new Object JavaDoc[] {
189             new Integer JavaDoc(1234),
190             new Long JavaDoc(1298341928234L),
191             new Double JavaDoc(123423.34),
192             new Float JavaDoc(1213332.12f),
193             new Short JavaDoc((short)134),
194             new Byte JavaDoc((byte)10),
195             new Character JavaDoc('a'),
196             new Integer JavaDoc(1432),
197             "SomeStringValue",
198             objectInFullMap,
199             BeanWithProperties.class,
200         };
201         return values;
202     }
203
204     public Object JavaDoc[] getNewSampleValues() {
205         Object JavaDoc[] values = new Object JavaDoc[] {
206             new Integer JavaDoc(223),
207             new Long JavaDoc(23341928234L),
208             new Double JavaDoc(23423.34),
209             new Float JavaDoc(213332.12f),
210             new Short JavaDoc((short)234),
211             new Byte JavaDoc((byte)20),
212             new Character JavaDoc('b'),
213             new Integer JavaDoc(232),
214             "SomeNewStringValue",
215             new Object JavaDoc(),
216             null,
217         };
218         return values;
219     }
220
221     /**
222      * Values is a dead copy in BeanMap, so refresh each time.
223      */

224     public void verifyValues() {
225         values = map.values();
226         super.verifyValues();
227     }
228
229     /**
230      * The mappings in a BeanMap are fixed on the properties the underlying
231      * bean has. Adding and removing mappings is not possible, thus this
232      * method is overridden to return false.
233      */

234     public boolean isPutAddSupported() {
235         return false;
236     }
237
238     /**
239      * The mappings in a BeanMap are fixed on the properties the underlying
240      * bean has. Adding and removing mappings is not possible, thus this
241      * method is overridden to return false.
242      */

243     public boolean isRemoveSupported() {
244         return false;
245     }
246
247     public Map JavaDoc makeFullMap() {
248         // note: These values must match (i.e. .equals() must return true)
249
// those returned from getSampleValues().
250
BeanWithProperties bean = new BeanWithProperties();
251         bean.setSomeIntValue(1234);
252         bean.setSomeLongValue(1298341928234L);
253         bean.setSomeDoubleValue(123423.34);
254         bean.setSomeFloatValue(1213332.12f);
255         bean.setSomeShortValue((short)134);
256         bean.setSomeByteValue((byte)10);
257         bean.setSomeCharValue('a');
258         bean.setSomeIntegerValue(new Integer JavaDoc(1432));
259         bean.setSomeStringValue("SomeStringValue");
260         bean.setSomeObjectValue(objectInFullMap);
261         return new BeanMap(bean);
262     }
263
264     public Map JavaDoc makeEmptyMap() {
265         return new BeanMap();
266     }
267
268     public String JavaDoc[] ignoredTests() {
269         // Ignore the serialization tests on collection views.
270
return new String JavaDoc[] {
271          "TestBeanMap.bulkTestMapEntrySet.testCanonicalEmptyCollectionExists",
272          "TestBeanMap.bulkTestMapEntrySet.testCanonicalFullCollectionExists",
273          "TestBeanMap.bulkTestMapKeySet.testCanonicalEmptyCollectionExists",
274          "TestBeanMap.bulkTestMapKeySet.testCanonicalFullCollectionExists",
275          "TestBeanMap.bulkTestMapValues.testCanonicalEmptyCollectionExists",
276          "TestBeanMap.bulkTestMapValues.testCanonicalFullCollectionExists",
277          "TestBeanMap.bulkTestMapEntrySet.testSimpleSerialization",
278          "TestBeanMap.bulkTestMapKeySet.testSimpleSerialization",
279          "TestBeanMap.bulkTestMapEntrySet.testSerializeDeserializeThenCompare",
280          "TestBeanMap.bulkTestMapKeySet.testSerializeDeserializeThenCompare"
281         };
282     }
283
284     /**
285      * Need to override this method because the "clear()" method on the bean
286      * map just returns the bean properties to their default states. It does
287      * not actually remove the mappings as per the map contract. The default
288      * testClear() methods checks that the clear method throws an
289      * UnsupportedOperationException since this class is not add/remove
290      * modifiable. In our case though, we do not always throw that exception.
291      */

292     public void testMapClear() {
293         //TODO: make sure a call to BeanMap.clear returns the bean to its
294
//default initialization values.
295
}
296
297     /**
298      * Need to override this method because the "put()" method on the bean
299      * doesn't work for this type of Map.
300      */

301     public void testMapPut() {
302         // see testBeanMapPutAllWriteable
303
}
304
305     public void testBeanMapClone() {
306         BeanMap map = (BeanMap)makeFullMap();
307         try {
308             BeanMap map2 = (BeanMap)((BeanMap)map).clone();
309
310             // make sure containsKey is working to verify the bean was cloned
311
// ok, and the read methods were properly initialized
312
Object JavaDoc[] keys = getSampleKeys();
313             for(int i = 0; i < keys.length; i++) {
314                 assertTrue("Cloned BeanMap should contain the same keys",
315                            map2.containsKey(keys[i]));
316             }
317         } catch (CloneNotSupportedException JavaDoc exception) {
318             fail("BeanMap.clone() should not throw a " +
319                  "CloneNotSupportedException when clone should succeed.");
320         }
321     }
322
323     public void testBeanMapPutAllWriteable() {
324         BeanMap map1 = (BeanMap)makeFullMap();
325         BeanMap map2 = (BeanMap)makeFullMap();
326         map2.put("someIntValue", new Integer JavaDoc(0));
327         map1.putAllWriteable(map2);
328         assertEquals(map1.get("someIntValue"), new Integer JavaDoc(0));
329     }
330
331     public void testMethodAccessor() throws Exception JavaDoc {
332         BeanMap map = (BeanMap) makeFullMap();
333         Method JavaDoc method = BeanWithProperties.class.getDeclaredMethod("getSomeIntegerValue", null);
334         assertEquals(method, map.getReadMethod("someIntegerValue"));
335     }
336     
337     public void testMethodMutator() throws Exception JavaDoc {
338         BeanMap map = (BeanMap) makeFullMap();
339         Method JavaDoc method = BeanWithProperties.class.getDeclaredMethod("setSomeIntegerValue", new Class JavaDoc[] {Integer JavaDoc.class});
340         assertEquals(method, map.getWriteMethod("someIntegerValue"));
341     }
342     
343 }
344
Popular Tags