1 10 package tests.jfun.yan; 11 12 import jfun.yan.Component; 13 import jfun.yan.Container; 14 import jfun.yan.containers.*; 15 import tests.jfun.yan.tck.BaseComponentTestCase; 16 import tests.jfun.yan.testmodel.PersonBean; 17 import tests.jfun.yan.testmodel.PurseBean; 18 19 import java.beans.IntrospectionException ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import jfun.yan.*; 24 import jfun.yan.containers.DefaultContainer; 25 import jfun.yan.factory.GlobalScope; 26 import jfun.yan.factory.ThreadLocalScope; 27 import junit.framework.TestSuite; 28 29 public class SetterInjectionComponentAdapterTestCase 30 extends tests.jfun.yan.tck.BaseComponentTestCase { 31 public static void main(String [] args){ 32 tests.jfun.yan.Utils.runTest(suite()); 33 } 34 private static TestSuite suite(){ 35 return new TestSuite(SetterInjectionComponentAdapterTestCase.class); 36 } 37 38 43 protected Component prepDEF_verifyWithoutDependencyWorks(Container yan){ 44 try{ 45 return Components.ctor(PersonBean.class).bean() 46 .withProperty("name", Components.value("Pico Container")); 47 } 48 catch(java.beans.IntrospectionException e){ 49 throw new IllegalStateException (e.getMessage()); 50 } 51 54 } 55 56 protected Component prepDEF_verifyDoesNotInstantiate(Container yan) { 57 yan.registerValue("Pico Container"); 58 try{ 59 return Components.ctor(DeadBody.class).bean(); 60 } 61 catch(java.beans.IntrospectionException e){ 62 throw new IllegalStateException (e.getMessage()); 63 } 64 68 } 69 70 protected Component prepDEF_visitable() { 71 try{ 72 return Components.bean(Components.ctor(PersonBean.class)) 73 .withProperty("name", Components.value("Pico Container")); 74 } 75 catch(java.beans.IntrospectionException e){ 76 throw new IllegalStateException (e.getMessage()); 77 } 78 81 } 82 83 protected Component prepSER_isSerializable(Container yan) { 84 try{ 85 yan.registerValue("Pico Container"); 86 return Components.bean(Components.ctor(PersonBean.class)); 87 } 88 catch(java.beans.IntrospectionException e){ 89 throw new IllegalStateException (e.getMessage()); 90 } 91 95 } 96 97 protected Component prepSER_isXStreamSerializable(Container yan) { 98 return prepSER_isSerializable(yan); 99 } 100 101 protected void prepDEF_isAbleToTakeParameters(Container yan) { 102 yan.registerValue("Pico Container"); 103 yan.registerConstructor(PersonBean.class); 104 try{ 105 yan.registerComponent(PersonBean.class, 106 Components.ctor(MoneyPurse.class).bean() 107 .withProperty("money", Components.value(new Double (100))) 108 ); 109 } 110 catch(java.beans.IntrospectionException e){ 111 throw new IllegalStateException (e.getMessage()); 112 } 113 120 } 121 122 public static class MoneyPurse 123 extends PurseBean { 124 double money; 125 126 public double getMoney() { 127 return money; 128 } 129 130 public void setMoney(double money) { 131 this.money = money; 132 } 133 } 134 135 protected Component prepVER_verificationFails(Container yan) { 136 yan.registerValue("Pico Container"); 137 yan.registerConstructor(PersonBean.class); 138 try{ 139 yan.registerComponent(PersonBean.class, 140 Components.bean(Components.ctor(MoneyPurse.class)) 141 ); 142 return yan.getComponent(PersonBean.class); 143 } 144 catch(java.beans.IntrospectionException e){ 145 throw new IllegalStateException (e.getMessage()); 146 } 147 153 } 154 155 protected Component prepINS_createsNewInstances(Container yan) { 156 yan.registerValue("Pico Container"); 157 try{ 158 return Components.bean(Components.ctor(PersonBean.class)); 159 } 160 catch(java.beans.IntrospectionException e){ 161 throw new IllegalStateException (e.getMessage()); 162 } 163 166 } 167 168 public static class Ghost 169 extends PersonBean { 170 public Ghost() { 171 throw new VerifyError ("test"); 172 } 173 } 174 175 protected Component prepINS_errorIsRethrown(Container yan) { 176 yan.registerValue("Pico Container"); 177 try{ 178 return Components.bean(Ghost.class); 179 } 180 catch(java.beans.IntrospectionException e){ 181 throw new IllegalStateException (e.getMessage()); 182 } 183 187 } 188 189 public static class DeadBody 190 extends PersonBean { 191 public DeadBody() throws Exception { 192 throw new RuntimeException ("test"); 193 } 194 } 195 196 protected Component prepINS_runtimeExceptionIsRethrown(Container yan) { 197 yan.registerValue("Pico Container"); 198 try{ 199 return Components.bean(Components.ctor(DeadBody.class)); 200 } 201 catch(java.beans.IntrospectionException e){ 202 throw new IllegalStateException (e.getMessage()); 203 } 204 208 } 209 210 public static class HidingPersion 211 extends PersonBean { 212 public HidingPersion() throws Exception { 213 throw new Exception ("test"); 214 } 215 } 216 217 protected Component prepINS_normalExceptionIsRethrownInsidePicoInvocationTargetInitializationException( 218 Container yan) { 219 yan.registerValue("Pico Container"); 220 try{ 221 return Components.bean(Components.ctor(HidingPersion.class)); 222 } 223 catch(java.beans.IntrospectionException e){ 224 throw new IllegalStateException (e.getMessage()); 225 } 230 } 231 232 protected Component prepRES_dependenciesAreResolved(Container yan) { 233 yan.registerValue("Pico Container"); 234 yan.registerConstructor(PersonBean.class); 235 try{ 236 return Components.bean(PurseBean.class); 237 } 238 catch(java.beans.IntrospectionException e){ 239 throw new IllegalStateException (e.getMessage()); 240 } 241 246 } 247 248 public static class WealthyPerson 249 extends PersonBean { 250 PurseBean purse; 251 252 public PurseBean getPurse() { 253 return purse; 254 } 255 256 public void setPurse(PurseBean purse) { 257 this.purse = purse; 258 } 259 } 260 261 protected Component prepRES_failingVerificationWithCyclicDependencyException(Container yan) { 262 yan.registerValue("Pico Container"); 263 yan.registerConstructor(PersonBean.class, WealthyPerson.class); 264 try{ 265 yan.registerComponent(PurseBean.class, 266 Components.bean(Components.ctor(PurseBean.class))); 267 return yan.getComponent(PurseBean.class); 268 } 269 catch(java.beans.IntrospectionException e){ 270 throw new IllegalStateException (e.getMessage()); 271 } 277 } 278 279 protected void prepRES_failingInstantiationWithCyclicDependencyException(Container yan) { 280 yan.registerValue("Pico Container"); 281 yan.registerConstructor(PersonBean.class, WealthyPerson.class); 282 try{ 283 yan.registerComponent(PurseBean.class, 284 Components.bean(Components.ctor(PurseBean.class))); 285 } 286 catch(java.beans.IntrospectionException e){ 287 throw new IllegalStateException (e.getMessage()); 288 } 289 295 } 296 297 public static class A { 298 private B b; 299 private String string = "default"; 300 private List list = new java.util.ArrayList (); 301 302 public void setB(B b) { 303 this.b = b; 304 } 305 306 public B getB() { 307 return b; 308 } 309 310 public String getString() { 311 return string; 312 } 313 314 public void setString(String string) { 315 this.string = string; 316 } 317 318 public List getList() { 319 return list; 320 } 321 322 public void setList(List list) { 323 this.list = list; 324 } 325 } 326 327 public static class B { 328 } 329 330 public void testAllUnsatisfiableDependenciesAreSignalled() 331 throws IntrospectionException { 332 Component aAdapter = 333 Components.bean(A.class); 334 Component bAdapter = 336 Components.bean(B.class).singleton(); 337 339 Container yan = new DefaultContainer(); 340 yan.verify(); 341 yan.registerComponent("b", bAdapter); 342 yan.registerComponent("a", aAdapter); 343 try { 344 yan.getInstance("a"); 346 } catch (UnsatisfiedComponentException e) { 347 assertEquals("The property list of type java.util.List for component <a> is not resolveable", 349 e.getMessage()); 350 } 353 yan.registerComponent("a", aAdapter.optionalProperty("string") 354 .ignoreProperty("list")); 355 final B b = (B) yan.getInstance("b"); 356 A a = (A) yan.getInstance("a"); 357 assertEquals("default", a.getString()); 358 assertEquals(new java.util.ArrayList (), a.getList()); 359 assertSame(b, a.getB()); 360 yan.registerValue("hello"); 361 a = (A) yan.getInstance("a"); 362 assertEquals("hello", a.getString()); 363 assertEquals(new java.util.ArrayList (), a.getList()); 364 assertSame(b, a.getB()); 365 final java.util.List list1 = new java.util.LinkedList (); 366 list1.add("abc"); 367 yan.registerValue(list1); 368 a = (A) yan.getInstance("a"); 369 assertEquals("hello", a.getString()); 370 assertEquals(new java.util.ArrayList (), a.getList()); 371 assertSame(b, a.getB()); 372 } 373 public void testVerifyAllUnsatisfiableDependenciesAreSignalled() 374 throws IntrospectionException { 375 Component aAdapter = 376 Components.bean(A.class); 377 Component bAdapter = 379 Components.bean(B.class); 380 382 Container yan = new DefaultContainer(); 383 yan.verify(); 384 yan.registerComponent("b", bAdapter); 385 yan.registerComponent("a", aAdapter); 386 try { 387 yan.verify(); 389 } catch (UnsatisfiedComponentException e) { 390 } 394 } 395 public static class C { 396 private B b; 397 private List l; 398 private final boolean asBean; 399 400 public C() { 401 asBean = true; 402 } 403 404 public C(B b) { 405 this.l = new ArrayList (); 406 this.b = b; 407 asBean = false; 408 } 409 410 public void setB(B b) { 411 this.b = b; 412 } 413 414 public B getB() { 415 return b; 416 } 417 418 public void setList(List l) { 419 this.l = l; 420 } 421 422 public List getList() { 423 return l; 424 } 425 426 public boolean instantiatedAsBean() { 427 return asBean; 428 } 429 } 430 431 public void testHybridBeans() 432 throws IntrospectionException { 433 Component bAdapter =Components.bean(B.class); 434 Component cAdapter =Components.bean(C.class); 436 440 Container yan = new DefaultContainer(); 441 yan.registerComponent("b", bAdapter); 442 yan.registerComponent("c", cAdapter); 443 yan.registerComponent("c0", cAdapter); 444 yan.registerConstructor(ArrayList .class, new Class [0]); 445 446 C c = (C) yan.getInstance("c"); 447 assertTrue(c.instantiatedAsBean()); 449 C c0 = (C) yan.getInstance("c0"); 450 assertTrue(c0.instantiatedAsBean()); 452 } 453 public interface IYin{ 454 public void setYang(IYang yang); 455 456 public IYang getYang(); 457 } 458 public interface IYang{ 459 public void setYin(IYin yang); 460 461 public IYin getYin(); 462 } 463 public static class Yin implements IYin { 464 private IYang yang; 465 466 public void setYang(IYang yang) { 467 this.yang = yang; 468 } 469 470 public IYang getYang() { 471 return yang; 472 } 473 } 474 475 public static class Yang implements IYang{ 476 private IYin yin; 477 478 public void setYin(IYin yin) { 479 this.yin = yin; 480 } 481 482 public IYin getYin() { 483 return yin; 484 } 485 } 486 487 public void testShouldBeAbleToHandleMutualDependenciesWithSetterInjection() 490 throws IntrospectionException { 491 try{ 492 Container yan = new DefaultContainer(); 493 495 final java.util.Map props = new HashMap (); 497 props.put("yin", Components.useType(IYin.class)); 498 props.put("yang", Components.useType(Yang.class)); 499 yan.registerComponent(Components.bean(Yin.class).singleton().singleton() 500 .withArgument(1, Components.value("bad ying")) 501 .withProperties(new String []{"yin","yang"}, 502 new Creator[]{Components.useType(IYin.class), 503 Components.useType(Yang.class)}).guard()); 504 yan.registerComponent(Components.bean(Yang.class).singleton(new GlobalScope()) 505 .withProperties(props) 506 .withProperty("nonexistent", Components.value("bad yang")) 507 .singleton(new ThreadLocalScope())); 508 509 IYin yin = (IYin) yan.getInstanceOfType(IYin.class); 510 Yang yang = (Yang) yan.getInstance(Yang.class); 511 512 assertSame(yin, yang.getYin()); 513 assertSame(yang, yin.getYang()); 514 } 515 catch(YanException e){ 516 e.printResolutionTrace(); 517 throw e; 518 } 519 } 520 public void testShouldBeAbleToHandleMutualDependenciesWithCachingProxy() 521 throws IntrospectionException { 522 Container yan = new SingletonProxyContainer(); 523 525 526 yan.registerComponent(Components.bean(Yin.class) 527 .withArgument(1, Components.value("bad ying")) 528 ); 529 yan.registerComponent(Components.bean(Yang.class) 530 .withProperty("nonexistent", Components.value("bad yang")) 531 ); 532 533 IYin yin = (IYin) yan.getInstanceOfType(IYin.class); 534 IYang yang = (IYang) yan.getInstanceOfType(IYang.class); 535 536 assertSame(yin, yang.getYin()); 537 assertSame(yang, yin.getYang()); 538 } 539 public void testShouldBeAbleToHandleMutualDependenciesWithProxy() 540 throws IntrospectionException { 541 Container yan = new ProxyContainer(); 542 544 545 yan.registerComponent(Components.bean(Yin.class, new String []{"yang"}) 546 .withArgument(1, Components.value("bad ying")) 547 ); 548 yan.registerComponent(Components.bean(Yang.class) 549 .withProperty("nonexistent", Components.value("bad yang")) 550 .singleton(new ThreadLocalScope()) 551 ); 552 yan.verify(); 553 IYin yin = (IYin) yan.getInstanceOfType(IYin.class); 554 IYang yang = (IYang) yan.getInstanceOfType(IYang.class); 555 556 assertNotSame(yin, yang.getYin()); 557 assertNotSame(yang, yin.getYang()); 558 } 559 public static Integer getNullString(){return null;} 560 public void testNullBean(){ 561 562 } 563 } 564 | Popular Tags |