1 7 package com.inversoft.beans.test; 8 9 10 import junit.framework.TestCase; 11 12 import com.inversoft.beans.BeanException; 13 import com.inversoft.beans.IndexedBeanProperty; 14 import com.inversoft.util.typeconverter.TypeConversionException; 15 16 17 24 public class IndexedBeanPropertyTest extends TestCase { 25 26 27 public IndexedBeanPropertyTest(String name) { 28 super(name); 29 } 30 31 35 public void testReadOnlyBeanProperty() { 36 37 try { 39 Bean1 bean1 = new Bean1(); 40 IndexedBeanProperty prop = create("readOnlyIndexed", Bean1.class); 41 42 assertEquals("Should return readOnly", prop.getPropertyValue(bean1, 0), "readOnly"); 43 44 } catch (BeanException be) { 45 fail(be.toString()); 46 } 47 48 try { 50 Bean1 bean1 = new Bean1(); 51 IndexedBeanProperty prop = create("readOnlyIndexed", Bean1.class); 52 53 prop.setPropertyValue(bean1, 0, "foo", true); 55 fail("Should have failed because read-only"); 56 57 } catch (BeanException be) { 58 assertTrue("Should NOT have a root cause or target exception because this is read-only", 60 be.getCause() == null && be.getTarget() == null); 61 } catch (TypeConversionException tce) { 62 fail(tce.toString()); 63 } 64 } 65 66 69 public void testSimpleBeanProperty() { 70 71 try { 73 Bean1 bean1 = new Bean1(); 74 IndexedBeanProperty prop = create("stringIndexed", Bean1.class); 75 String value = "foo"; 76 77 prop.setPropertyValue(bean1, 0, value); 79 80 assertSame("Should point to same object", bean1.getStringIndexed(0), value); 82 assertSame("Should point to same object", prop.getPropertyValue(bean1, 0), value); 83 } catch (BeanException be) { 84 fail(be.toString()); 85 } 86 } 87 88 91 public void testNonIndicesFail() { 92 try { 93 Bean1 bean1 = new Bean1(); 94 IndexedBeanProperty prop = create("stringIndexed", Bean1.class); 95 96 prop.getPropertyValue(bean1); 97 fail("Should have failed"); 98 } catch (BeanException be) { 99 } 101 102 try { 103 Bean1 bean1 = new Bean1(); 104 IndexedBeanProperty prop = create("stringIndexed", Bean1.class); 105 String value = "foo"; 106 107 prop.setPropertyValue(bean1, value); 108 fail("Should have failed"); 109 } catch (BeanException be) { 110 } 112 113 try { 114 Bean1 bean1 = new Bean1(); 115 IndexedBeanProperty prop = create("stringIndexed", Bean1.class); 116 String value = "foo"; 117 118 prop.setPropertyValue(bean1, value, true); 119 fail("Should have failed"); 120 } catch (BeanException be) { 121 } 123 } 124 125 128 public void testBadIndicesFail() { 129 try { 130 Bean1 bean1 = new Bean1(); 131 IndexedBeanProperty prop = create("stringIndexed", Bean1.class); 132 133 prop.getPropertyValue(bean1, "badIndex"); 134 fail("Should have failed"); 135 } catch (BeanException be) { 136 } 138 139 try { 140 Bean1 bean1 = new Bean1(); 141 IndexedBeanProperty prop = create("stringIndexed", Bean1.class); 142 String value = "foo"; 143 144 prop.setPropertyValue(bean1, "foo", value, true); 145 fail("Should have failed"); 146 } catch (BeanException be) { 147 } 149 } 150 151 155 public void testNullValue() { 156 157 try { 159 Bean1 bean1 = new Bean1(); 160 IndexedBeanProperty prop = create("stringIndexed", Bean1.class); 161 String value = null; 162 163 prop.setPropertyValue(bean1, 0, value, true); 164 } catch (BeanException be) { 165 fail(be.toString()); 166 } catch (TypeConversionException tce) { 167 fail(tce.toString()); 168 } 169 170 try { 172 Bean1 bean1 = new Bean1(); 173 IndexedBeanProperty prop = create("integerIndexed", Bean1.class); 174 Integer value = null; 175 176 prop.setPropertyValue(bean1, 0, value, true); 177 } catch (BeanException be) { 178 fail(be.toString()); 179 } catch (TypeConversionException tce) { 180 fail(tce.toString()); 181 } 182 183 try { 186 Bean1 bean1 = new Bean1(); 187 IndexedBeanProperty prop = create("integerIndexed", Bean1.class); 188 String [] values = new String [0]; 189 190 prop.setPropertyValue(bean1, 0, values, true); 191 assertTrue("Should be null", bean1.getInteger1() == null); 192 } catch (BeanException be) { 193 fail(be.toString()); 194 } catch (TypeConversionException tce) { 195 fail(tce.toString()); 196 } 197 198 try { 200 Bean1 bean1 = new Bean1(); 201 IndexedBeanProperty prop = create("booleanIndexed", Bean1.class); 202 Object value = null; 203 204 bean1.setBooleanIndexed(0, true); 205 prop.setPropertyValue(bean1, 0, value, true); 206 assertTrue("Should have been set to false", !bean1.isBoolean1()); 207 } catch (BeanException be) { 208 fail(be.toString()); 209 } catch (TypeConversionException tce) { 210 fail(tce.toString()); 211 } 212 213 try { 215 Bean1 bean1 = new Bean1(); 216 IndexedBeanProperty prop = create("intIndexed", Bean1.class); 217 Object value = null; 218 219 bean1.setIntIndexed(0, 42); 220 prop.setPropertyValue(bean1, 0, value, true); 221 assertTrue("Should have been set to 0", bean1.getInt1() == 0); 222 } catch (BeanException be) { 223 fail(be.toString()); 224 } catch (TypeConversionException tce) { 225 fail(tce.toString()); 226 } 227 } 228 229 232 public void testAutoConversion() { 233 234 try { 236 Bean1 bean1 = new Bean1(); 237 IndexedBeanProperty prop = create("integerIndexed", Bean1.class); 238 String value = "1024"; 239 240 prop.setPropertyValue(bean1, 0, value, true); 241 242 assertTrue("Should have converted value to new Integer(1024)", 243 bean1.getIntegerIndexed(0).intValue() == 1024); 244 } catch (BeanException be) { 245 fail(be.toString()); 246 } catch (TypeConversionException tce) { 247 fail(tce.toString()); 248 } 249 } 250 251 254 public void testSecondConstructor() { 255 256 try { 257 Bean1 bean1 = new Bean1(); 258 IndexedBeanProperty prop = create("integerIndexed", "com.inversoft.beans.test.Bean1"); 259 String value = "1024"; 260 261 prop.setPropertyValue(bean1, 0, value, true); 262 263 assertTrue("Should have converted value to new Integer(1024)", 264 bean1.getIntegerIndexed(0).intValue() == 1024); 265 } catch (BeanException be) { 266 fail(be.toString()); 267 } catch (TypeConversionException tce) { 268 fail(tce.toString()); 269 } 270 } 271 272 275 public void testSecondConstructorFailure() { 276 277 try { 278 create("integerIndexed", "com.inversoft.beans.test.NotAClass"); 279 fail("Should have failed because class String is incorrect"); 280 } catch (BeanException be) { 281 assertTrue("Should have a root cause of ClassNotFoundException", 282 be.getCause() != null && be.getCause() instanceof ClassNotFoundException ); 283 } 284 } 285 286 289 public void testInvalidProperty() { 290 291 try { 292 create("notAProperty", Bean1.class); 293 fail("Should have failed because notAProperty does not exist"); 294 } catch (BeanException be) { 295 assertTrue("Should have a root cause of NoSuchMethodException", 296 be.getCause() != null && be.getCause() instanceof NoSuchMethodException ); 297 assertTrue("Should NOT have a target", be.getTarget() == null); 298 } 299 300 try { 301 create(" ", Bean1.class); 302 fail("Should have failed because the property is white space"); 303 } catch (BeanException be) { 304 assertTrue("Should NOT have a root cause", be.getCause() == null); 306 assertTrue("Should NOT have a target", be.getTarget() == null); 307 } 308 309 try { 310 create(" a", Bean1.class); 311 fail("Should have failed because the property starts with white space"); 312 } catch (BeanException be) { 313 assertTrue("Should have a root cause of NoSuchMethodException", 315 be.getCause() != null && be.getCause() instanceof NoSuchMethodException ); 316 assertTrue("Should NOT have a target", be.getTarget() == null); 317 } 318 319 try { 320 create("", Bean1.class); 321 fail("Should have failed because the property is empty"); 322 } catch (BeanException be) { 323 assertTrue("Should NOT have a root cause", be.getCause() == null); 325 assertTrue("Should NOT have a target", be.getTarget() == null); 326 } 327 } 328 329 332 public void testCache() { 333 try { 334 IndexedBeanProperty bp = IndexedBeanProperty.getIndexedInstance("indexed", Bean1.class); 335 IndexedBeanProperty bp2 = IndexedBeanProperty.getIndexedInstance("indexed", Class.forName("com.inversoft.beans.test.Bean1")); 336 IndexedBeanProperty bp3 = IndexedBeanProperty.getIndexedInstance("indexed", new Bean1().getClass()); 337 338 assertSame("Shoudl be same object instance", bp, bp2); 339 assertSame("Shoudl be same object instance", bp, bp3); 340 } catch (BeanException be) { 341 fail(be.toString()); 342 } catch (Exception e) { 343 fail(e.toString()); 344 } 345 346 try { 347 IndexedBeanProperty.getIndexedInstance("foo.bar", Bean1.class); 348 fail("Should have failed"); 349 } catch (BeanException be) { 350 } 352 } 353 354 358 protected IndexedBeanProperty create(String property, Class bean) 359 throws BeanException { 360 return new IndexedBeanProperty(property, bean); 361 } 362 363 367 protected IndexedBeanProperty create(String property, String bean) 368 throws BeanException { 369 return new IndexedBeanProperty(property, bean); 370 } 371 } | Popular Tags |