1 17 18 package org.sape.carbon.core.util.reflection.test; 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashSet ; 24 25 import org.sape.carbon.core.util.reflection.BeanUtil; 26 27 import junit.extensions.ActiveTestSuite; 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 32 40 public class BeanUtilTest extends TestCase { 41 public BeanUtilTest(String name) { 42 super(name); 43 } 44 45 49 public void testGetObjectAttribute() 50 throws 51 NoSuchMethodException , 52 InvocationTargetException , 53 IllegalAccessException { 54 55 String stringValue = "TEST VALUE"; 56 SimpleTestBean simpleTestBean = new SimpleTestBean(); 57 simpleTestBean.setSimpleString(stringValue); 58 59 String result = (String ) BeanUtil.getObjectAttribute( 60 simpleTestBean, 61 "simpleString"); 62 63 if(!stringValue.equals(result)) { 64 fail(); 65 } 66 } 67 68 73 public void testGetObjectAttributeEmbeddedBean() 74 throws 75 NoSuchMethodException , 76 InvocationTargetException , 77 IllegalAccessException { 78 79 String stringValue = "TEST VALUE"; 80 81 SimpleTestBean simpleTestBean = new SimpleTestBean(); 82 simpleTestBean.setSimpleString(stringValue); 83 84 ComplicatedTestBean innerComplicatedTestBean = 85 new ComplicatedTestBean(); 86 innerComplicatedTestBean.setSimpleTestBean(simpleTestBean); 87 88 ComplicatedTestBean outerComplicatedTestBean = 89 new ComplicatedTestBean(); 90 outerComplicatedTestBean.setComplicatedTestBean(innerComplicatedTestBean); 91 92 String result = (String ) BeanUtil.getObjectAttribute( 93 outerComplicatedTestBean, 94 "complicatedTestBean.simpleTestBean.simpleString"); 95 96 if(!stringValue.equals(result)) { 97 fail(); 98 } 99 } 100 101 106 public void testGetObjectAttributePrimitiveInt() 107 throws 108 NoSuchMethodException , 109 InvocationTargetException , 110 IllegalAccessException { 111 112 int intValue = 10; 113 SimpleTestBean simpleTestBean = new SimpleTestBean(); 114 simpleTestBean.setPrimitiveInt(intValue); 115 116 Integer result = (Integer ) BeanUtil.getObjectAttribute( 117 simpleTestBean, 118 "primitiveInt"); 119 120 if(intValue != result.intValue()) { 121 fail(); 122 } 123 } 124 125 129 public void testGetObjectAttributeReadOnly() 130 throws 131 NoSuchMethodException , 132 InvocationTargetException , 133 IllegalAccessException { 134 135 String stringValue = "TEST VALUE"; 136 SimpleTestBean simpleTestBean = new SimpleTestBean(stringValue); 137 138 String result = (String ) BeanUtil.getObjectAttribute( 139 simpleTestBean, 140 "readOnlyString"); 141 142 if(!stringValue.equals(result)) { 143 fail(); 144 } 145 } 146 147 152 public void testGetObjectAttributeFailWriteOnly() 153 throws 154 NoSuchMethodException , 155 InvocationTargetException , 156 IllegalAccessException { 157 158 String stringValue = "TEST VALUE"; 159 SimpleTestBean simpleTestBean = new SimpleTestBean(); 160 simpleTestBean.setWriteOnlyString(stringValue); 161 162 try { 163 String result = (String ) BeanUtil.getObjectAttribute( 164 simpleTestBean, 165 "writeOnlyString"); 166 fail(); 167 } 168 catch (IllegalAccessException e) { 169 } 171 172 } 173 174 179 public void testGetObjectAttributeSimpleBoolean() 180 throws 181 NoSuchMethodException , 182 InvocationTargetException , 183 IllegalAccessException { 184 185 boolean booleanValue = true; 186 SimpleTestBean simpleTestBean = new SimpleTestBean(); 187 simpleTestBean.setSimpleBoolean(booleanValue); 188 189 Boolean result = (Boolean ) BeanUtil.getObjectAttribute( 190 simpleTestBean, 191 "simpleBoolean"); 192 193 if(booleanValue != result.booleanValue()) { 194 fail(); 195 } 196 } 197 198 203 public void testGetObjectAttributeIncorrectBoolean() 204 throws 205 NoSuchMethodException , 206 InvocationTargetException , 207 IllegalAccessException { 208 209 boolean booleanValue = true; 210 SimpleTestBean simpleTestBean = new SimpleTestBean(); 211 simpleTestBean.setIncorrectBoolean(booleanValue); 212 213 Boolean result = (Boolean ) BeanUtil.getObjectAttribute( 214 simpleTestBean, 215 "incorrectBoolean"); 216 217 if(booleanValue != result.booleanValue()) { 218 fail(); 219 } 220 } 221 222 227 public void testGetObjectAttributeOverlapReadBoolean() 228 throws 229 NoSuchMethodException , 230 InvocationTargetException , 231 IllegalAccessException { 232 233 boolean booleanValue = true; 234 SimpleTestBean simpleTestBean = new SimpleTestBean(); 235 simpleTestBean.setOverlapReadBoolean(booleanValue); 236 237 Boolean result = (Boolean ) BeanUtil.getObjectAttribute( 238 simpleTestBean, 239 "overlapReadBoolean"); 240 241 if(booleanValue != result.booleanValue()) { 242 fail(); 243 } 244 } 245 246 247 248 252 public void testGetObjectAttributeCollection() 253 throws 254 NoSuchMethodException , 255 InvocationTargetException , 256 IllegalAccessException { 257 258 Collection collection = new HashSet (); 259 collection.add("Test String"); 260 261 SimpleTestBean simpleTestBean = new SimpleTestBean(); 262 simpleTestBean.setCollection(collection); 263 264 Collection result = (Collection ) BeanUtil.getObjectAttribute( 265 simpleTestBean, 266 "collection"); 267 if(!collection.equals(result)) { 268 fail(); 269 } 270 } 271 272 276 public void testGetObjectAttributeCollectionArrayList() 277 throws 278 NoSuchMethodException , 279 InvocationTargetException , 280 IllegalAccessException { 281 282 Collection collection = new ArrayList (); 283 collection.add("Test String"); 284 285 SimpleTestBean simpleTestBean = new SimpleTestBean(); 286 simpleTestBean.setCollection(collection); 287 288 Collection result = (Collection ) BeanUtil.getObjectAttribute( 289 simpleTestBean, 290 "collection"); 291 if(!collection.equals(result)) { 292 fail(); 293 } 294 } 295 296 301 public void testGetObjectAttributeWithNull() 302 throws 303 NoSuchMethodException , 304 InvocationTargetException , 305 IllegalAccessException { 306 307 ComplicatedTestBean outerComplicatedTestBean = 308 new ComplicatedTestBean(); 309 310 String result = (String ) BeanUtil.getObjectAttribute( 311 outerComplicatedTestBean, 312 "complicatedTestBean.simpleTestBean.simpleString"); 313 314 if(result != null) { 315 fail("Expected to get back a null reference but got a value."); 316 } 317 } 318 319 323 public void testSetObjectAttribute() 324 throws 325 IllegalAccessException , 326 IllegalArgumentException , 327 InvocationTargetException , 328 NoSuchMethodException { 329 330 String stringValue = "TEST VALUE"; 331 SimpleTestBean simpleTestBean = new SimpleTestBean(); 332 333 BeanUtil.setObjectAttribute( 334 simpleTestBean, "simpleString", stringValue); 335 336 if(!stringValue.equals(simpleTestBean.getSimpleString())) { 337 fail(); 338 } 339 } 340 341 346 public void testSetObjectAttributeEmbeddedBean() 347 throws 348 IllegalAccessException , 349 IllegalArgumentException , 350 InvocationTargetException , 351 NoSuchMethodException { 352 353 String stringValue = "TEST VALUE"; 354 355 SimpleTestBean simpleTestBean = new SimpleTestBean(); 356 357 ComplicatedTestBean innerComplicatedTestBean = 358 new ComplicatedTestBean(); 359 innerComplicatedTestBean.setSimpleTestBean(simpleTestBean); 360 361 ComplicatedTestBean outerComplicatedTestBean = 362 new ComplicatedTestBean(); 363 outerComplicatedTestBean.setComplicatedTestBean(innerComplicatedTestBean); 364 365 BeanUtil.setObjectAttribute( 366 outerComplicatedTestBean, "complicatedTestBean.simpleTestBean.simpleString", stringValue); 367 368 if(!stringValue.equals(simpleTestBean.getSimpleString())) { 369 fail(); 370 } 371 } 372 373 378 public void testSetObjectAttributePrimitiveInt() 379 throws 380 IllegalAccessException , 381 IllegalArgumentException , 382 InvocationTargetException , 383 NoSuchMethodException { 384 385 int intValue = 10; 386 SimpleTestBean simpleTestBean = new SimpleTestBean(); 387 388 BeanUtil.setObjectAttribute( 389 simpleTestBean, "primitiveInt", new Integer (intValue)); 390 391 if(intValue != simpleTestBean.getPrimitiveInt()) { 392 fail(); 393 } 394 395 } 396 397 401 public void testSetObjectAttributeFailReadOnly() 402 throws 403 NoSuchMethodException , 404 InvocationTargetException , 405 IllegalAccessException { 406 407 String stringValue = "TEST VALUE"; 408 SimpleTestBean simpleTestBean = new SimpleTestBean(); 409 410 try { 411 BeanUtil.setObjectAttribute( 412 simpleTestBean, "readOnlyString", stringValue); 413 414 fail("Successfully set a read-only property."); 415 } 416 catch (IllegalAccessException e) { 417 } 419 420 } 421 422 426 public void testSetObjectAttributeWriteOnly() 427 throws 428 NoSuchMethodException , 429 InvocationTargetException , 430 IllegalAccessException { 431 432 String stringValue = "TEST VALUE"; 433 SimpleTestBean simpleTestBean = new SimpleTestBean(); 434 435 BeanUtil.setObjectAttribute( 436 simpleTestBean, "writeOnlyString", stringValue); 437 438 if(!stringValue.equals(simpleTestBean.writeOnlyString)) { 441 fail(); 442 } 443 } 444 445 449 public void testSetObjectAttributeSimpleBoolean() 450 throws 451 NoSuchMethodException , 452 InvocationTargetException , 453 IllegalAccessException { 454 455 boolean booleanValue = true; 456 SimpleTestBean simpleTestBean = new SimpleTestBean(); 457 458 BeanUtil.setObjectAttribute( 459 simpleTestBean, "simpleBoolean", new Boolean (booleanValue)); 460 461 if(booleanValue != simpleTestBean.isSimpleBoolean()) { 462 fail("Object booleanValue [" + booleanValue + "] was not " 463 + "equals to simpleTestBean.isSimpleBoolean() [" 464 + simpleTestBean.isSimpleBoolean() + "] after setter."); 465 } 466 } 467 468 473 public void testSetObjectAttributeIncorrectBoolean() 474 throws 475 NoSuchMethodException , 476 InvocationTargetException , 477 IllegalAccessException { 478 479 boolean booleanValue = true; 480 SimpleTestBean simpleTestBean = new SimpleTestBean(); 481 482 BeanUtil.setObjectAttribute( 483 simpleTestBean, "incorrectBoolean", new Boolean (booleanValue)); 484 485 if(booleanValue != simpleTestBean.getIncorrectBoolean()) { 486 fail("Object booleanValue [" + booleanValue + "] was not " 487 + "equals to simpleTestBean.isSimpleBoolean() [" 488 + simpleTestBean.isSimpleBoolean() + "] after setter."); 489 } 490 } 491 492 496 public void testSetObjectAttributeCollection() 497 throws 498 NoSuchMethodException , 499 InvocationTargetException , 500 IllegalAccessException { 501 502 HashSet hashSet = new HashSet (); 503 hashSet.add("Test String"); 504 SimpleTestBean simpleTestBean = new SimpleTestBean(); 505 506 BeanUtil.setObjectAttribute( 507 simpleTestBean, "collection", hashSet); 508 509 if(!hashSet.equals(simpleTestBean.getCollection())) { 510 fail("Object hashSet [" + hashSet + "] was not " 511 + "equals to simpleTestBean.getCollection() [" 512 + simpleTestBean.isSimpleBoolean() + "] after setter."); 513 } 514 } 515 516 520 public void testSetObjectAttributeCollectionArrayList() 521 throws 522 NoSuchMethodException , 523 InvocationTargetException , 524 IllegalAccessException { 525 526 ArrayList arrayList = new ArrayList (); 527 arrayList.add("Test String"); 528 SimpleTestBean simpleTestBean = new SimpleTestBean(); 529 530 BeanUtil.setObjectAttribute( 531 simpleTestBean, "collection", arrayList); 532 533 if(!arrayList.equals(simpleTestBean.getCollection())) { 534 fail("Object collection [" + arrayList + "] was not " 535 + "equals to simpleTestBean.getCollection() [" 536 + simpleTestBean.isSimpleBoolean() + "] after setter."); 537 } 538 539 } 540 541 547 public void testSetObjectAttributeWithNull() 548 throws 549 NoSuchMethodException , 550 InvocationTargetException , 551 IllegalAccessException { 552 553 String stringValue = "Test Value"; 554 ComplicatedTestBean outerComplicatedTestBean = 555 new ComplicatedTestBean(); 556 557 try { 558 BeanUtil.setObjectAttribute( 559 outerComplicatedTestBean, 560 "complicatedTestBean.simpleTestBean.simpleString", 561 stringValue); 562 563 fail("Call failed to throw a NullPointerException."); 564 } 565 catch(NullPointerException e) { 566 } 568 569 } 570 571 575 public static Test suite() { 576 TestSuite masterSuite = new TestSuite(); 577 Test singleThreadedTests = getSingleThreadedTests(); 579 if (singleThreadedTests != null) { 580 masterSuite.addTest(singleThreadedTests); 581 } 582 Test multiThreadedTests = getMultiThreadedTests(); 584 if (multiThreadedTests != null) { 585 masterSuite.addTest(multiThreadedTests); 586 } 587 return masterSuite; 588 } 589 590 596 private static Test getSingleThreadedTests() { 597 TestSuite suite = new TestSuite(); 598 599 suite.addTest(new BeanUtilTest("testGetObjectAttribute")); 600 suite.addTest(new BeanUtilTest("testGetObjectAttributeEmbeddedBean")); 601 suite.addTest(new BeanUtilTest("testGetObjectAttributePrimitiveInt")); 602 suite.addTest(new BeanUtilTest("testGetObjectAttributeReadOnly")); 603 suite.addTest(new BeanUtilTest("testGetObjectAttributeFailWriteOnly")); 604 suite.addTest(new BeanUtilTest("testGetObjectAttributeSimpleBoolean")); 605 suite.addTest(new BeanUtilTest("testGetObjectAttributeCollection")); 606 suite.addTest(new BeanUtilTest("testGetObjectAttributeCollectionArrayList")); 607 suite.addTest(new BeanUtilTest("testGetObjectAttributeWithNull")); 608 suite.addTest(new BeanUtilTest("testGetObjectAttributeIncorrectBoolean")); 609 suite.addTest(new BeanUtilTest("testGetObjectAttributeOverlapReadBoolean")); 610 611 suite.addTest(new BeanUtilTest("testSetObjectAttribute")); 612 suite.addTest(new BeanUtilTest("testSetObjectAttributeEmbeddedBean")); 613 suite.addTest(new BeanUtilTest("testSetObjectAttributePrimitiveInt")); 614 suite.addTest(new BeanUtilTest("testSetObjectAttributeFailReadOnly")); 615 suite.addTest(new BeanUtilTest("testSetObjectAttributeWriteOnly")); 616 suite.addTest(new BeanUtilTest("testSetObjectAttributeSimpleBoolean")); 617 suite.addTest(new BeanUtilTest("testSetObjectAttributeCollection")); 618 suite.addTest(new BeanUtilTest("testSetObjectAttributeCollectionArrayList")); 619 suite.addTest(new BeanUtilTest("testSetObjectAttributeWithNull")); 620 suite.addTest(new BeanUtilTest("testSetObjectAttributeIncorrectBoolean")); 621 622 return suite; 623 } 624 625 630 private static Test getMultiThreadedTests() { 631 TestSuite suite = new ActiveTestSuite(); 632 633 639 640 return suite; 641 } 642 643 651 private static void addTest(TestSuite suite, String testName, int number) { 652 for (int count = 0; count < number; count++) { 653 suite.addTest(new BeanUtilTest(testName)); 654 } 655 } 656 } 657 | Popular Tags |