1 16 package org.apache.commons.collections; 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 27 35 public class TestBeanMap extends AbstractTestMap { 36 37 public TestBeanMap(String testName) { 38 super(testName); 39 } 40 41 public static void main(String [] args) { 42 TestRunner.run(suite()); 43 } 44 45 public static Test suite() { 46 return BulkTest.makeSuite(TestBeanMap.class); 47 } 48 49 66 67 68 public static class BeanWithProperties implements Serializable { 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 someInteger; 77 private String someString; 78 private Object 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 getSomeStringValue() { 130 return someString; 131 } 132 public void setSomeStringValue(String value) { 133 someString = value; 134 } 135 136 public Integer getSomeIntegerValue() { 137 return someInteger; 138 } 139 public void setSomeIntegerValue(Integer value) { 140 someInteger = value; 141 } 142 143 public Object getSomeObjectValue() { 144 return someObject; 145 } 146 public void setSomeObjectValue(Object value) { 147 someObject = value; 148 } 149 } 150 151 public Object [] getSampleKeys() { 162 Object [] keys = new Object [] { 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 184 private Object objectInFullMap = new Object (); 185 186 public Object [] getSampleValues() { 188 Object [] values = new Object [] { 189 new Integer (1234), 190 new Long (1298341928234L), 191 new Double (123423.34), 192 new Float (1213332.12f), 193 new Short ((short)134), 194 new Byte ((byte)10), 195 new Character ('a'), 196 new Integer (1432), 197 "SomeStringValue", 198 objectInFullMap, 199 BeanWithProperties.class, 200 }; 201 return values; 202 } 203 204 public Object [] getNewSampleValues() { 205 Object [] values = new Object [] { 206 new Integer (223), 207 new Long (23341928234L), 208 new Double (23423.34), 209 new Float (213332.12f), 210 new Short ((short)234), 211 new Byte ((byte)20), 212 new Character ('b'), 213 new Integer (232), 214 "SomeNewStringValue", 215 new Object (), 216 null, 217 }; 218 return values; 219 } 220 221 224 public void verifyValues() { 225 values = map.values(); 226 super.verifyValues(); 227 } 228 229 234 public boolean isPutAddSupported() { 235 return false; 236 } 237 238 243 public boolean isRemoveSupported() { 244 return false; 245 } 246 247 public Map makeFullMap() { 248 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 (1432)); 259 bean.setSomeStringValue("SomeStringValue"); 260 bean.setSomeObjectValue(objectInFullMap); 261 return new BeanMap(bean); 262 } 263 264 public Map makeEmptyMap() { 265 return new BeanMap(); 266 } 267 268 public String [] ignoredTests() { 269 return new String [] { 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 292 public void testMapClear() { 293 } 296 297 301 public void testMapPut() { 302 } 304 305 public void testBeanMapClone() { 306 BeanMap map = (BeanMap)makeFullMap(); 307 try { 308 BeanMap map2 = (BeanMap)((BeanMap)map).clone(); 309 310 Object [] 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 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 (0)); 327 map1.putAllWriteable(map2); 328 assertEquals(map1.get("someIntValue"), new Integer (0)); 329 } 330 331 public void testMethodAccessor() throws Exception { 332 BeanMap map = (BeanMap) makeFullMap(); 333 Method method = BeanWithProperties.class.getDeclaredMethod("getSomeIntegerValue", null); 334 assertEquals(method, map.getReadMethod("someIntegerValue")); 335 } 336 337 public void testMethodMutator() throws Exception { 338 BeanMap map = (BeanMap) makeFullMap(); 339 Method method = BeanWithProperties.class.getDeclaredMethod("setSomeIntegerValue", new Class [] {Integer .class}); 340 assertEquals(method, map.getWriteMethod("someIntegerValue")); 341 } 342 343 } 344 | Popular Tags |