1 7 package com.inversoft.beans.test; 8 9 10 import java.lang.reflect.InvocationTargetException ; 11 12 import junit.framework.TestCase; 13 14 import com.inversoft.beans.BeanException; 15 import com.inversoft.beans.BeanProperty; 16 import com.inversoft.beans.IndexedBeanProperty; 17 import com.inversoft.util.typeconverter.TypeConversionException; 18 19 20 26 public class BeanPropertyTest extends TestCase { 27 28 31 public BeanPropertyTest(String name) { 32 super(name); 33 } 34 35 36 40 public void testReadOnlyBeanProperty() { 41 42 try { 44 Bean1 bean1 = new Bean1(); 45 BeanProperty prop = create("readOnly", Bean1.class); 46 47 assertTrue("Should return readOnly", prop.getPropertyValue(bean1).equals("readOnly")); 48 49 } catch (BeanException be) { 50 fail(be.toString()); 51 } 52 53 try { 55 Bean1 bean1 = new Bean1(); 56 BeanProperty prop = create("readOnly", Bean1.class); 57 58 prop.setPropertyValue(bean1, "foo", true); 60 fail("Should have failed because read-only"); 61 62 } catch (BeanException be) { 63 assertTrue("Should NOT have a root cause or target exception because this is read-only", 65 be.getCause() == null && be.getTarget() == null); 66 } catch (TypeConversionException tce) { 67 fail(tce.toString()); 68 } 69 } 70 71 74 public void testInstantiation() { 75 try { 76 BeanProperty prop = create("readOnly", Bean1.class); 77 Bean1 bean1 = (Bean1) prop.instantiate(); 78 assertNotNull(bean1); 79 } catch (BeanException be) { 80 fail(be.toString()); 81 } 82 } 83 84 87 public void testSimpleBeanProperty() { 88 89 try { 91 Bean1 bean1 = new Bean1(); 92 BeanProperty prop = create("string1", Bean1.class); 93 String value = "foo"; 94 95 prop.setPropertyValue(bean1, value); 97 98 assertTrue("Should point to same object", prop.getPropertyValue(bean1) == value); 100 } catch (BeanException be) { 101 fail(be.toString()); 102 } 103 } 104 105 109 public void testNullValue() { 110 111 try { 113 Bean1 bean1 = new Bean1(); 114 BeanProperty prop = create("string1", Bean1.class); 115 String [] values = null; 116 117 prop.setPropertyValue(bean1, values, true); 118 } catch (BeanException be) { 119 fail(be.toString()); 120 } catch (TypeConversionException tce) { 121 fail(tce.toString()); 122 } 123 124 try { 126 Bean1 bean1 = new Bean1(); 127 BeanProperty prop = create("integer1", Bean1.class); 128 String [] values = null; 129 130 prop.setPropertyValue(bean1, values, true); 131 } catch (BeanException be) { 132 fail(be.toString()); 133 } catch (TypeConversionException tce) { 134 fail(tce.toString()); 135 } 136 137 try { 140 Bean1 bean1 = new Bean1(); 141 BeanProperty prop = create("integer1", Bean1.class); 142 String [] values = new String [0]; 143 144 prop.setPropertyValue(bean1, values, true); 145 assertTrue("Should be null", bean1.getInteger1() == null); 146 } catch (BeanException be) { 147 fail(be.toString()); 148 } catch (TypeConversionException tce) { 149 fail(tce.toString()); 150 } 151 152 try { 154 Bean1 bean1 = new Bean1(); 155 BeanProperty prop = create("boolean1", Bean1.class); 156 Object value = null; 157 158 bean1.setBoolean1(true); 159 prop.setPropertyValue(bean1, value, true); 160 assertTrue("Should have been set to false", !bean1.isBoolean1()); 161 } catch (BeanException be) { 162 fail(be.toString()); 163 } catch (TypeConversionException tce) { 164 fail(tce.toString()); 165 } 166 167 try { 169 Bean1 bean1 = new Bean1(); 170 BeanProperty prop = create("int1", Bean1.class); 171 Object value = null; 172 173 bean1.setInt1(42); 174 prop.setPropertyValue(bean1, value, true); 175 assertTrue("Should have been set to 0", bean1.getInt1() == 0); 176 } catch (BeanException be) { 177 fail(be.toString()); 178 } catch (TypeConversionException tce) { 179 fail(tce.toString()); 180 } 181 182 try { 184 Bean1 bean1 = new Bean1(); 185 BeanProperty prop = create("char1", Bean1.class); 186 Object value = null; 187 188 bean1.setChar1('a'); 189 prop.setPropertyValue(bean1, value, true); 190 assertTrue("Should have been set to \u0000", bean1.getChar1() == '\u0000'); 191 } catch (BeanException be) { 192 fail(be.toString()); 193 } catch (TypeConversionException tce) { 194 fail(tce.toString()); 195 } 196 } 197 198 201 public void testAutoConversion() { 202 203 try { 205 Bean1 bean1 = new Bean1(); 206 BeanProperty prop = create("integer1", Bean1.class); 207 String value = "1024"; 208 209 prop.setPropertyValue(bean1, value, true); 210 211 assertTrue("Should have converted value to new Integer(1024)", 212 bean1.getInteger1().intValue() == 1024); 213 } catch (BeanException be) { 214 fail(be.toString()); 215 } catch (TypeConversionException tce) { 216 fail(tce.toString()); 217 } 218 } 219 220 223 public void testSecondConstructor() { 224 225 try { 226 Bean1 bean1 = new Bean1(); 227 BeanProperty prop = create("integer1", "com.inversoft.beans.test.Bean1"); 228 String value = "1024"; 229 230 prop.setPropertyValue(bean1, value, true); 231 232 assertTrue("Should have converted value to new Integer(1024)", 233 bean1.getInteger1().intValue() == 1024); 234 } catch (BeanException be) { 235 fail(be.toString()); 236 } catch (TypeConversionException tce) { 237 fail(tce.toString()); 238 } 239 } 240 241 244 public void testSecondConstructorFailure() { 245 246 try { 247 create("integer1", "com.inversoft.beans.test.NotAClass"); 248 fail("Should have failed because class String is incorrect"); 249 } catch (BeanException be) { 250 assertTrue("Should have a root cause of ClassNotFoundException", 251 be.getCause() != null && be.getCause() instanceof ClassNotFoundException ); 252 } 253 } 254 255 258 public void testInvalidProperty() { 259 260 try { 261 create("notAProperty", Bean1.class); 262 fail("Should have failed because notAProperty does not exist"); 263 } catch (BeanException be) { 264 assertTrue("Should have a root cause of NoSuchMethodException", 265 be.getCause() != null && be.getCause() instanceof NoSuchMethodException ); 266 assertTrue("Should NOT have a target", be.getTarget() == null); 267 } 268 269 271 try { 272 create(" ", Bean1.class); 273 fail("Should have failed because the property is white space"); 274 } catch (BeanException be) { 275 assertTrue("Should NOT have a root cause", be.getCause() == null); 277 assertTrue("Should NOT have a target", be.getTarget() == null); 278 } 279 280 try { 281 create(" a", Bean1.class); 282 fail("Should have failed because the property starts with white space"); 283 } catch (BeanException be) { 284 assertTrue("Should have a root cause of NoSuchMethodException", 286 be.getCause() != null && be.getCause() instanceof NoSuchMethodException ); 287 assertTrue("Should NOT have a target", be.getTarget() == null); 288 } 289 290 try { 291 create("", Bean1.class); 292 fail("Should have failed because the property is empty"); 293 } catch (BeanException be) { 294 assertTrue("Should NOT have a root cause", be.getCause() == null); 296 assertTrue("Should NOT have a target", be.getTarget() == null); 297 } 298 } 299 300 303 public void testExceptionalProperty() { 304 305 try { 306 Bean1 bean1 = new Bean1(); 307 BeanProperty prop = create("throwsAnException", Bean1.class); 308 309 prop.getPropertyValue(bean1); 310 fail("Should have failed because throwsAnException throws a TestException"); 311 } catch (BeanException be) { 312 assertTrue("Should have a root cause of InvocationTargetException", 313 be.getCause() instanceof InvocationTargetException ); 314 assertTrue("Should have a target of TestException", 315 be.getTarget() instanceof TestException); 316 } 317 318 try { 319 Bean1 bean1 = new Bean1(); 320 BeanProperty prop = create("throwsAnException", Bean1.class); 321 322 prop.setPropertyValue(bean1, "foo"); 323 fail("Should have failed because throwsAnException throws a TestException"); 324 } catch (BeanException be) { 325 assertTrue("Should have a root cause of InvocationTargetException", 326 be.getCause() instanceof InvocationTargetException ); 327 assertTrue("Should have a target of TestException", 328 be.getTarget() instanceof TestException); 329 } 330 } 331 332 335 public void testNonAccessibleProperty() { 336 337 try { 338 create("notAccessible", Bean1.class); 339 fail("Should have failed because notAccessible is private"); 340 } catch (BeanException be) { 341 assertTrue("Should have a root cause of NoSuchMethodException", 342 be.getCause() instanceof NoSuchMethodException ); 343 } 344 } 345 346 349 public void testIndexOnly() { 350 351 try { 352 BeanProperty prop = create("string1", Bean1.class); 353 prop.getPropertyValue(new Bean1(), "key"); 354 fail("Should have failed because key operation not supported"); 355 } catch (BeanException be) { 356 } 358 } 359 360 363 public void testCache() { 364 try { 365 BeanProperty bp = BeanProperty.getInstance("string1", Bean1.class); 366 BeanProperty bp2 = BeanProperty.getInstance("string1", Class.forName("com.inversoft.beans.test.Bean1")); 367 BeanProperty bp3 = BeanProperty.getInstance("string1", new Bean1().getClass()); 368 369 assertSame("Shoudl be same object instance", bp, bp2); 370 assertSame("Shoudl be same object instance", bp, bp3); 371 } catch (BeanException be) { 372 fail(be.toString()); 373 } catch (Exception e) { 374 fail(e.toString()); 375 } 376 377 try { 378 BeanProperty.getInstance("", Bean1.class); 379 fail("Should have failed"); 380 } catch (BeanException be) { 381 } 383 384 try { 385 BeanProperty.getInstance("foo.bar", Bean1.class); 386 fail("Should have failed"); 387 } catch (BeanException be) { 388 } 390 391 try { 393 IndexedBeanProperty.getIndexedInstance("stringIndexed", Bean1.class); 394 BeanProperty.getInstance("stringIndexed", Bean1.class); 395 fail("Should have failed"); 396 } catch (BeanException be) { 397 } 399 } 400 401 405 protected BeanProperty create(String property, Class bean) 406 throws BeanException { 407 return new BeanProperty(property, bean); 408 } 409 410 414 protected BeanProperty create(String property, String bean) 415 throws BeanException { 416 return new BeanProperty(property, bean); 417 } 418 } | Popular Tags |