001: /*
002: * Copyright 2001-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;
017:
018: import java.io.Serializable;
019: import java.lang.reflect.Method;
020: import java.util.Map;
021:
022: import junit.framework.Test;
023: import junit.textui.TestRunner;
024:
025: import org.apache.commons.collections.map.AbstractTestMap;
026:
027: /**
028: * Test cases for BeanMap
029: *
030: * @version $Revision: 201765 $ $Date: 2005-06-25 17:39:34 +0100 (Sat, 25 Jun 2005) $
031: *
032: * @author Morgan Delagrange
033: * @author Stephen Colebourne
034: */
035: public class TestBeanMap extends AbstractTestMap {
036:
037: public TestBeanMap(String testName) {
038: super (testName);
039: }
040:
041: public static void main(String[] args) {
042: TestRunner.run(suite());
043: }
044:
045: public static Test suite() {
046: return BulkTest.makeSuite(TestBeanMap.class);
047: }
048:
049: /*
050: note to self. The getter and setter methods were generated by copying the
051: field declarations and using the following regular expression search and
052: replace:
053:
054: From:
055: private \(.*\) some\(.*\);
056: To:
057: public \1 getSome\2Value() {
058: return some\2;
059: }
060: public void setSome\2Value(\1 value) {
061: some\2 = value;
062: }
063:
064: Also note: The sample keys and mappings were generated manually.
065: */
066:
067: public static class BeanWithProperties implements Serializable {
068: private int someInt;
069: private long someLong;
070: private double someDouble;
071: private float someFloat;
072: private short someShort;
073: private byte someByte;
074: private char someChar;
075: private Integer someInteger;
076: private String someString;
077: private Object someObject;
078:
079: public int getSomeIntValue() {
080: return someInt;
081: }
082:
083: public void setSomeIntValue(int value) {
084: someInt = value;
085: }
086:
087: public long getSomeLongValue() {
088: return someLong;
089: }
090:
091: public void setSomeLongValue(long value) {
092: someLong = value;
093: }
094:
095: public double getSomeDoubleValue() {
096: return someDouble;
097: }
098:
099: public void setSomeDoubleValue(double value) {
100: someDouble = value;
101: }
102:
103: public float getSomeFloatValue() {
104: return someFloat;
105: }
106:
107: public void setSomeFloatValue(float value) {
108: someFloat = value;
109: }
110:
111: public short getSomeShortValue() {
112: return someShort;
113: }
114:
115: public void setSomeShortValue(short value) {
116: someShort = value;
117: }
118:
119: public byte getSomeByteValue() {
120: return someByte;
121: }
122:
123: public void setSomeByteValue(byte value) {
124: someByte = value;
125: }
126:
127: public char getSomeCharValue() {
128: return someChar;
129: }
130:
131: public void setSomeCharValue(char value) {
132: someChar = value;
133: }
134:
135: public String getSomeStringValue() {
136: return someString;
137: }
138:
139: public void setSomeStringValue(String value) {
140: someString = value;
141: }
142:
143: public Integer getSomeIntegerValue() {
144: return someInteger;
145: }
146:
147: public void setSomeIntegerValue(Integer value) {
148: someInteger = value;
149: }
150:
151: public Object getSomeObjectValue() {
152: return someObject;
153: }
154:
155: public void setSomeObjectValue(Object value) {
156: someObject = value;
157: }
158: }
159:
160: // note to self. The Sample keys were generated by copying the field
161: // declarations and using the following regular expression search and replace:
162: //
163: // From:
164: // private \(.*\) some\(.*\);
165: // To:
166: // "some\2Value",
167: //
168: // Then, I manually added the "class" key, which is a property that exists for
169: // all beans (and all objects for that matter.
170: public Object[] getSampleKeys() {
171: Object[] keys = new Object[] { "someIntValue", "someLongValue",
172: "someDoubleValue", "someFloatValue", "someShortValue",
173: "someByteValue", "someCharValue", "someIntegerValue",
174: "someStringValue", "someObjectValue", "class", };
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 objectInFullMap = new Object();
185:
186: // note to self: the sample values were created manually
187: public Object[] getSampleValues() {
188: Object[] values = new Object[] { new Integer(1234),
189: new Long(1298341928234L), new Double(123423.34),
190: new Float(1213332.12f), new Short((short) 134),
191: new Byte((byte) 10), new Character('a'),
192: new Integer(1432), "SomeStringValue", objectInFullMap,
193: BeanWithProperties.class, };
194: return values;
195: }
196:
197: public Object[] getNewSampleValues() {
198: Object[] values = new Object[] { new Integer(223),
199: new Long(23341928234L), new Double(23423.34),
200: new Float(213332.12f), new Short((short) 234),
201: new Byte((byte) 20), new Character('b'),
202: new Integer(232), "SomeNewStringValue", new Object(),
203: null, };
204: return values;
205: }
206:
207: /**
208: * Values is a dead copy in BeanMap, so refresh each time.
209: */
210: public void verifyValues() {
211: values = map.values();
212: super .verifyValues();
213: }
214:
215: /**
216: * The mappings in a BeanMap are fixed on the properties the underlying
217: * bean has. Adding and removing mappings is not possible, thus this
218: * method is overridden to return false.
219: */
220: public boolean isPutAddSupported() {
221: return false;
222: }
223:
224: /**
225: * The mappings in a BeanMap are fixed on the properties the underlying
226: * bean has. Adding and removing mappings is not possible, thus this
227: * method is overridden to return false.
228: */
229: public boolean isRemoveSupported() {
230: return false;
231: }
232:
233: public Map makeFullMap() {
234: // note: These values must match (i.e. .equals() must return true)
235: // those returned from getSampleValues().
236: BeanWithProperties bean = new BeanWithProperties();
237: bean.setSomeIntValue(1234);
238: bean.setSomeLongValue(1298341928234L);
239: bean.setSomeDoubleValue(123423.34);
240: bean.setSomeFloatValue(1213332.12f);
241: bean.setSomeShortValue((short) 134);
242: bean.setSomeByteValue((byte) 10);
243: bean.setSomeCharValue('a');
244: bean.setSomeIntegerValue(new Integer(1432));
245: bean.setSomeStringValue("SomeStringValue");
246: bean.setSomeObjectValue(objectInFullMap);
247: return new BeanMap(bean);
248: }
249:
250: public Map makeEmptyMap() {
251: return new BeanMap();
252: }
253:
254: public String[] ignoredTests() {
255: // Ignore the serialization tests on collection views.
256: return new String[] {
257: "TestBeanMap.bulkTestMapEntrySet.testCanonicalEmptyCollectionExists",
258: "TestBeanMap.bulkTestMapEntrySet.testCanonicalFullCollectionExists",
259: "TestBeanMap.bulkTestMapKeySet.testCanonicalEmptyCollectionExists",
260: "TestBeanMap.bulkTestMapKeySet.testCanonicalFullCollectionExists",
261: "TestBeanMap.bulkTestMapValues.testCanonicalEmptyCollectionExists",
262: "TestBeanMap.bulkTestMapValues.testCanonicalFullCollectionExists",
263: "TestBeanMap.bulkTestMapEntrySet.testSimpleSerialization",
264: "TestBeanMap.bulkTestMapKeySet.testSimpleSerialization",
265: "TestBeanMap.bulkTestMapEntrySet.testSerializeDeserializeThenCompare",
266: "TestBeanMap.bulkTestMapKeySet.testSerializeDeserializeThenCompare" };
267: }
268:
269: /**
270: * Need to override this method because the "clear()" method on the bean
271: * map just returns the bean properties to their default states. It does
272: * not actually remove the mappings as per the map contract. The default
273: * testClear() methods checks that the clear method throws an
274: * UnsupportedOperationException since this class is not add/remove
275: * modifiable. In our case though, we do not always throw that exception.
276: */
277: public void testMapClear() {
278: //TODO: make sure a call to BeanMap.clear returns the bean to its
279: //default initialization values.
280: }
281:
282: /**
283: * Need to override this method because the "put()" method on the bean
284: * doesn't work for this type of Map.
285: */
286: public void testMapPut() {
287: // see testBeanMapPutAllWriteable
288: }
289:
290: public void testBeanMapClone() {
291: BeanMap map = (BeanMap) makeFullMap();
292: try {
293: BeanMap map2 = (BeanMap) ((BeanMap) map).clone();
294:
295: // make sure containsKey is working to verify the bean was cloned
296: // ok, and the read methods were properly initialized
297: Object[] keys = getSampleKeys();
298: for (int i = 0; i < keys.length; i++) {
299: assertTrue(
300: "Cloned BeanMap should contain the same keys",
301: map2.containsKey(keys[i]));
302: }
303: } catch (CloneNotSupportedException exception) {
304: fail("BeanMap.clone() should not throw a "
305: + "CloneNotSupportedException when clone should succeed.");
306: }
307: }
308:
309: public void testBeanMapPutAllWriteable() {
310: BeanMap map1 = (BeanMap) makeFullMap();
311: BeanMap map2 = (BeanMap) makeFullMap();
312: map2.put("someIntValue", new Integer(0));
313: map1.putAllWriteable(map2);
314: assertEquals(map1.get("someIntValue"), new Integer(0));
315: }
316:
317: public void testMethodAccessor() throws Exception {
318: BeanMap map = (BeanMap) makeFullMap();
319: Method method = BeanWithProperties.class.getDeclaredMethod(
320: "getSomeIntegerValue", (Class[]) null);
321: assertEquals(method, map.getReadMethod("someIntegerValue"));
322: }
323:
324: public void testMethodMutator() throws Exception {
325: BeanMap map = (BeanMap) makeFullMap();
326: Method method = BeanWithProperties.class.getDeclaredMethod(
327: "setSomeIntegerValue", new Class[] { Integer.class });
328: assertEquals(method, map.getWriteMethod("someIntegerValue"));
329: }
330:
331: }
|