1 16 package org.apache.commons.beanutils; 17 18 import java.io.Serializable ; 19 import java.lang.reflect.Method ; 20 import java.util.Map ; 21 22 import junit.framework.Test; 23 import junit.textui.TestRunner; 24 25 import org.apache.commons.collections.map.AbstractTestMap; 26 import org.apache.commons.collections.BulkTest; 27 28 36 public class TestBeanMap extends AbstractTestMap { 37 38 public TestBeanMap(String testName) { 39 super(testName); 40 } 41 42 public static void main(String [] args) { 43 TestRunner.run(suite()); 44 } 45 46 public static Test suite() { 47 return BulkTest.makeSuite(TestBeanMap.class); 48 } 49 50 67 68 69 public static class BeanWithProperties implements Serializable { 70 private int someInt; 71 private long someLong; 72 private double someDouble; 73 private float someFloat; 74 private short someShort; 75 private byte someByte; 76 private char someChar; 77 private Integer someInteger; 78 private String someString; 79 private Object someObject; 80 81 public int getSomeIntValue() { 82 return someInt; 83 } 84 public void setSomeIntValue(int value) { 85 someInt = value; 86 } 87 88 public long getSomeLongValue() { 89 return someLong; 90 } 91 public void setSomeLongValue(long value) { 92 someLong = value; 93 } 94 95 public double getSomeDoubleValue() { 96 return someDouble; 97 } 98 public void setSomeDoubleValue(double value) { 99 someDouble = value; 100 } 101 102 public float getSomeFloatValue() { 103 return someFloat; 104 } 105 public void setSomeFloatValue(float value) { 106 someFloat = value; 107 } 108 109 public short getSomeShortValue() { 110 return someShort; 111 } 112 public void setSomeShortValue(short value) { 113 someShort = value; 114 } 115 116 public byte getSomeByteValue() { 117 return someByte; 118 } 119 public void setSomeByteValue(byte value) { 120 someByte = value; 121 } 122 123 public char getSomeCharValue() { 124 return someChar; 125 } 126 public void setSomeCharValue(char value) { 127 someChar = value; 128 } 129 130 public String getSomeStringValue() { 131 return someString; 132 } 133 public void setSomeStringValue(String value) { 134 someString = value; 135 } 136 137 public Integer getSomeIntegerValue() { 138 return someInteger; 139 } 140 public void setSomeIntegerValue(Integer value) { 141 someInteger = value; 142 } 143 144 public Object getSomeObjectValue() { 145 return someObject; 146 } 147 public void setSomeObjectValue(Object value) { 148 someObject = value; 149 } 150 } 151 152 public Object [] getSampleKeys() { 163 Object [] keys = new Object [] { 164 "someIntValue", 165 "someLongValue", 166 "someDoubleValue", 167 "someFloatValue", 168 "someShortValue", 169 "someByteValue", 170 "someCharValue", 171 "someIntegerValue", 172 "someStringValue", 173 "someObjectValue", 174 "class", 175 }; 176 return keys; 177 } 178 179 185 private Object objectInFullMap = new Object (); 186 187 public Object [] getSampleValues() { 189 Object [] values = new Object [] { 190 new Integer (1234), 191 new Long (1298341928234L), 192 new Double (123423.34), 193 new Float (1213332.12f), 194 new Short ((short)134), 195 new Byte ((byte)10), 196 new Character ('a'), 197 new Integer (1432), 198 "SomeStringValue", 199 objectInFullMap, 200 BeanWithProperties.class, 201 }; 202 return values; 203 } 204 205 public Object [] getNewSampleValues() { 206 Object [] values = new Object [] { 207 new Integer (223), 208 new Long (23341928234L), 209 new Double (23423.34), 210 new Float (213332.12f), 211 new Short ((short)234), 212 new Byte ((byte)20), 213 new Character ('b'), 214 new Integer (232), 215 "SomeNewStringValue", 216 new Object (), 217 null, 218 }; 219 return values; 220 } 221 222 225 public void verifyValues() { 226 values = map.values(); 227 super.verifyValues(); 228 } 229 230 235 public boolean isPutAddSupported() { 236 return false; 237 } 238 239 244 public boolean isRemoveSupported() { 245 return false; 246 } 247 248 public Map makeFullMap() { 249 BeanWithProperties bean = new BeanWithProperties(); 252 bean.setSomeIntValue(1234); 253 bean.setSomeLongValue(1298341928234L); 254 bean.setSomeDoubleValue(123423.34); 255 bean.setSomeFloatValue(1213332.12f); 256 bean.setSomeShortValue((short)134); 257 bean.setSomeByteValue((byte)10); 258 bean.setSomeCharValue('a'); 259 bean.setSomeIntegerValue(new Integer (1432)); 260 bean.setSomeStringValue("SomeStringValue"); 261 bean.setSomeObjectValue(objectInFullMap); 262 return new BeanMap(bean); 263 } 264 265 public Map makeEmptyMap() { 266 return new BeanMap(); 267 } 268 269 public String [] ignoredTests() { 270 return new String [] { 272 "TestBeanMap.bulkTestMapEntrySet.testCanonicalEmptyCollectionExists", 273 "TestBeanMap.bulkTestMapEntrySet.testCanonicalFullCollectionExists", 274 "TestBeanMap.bulkTestMapKeySet.testCanonicalEmptyCollectionExists", 275 "TestBeanMap.bulkTestMapKeySet.testCanonicalFullCollectionExists", 276 "TestBeanMap.bulkTestMapValues.testCanonicalEmptyCollectionExists", 277 "TestBeanMap.bulkTestMapValues.testCanonicalFullCollectionExists", 278 "TestBeanMap.bulkTestMapEntrySet.testSimpleSerialization", 279 "TestBeanMap.bulkTestMapKeySet.testSimpleSerialization", 280 "TestBeanMap.bulkTestMapEntrySet.testSerializeDeserializeThenCompare", 281 "TestBeanMap.bulkTestMapKeySet.testSerializeDeserializeThenCompare" 282 }; 283 } 284 285 293 public void testMapClear() { 294 } 297 298 302 public void testMapPut() { 303 } 305 306 public void testBeanMapClone() { 307 BeanMap map = (BeanMap)makeFullMap(); 308 try { 309 BeanMap map2 = (BeanMap)((BeanMap)map).clone(); 310 311 Object [] keys = getSampleKeys(); 314 for(int i = 0; i < keys.length; i++) { 315 assertTrue("Cloned BeanMap should contain the same keys", 316 map2.containsKey(keys[i])); 317 } 318 } catch (CloneNotSupportedException exception) { 319 fail("BeanMap.clone() should not throw a " + 320 "CloneNotSupportedException when clone should succeed."); 321 } 322 } 323 324 public void testBeanMapPutAllWriteable() { 325 BeanMap map1 = (BeanMap)makeFullMap(); 326 BeanMap map2 = (BeanMap)makeFullMap(); 327 map2.put("someIntValue", new Integer (0)); 328 map1.putAllWriteable(map2); 329 assertEquals(map1.get("someIntValue"), new Integer (0)); 330 } 331 332 public void testMethodAccessor() throws Exception { 333 BeanMap map = (BeanMap) makeFullMap(); 334 Method method = BeanWithProperties.class.getDeclaredMethod("getSomeIntegerValue", null); 335 assertEquals(method, map.getReadMethod("someIntegerValue")); 336 } 337 338 public void testMethodMutator() throws Exception { 339 BeanMap map = (BeanMap) makeFullMap(); 340 Method method = BeanWithProperties.class.getDeclaredMethod("setSomeIntegerValue", new Class [] {Integer .class}); 341 assertEquals(method, map.getWriteMethod("someIntegerValue")); 342 } 343 344 } 345 | Popular Tags |