001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.beanutils;
018:
019: import java.io.Serializable;
020: import java.lang.reflect.Method;
021: import java.util.Map;
022: import java.util.HashMap;
023:
024: import junit.framework.Test;
025: import junit.textui.TestRunner;
026:
027: import org.apache.commons.collections.map.AbstractTestMap;
028: import org.apache.commons.collections.BulkTest;
029: import org.apache.commons.collections.Transformer;
030:
031: /**
032: * Test cases for BeanMap
033: *
034: * @version $Revision: 557796 $ $Date: 2007-07-19 23:28:49 +0100 (Thu, 19 Jul 2007) $
035: *
036: * @author Morgan Delagrange
037: * @author Stephen Colebourne
038: */
039: public class BeanMapTestCase extends AbstractTestMap {
040:
041: public BeanMapTestCase(String testName) {
042: super (testName);
043: }
044:
045: public static void main(String[] args) {
046: TestRunner.run(suite());
047: }
048:
049: public static Test suite() {
050: return BulkTest.makeSuite(BeanMapTestCase.class);
051: }
052:
053: /*
054: note to self. The getter and setter methods were generated by copying the
055: field declarations and using the following regular expression search and
056: replace:
057:
058: From:
059: private \(.*\) some\(.*\);
060: To:
061: public \1 getSome\2Value() {
062: return some\2;
063: }
064: public void setSome\2Value(\1 value) {
065: some\2 = value;
066: }
067:
068: Also note: The sample keys and mappings were generated manually.
069: */
070:
071: public static class BeanWithProperties implements Serializable {
072: private int someInt;
073: private long someLong;
074: private double someDouble;
075: private float someFloat;
076: private short someShort;
077: private byte someByte;
078: private char someChar;
079: private Integer someInteger;
080: private String someString;
081: private Object someObject;
082:
083: public int getSomeIntValue() {
084: return someInt;
085: }
086:
087: public void setSomeIntValue(int value) {
088: someInt = value;
089: }
090:
091: public long getSomeLongValue() {
092: return someLong;
093: }
094:
095: public void setSomeLongValue(long value) {
096: someLong = value;
097: }
098:
099: public double getSomeDoubleValue() {
100: return someDouble;
101: }
102:
103: public void setSomeDoubleValue(double value) {
104: someDouble = value;
105: }
106:
107: public float getSomeFloatValue() {
108: return someFloat;
109: }
110:
111: public void setSomeFloatValue(float value) {
112: someFloat = value;
113: }
114:
115: public short getSomeShortValue() {
116: return someShort;
117: }
118:
119: public void setSomeShortValue(short value) {
120: someShort = value;
121: }
122:
123: public byte getSomeByteValue() {
124: return someByte;
125: }
126:
127: public void setSomeByteValue(byte value) {
128: someByte = value;
129: }
130:
131: public char getSomeCharValue() {
132: return someChar;
133: }
134:
135: public void setSomeCharValue(char value) {
136: someChar = value;
137: }
138:
139: public String getSomeStringValue() {
140: return someString;
141: }
142:
143: public void setSomeStringValue(String value) {
144: someString = value;
145: }
146:
147: public Integer getSomeIntegerValue() {
148: return someInteger;
149: }
150:
151: public void setSomeIntegerValue(Integer value) {
152: someInteger = value;
153: }
154:
155: public Object getSomeObjectValue() {
156: return someObject;
157: }
158:
159: public void setSomeObjectValue(Object value) {
160: someObject = value;
161: }
162: }
163:
164: // note to self. The Sample keys were generated by copying the field
165: // declarations and using the following regular expression search and replace:
166: //
167: // From:
168: // private \(.*\) some\(.*\);
169: // To:
170: // "some\2Value",
171: //
172: // Then, I manually added the "class" key, which is a property that exists for
173: // all beans (and all objects for that matter.
174: public Object[] getSampleKeys() {
175: Object[] keys = new Object[] { "someIntValue", "someLongValue",
176: "someDoubleValue", "someFloatValue", "someShortValue",
177: "someByteValue", "someCharValue", "someIntegerValue",
178: "someStringValue", "someObjectValue", "class", };
179: return keys;
180: }
181:
182: /**
183: * An object value that will be stored in the bean map as a value. Need
184: * to save this externally so that we can make sure the object instances
185: * are equivalent since getSampleValues() would otherwise construct a new
186: * and different Object each time.
187: **/
188: private Object objectInFullMap = new Object();
189:
190: // note to self: the sample values were created manually
191: public Object[] getSampleValues() {
192: Object[] values = new Object[] { new Integer(1234),
193: new Long(1298341928234L), new Double(123423.34),
194: new Float(1213332.12f), new Short((short) 134),
195: new Byte((byte) 10), new Character('a'),
196: new Integer(1432), "SomeStringValue", objectInFullMap,
197: BeanWithProperties.class, };
198: return values;
199: }
200:
201: public Object[] getNewSampleValues() {
202: Object[] values = new Object[] { new Integer(223),
203: new Long(23341928234L), new Double(23423.34),
204: new Float(213332.12f), new Short((short) 234),
205: new Byte((byte) 20), new Character('b'),
206: new Integer(232), "SomeNewStringValue", new Object(),
207: null, };
208: return values;
209: }
210:
211: /**
212: * Values is a dead copy in BeanMap, so refresh each time.
213: */
214: public void verifyValues() {
215: values = map.values();
216: super .verifyValues();
217: }
218:
219: /**
220: * The mappings in a BeanMap are fixed on the properties the underlying
221: * bean has. Adding and removing mappings is not possible, thus this
222: * method is overridden to return false.
223: */
224: public boolean isPutAddSupported() {
225: return false;
226: }
227:
228: /**
229: * The mappings in a BeanMap are fixed on the properties the underlying
230: * bean has. Adding and removing mappings is not possible, thus this
231: * method is overridden to return false.
232: */
233: public boolean isRemoveSupported() {
234: return false;
235: }
236:
237: public Map makeFullMap() {
238: // note: These values must match (i.e. .equals() must return true)
239: // those returned from getSampleValues().
240: BeanWithProperties bean = new BeanWithProperties();
241: bean.setSomeIntValue(1234);
242: bean.setSomeLongValue(1298341928234L);
243: bean.setSomeDoubleValue(123423.34);
244: bean.setSomeFloatValue(1213332.12f);
245: bean.setSomeShortValue((short) 134);
246: bean.setSomeByteValue((byte) 10);
247: bean.setSomeCharValue('a');
248: bean.setSomeIntegerValue(new Integer(1432));
249: bean.setSomeStringValue("SomeStringValue");
250: bean.setSomeObjectValue(objectInFullMap);
251: return new BeanMap(bean);
252: }
253:
254: public Map makeEmptyMap() {
255: return new BeanMap();
256: }
257:
258: public String[] ignoredTests() {
259: // Ignore the serialization tests on collection views.
260: return new String[] {
261: "TestBeanMap.bulkTestMapEntrySet.testCanonicalEmptyCollectionExists",
262: "TestBeanMap.bulkTestMapEntrySet.testCanonicalFullCollectionExists",
263: "TestBeanMap.bulkTestMapKeySet.testCanonicalEmptyCollectionExists",
264: "TestBeanMap.bulkTestMapKeySet.testCanonicalFullCollectionExists",
265: "TestBeanMap.bulkTestMapValues.testCanonicalEmptyCollectionExists",
266: "TestBeanMap.bulkTestMapValues.testCanonicalFullCollectionExists",
267: "TestBeanMap.bulkTestMapEntrySet.testSimpleSerialization",
268: "TestBeanMap.bulkTestMapKeySet.testSimpleSerialization",
269: "TestBeanMap.bulkTestMapEntrySet.testSerializeDeserializeThenCompare",
270: "TestBeanMap.bulkTestMapKeySet.testSerializeDeserializeThenCompare" };
271: }
272:
273: /**
274: * Need to override this method because the "clear()" method on the bean
275: * map just returns the bean properties to their default states. It does
276: * not actually remove the mappings as per the map contract. The default
277: * testClear() methods checks that the clear method throws an
278: * UnsupportedOperationException since this class is not add/remove
279: * modifiable. In our case though, we do not always throw that exception.
280: */
281: public void testMapClear() {
282: //TODO: make sure a call to BeanMap.clear returns the bean to its
283: //default initialization values.
284: }
285:
286: /**
287: * Need to override this method because the "put()" method on the bean
288: * doesn't work for this type of Map.
289: */
290: public void testMapPut() {
291: // see testBeanMapPutAllWriteable
292: }
293:
294: public void testBeanMapClone() {
295: BeanMap map = (BeanMap) makeFullMap();
296: try {
297: BeanMap map2 = (BeanMap) ((BeanMap) map).clone();
298:
299: // make sure containsKey is working to verify the bean was cloned
300: // ok, and the read methods were properly initialized
301: Object[] keys = getSampleKeys();
302: for (int i = 0; i < keys.length; i++) {
303: assertTrue(
304: "Cloned BeanMap should contain the same keys",
305: map2.containsKey(keys[i]));
306: }
307: } catch (CloneNotSupportedException exception) {
308: fail("BeanMap.clone() should not throw a "
309: + "CloneNotSupportedException when clone should succeed.");
310: }
311: }
312:
313: public void testBeanMapPutAllWriteable() {
314: BeanMap map1 = (BeanMap) makeFullMap();
315: BeanMap map2 = (BeanMap) makeFullMap();
316: map2.put("someIntValue", new Integer(0));
317: map1.putAllWriteable(map2);
318: assertEquals(map1.get("someIntValue"), new Integer(0));
319: }
320:
321: public void testMethodAccessor() throws Exception {
322: BeanMap map = (BeanMap) makeFullMap();
323: Method method = BeanWithProperties.class.getDeclaredMethod(
324: "getSomeIntegerValue", null);
325: assertEquals(method, map.getReadMethod("someIntegerValue"));
326: }
327:
328: public void testMethodMutator() throws Exception {
329: BeanMap map = (BeanMap) makeFullMap();
330: Method method = BeanWithProperties.class.getDeclaredMethod(
331: "setSomeIntegerValue", new Class[] { Integer.class });
332: assertEquals(method, map.getWriteMethod("someIntegerValue"));
333: }
334:
335: /**
336: * Test the default transformers using the getTypeTransformer() method
337: */
338: public void testGetTypeTransformerMethod() {
339: BeanMap beanMap = new BeanMap();
340: assertEquals("Boolean.TYPE", Boolean.TRUE, beanMap
341: .getTypeTransformer(Boolean.TYPE).transform("true"));
342: assertEquals("Character.TYPE", new Character('B'), beanMap
343: .getTypeTransformer(Character.TYPE).transform("BCD"));
344: assertEquals("Byte.TYPE", new Byte((byte) 1), beanMap
345: .getTypeTransformer(Byte.TYPE).transform("1"));
346: assertEquals("Short.TYPE", new Short((short) 2), beanMap
347: .getTypeTransformer(Short.TYPE).transform("2"));
348: assertEquals("Integer.TYPE", new Integer(3), beanMap
349: .getTypeTransformer(Integer.TYPE).transform("3"));
350: assertEquals("Long.TYPE", new Long(4), beanMap
351: .getTypeTransformer(Long.TYPE).transform("4"));
352: assertEquals("Float.TYPE", new Float("5"), beanMap
353: .getTypeTransformer(Float.TYPE).transform("5"));
354: assertEquals("Double.TYPE", new Double("6"), beanMap
355: .getTypeTransformer(Double.TYPE).transform("6"));
356: }
357:
358: /**
359: * Test the default transformers via the public static Map instance
360: */
361: public void testGetDefaultTransformersMap() {
362: BeanMap beanMap = new BeanMap();
363: assertEquals("Boolean.TYPE", Boolean.TRUE,
364: ((Transformer) BeanMap.defaultTransformers
365: .get(Boolean.TYPE)).transform("true"));
366: assertEquals("Character.TYPE", new Character('B'),
367: ((Transformer) BeanMap.defaultTransformers
368: .get(Character.TYPE)).transform("BCD"));
369: assertEquals("Byte.TYPE", new Byte((byte) 1),
370: ((Transformer) BeanMap.defaultTransformers
371: .get(Byte.TYPE)).transform("1"));
372: assertEquals("Short.TYPE", new Short((short) 2),
373: ((Transformer) BeanMap.defaultTransformers
374: .get(Short.TYPE)).transform("2"));
375: assertEquals("Integer.TYPE", new Integer(3),
376: ((Transformer) BeanMap.defaultTransformers
377: .get(Integer.TYPE)).transform("3"));
378: assertEquals("Long.TYPE", new Long(4),
379: ((Transformer) BeanMap.defaultTransformers
380: .get(Long.TYPE)).transform("4"));
381: assertEquals("Float.TYPE", new Float("5"),
382: ((Transformer) BeanMap.defaultTransformers
383: .get(Float.TYPE)).transform("5"));
384: assertEquals("Double.TYPE", new Double("6"),
385: ((Transformer) BeanMap.defaultTransformers
386: .get(Double.TYPE)).transform("6"));
387: }
388:
389: /**
390: * Test the default transformers HashMap
391: */
392: public void testDefaultTransformersMap() {
393: assertEquals("Size", 8, BeanMap.defaultTransformers.size());
394: assertEquals("entrySet", 8, BeanMap.defaultTransformers
395: .entrySet().size());
396: assertEquals("keySet", 8, BeanMap.defaultTransformers.keySet()
397: .size());
398: assertEquals("values", 8, BeanMap.defaultTransformers.values()
399: .size());
400: assertFalse("isEmpty", BeanMap.defaultTransformers.isEmpty());
401: assertTrue("containsKey(Double)", BeanMap.defaultTransformers
402: .containsKey(Double.TYPE));
403: assertFalse("containsKey(Object)", BeanMap.defaultTransformers
404: .containsKey(Object.class));
405: assertTrue("containsValue(double)", BeanMap.defaultTransformers
406: .containsValue(BeanMap.defaultTransformers
407: .get(Double.TYPE)));
408: assertFalse("containsValue(Object)",
409: BeanMap.defaultTransformers.containsValue(Object.class));
410:
411: try {
412: BeanMap.defaultTransformers.clear();
413: fail("clear() - expected UnsupportedOperationException");
414: } catch (UnsupportedOperationException e) {
415: // expected result
416: }
417: try {
418: BeanMap.defaultTransformers.put("FOO", null);
419: fail("put() - expected UnsupportedOperationException");
420: } catch (UnsupportedOperationException e) {
421: // expected result
422: }
423: try {
424: BeanMap.defaultTransformers.putAll(new HashMap());
425: fail("putAll() - expected UnsupportedOperationException");
426: } catch (UnsupportedOperationException e) {
427: // expected result
428: }
429: try {
430: BeanMap.defaultTransformers.remove("FOO");
431: fail("remove() - expected UnsupportedOperationException");
432: } catch (UnsupportedOperationException e) {
433: // expected result
434: }
435: }
436:
437: }
|