1 2 11 package tests.jfun.yan.tck; 12 import jfun.yan.*; 13 import junit.framework.Assert; 14 import junit.framework.TestCase; 15 import junit.framework.TestSuite; 16 17 import tests.jfun.models.Automobile; 18 import tests.jfun.models.CopyCommand; 19 import tests.jfun.models.Echo; 20 import tests.jfun.models.MyBean; 21 import tests.jfun.models.Struct; 22 import tests.jfun.yan.testmodel.CarEngine; 24 import tests.jfun.yan.testmodel.DependsOnTouchable; 25 26 import tests.jfun.yan.testmodel.Mtds; 27 28 import tests.jfun.yan.testmodel.SimpleTouchable; 29 import tests.jfun.yan.testmodel.StatefulResource; 30 import tests.jfun.yan.testmodel.Touchable; 31 import tests.jfun.yan.testmodel.Washable; 32 import tests.jfun.yan.testmodel.WashableTouchable; 33 34 35 import java.beans.IntrospectionException ; 36 37 import java.io.ByteArrayInputStream ; 38 import java.io.ByteArrayOutputStream ; 39 import java.io.IOException ; 40 import java.io.ObjectInputStream ; 41 import java.io.ObjectOutputStream ; 42 import java.lang.reflect.Method ; 43 44 import java.util.ArrayList ; 45 46 import java.util.Collection ; 47 import java.util.HashMap ; 48 import java.util.Map ; 49 import java.util.Stack ; 50 51 52 53 import jfun.yan.Component; 54 import jfun.yan.Components; 55 import jfun.yan.Creator; 56 import jfun.yan.IrresolveableArgumentException; 57 import jfun.yan.UnresolvedComponentException; 58 import jfun.yan.Container; 59 import jfun.yan.containers.DefaultContainer; 60 import jfun.yan.containers.DelegatingContainer; 61 import jfun.yan.containers.SimpleContainer; 62 import jfun.yan.etc.Beans; 63 import jfun.yan.factory.ThreadLocalScope; 64 import jfun.yan.function.Function; 65 66 import jfun.yan.lifecycle.ExceptionHandlers; 67 68 import jfun.yan.lifecycle.Lifecycle; 69 import jfun.yan.lifecycle.Phase; 70 import jfun.yan.lifecycle.DefaultLifecycleDescriptor; 71 import jfun.yan.lifecycle.DefaultLifecycleManager; 72 73 76 public class BaseContainerTestCase extends TestCase implements java.io.Serializable { 77 public static void main(String [] args){ 78 tests.jfun.yan.Utils.runTest(suite()); 79 } 80 private static TestSuite suite(){ 81 return new TestSuite(BaseContainerTestCase.class); 82 } 83 protected final DefaultContainer 84 createPicoContainerWithDependsOnTouchableOnly(){ 85 DefaultContainer yan = new DefaultContainer(); 86 yan.registerConstructor(DependsOnTouchable.class); 87 return yan; 88 89 } 90 91 protected final DefaultContainer createPicoContainerWithTouchableAndDependsOnTouchable() { 92 DefaultContainer yan = createPicoContainerWithDependsOnTouchableOnly(); 93 yan.registerConstructor(Touchable.class, SimpleTouchable.class); 94 return yan; 95 } 96 97 public void testUpDownDependenciesCannotBeFollowed() 98 throws Exception { 99 Container parent = createYanContainer(); 100 Container child = createYanContainer().inherit(parent); 101 child.registerConstructor(ComponentF.class); 104 parent.registerConstructor(ComponentA.class); 105 parent.registerConstructor(ComponentB.class); 106 child.registerConstructor(ComponentC.class); 107 try{ 108 child.getInstance(ComponentF.class); 109 fail("should not resolve C"); 110 } 111 catch(IrresolveableArgumentException e){ 112 assertEquals(3, e.getResolutionTrace().size()); 114 assertEntry(e, 0, "parameter 1 of <public tests.jfun.yan.tck.BaseContainerTestCase$ComponentA(tests.jfun.yan.tck.BaseContainerTestCase$ComponentB,tests.jfun.yan.tck.BaseContainerTestCase$ComponentC)>"); 115 assertEntry(e, 1, "parameter 0 of <public tests.jfun.yan.tck.BaseContainerTestCase$ComponentF(tests.jfun.yan.tck.BaseContainerTestCase$ComponentA)>"); 116 assertEntry(e, 2, "getInstance <class tests.jfun.yan.tck.BaseContainerTestCase$ComponentF>"); 117 assertParameter(e, 0, ComponentA.class, 1); 118 assertParameter(e, 1, ComponentF.class, 0); 119 assertEquals(ComponentC.class, e.getParameterType()); 120 } 121 122 child.registerConstructor(ComponentF1.class); 124 parent.registerConstructor(ComponentA1.class); 125 parent.registerConstructor(ComponentB1.class); 126 parent.registerConstructor(ComponentC1.class); 127 child.registerConstructor(ComponentD.class); 128 try{ 129 child.getInstance(ComponentF1.class); 130 fail("should not resolve D"); 131 } 132 catch(IrresolveableArgumentException e){ 133 assertEquals(4, e.getResolutionTrace().size()); 135 assertEntry(e, 3, "getInstance <class tests.jfun.yan.tck.BaseContainerTestCase$ComponentF1>"); 136 assertParameter(e, 0, ComponentC1.class, 0); 137 assertParameter(e, 1, ComponentA1.class, 1); 138 assertParameter(e, 2, ComponentF1.class, 0); 139 assertEquals(ComponentD.class, e.getParameterType()); 140 } 141 142 143 } 144 145 public void testRegisteredComponentsExistAndAreTheCorrectTypes() 146 throws Exception { 147 Container yan = createPicoContainerWithTouchableAndDependsOnTouchable(); 148 yan = trans(yan); 149 yan.verify(); 150 assertNotNull("Container should have Touchable component", 151 yan.getInstance(Touchable.class)); 152 assertNotNull("Container should have DependsOnTouchable component", 153 yan.getInstance(DependsOnTouchable.class)); 154 assertTrue("Component should be instance of Touchable", 155 yan.getInstance(Touchable.class) instanceof Touchable); 156 assertTrue("Component should be instance of DependsOnTouchable", 157 yan.getInstance(DependsOnTouchable.class) instanceof DependsOnTouchable); 158 assertNull("should not have non existent component", yan.getComponent(Map .class)); 159 } 160 161 public void testRegistersSingleInstance(){ 162 DefaultContainer yan = new DefaultContainer(); 163 StringBuffer sb = new StringBuffer (); 164 yan.registerValue(sb); 165 yan.verify(); 166 assertSame(sb, yan.getInstance(StringBuffer .class)); 167 } 168 public void testGettingComponentWithMissingDependencyFails(){ 169 SimpleContainer picoContainer = createPicoContainerWithDependsOnTouchableOnly(); 170 try { 171 picoContainer.getInstance(DependsOnTouchable.class); 172 fail("should need a Touchable"); 173 } catch (jfun.yan.YanException e) { 174 assertEquals(2, e.getResolutionTrace().size()); 175 assertParameter(e, 0, DependsOnTouchable.class, 0); 176 } 177 } 178 public void testVerifyWithMissingDependencyFails(){ 179 SimpleContainer picoContainer = createPicoContainerWithDependsOnTouchableOnly(); 180 try { 181 picoContainer.verify(); 182 fail("should need a Touchable"); 183 } catch (jfun.yan.YanException e) { 184 } 185 } 186 public void testExternallyInstantiatedObjectsCanBeRegistgeredAndLookedUp(){ 187 DefaultContainer yan = new DefaultContainer(); 188 final HashMap map = new HashMap (); 189 yan.registerValue(Map .class, map); 190 yan.verify(); 191 assertSame(map, yan.getInstance(Map .class)); 192 } 193 194 195 public void testLookupWithUnregisteredKeyReturnsNull(){ 196 DefaultContainer yan = new DefaultContainer(); 197 assertNull(yan.getComponent(String .class)); 198 } 199 200 public static class ListAdder { 201 public ListAdder(Collection list) { 202 list.add("something"); 203 } 204 } 205 206 207 public void testCyclicDependencyThrowsCyclicDependencyException() 208 throws Exception { 209 Container yan = new DefaultContainer(); 210 yan.registerConstructor(ComponentB.class); 211 yan.registerConstructor(ComponentD.class); 212 yan.registerConstructor(ComponentE.class); 213 yan = trans(yan); 214 215 try { 216 yan.getInstance(ComponentD.class); 217 fail("CyclicDependencyException expected"); 218 } catch (jfun.yan.CyclicDependencyException e) { 219 assertEquals(3, e.getResolutionTrace().size()); 226 assertEntry(e, 2, "getInstance <class tests.jfun.yan.tck.BaseContainerTestCase$ComponentD>"); 227 assertParameter(e, 0, ComponentE.class, 0); 228 assertParameter(e, 1, ComponentD.class, 0); 229 } catch (StackOverflowError e) { 231 fail(); 232 } 233 } 234 protected void assertParameter(YanException e, int i, Class c, int k){ 235 assertParameter(e, i, Functions.ctor(c), k); 236 } 237 protected void assertParameter(YanException e, int i, Function f, int k){ 238 final Stack trace = e.getResolutionTrace(); 239 assertEquals(new ParameterEntry(f, k), trace.get(i)); 240 } 241 protected void assertTraceSize(YanException e, int s){ 242 assertEquals(s, e.getResolutionTrace().size()); 243 } 244 protected void assertEntry(YanException e, int i, String txt){ 245 assertEquals(txt, ""+e.getResolutionTrace().get(i)); 246 } 247 public void assertEntry(YanException e, int i, Object obj){ 248 assertEquals(obj, e.getResolutionTrace().get(i)); 249 } 250 private void assertProperty(YanException e, int i, Class c, Object k){ 251 final Stack trace = e.getResolutionTrace(); 252 assertEquals(new PropertyEntry(c, k), trace.get(i)); 253 } 254 public void testVerifyCyclicDependencyThrowsCyclicDependencyException() { 255 DefaultContainer yan = new DefaultContainer(); 256 yan.registerConstructor(ComponentB.class); 257 yan.registerConstructor(ComponentD.class); 258 yan.registerConstructor(ComponentE.class); 259 260 try { 261 yan.verify(); 262 fail("CyclicDependencyException expected"); 263 } catch (jfun.yan.CyclicDependencyException e) { 264 assertEquals(3, e.getResolutionTrace().size()); 265 assertParameter(e, 0, ComponentE.class, 0); 266 assertParameter(e, 1, ComponentD.class, 0); 267 assertEntry(e, 2, "verify <class tests.jfun.yan.tck.BaseContainerTestCase$ComponentD>"); 268 } catch (StackOverflowError e) { 269 fail(); 270 } 271 } 272 public void testRemovalNonRegisteredComponentAdapterWorksAndReturnsNull() { 273 final DefaultContainer picoContainer = new DefaultContainer(); 274 picoContainer.unregisterComponent("COMPONENT DOES NOT EXIST"); 275 } 276 public static class NeedsTouchable { 277 public Touchable touchable; 278 279 public NeedsTouchable(Touchable touchable) { 280 this.touchable = touchable; 281 } 282 } 283 284 public static class NeedsWashable { 285 public Washable washable; 286 287 public NeedsWashable(Washable washable) { 288 this.washable = washable; 289 } 290 } 291 292 public void testSameInstanceCanBeUsedAsDifferentType() { 293 DefaultContainer yan = new DefaultContainer(); 294 yan.registerConstructor("wt", WashableTouchable.class); 295 yan.registerConstructor("nw", NeedsWashable.class); 296 yan.registerConstructor("nt", NeedsTouchable.class); 297 298 NeedsWashable nw = (NeedsWashable) yan.getInstance("nw"); 299 NeedsTouchable nt = (NeedsTouchable) yan.getInstance("nt"); 300 assertSame(nw.washable, nt.touchable); 301 } 302 303 304 public static class JMSService { 305 public final String serverid; 306 public final String path; 307 308 public JMSService(String serverid, String path) { 309 this.serverid = serverid; 310 this.path = path; 311 } 312 } 313 314 public void testPico52() { 316 DefaultContainer yan = new DefaultContainer(); 317 318 yan.registerComponent("foo", 319 Components.singleton( 320 Components.withArguments( 321 Components.ctor(JMSService.class), 322 new jfun.yan.Creator[]{ 323 Components.value("0"), 324 Components.value("something") 325 }) 326 ) 327 ); 328 JMSService jms = (JMSService) yan.getInstance("foo"); 329 assertEquals("0", jms.serverid); 330 assertEquals("something", jms.path); 331 } 332 333 public static class ComponentA { 334 public ComponentA(ComponentB b, ComponentC c) { 335 Assert.assertNotNull(b); 336 Assert.assertNotNull(c); 337 } 338 } 339 340 public static class ComponentA1 { 341 public ComponentA1(ComponentB1 b, ComponentC1 c) { 342 Assert.assertNotNull(b); 343 Assert.assertNotNull(c); 344 } 345 } 346 public static class ComponentB1 { 347 } 348 349 public static class ComponentC1 { 350 public ComponentC1(ComponentD d){ 351 Assert.assertNotNull(d); 352 } 353 } 354 355 356 public static class ComponentB { 357 } 358 359 public static class ComponentC { 360 } 361 362 public static class ComponentD { 363 public ComponentD(ComponentE e, ComponentB b) { 364 Assert.assertNotNull(e); 365 Assert.assertNotNull(b); 366 } 367 } 368 369 public static class ComponentE { 370 public ComponentE(ComponentD d) { 371 Assert.assertNotNull(d); 372 } 373 } 374 375 public static class ComponentF { 376 public ComponentF(ComponentA a) { 377 Assert.assertNotNull(a); 378 } 379 } 380 381 public static class ComponentF1 { 382 public ComponentF1(ComponentA1 a) { 383 Assert.assertNotNull(a); 384 } 385 } 386 387 public static class ContainerDependency { 388 public ContainerDependency(SimpleContainer container) { 389 assertNotNull(container); 390 } 391 } 392 393 397 404 405 406 static class Foo implements java.io.Serializable { 407 public boolean started; 408 public boolean stopped; 409 public boolean disposed; 410 411 public void start() { 412 started = true; 413 } 414 415 public void stop() { 416 stopped = true; 417 } 418 419 public void dispose() { 420 disposed = true; 421 } 422 423 } 424 494 public void testComponentInstancesFromParentsAreDirectlyAccessible2() { 495 final Container a = new DefaultContainer(); 496 final Container b = new DefaultContainer().inherit(a); 497 final Container c = new DefaultContainer().inherit(b); 498 499 Object ao = new Object (); 500 Object bo = new Object (); 501 Object co = new Object (); 502 503 a.registerValue("a", ao); 504 b.registerValue("b", bo); 505 c.registerValue("c", co); 506 507 assertEquals(1, a.getInstances().size()); 508 assertEquals(2, b.getInstances().size()); 509 assertEquals(3, c.getInstances().size()); 510 } 511 569 570 571 572 public void testAmbiguousDependencies(){ 573 574 DefaultContainer yan = new DefaultContainer(); 575 576 yan.registerConstructor(SimpleTouchable.class); 578 yan.registerConstructor(DerivedTouchable.class); 579 580 yan.registerConstructor(DependsOnTouchable.class); 582 assertEquals(DependsOnTouchable.class, yan.getComponentType(DependsOnTouchable.class)); 583 assertNull(yan.getComponentType(Touchable.class)); 584 try { 585 yan.getInstance(DependsOnTouchable.class); 586 fail("DependsOnTouchable should have been confused about the two Touchables"); 587 } catch (jfun.yan.AmbiguousComponentResolutionException e) { 588 assertEquals(2, e.getResolutionTrace().size()); 589 assertParameter(e, 0, DependsOnTouchable.class, 0); 590 assertEquals(Touchable.class, e.getComponentKey()); 591 if(DerivedTouchable.class.equals(e.getType1())){ 592 assertEquals(SimpleTouchable.class, e.getType2()); 593 } 594 else if(SimpleTouchable.class.equals(e.getType1())){ 595 assertEquals(DerivedTouchable.class, e.getType2()); 596 } 597 else fail(); 598 599 } 600 } 601 602 public static class DerivedTouchable extends SimpleTouchable { 603 public DerivedTouchable() { 604 } 605 } 606 protected final Container createYanContainer(){ 607 final Container yan = getContainerImpl(); 608 return new DelegatingContainer(yan){ 609 public void registerComponent(Object key, Component cc){ 610 test(cc); 611 super.registerComponent(key, cc); 612 } 613 public void registerComponent(Component cc){ 614 test(cc); 615 super.registerComponent(cc); 616 } 617 private void test(Component cc){ 618 assertEquals(cc, cc); 619 if(cc!=null) 620 assertFalse(cc.equals(null)); 621 final HashMap hmap = new HashMap (); 622 hmap.put(cc, cc); 623 final Component cc1 = (Component)hmap.get(cc); 624 assertEquals(cc, cc1); 625 assertEquals(""+cc, ""+cc1); 626 } 627 }; 628 } 629 protected Container getContainerImpl(){ 630 return new DefaultContainer(); 631 } 632 public void testSize(){ 633 final Container yan = createYanContainer(); 634 String s1 = "hello world"; 635 yan.registerValue(s1); 636 yan.registerValue(s1); 637 yan.registerComponent(String .class, Components.value(s1).singleton()); 638 yan.registerConstructor(java.util.ArrayList .class, (Class [])null); 639 assertEquals(2, yan.keys().size()); 640 assertEquals(2, yan.getInstances().size()); 641 } 642 665 public void testSubsume(){ 666 final Container yan = createYanContainer(); 667 yan.registerComponent(Components.ctor(String .class, null).subsume(CharSequence .class)); 668 final Object str1 = yan.getInstance(CharSequence .class); 669 final Object str2 = yan.getInstanceOfType(CharSequence .class); 670 assertEquals(str1, ""); 671 assertEquals(str2, ""); 672 if(str1!=str2){ 673 try{ 674 yan.getInstanceOfType(String .class); 675 fail("should have failed"); 676 } 677 catch(UnresolvedComponentException e){ 678 assertEquals(1, e.getResolutionTrace().size()); 679 assertEntry(e, 0, "getInstanceOfType <java.lang.String>"); 680 assertEquals(String .class, e.getComponentKey()); 681 } 682 } 683 } 684 public void testArray(){ 685 final Container yan = createYanContainer(); 686 yan.registerComponent(int[].class, 687 Components.static_method(java.lang.reflect.Array .class, 688 "newInstance", new Class []{Class .class, int.class}) 689 .cast(int[].class) 690 .withArgument(0, Components.useKey("array_dimension"))); 691 yan.registerValue(new Integer (5)); 692 yan.registerValue("array_dimension", int.class); 693 assertTrue(yan.getInstance(int[].class) instanceof int[]); 694 assertEquals(5, ((int[])yan.getInstanceOfType(int[].class)).length); 695 } 696 public static Object returnNull(){return null;} 697 public Object id(Object obj){return obj;} 698 public int id(int i){return i;} 699 private void testNullInstance(Container yan){ 700 yan.registerComponent("target", Components.method(this, 701 "id", new Class []{Object .class})); 702 yan.verify(); 703 assertSame(null, yan.getInstance("target")); 704 assertEquals(2, yan.getInstancesOfType(Object .class).size()); 705 assertSame(null, yan.getInstancesOfType(Object .class).get(0)); 706 assertSame(null, yan.getInstancesOfType(Object .class).get(1)); 707 yan.verify(); 709 710 yan.registerComponent("target", Components.method(this, 711 "id", new Class []{Object .class}) 712 .withArgument(0, Components.value(null))); 713 assertSame(null, yan.getInstance("target")); 714 yan.registerComponent("target", Components.method(this, 715 "id", new Class []{int.class}) 716 .withArgument(0, Components.value(null)) 717 .singleton().singleton() 718 .singleton(new ThreadLocalScope())); 719 720 try{ 721 yan.verify(); 722 fail("should have failed"); 723 } 724 catch(ParameterTypeMismatchException e){ 725 assertEquals(2, e.getResolutionTrace().size()); 726 assertParameter(e, 0, 727 Functions.method(this, "id", new Class []{int.class}), 728 0); 729 assertEntry(e, 1, "verify <target>"); 730 assertEquals("target", e.getComponentKey()); 731 assertEquals(int.class, e.getExpectedType()); 732 assertEquals(void.class, e.getActualType()); 733 } 734 } 735 public void testNullInstance(){ 736 final Container yan = createYanContainer(); 737 yan.registerStaticMethod(BaseContainerTestCase.class, "returnNull"); 738 testNullInstance(yan); 739 yan.registerStaticMethod(BaseContainerTestCase.class, "returnNull", null); 740 testNullInstance(yan); 741 yan.registerStaticMethod(Object .class, BaseContainerTestCase.class, "returnNull"); 742 testNullInstance(yan); 743 yan.registerStaticMethod(Object .class, BaseContainerTestCase.class, "returnNull", new Class [0]); 744 testNullInstance(yan); 745 } 746 747 public static class PersonBean{ 748 private String name; 749 public PersonBean(){} 750 public PersonBean(String name){ 751 this.name = name; 752 } 753 public String getName(){return name;} 754 public void setName(String name){ 755 this.name = name; 756 } 757 } 758 public void testTypeMismatchForPropertyAndParameter() 759 throws Exception { 760 final Container yan = createYanContainer(); 761 yan.registerComponent( 762 Components.ctor(PersonBean.class, new Class []{String .class}) 763 .withArgument(0, Components.value(new int[0]))); 764 try{ 765 yan.verify(); 766 fail("should have type failed"); 767 } 768 catch(ParameterTypeMismatchException e){ 769 assertEquals(2, e.getResolutionTrace().size()); 770 assertParameter(e, 0, Functions.ctor(PersonBean.class, new Class []{String .class}), 771 0); 772 assertEquals(PersonBean.class, e.getComponentKey()); 773 assertEquals(0, e.getOrdinalPosition()); 774 assertEquals(String .class, e.getExpectedType()); 775 assertEquals(int[].class, e.getActualType()); 776 } 777 final HashMap props = new HashMap (); 779 props.put("name", Components.ctor(ArrayList .class, null)); 780 yan.registerComponent(Components.bean(PersonBean.class) 781 .withProperties(props)); 782 try{ 783 yan.verify(); 784 fail("should have type failed"); 785 } 786 catch(PropertyTypeMismatchException e){ 787 assertEquals(3, e.getResolutionTrace().size()); 789 assertProperty(e, 0, PersonBean.class, "name"); 790 791 assertEntry(e, 1, "bean"); 792 assertEntry(e, 2, "verify <class tests.jfun.yan.tck.BaseContainerTestCase$PersonBean>"); 793 assertEquals(PersonBean.class, e.getComponentKey()); 794 assertEquals("name", e.getPropertyKey()); 795 assertEquals(String .class, e.getExpectedType()); 796 assertEquals(ArrayList .class, e.getActualType()); 797 } 798 } 799 public void testContainerAsObject(){ 800 final Container yan1 = createYanContainer(); 801 final Container yan2 = createYanContainer(); 802 assertEquals(yan1, yan2); 803 yan1.registerConstructor(Object .class); 804 assertFalse(yan1.equals(yan2)); 805 yan1.getInstancesOfType(Object .class); 806 yan1.unregisterComponent(Object .class); 807 assertEquals(yan1, yan2); 808 final HashMap map = new HashMap (); 809 map.put(yan1, yan1); 810 assertEquals(yan1, map.get(yan1)); 811 } 812 public class Person{ 813 private String name; 814 private int age; 815 private String nickname; 816 private int id; 817 818 Person(int id) { 819 super(); 820 this.id = id; 821 } 822 823 public int getId() { 824 return id; 825 } 826 public int getAge() { 827 return age; 828 } 829 public void setAge(int age) { 830 this.age = age; 831 } 832 public String getName() { 833 return name; 834 } 835 public void setName(String name) { 836 this.name = name; 837 } 838 public String getNickname() { 839 return nickname; 840 } 841 public void setNickname(String nickname) { 842 this.nickname = nickname; 843 } 844 } 845 public Person newPerson(int id){ 846 return new Person(id); 847 } 848 public Person newSuperman(int id){ 849 return new Person(id); 850 } 851 public interface IdFactory{ 852 public int newId(); 853 } 854 public class IdGen implements IdFactory{ 855 private int seed = 0; 856 public int newId(){return ++seed;} 857 } 858 protected Container getContainerWithPerson() 859 throws Exception { 860 try{ 861 Components.method(this, "newPerson").bean( 862 new String []{"name", "name2"}); 863 fail("should have failed with bad property"); 864 } 865 catch(IllegalPropertyNameException e){ 866 assertEquals(0, e.getResolutionTrace().size()); 867 assertEquals("name2", e.getPropertyName()); 868 assertEquals(Person.class, e.getComponentType()); 869 } 870 try{ 871 Components.bean(Components.method(this, "newPerson"), 872 new String []{"name", "name"}); 873 fail("should have failed with duplicate property name"); 874 } 875 catch(IllegalArgumentException e){ 876 assertEquals("duplicate property name: name", e.getMessage()); 877 } 878 try{ 879 Components.bean(Components.method(this, "newPerson"), 880 new String []{"name", "id"}); 881 fail("should have failed with non-writable property"); 882 } 883 catch(NonWritablePropertyException e){ 884 assertTraceSize(e, 0); 885 assertEquals("id", e.getPropertyName()); 886 assertEquals(Person.class, e.getComponentType()); 887 } 888 Container yan = createYanContainer(); 889 Container yan2 = createYanContainer(); 890 yan.registerComponent("supermanref", 891 Components.useKey("superman").bean() 892 .withProperty("name", Components.useKey("personName")) 893 .withProperty("age", Components.useType(Integer .class)) 894 .withProperty("nickname", Components.useContainer(yan2)) 895 ); 896 yan2.registerValue("Gandolf"); 897 try{ 898 yan.verify(); 899 } 900 catch(UnresolvedComponentException e){ 901 assertTraceSize(e, 3); 902 assertEntry(e, 0, "useKey <superman>"); 903 assertEntry(e, 1, "bean"); 904 assertEntry(e, 2, "verify <supermanref>"); 905 assertEquals("superman", e.getComponentKey()); 906 } 907 yan.registerComponent("badsupermanref", 908 Components.bean( 909 Components.useKey("superman"), 910 new String []{"name","age","gender"} 911 ) 912 .withProperty("name", Components.useKey("personName")) 913 .withProperty("age", Components.useType(Integer .class)) 914 .withProperty("nickname", Components.useContainer(yan2)) 915 916 ); 917 yan.registerComponent("superman", 918 Components.method(this, "newSuperman") 919 ); 920 try{ 921 yan.registerValue(new Integer (-10)); 922 yan.getInstance("badsupermanref"); 923 fail("should have failed with IllegalPropertyNameException"); 924 } 925 catch(IllegalPropertyNameException e){ 926 assertTraceSize(e, 2); 928 assertEntry(e, 1, "getInstance <badsupermanref>"); 929 assertEntry(e, 0, "bean"); 930 assertEquals("gender", e.getPropertyName()); 932 assertEquals(Person.class, e.getComponentType()); 933 yan.unregisterComponentsOfType(Integer .class); 934 } 935 yan.unregisterComponent("superman"); 936 yan.unregisterComponent("supermanref"); 937 yan.unregisterComponent("badsupermanref"); 938 final Component anonymous_person = Components.bean( 939 Components.method(this, "newPerson"), 940 new String []{"name","age"} 941 ) 942 .withProperty("name", Components.useKey("personName")) 943 .withProperty("age", Components.useType(Integer .class)); 944 945 yan.registerComponent(anonymous_person); 946 final Component bad_person = Components.bean( 947 Components.value(null).subsume(Person.class), 948 new String []{"name","age"} 949 ) 950 .withProperty("name", Components.useKey("personName")) 951 .withProperty("age", Components.useType(Integer .class)); 952 yan.registerComponent("bad person",bad_person); 953 try{ 954 yan.verify(); 955 fail("should have failed with IrresolveableArgumentException"); 956 } 957 catch(IrresolveableArgumentException e){ 958 assertTraceSize(e, 3); 959 assertParameter(e, 0, Functions.method(this, "newPerson"), 0); 960 assertEntry(e, 1, ""+anonymous_person); 961 assertEntry(e, 2, "verify <class tests.jfun.yan.tck.BaseContainerTestCase$Person>"); 962 assertEquals(0, e.getOrdinalPosition()); 963 assertEquals(Person.class, e.getComponentKey()); 964 assertEquals(int.class, e.getParameterType()); 965 } 966 yan.registerComponent(Components.method(new IdGen(), "newId")); 967 try{ 968 yan.verify(); 969 fail("should have failed with UnresolvedComponentException"); 970 } 971 catch(UnresolvedComponentException e){ 972 assertEquals("personName", e.getComponentKey()); 973 } 974 yan.registerValue("personName", "Yan"); 975 yan.registerValue(new Integer (16)); 976 yan.verify(); 977 try{ 978 yan.getInstance("bad person"); 979 fail("should have failed with null pointer"); 980 }catch(NullBeanObjectException e){ 981 assertTraceSize(e, 2); 982 assertEntry(e, 0, ""+bad_person); 983 assertEntry(e, 1, "getInstance <bad person>"); 984 assertEquals("null component", e.getMessage()); 985 } 986 987 return yan; 988 } 989 public void testBeanWithSelectedProperties() 990 throws Exception { 991 Container yan = getContainerWithPerson(); 992 final Person person = (Person)yan.getInstance(Person.class); 993 assertEquals("Yan", person.getName()); 994 assertEquals(16, person.getAge()); 995 assertSame(null, person.getNickname()); 996 assertEquals(1, person.getId()); 997 } 998 public int expectArray(int[][] arr){return 1;} 999 public void testBasics(){ 1000 final Container yan = createYanContainer(); 1001 yan.registerValue("abc"); 1002 try{ 1003 yan.getInstance("x"); 1004 fail("should have failed with UnresolvedComponentException"); 1005 } 1006 catch(UnresolvedComponentException e){ 1007 assertEquals("x", e.getComponentKey()); 1008 } 1009 assertEquals(yan, yan); 1010 assertEquals(yan.toString(), yan.toString()); 1011 yan.registerComponent("target", Components.method(this, "expectArray")); 1012 try{ 1013 yan.verify(); 1014 fail("should have failed with IrresolveableArgumentException"); 1015 } 1016 catch(IrresolveableArgumentException e){ 1017 assertEquals("target", e.getComponentKey()); 1018 assertEquals(int[][].class, e.getParameterType()); 1019 assertEquals(0, e.getOrdinalPosition()); 1020 assertEquals("The #0 argument of type int[][] for component <target> is not resolveable", e.getMessage()); 1021 } 1022 try{ 1023 yan.registerComponent(Components.useKey("target")); 1024 fail("should have failed with UnknownComponentTypeException"); 1025 } 1026 catch(UnknownComponentTypeException e){} 1027 try{ 1028 yan.registerComponent(Components.useType(String .class)); 1029 fail("should have failed with UnknownComponentTypeException"); 1030 } 1031 catch(UnknownComponentTypeException e){ 1032 assertTraceSize(e, 0); 1033 } 1034 yan.unregisterComponentsOfType(int.class); 1035 yan.verify(); 1036 yan.registerComponent("target_also", Components.useKey("target")); 1037 try{ 1038 yan.verify(); 1039 fail("should have failed with UnresolvedComponentException"); 1040 } 1041 catch(UnresolvedComponentException e){ 1042 assertEquals("target", e.getComponentKey()); 1043 } 1044 yan.registerComponent("target", 1045 Components.method(this, "expectArray") 1046 .withArgument(0, Components.value(new int[10]))); 1047 try{ 1048 yan.verify(); 1049 fail("should have failed with ParameterTypeMismatchException"); 1050 } 1051 catch(ParameterTypeMismatchException e){ 1052 assertTraceSize(e, 3); 1053 assertParameter(e, 0, Functions.method(this, "expectArray"), 0); 1054 assertEntry(e, 1, "useKey <target>"); 1055 assertEntry(e, 2, "verify <target_also>"); 1056 assertEquals("target_also", e.getComponentKey()); 1057 assertEquals(int[][].class, e.getExpectedType()); 1058 assertEquals(int[].class, e.getActualType()); 1059 assertEquals(0, e.getOrdinalPosition()); 1060 assertTrue(e.getMessage().startsWith( 1061 "type mismatch for The #0 parameter of component <target_also>") 1062 ); 1063 } 1064 final Container yan2 = createYanContainer(); 1065 yan.registerComponent("target", Components.useType(yan2, String .class)); 1066 try{ 1067 yan.verify(); 1068 fail("should have failed with UnresolvedComponentException"); 1069 } 1070 catch(UnresolvedComponentException e){ 1071 assertEquals(String .class, e.getComponentKey()); 1072 } 1073 try{ 1074 yan.getInstance("target_also"); 1075 fail("should have failed with UnresolvedComponentException"); 1076 } 1077 catch(UnresolvedComponentException e){ 1078 assertEquals(String .class, e.getComponentKey()); 1079 } 1080 yan2.registerValue("target arrived!"); 1081 assertEquals("target arrived!", yan.getInstance("target")); 1082 } 1083 public void testLifeycles() 1084 throws Throwable { 1085 final Container yan = new DefaultContainer(); 1086 final Component real_engine = 1087 Components.useKey("real engine").singleton(new ThreadLocalScope()); 1088 final DefaultLifecycleDescriptor desc = new DefaultLifecycleDescriptor(); 1089 desc.setCloser(new Phase(desc.getCloser().getPhaseKey(), ExceptionHandlers.suppresser())); 1090 desc.setStopper(new Phase(desc.getStopper().getPhaseKey(), ExceptionHandlers.suppresser())); 1091 final DefaultLifecycleManager lm = new DefaultLifecycleManager(desc); 1092 final DefaultLifecycleManager.DefaultLifecycle lc = lm.newLifecycle() 1093 .starter("start") 1094 .stopper("stop") 1095 .initializer("initialize") 1096 .disposer("destroy"); 1097 final Component engine_lc = lc.manage(real_engine); 1098 yan.registerComponent(CarEngine.class, engine_lc); 1099 1109 yan.registerConstructor("real engine", CarEngine.class); 1110 final Component sttfrc = Components.ctor(StatefulResource.class) 1111 .withArgument(0, Components.useType(CarEngine.class)); 1112 yan.registerComponent(lc.manage(sttfrc)); 1113 1124 final Component car_engine = yan.getComponentOfType(CarEngine.class); 1125 assertNull(car_engine.getType()); 1126 1127 final StatefulResource resource = (StatefulResource)yan.getInstanceOfType(StatefulResource.class); 1128 final CarEngine engine = (CarEngine)yan.getInstanceOfType(CarEngine.class); 1129 assertEquals(0, engine.getStarted()); 1130 assertEquals(0, engine.getStopped()); 1131 assertEquals(0, engine.getInit()); 1132 assertEquals(0, engine.getDisposed()); 1133 1134 assertEquals(0, resource.getInitCount()); 1135 assertEquals(0, resource.getDestroyedCount()); 1136 lm.init(); 1138 assertEquals(0, engine.getStarted()); 1139 assertEquals(0, engine.getStopped()); 1140 assertEquals(1, engine.getInit()); 1141 assertEquals(0, engine.getDisposed()); 1142 1143 assertEquals(1, resource.getInitCount()); 1144 assertEquals(0, resource.getDestroyedCount()); 1145 try{ 1146 lm.start(); 1148 fail("should have failed with unsupported"); 1149 } 1150 catch(UnsupportedOperationException e){ 1151 assertEquals("start", e.getMessage()); 1152 } 1153 assertEquals(1, engine.getStarted()); 1154 assertEquals(0, engine.getStopped()); 1155 assertEquals(1, engine.getInit()); 1156 assertEquals(0, engine.getDisposed()); 1157 1158 assertEquals(1, resource.getInitCount()); 1159 assertEquals(0, resource.getDestroyedCount()); 1160 lm.init(); 1162 assertEquals(1, engine.getStarted()); 1163 assertEquals(0, engine.getStopped()); 1164 assertEquals(1, engine.getInit()); 1165 assertEquals(0, engine.getDisposed()); 1166 1167 assertEquals(1, resource.getInitCount()); 1168 assertEquals(0, resource.getDestroyedCount()); 1169 lm.stop(); 1171 assertEquals(1, engine.getStarted()); 1172 assertEquals(1, engine.getStopped()); 1173 assertEquals(1, engine.getInit()); 1174 assertEquals(0, engine.getDisposed()); 1175 1176 assertEquals(1, resource.getInitCount()); 1177 assertEquals(0, resource.getDestroyedCount()); 1178 lm.dispose(); 1181 assertEquals(1, engine.getStarted()); 1182 assertEquals(1, engine.getStopped()); 1183 assertEquals(1, engine.getInit()); 1184 assertEquals(1, engine.getDisposed()); 1185 1186 assertEquals(1, resource.getInitCount()); 1187 assertEquals(1, resource.getDestroyedCount()); 1188 1189 try{ 1190 lm.start(); 1192 fail("should have failed with unsupported"); 1193 } 1194 catch(UnsupportedOperationException e){ 1195 assertEquals("start", e.getMessage()); 1196 } 1197 assertEquals(2, engine.getStarted()); 1198 assertEquals(1, engine.getStopped()); 1199 assertEquals(1, engine.getInit()); 1200 assertEquals(1, engine.getDisposed()); 1201 1202 assertEquals(1, resource.getInitCount()); 1203 assertEquals(1, resource.getDestroyedCount()); 1204 1205 1206 lm.stop(); 1208 assertEquals(2, engine.getStarted()); 1209 assertEquals(2, engine.getStopped()); 1210 assertEquals(1, engine.getInit()); 1211 assertEquals(1, engine.getDisposed()); 1212 1213 assertEquals(1, resource.getInitCount()); 1214 assertEquals(1, resource.getDestroyedCount()); 1215 1216 1226 } 1227 1243 public void testUseBadContainerKeyAndContainerType(){ 1244 final Container yan = createYanContainer(); 1245 yan.registerComponent("use_bad", Components.useKey(yan, "badkey")); 1246 try{ 1247 yan.verify(); 1248 fail("should have failed with UnresolvedComponentException"); 1249 } 1250 catch(UnresolvedComponentException e){ 1251 assertEquals("badkey", e.getComponentKey()); 1252 yan.unregisterComponent("use_bad"); 1253 } 1254 yan.registerValue(char.class, new Character ('a')); 1255 yan.registerComponent("use_badtype", 1256 Components.useType(createYanContainer(), char.class)); 1257 try{ 1258 yan.verify(); 1259 fail("should have failed with UnresolvedComponentException"); 1260 } 1261 catch(UnresolvedComponentException e){ 1262 assertEquals(char.class, e.getComponentKey()); 1263 yan.unregisterComponent("use_badtype"); 1264 } 1265 } 1266 public void testUseBadKeyAndType(){ 1267 final Container yan = createYanContainer(); 1268 yan.registerComponent("use_bad", Components.useKey("badkey")); 1269 try{ 1270 yan.verify(); 1271 fail("should have failed with UnresolvedComponentException"); 1272 } 1273 catch(UnresolvedComponentException e){ 1274 assertEquals("badkey", e.getComponentKey()); 1275 yan.unregisterComponent("use_bad"); 1276 } 1277 1278 yan.registerComponent("use_badtype", 1279 Components.useType(char.class)); 1280 try{ 1281 yan.verify(); 1282 fail("should have failed with UnresolvedComponentException"); 1283 } 1284 catch(UnresolvedComponentException e){ 1285 assertEquals(char.class, e.getComponentKey()); 1286 yan.unregisterComponent("use_badtype"); 1287 } 1288 } 1289 1290 public void testBadUseContainerWithArguments(){ 1291 final Container yan1 = createYanContainer(); 1292 final Container yan2 = createYanContainer(); 1293 final Part ref2 = Components.useContainer(yan2); 1294 yan1.registerComponent("target", 1295 Components.ctor(NeedsTouchable.class) 1296 .withArguments(ref2)); 1297 run_testBadUseContainerForArguments(yan1, yan2); 1298 } 1299 public void testBadUseContainerWithArgument(){ 1300 final Container yan1 = createYanContainer(); 1301 final Container yan2 = createYanContainer(); 1302 final Part ref2 = Components.useContainer(yan2); 1303 yan1.registerComponent("target", 1304 Components.ctor(NeedsTouchable.class) 1305 .withArgument(0, ref2)); 1306 run_testBadUseContainerForArguments(yan1, yan2); 1307 } 1308 private void run_testBadUseContainerForArguments(final Container yan1, final Container yan2){ 1309 yan1.registerConstructor(SimpleTouchable.class); 1310 try{ 1311 yan1.verify(); 1312 fail("should have failed with UnresolvedComponentException"); 1313 } 1314 catch(UnresolvedComponentException e){ 1315 assertTraceSize(e, 2); 1316 assertParameter(e, 0, NeedsTouchable.class, 0); 1317 assertEntry(e, 1, "verify <target>"); 1318 assertEquals(Touchable.class, e.getComponentKey()); 1319 } 1320 try{ 1321 yan1.getInstance("target"); 1322 fail("should have failed with UnresolvedComponentException"); 1323 } 1324 catch(UnresolvedComponentException e){ 1325 assertTraceSize(e, 2); 1326 assertParameter(e, 0, NeedsTouchable.class, 0); 1327 assertEntry(e, 1, "getInstance <target>"); 1328 assertEquals(Touchable.class, e.getComponentKey()); 1329 } 1330 yan2.registerConstructor(WashableTouchable.class); 1331 yan1.verify(); 1332 assertEquals(NeedsTouchable.class, yan1.getInstance("target").getClass()); 1333 1334 } 1335 1336 public void testBadUseContainerWithProperties() 1337 throws Exception { 1338 final Container yan1 = createYanContainer(); 1339 final Container yan2 = createYanContainer(); 1340 final Part ref2 = Components.useContainer(yan2); 1341 yan1.registerComponent("target", 1342 Components.bean(PersonBean.class) 1343 .withProperties(ref2)); 1344 run_testBadUseContainerForProperties(yan1, yan2); 1346 } 1347 public void testBadUseContainerWithProperty() 1348 throws Exception { 1349 final Container yan1 = createYanContainer(); 1350 final Container yan2 = createYanContainer(); 1351 final Part ref2 = Components.useContainer(yan2); 1352 yan1.registerComponent("target", 1353 Components.bean(PersonBean.class) 1354 .withProperty("name", ref2)); 1355 run_testBadUseContainerForProperties(yan1, yan2); 1357 } 1358 public void testUseAll(){ 1359 final Container yan = createYanContainer(); 1360 yan.registerComponent("target", Components.useAll(String .class)); 1361 yan.registerValue("abc"); 1362 yan.registerValue("yet another", "cde"); 1363 yan.verify(); 1364 final java.util.List strs = (java.util.List )yan.getInstance("target"); 1365 assertEquals(2, strs.size()); 1366 assertEquals("abc", strs.get(0)); 1367 assertEquals("cde", strs.get(1)); 1368 } 1369 1370 public void testUseAllInDifferentContainer(){ 1371 final Container yan = createYanContainer(); 1372 final Container yan2 = createYanContainer(); 1373 yan.registerComponent("target", Components.useAll(yan2, String .class)); 1374 yan.verify(); 1375 yan2.registerValue("abc"); 1376 yan.registerValue("yet another", "cde"); 1377 yan.verify(); 1378 final java.util.List strs = (java.util.List )yan.getInstance("target"); 1379 assertEquals(1, strs.size()); 1380 assertEquals("abc", strs.get(0)); 1381 yan.verifyComponent( 1382 Components.useAll(yan2, String .class) 1383 ); 1384 final java.util.List strs2 = (java.util.List )yan.instantiateComponent( 1385 Components.useAll(yan2, String .class) 1386 ); 1387 assertEquals(1, strs2.size()); 1388 assertEquals("abc", strs2.get(0)); 1389 1390 } 1391 public void testReturnState(){ 1392 final Container yan = createYanContainer(); 1393 yan.registerComponent( 1394 Components.returnState(Components.useKey("xxx").withState("hello") 1395 ) 1396 ); 1397 yan.verify(); 1398 assertEquals("hello", yan.getInstance(Object .class)); 1399 } 1400 public void testReturnType(){ 1401 final Container yan = createYanContainer(); 1402 yan.registerComponent(Class .class, 1403 Components.returnType(Components.useKey("xxx").cast(String .class)) 1404 ); 1405 yan.verify(); 1406 assertEquals(String .class, yan.getInstance(Class .class)); 1407 } 1408 public void testReturnVerification(){ 1409 final Container yan = createYanContainer(); 1410 yan.registerComponent(Class .class, 1411 Components.returnVerification(Components.useKey("xxx").cast(String .class)) 1412 ); 1413 try{ 1414 yan.verify(); 1415 fail("should have failed with UnresolvedComponentException"); 1416 } 1417 catch(UnresolvedComponentException e){ 1418 assertTraceSize(e, 4); 1420 assertEntry(e, 0, "useKey <xxx>"); 1421 assertEntry(e, 1, "cast <useKey <xxx>> as java.lang.String"); 1422 assertEntry(e, 2, "returnVerification <cast <useKey <xxx>> as java.lang.String>"); 1423 assertEntry(e, 3, "verify <class java.lang.Class>"); 1424 assertEquals("xxx", e.getComponentKey()); 1425 } 1426 try{ 1427 yan.getInstance(Class .class); 1428 fail("should have failed with UnresolvedComponentException"); 1429 } 1430 catch(UnresolvedComponentException e){ 1431 assertTraceSize(e, 4); 1432 assertEntry(e, 0, "useKey <xxx>"); 1433 assertEntry(e, 1, "cast <useKey <xxx>> as java.lang.String"); 1434 assertEntry(e, 2, "returnVerification <cast <useKey <xxx>> as java.lang.String>"); 1435 assertEntry(e, 3, "getInstance <class java.lang.Class>"); 1436 assertEquals("xxx", e.getComponentKey()); 1437 } 1438 yan.registerValue("xxx", 1439 Components.useKey("bad").subsume(int.class).incomplete()); 1440 yan.verify(); 1441 assertEquals(String .class, yan.getInstance(Class .class)); 1442 } 1443 1444 1445 private void run_testBadUseContainerForProperties(Container yan1, Container yan2){ 1446 yan1.registerValue("abc"); 1447 try{ 1448 yan1.verify(); 1449 fail("should have failed with UnresolvedComponentException"); 1450 } 1451 catch(UnresolvedComponentException e){ 1452 assertTraceSize(e, 3); 1454 assertProperty(e, 0, PersonBean.class, "name"); 1455 assertEntry(e, 1, "bean"); 1456 assertEntry(e, 2, "verify <target>"); 1457 assertEquals(String .class, e.getComponentKey()); 1458 } 1459 try{ 1460 yan1.getInstance("target"); 1461 fail("should have failed with UnresolvedComponentException"); 1462 } 1463 catch(UnresolvedComponentException e){ 1464 assertTraceSize(e, 3); 1465 assertProperty(e, 0, PersonBean.class, "name"); 1466 assertEntry(e, 1, "bean"); 1467 assertEntry(e, 2, "getInstance <target>"); 1468 assertEquals(String .class, e.getComponentKey()); 1469 } 1470 yan2.registerValue("7 yan"); 1471 yan1.verify(); 1472 assertEquals("7 yan", ((PersonBean)yan1.getInstance("target")).getName()); 1473 1474 } 1475 1476 1477 1478 public static final class Civilian{ 1479 private String dog; 1480 private String cat; 1481 private String wife = "do not need"; 1482 private final int id; 1483 private final java.util.Date birthday; 1484 public Civilian(int id, final java.util.Date d){ 1485 this.id = id; 1486 this.birthday = d; 1487 } 1488 public String getCat() { 1489 return cat; 1490 } 1491 public void setCat(String cat) { 1492 this.cat = cat; 1493 } 1494 public String getDog() { 1495 return dog; 1496 } 1497 public void setDog(String dog) { 1498 this.dog = dog; 1499 } 1500 public String getWife() { 1501 return wife; 1502 } 1503 public void setWife(String wife) { 1504 this.wife = wife; 1505 } 1506 1507 public int getId() { 1508 return id; 1509 } 1510 1511 public java.util.Date getBirthday() { 1512 return birthday; 1513 } 1514 } 1515 public void testBeanByName() 1516 throws Exception { 1517 final Container yan = createYanContainer(); 1518 yan.registerValue("dog", "Bach"); 1519 yan.registerComponent("civilian", Beans.byName( 1521 Components.ctor(Civilian.class) 1522 .withDefaultArgument(0, Components.value(new Integer (-1))) 1523 .optionalParameter(1) 1525 .bean() 1526 .optionalProperties() 1527 .withDefaultProperty("cat", Components.value("Chopin")) 1528 )); 1529 1530 yan.registerComponent("civilian2", Beans.byName(Components.useKey("civilian"))); 1531 yan.verify(); 1532 Civilian civ = (Civilian)yan.getInstance("civilian"); 1533 assertEquals("Bach", civ.getDog()); 1534 assertEquals("Chopin", civ.getCat()); 1535 assertEquals("do not need", civ.getWife()); 1536 assertEquals(-1, civ.getId()); 1537 assertNull(civ.getBirthday()); 1538 civ = (Civilian)yan.getInstance("civilian2"); 1539 assertEquals("Bach", civ.getDog()); 1540 assertEquals("Chopin", civ.getCat()); 1541 assertEquals("do not need", civ.getWife()); 1542 assertEquals(-1, civ.getId()); 1543 assertNull(civ.getBirthday()); 1544 } 1545 public void testBeanByQualifiedName() 1546 throws Exception { 1547 try{ 1548 final Container yan = createYanContainer(); 1549 yan.registerValue("tests.jfun.yan.tck.BaseContainerTestCase$Civilian.dog", "Bach"); 1550 yan.registerValue("tests.jfun.yan.tck.BaseContainerTestCase$Civilian.cat", "Chopin"); 1551 yan.registerComponent("civilian", Beans.byQualifiedName( 1552 Components.ctor(Civilian.class) 1553 .withDefaultArgument(0, Components.value(new Integer (-1))) 1554 .optionalParameters() 1555 .bean() 1556 .optionalProperties() 1557 )); 1558 1559 yan.registerComponent("civilian2", Beans.byQualifiedName(Components.useKey("civilian"))); 1560 yan.verify(); 1561 Civilian civ = (Civilian)yan.getInstance("civilian"); 1562 assertEquals("Bach", civ.getDog()); 1563 assertEquals("Chopin", civ.getCat()); 1564 assertEquals("do not need", civ.getWife()); 1565 assertEquals(-1, civ.getId()); 1566 assertNull(civ.getBirthday()); 1567 civ = (Civilian)yan.getInstance("civilian2"); 1568 assertEquals("Bach", civ.getDog()); 1569 assertEquals("Chopin", civ.getCat()); 1570 assertEquals("do not need", civ.getWife()); 1571 assertEquals(-1, civ.getId()); 1572 assertNull(civ.getBirthday()); 1573 } 1574 catch(YanException e){ 1575 e.printResolutionTrace(); 1576 throw e; 1577 } 1578 } 1579 public void testMonadPlus(){ 1580 final Container yan = createYanContainer(); 1581 yan.registerComponent("a", Monad.mzero()); 1582 try{ 1583 yan.verify(); 1584 fail("should have failed with ComponentResolutionException"); 1585 } 1586 catch(ComponentResolutionException e){ 1587 assertTraceSize(e, 2); 1588 assertEntry(e, 0, "fail <mzero>"); 1589 assertEquals("mzero", e.getMessage()); 1590 } 1591 yan.registerComponent("a", Monad.mzero().optional()); 1592 try{ 1593 yan.getInstance("a"); 1594 fail("should have failed with DefaultingException"); 1595 } 1596 catch(DefaultingException e){} 1597 yan.registerComponent("a", Components.useKey("b").option("hello world")); 1598 assertEquals("hello world", yan.getInstance("a")); 1599 } 1600 public void testContainerIsSerializable()throws Exception { 1601 getTouchableFromSerializedContainer(); 1602 } 1603 private Container trans(Container yan) 1604 throws java.io.IOException , ClassNotFoundException { 1605 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 1606 ObjectOutputStream oos = new ObjectOutputStream (baos); 1607 1608 oos.writeObject(yan); 1609 ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (baos.toByteArray())); 1610 1611 return (Container) ois.readObject(); 1612 } 1613 private Touchable getTouchableFromSerializedContainer() 1614 throws IOException , ClassNotFoundException { 1615 Container yan = createPicoContainerWithTouchableAndDependsOnTouchable(); 1616 yan.registerComponent("list", 1618 Components.ctor(ArrayList .class, new Class []{int.class}) 1619 .withArgument(0, Components.value(10))); 1620 1621 1622 yan = trans(yan); 1623 1624 DependsOnTouchable dependsOnTouchable = (DependsOnTouchable) 1625 yan.getInstanceOfType(DependsOnTouchable.class); 1626 assertNotNull(dependsOnTouchable); 1627 return (Touchable) yan.getInstanceOfType(Touchable.class); 1628 } 1629 1630 public void testSerializedContainerCanRetrieveImplementation() 1631 throws Exception { 1632 1633 Touchable touchable = getTouchableFromSerializedContainer(); 1634 1635 SimpleTouchable simpleTouchable = (SimpleTouchable) touchable; 1636 1637 assertTrue(simpleTouchable.wasTouched); 1638 } 1639 public static final class GiveBean{ 1640 private boolean awake = false; 1641 private String fname; 1642 private String middle_initial; 1643 private int age; 1644 public void giveAge(int age){ 1645 this.age = age; 1646 } 1647 public void setName(String fn, char mi){ 1648 this.fname = fn; 1649 this.middle_initial = ""+mi; 1650 } 1651 public void wakeup(){ 1652 awake = true; 1653 } 1654 1655 public int getAge() { 1656 return age; 1657 } 1658 public boolean isAwake() { 1659 return awake; 1660 } 1661 public String getFname() { 1662 return fname; 1663 } 1664 public String getMiddleInitial() { 1665 return middle_initial; 1666 } 1667 public void giveTime(long l){} 1668 } 1669 Component custProperty(Component c, final String giver){ 1670 return c.followedBy(new ComponentBinder(){ 1671 public Creator bind(Object obj){ 1672 return Components.method(obj, giver); 1673 } 1674 public Verifiable verify(Class t){ 1675 return Components.method(t, null, giver); 1676 } 1677 public Class bindType(Class t){ 1678 return Functions.method(t, null, giver).getReturnType(); 1679 } 1680 }); 1681 } 1682 1683 Component custProperties(Component c, String [] givers){ 1684 Component r = c; 1685 for(int i=0;i<givers.length;i++){ 1686 r = custProperty(r, givers[i]); 1687 } 1688 return r; 1689 } 1690 public void testGiveBean(){ 1691 runTestGiveBean(custProperties(Components.ctor(GiveBean.class) 1692 , new String []{"giveAge", "setName", "wakeup"})); 1693 } 1694 public void testGiveBeanWithVerify(){ 1695 runTestGiveBean(Components.ctor(GiveBean.class).bind(gbean( 1696 new String []{"giveAge", "setName", "wakeup"})) 1697 ); 1698 } 1699 public void testUnsatisfiableGbean(){ 1700 try{ 1701 verifyGiveBean(Components.ctor(GiveBean.class).bind(gbean( 1702 new String []{"giveAge", "setName", "wakeup", "giveTime"})) 1703 .label("gbean") 1704 ); 1705 fail("should have failed with IrresolveableArgumentException"); 1706 } 1707 catch(IrresolveableArgumentException e){ 1708 assertEntry(e, 0, 1710 "parameter 0 of <public void tests.jfun.yan.tck.BaseContainerTestCase$GiveBean.giveTime(long)>"); 1711 } 1713 } 1714 public void testFloatingMethod(){ 1715 final Class type = Mtds.class; 1716 Container yan = createYanContainer(); 1717 try{ 1718 Components.static_method(type, "greet"); 1719 fail("should have failed with IllegalArgumentException"); 1720 } 1721 catch(IllegalArgumentException e){} 1722 try{ 1723 Components.fun(Functions.instance_method(type, "getGreeting")); 1724 fail("should have failed with IllegalArgumentException"); 1725 } 1726 catch(IllegalArgumentException e){} 1727 yan.registerComponent(Components.static_method(type, "instance")); 1728 yan.registerValue("Tom"); 1729 yan.registerComponent("target", 1730 Components.fun(Functions.instance_method(type, "greet")) 1731 .withArgument(1, Components.value("Jerry"))); 1732 assertEquals("hello Jerry, I am Tom", ""+yan.getInstance("target")); 1733 yan.registerComponent(Components.static_method(type, "instance")); 1734 yan.registerComponent("target", 1735 Components.fun(Functions.instance_method(type, "greet")) 1736 .withArgument(0, Components.useType(Mtds.class) 1738 .withArgument(0, Components.value("Jerry")) 1740 .withArgument(0, Components.value("should not see me")) 1741 ) 1742 ); 1743 assertEquals("hello Tom, I am Jerry", ""+yan.getInstance("target")); 1744 1745 } 1746 public static String tag(String prefix, int i){ 1747 return prefix+i; 1748 } 1749 public void testComponentArgumentsShouldNotPropogateToParts(){ 1750 final Container yan = createYanContainer(); 1751 yan.registerValue("default prefix"); 1752 yan.registerComponent(Components.value(1)); 1753 final Component tag = Components.method(this, "tag"); 1754 final Component target = tag 1755 .withArgument(0, Components.method(this, "tag")) 1756 .withArgument(1, Components.value(0)); 1757 yan.registerComponent("target", target); 1758 final Object result = yan.getInstance("target"); 1759 assertEquals("default prefix10", ""+result); 1760 } 1761 public void testUsePropertyCannotBeAppliedToTopLevel(){ 1762 final Component use1 = Components.useProperty(String .class, "prop1", String .class) 1763 .withProperty("prop1", Components.value("hello")); 1764 final Container yan = createYanContainer(); 1765 yan.registerComponent("target", use1); 1766 try{ 1767 yan.verify(); 1768 } 1769 catch(YanException e){ 1770 assertTraceSize(e, 2); 1771 assertEquals("useProperty can only be used for parameter or property", 1773 e.getMessage()); 1774 } 1775 try{ 1776 yan.instantiateComponent(use1); 1777 } 1779 catch(YanException e){ 1780 assertTraceSize(e, 2); 1782 assertEquals("useProperty can only be used for parameter or property", 1784 e.getMessage()); 1785 } 1786 1787 } 1788 public void testUseArgumentCannotBeAppliedToTopLevel(){ 1789 final Component use1 = Components.useArgument(Functions.ctor(String .class,null), 1790 0, String .class) 1791 .withArgument(0, Components.value("hello")); 1792 final Container yan = createYanContainer(); 1793 yan.registerComponent("target", use1); 1794 try{ 1795 yan.verify(); 1796 } 1797 catch(YanException e){ 1798 assertTraceSize(e, 2); 1799 assertEquals("useArgument can only be used for parameter or property", 1801 e.getMessage()); 1802 } 1803 try{ 1804 yan.getInstances(); 1805 } 1806 catch(YanException e){ 1807 assertTraceSize(e, 2); 1808 assertEquals("useArgument can only be used for parameter or property", 1810 e.getMessage()); 1811 } 1812 1813 } 1814 public void testPropertiesAndArgumentsApplyToBothBeanComponentAndBaseComponent() 1815 throws Exception { 1816 final Container yan = createYanContainer(); 1817 final Component c1 = Components.ctor(MyBean.class, null); 1818 final Component c2 = c1.bean(new String []{"1","nums"}) 1819 .withProperties(new String []{"1","nums"}, 1820 new Component[]{Components.value(true), 1821 Components.value(new int[]{1,2,3}) 1822 }) 1823 .bean(new String []{"name","short"}); 1824 final Component target = c2 1825 .withProperties(new String []{"1","name","short","nums"}, 1826 new Component[]{Components.value(false), 1827 Components.value("Ent"), 1828 Components.value((short)-1), 1829 Components.value(new int[]{1,2,3,4})}); 1830 yan.verifyComponent(target); 1831 final MyBean mb = (MyBean)yan.instantiateComponent(target); 1832 assertTrue(mb.is1()); 1833 assertEquals("Ent", mb.getName()); 1834 assertEquals((short)-1, mb.getShort()); 1835 assertEquals(3, mb.getNums().length); 1836 } 1837 public void testConstructorParametesUsedAsMandatoryProperties() 1838 throws Exception { 1839 final Component c1 = Components.ctor(CopyCommand.class); 1840 final Component cc = Beans.beanComponent(c1, new String []{"source","dest"}); 1841 1842 final Container yan = createYanContainer(); 1843 verifyPropertyNotResolved(yan, cc); 1844 yan.registerComponent(boolean.class, Components.value(true)); 1845 verifyPropertyNotResolved(yan, cc); 1846 assertComponentInfo(c1, 2, 1848 "0=class java.lang.String, 1=class java.lang.String", "", 1849 tests.jfun.models.CopyCommand.class 1850 ); 1851 assertComponentInfo(cc, 0, "", 1852 "source=class java.lang.String, dest=class java.lang.String, force=boolean, options=class java.lang.String", 1853 tests.jfun.models.CopyCommand.class 1854 ); 1855 final Component cc_src = cc.withProperty("source", Components.value("d:/")); 1858 assertComponentInfo(cc_src, 0, "", 1860 "dest=class java.lang.String, force=boolean, options=class java.lang.String", 1861 tests.jfun.models.CopyCommand.class 1862 ); 1863 verifyPropertyNotResolved(yan, cc_src); 1864 final Component cc_src_dst = cc_src.withProperty("dest", Components.value("e:/abc")); 1865 assertComponentInfo(cc_src_dst, 0, "", 1867 "force=boolean, options=class java.lang.String", 1868 tests.jfun.models.CopyCommand.class 1869 ); 1870 final CopyCommand cm = (CopyCommand)yan.instantiateComponent(cc_src_dst); 1871 assertEquals("d:/", cm.getSource()); 1872 assertEquals("e:/abc", cm.getDest()); 1873 assertTrue(cm.isForce()); 1874 assertEquals("", cm.getOptions()); 1875 } 1876 private void assertComponentInfo( 1877 Component c, int pnum, String params, String props, Class type){ 1878 final jfun.yan.ComponentInfo info = 1879 jfun.yan.Introspector.getComponentInfo(c); 1880 assertEquals(pnum, info.getParameterCount()); 1881 assertEquals("type = " + jfun.util.Misc.getTypeName(type) 1882 +"\nparameters = {" + params + "}" 1883 +"\nproperties = {" + props + "}", info.toString()); 1884 } 1885 private void verifyPropertyNotResolved(Container yan, Component cc){ 1886 try{ 1887 yan.instantiateComponent(cc); 1888 fail("should have failed"); 1889 } 1890 catch(IrresolveablePropertyException e){} 1891 } 1892 public static class MultiBean{ 1893 public static class InnerBean{ 1894 1895 private boolean active; 1896 public java.util.List getList(){ 1897 return new ArrayList (); 1898 } 1899 public Object getListAsObject(){ 1900 return new ArrayList (); 1901 } 1902 public void setActive(boolean a){ 1903 this.active = a; 1904 } 1905 public boolean isActive(){ 1906 return active; 1907 } 1908 } 1909 private InnerBean inner; 1910 private InnerBean[] inners = new InnerBean[2]; 1911 public InnerBean getInner(){ 1912 return inner; 1913 } 1914 public void setInner(InnerBean inner){ 1915 this.inner = inner; 1916 } 1917 public Object getInnerAsObject(){ 1918 return inner; 1919 } 1920 public InnerBean[] getInners(){return inners;} 1921 public void setInners(InnerBean[] inners){ 1922 this.inners = inners; 1923 } 1924 public InnerBean getInners(int i){ 1925 return inners[i]; 1926 } 1927 public void setInners(int i, InnerBean inner){ 1928 inners[i] = inner; 1929 } 1930 } 1931 public void testHigherOrderMethod() 1932 throws Exception { 1933 final Component c = Components.ctor(ArrayList .class, null); 1934 testHigherOrderMethod(c); 1935 testHigherOrderMethod(c.cast(null)); 1936 final Component nested = Components.ctor(MultiBean.class); 1937 testHigherOrderMethod( 1938 nested.bean(new String []{"inner"}) 1939 .withProperty("inner", 1940 Components.ctor(MultiBean.InnerBean.class) 1941 ) 1942 .method("getInner").method("getList") 1943 ); 1944 1945 testHigherOrderMethod( 1946 nested.cast(null).bean(new String []{"inner"}) 1947 .withProperty("inner", 1948 Components.ctor(MultiBean.InnerBean.class) 1949 ) 1950 .method("getInner").method("getList") 1951 ); 1952 testHigherOrderMethod( 1953 nested.cast(null).bean(new String []{"inner"}) 1954 .withProperty("inner", 1955 Components.ctor(MultiBean.InnerBean.class) 1956 ) 1957 .method("getInner").method("getListAsObject") 1958 ); 1959 try{ 1960 testHigherOrderMethod( 1961 nested.bean(new String []{"inner"}) 1962 .withProperty("inner", 1963 Components.ctor(MultiBean.InnerBean.class) 1964 ) 1965 .method("getInner").method("getListAsObject") 1966 ); 1967 fail("should have failed with IllegalArgumentException"); 1968 } 1969 catch(IllegalArgumentException e){ 1970 assertEquals("method java.lang.Object.size not found", e.getMessage()); 1971 } 1972 1973 1974 testHigherOrderMethod( 1976 nested.bean(new String []{"inner"}) 1977 .withProperty("inner", 1978 Components.ctor(MultiBean.InnerBean.class) 1979 ) 1980 .getter("inner").getter("list") 1981 ); 1982 testHigherOrderMethod( 1983 nested.setter("inner") 1984 .withArgument(0, 1985 Components.ctor(MultiBean.InnerBean.class) 1986 ) 1987 .getter("inner").getter("list") 1988 ); 1989 testHigherOrderMethod( 1990 nested.cast(null).bean(new String []{"inner"}) 1991 .withProperty("inner", 1992 Components.ctor(MultiBean.InnerBean.class) 1993 ) 1994 .getter("inner").getter("list") 1995 ); 1996 testHigherOrderMethod( 1997 nested.cast(null).setter("inner") 1998 .withArgument(0, 1999 Components.ctor(MultiBean.InnerBean.class) 2000 ) 2001 .getter("inner").getter("list") 2002 ); 2003 try{ 2004 testHigherOrderMethod( 2005 nested.bean(new String []{"inner"}) 2006 .withProperty("inner", 2007 Components.ctor(MultiBean.InnerBean.class) 2008 ) 2009 .getter("inner").getter("listAsObject") 2010 ); 2011 fail("should have failed with IllegalArgumentException"); 2012 } 2013 catch(IllegalArgumentException e){ 2014 assertEquals("method java.lang.Object.size not found", e.getMessage()); 2015 } 2016 testHigherOrderMethod( 2017 nested.cast(null).bean(new String []{"inner"}) 2018 .withProperty("inner", 2019 Components.ctor(MultiBean.InnerBean.class) 2020 ) 2021 .getter("innerAsObject").method("getListAsObject") 2022 ); 2023 testHigherOrderMethod( 2024 nested.cast(null).bean(new String []{"inner"}) 2025 .withProperty("inner", 2026 Components.ctor(MultiBean.InnerBean.class) 2027 ) 2028 .getter("innerAsObject").getter("listAsObject") 2029 ); 2030 try{ 2031 testHigherOrderMethod( 2032 nested.bean(new String []{"inner"}) 2033 .withProperty("inner", 2034 Components.ctor(MultiBean.InnerBean.class) 2035 ) 2036 .getter("innerAsObject").getter("list") 2037 ); 2038 fail("should have failed with IllegalArgumentException"); 2039 } 2040 catch(jfun.util.beans.NoSuchPropertyException e){ 2041 assertEquals("property <java.lang.Object.list> cannot be found", e.getMessage()); 2042 assertEquals("list", e.getPropertyName()); 2043 assertEquals(Object .class, e.getBeanClass()); 2044 } 2045 } 2046 2047 private Component innerInstance(boolean a) 2048 throws IntrospectionException { 2049 return Components.ctor(MultiBean.InnerBean.class).setter("active") 2050 .withArgument(0, Components.value(a)); 2051 } 2052 public void testHigherOrderMethodWithIndexedProperties() 2053 throws Exception { 2054 final Component nested = Components.ctor(MultiBean.class); 2055 final Component inner1 = innerInstance(true); 2056 final Component inner2 = innerInstance(false); 2057 Component data = nested.setter("inner") 2058 .withArgument(0, inner1) 2059 .setter("inners", 0) 2060 .withArgument(0, inner2) 2061 .setter("inners", 1) 2062 .withArgument(0, innerInstance(true)); 2063 testHigherOrderMethod( 2064 data 2065 .getter("inners", 0).getter("list") 2066 ); 2067 assertBool(data.getter("inners", 0).getter("active"), false); 2068 assertBool(data.getter("inners", 1).getter("active"), true); 2069 2070 2071 data = nested.cast(null).setter("inner") 2072 .withArgument(0, inner1) 2073 .setter("inners", 0) 2074 .withArgument(0, inner2) 2075 .setter("inners", 1) 2076 .withArgument(0, innerInstance(true)) 2077 ; 2078 testHigherOrderMethod( 2079 data 2080 .getter("inner").getter("list") 2081 ); 2082 assertBool(data.getter("inners", 0).getter("active"), false); 2083 assertBool(data.getter("inners", 1).getter("active"), true); 2084 2085 } 2086 public void testComponentManagedByMoreThanOneLifecycleManagers() 2087 throws Throwable { 2088 DefaultLifecycleManager man1 = new DefaultLifecycleManager(); 2089 DefaultLifecycleManager man2 = new DefaultLifecycleManager(); 2090 Component c = Components.ctor(Automobile.class); 2091 c = man1.newLifecycle() 2092 .starter("start1") 2093 .stopper("stop1") 2094 .manage(c); 2095 c = man2.newLifecycle() 2096 .starter("start2") 2097 .stopper("stop2") 2098 .manage(c); 2099 Container yan = createYanContainer(); 2100 yan.registerComponent("target", c); 2101 Automobile a = (Automobile)yan.getInstance("target"); 2102 assertFalse(a.isRunning1()); 2103 assertFalse(a.isRunning2()); 2104 man1.start(); 2105 assertTrue(a.isRunning1()); 2106 assertFalse(a.isRunning2()); 2107 man2.start(); 2108 assertTrue(a.isRunning1()); 2109 assertTrue(a.isRunning2()); 2110 man1.stop(); 2111 assertFalse(a.isRunning1()); 2112 assertTrue(a.isRunning2()); 2113 man2.stop(); 2114 assertFalse(a.isRunning1()); 2115 assertFalse(a.isRunning2()); 2116 } 2117 public void testParameterCustomizationShouldNotPropogateToTheNextLevel() 2118 throws Exception { 2119 Container yan = this.createYanContainer(); 2120 yan.registerValue("autowired"); 2121 final Component manual = Components.value("manualwired"); 2122 final Component cc1 = Components.ctor(Echo.class).method("newInstance") 2123 .withArgument(0, manual); 2124 final Component cc2 = Components.ctor(Echo.class).method("add") 2125 .withArgument(0, manual); 2126 final Component cc3 = Components.static_method(Echo.class, "newInstance") 2127 .method("add") 2128 .withArgument(0, manual); 2129 final Component cc4 = Components.static_method(Echo.class, "newInstance") 2130 .bean() 2131 .method("add") 2132 .withArgument(0, manual); 2133 final Component cc5 = Components.static_method(Echo.class, "newInstance") 2134 .bean() 2135 .withArgument(0, manual) 2136 .method("add") 2137 .withArgument(0, manual); 2138 2139 yan.registerComponent("cc1", cc1); 2140 yan.registerComponent("cc2", cc2); 2141 yan.registerComponent("cc3", cc3); 2142 yan.registerComponent("cc4", cc4); 2143 yan.registerComponent("cc5", cc5); 2144 yan.registerComponent("cc6", cc4.singleton()); 2145 assertEquals("manualwired", yan.getInstance("cc1").toString()); 2146 assertEquals("autowiredmanualwired", yan.getInstance("cc2").toString()); 2147 assertEquals("autowiredmanualwired", yan.getInstance("cc3").toString()); 2148 assertEquals("autowiredmanualwired", yan.getInstance("cc4").toString()); 2149 assertEquals("manualwiredmanualwired", yan.getInstance("cc5").toString()); 2150 assertEquals("autowiredmanualwired", yan.getInstance("cc6").toString()); 2151 } 2152 private void assertBool(Component c, boolean a){ 2153 final Container yan = createYanContainer(); 2154 yan.registerComponent("target", c); 2155 final Boolean result = (Boolean )yan.getInstance("target"); 2156 assertEquals(a, result.booleanValue()); 2157 } 2158 private void testHigherOrderMethod(Component c) 2159 throws Exception { 2160 try{ 2161 final Component sz0 = c.method("size"); 2162 final Container yan = createYanContainer(); 2163 yan.registerComponent("list", c); 2164 yan.registerComponent("size", sz0); 2165 assertEquals(new Integer (0), yan.getInstance("size")); 2166 final Component sz1 = c.followedBy(new Binder(){ 2167 public Creator bind(Object l){ 2168 return Components.value(l).method("add", new Class []{Object .class}) 2169 .withArgument(0, Components.useKey("elem1")); 2170 } 2171 }).method("size", null); 2172 yan.registerValue("elem1", "val1"); 2173 yan.registerComponent("size", sz1); 2174 assertEquals(new Integer (1), yan.getInstance("size")); 2175 2176 final Component sz2 = c.followedBy(new Binder(){ 2177 public Creator bind(Object l){ 2178 final Component adder = 2179 Components.value(l).method("add", new Class []{Object .class}); 2180 2181 return adder 2182 .withArgument(0, Components.useKey("elem1")) 2183 .seq(adder.withArgument(0, Components.useKey("elem2"))); 2184 } 2185 }).method(java.util.List .class.getMethod("size", new Class [0])); 2186 yan.registerValue("elem2", "val2"); 2187 yan.registerComponent("size", sz2); 2188 assertEquals(new Integer (2), yan.getInstance("size")); 2189 assertFalse(sz0.equals(sz1)); 2190 assertFalse(sz1.equals(sz0)); 2191 assertFalse(sz0.equals(sz2)); 2192 assertFalse(sz2.equals(sz0)); 2193 assertFalse(sz2.equals(sz1)); 2194 assertFalse(sz1.equals(sz2)); 2195 assertHash(sz0); 2196 assertHash(sz1); 2197 assertHash(sz2); 2198 } 2199 catch(YanException e){ 2200 e.printResolutionTrace(); 2201 throw e; 2202 } 2203 } 2204 2205 protected void assertError(Component c, Class etype, String msg) 2206 throws Throwable { 2207 Container yan = createYanContainer(); 2208 yan.registerComponent("target", c); 2209 try{ 2210 yan.getInstance("target"); 2211 } 2212 catch(Throwable e){ 2213 if(etype.isInstance(e)){ 2214 assertEquals(msg, e.getMessage()); 2215 return; 2216 } 2217 else{ 2218 if(e instanceof YanException){ 2219 final Throwable cause = ((YanException)e).getCause(); 2220 if(cause!=null){ 2221 if(etype.isInstance(cause)){ 2222 assertEquals(cause.getClass().getName()+": " + msg, e.getMessage()); 2223 return; 2224 } 2225 } 2226 } 2227 throw e; 2228 } 2229 } 2230 fail("should have failed with: "+msg); 2231 } 2232 protected void assertResult(Component c, Object val){ 2233 Container yan = createYanContainer(); 2234 yan.registerComponent("target", c); 2235 assertEquals(val, yan.getInstance("target")); 2236 } 2237 private void assertHash(Object obj){ 2238 final HashMap map = new HashMap (); 2239 assertEquals(obj, obj); 2240 map.put(obj, obj); 2241 assertEquals(obj, map.get(obj)); 2242 } 2243 private Container getGbeanContainer(Component gb){ 2244 Container yan = createYanContainer(); 2245 yan.registerValue("firstname", "Jack"); 2246 yan.registerValue("middle", new Character ('M')); 2248 yan.registerValue("age", new Integer (10)); 2249 2250 yan.registerComponent("target", gb); 2251 return yan; 2252 } 2253 private void runTestGiveBean(Component gb){ 2254 final Container yan = getGbeanContainer(gb); 2255 yan.verify(); 2256 final GiveBean target = (GiveBean)yan.getInstance("target"); 2257 assertEquals("Jack", target.getFname()); 2258 assertEquals("M", target.getMiddleInitial()); 2259 assertEquals(10, target.getAge()); 2260 assertTrue(target.isAwake()); 2261 } 2262 private void verifyGiveBean(Component gb){ 2263 getGbeanContainer(gb).verify(); 2264 } 2265 private ComponentBinder gbean(final String [] givers){ 2266 return new ComponentBinder(){ 2267 public Creator bind(Object v){ 2268 return giveProperties(v, givers); 2269 } 2270 public Verifiable verify(Class t){ 2271 return verifyProperties(t, givers); 2272 } 2273 public Class bindType(Class t){ 2274 return void.class; 2275 } 2276 }; 2277 } 2278 private Creator giveProperties(Object v, String [] givers){ 2279 Component r = Components.value(v); 2280 for(int i=0; i<givers.length; i++){ 2281 r = r.followedBy(Components.method(v, givers[i])); 2282 } 2283 return r; 2284 } 2285 private Verifiable verifyProperties(Class t, String [] givers){ 2286 Component r = Components.value(null).cast(t); 2287 for(int i=0; i<givers.length; i++){ 2288 r = r.followedBy( 2289 Components.method(t, null, givers[i]) 2290 ); 2291 } 2292 return r; 2293 } 2294} 2295 | Popular Tags |