1 6 7 package org.jfox.ioc; 8 9 import junit.framework.TestCase; 10 import org.jfox.ioc.component.TestComponent; 11 import org.jfox.ioc.component.TestComponentA; 12 import org.jfox.ioc.component.TestComponentB; 13 import org.jfox.ioc.component.TestComponentC; 14 import org.jfox.ioc.component.TestComponentD; 15 import org.jfox.ioc.depend.ComponentRefDependency; 16 import org.jfox.ioc.depend.Dependency; 17 import org.jfox.ioc.depend.DependencyPack; 18 import org.jfox.ioc.depend.ObjectDependency; 19 20 23 24 public class RegistryTest extends TestCase { 25 Registry registry; 26 27 public void setUp() throws Exception { 28 super.setUp(); 29 registry = Registry.getInstance(); 30 } 31 32 public void tearDown() throws Exception { 33 super.tearDown(); 34 } 36 37 public void testRegisterComponent() throws Exception { 38 registry.registerComponent(TestComponentA.class); 40 TestComponentA ta = (TestComponentA)registry.getComponentInstance(TestComponentA.class); 41 assertEquals(ta.getName(),""); 42 } 43 44 public void testRegisterComponentByComponentName() throws Exception { 45 ComponentName name = ComponentName.newInstance(TestComponentA.class,"test"); 46 registry.registerComponent(name,name.getType()); 47 TestComponentA ta = (TestComponentA) registry.getComponentInstance(name); 48 assertEquals(ta.getName(), ""); 49 ta.setName("Hello,World!"); 50 TestComponentA taa = (TestComponentA) registry.getComponentInstance(name); 51 assertEquals(taa.getName(), "Hello,World!"); 52 } 53 54 public void testRegisterComponentNoneParameterConstructor() throws Exception { 55 ComponentMeta meta = registry.registerComponent(TestComponentB.class, 56 Dependency.EMPTY_PARAMETERS); 57 System.out.println("ComponentMeta: " + meta); 58 System.out.println("ComponentName: " + meta.getComponentName()); 59 60 TestComponent test = (TestComponent) registry.getComponentInstance(meta.getComponentName()); 61 62 System.out.println("ComponentMeta: " + meta); 63 assertEquals(test.getName(), "Hello,World!"); 64 } 65 66 public void testRegisterComponentWithParameterConstructor() throws Exception { 67 ComponentName name = ComponentName.newInstance(TestComponentB.class,"parameter"); 68 ComponentMeta meta = registry.registerComponent(name, 69 name.getType(), 70 new Dependency[]{ 71 new ObjectDependency("Hello,World!") 72 }); 73 System.out.println("ComponentMeta: " + meta); 74 System.out.println("ComponentName: " + meta.getComponentName()); 75 76 TestComponent test = (TestComponent) registry.getComponentInstance(meta.getComponentName()); 77 78 System.out.println("ComponentMeta: " + meta); 79 assertEquals(test.getName(), "Hello,World!"); 80 } 81 82 public void testRegisterComponentWithSetter() throws Exception { 83 ComponentMeta meta = registry.registerComponent(TestComponentC.class, 84 Dependency.EMPTY_PARAMETERS, 85 new DependencyPack[]{ 86 new DependencyPack("name", new ObjectDependency("Hello,Everybody!")) 87 }, 88 true, 89 false); 90 System.out.println("ComponentMeta: " + meta); 91 System.out.println("ComponentName: " + meta.getComponentName()); 92 93 TestComponentC test = (TestComponentC) registry.getComponentInstance(meta.getComponentName()); 94 95 System.out.println("ComponentMeta: " + meta); 96 assertEquals(test.getName(), "Hello,Everybody!"); 97 } 98 99 100 public void testRegistryMulti() throws Exception { 101 ComponentName name1 = ComponentName.newInstance(TestComponentA.class, "test1"); 102 registry.registerComponent(name1,name1.getType()); 103 ComponentName name2 = ComponentName.newInstance(TestComponentA.class, "test2"); 104 registry.registerComponent(name2,name1.getType()); 105 106 TestComponentA ta1 = (TestComponentA)registry.getComponentInstance(name1); 107 ta1.setName("Hello,World!"); 108 TestComponentA ta2 = (TestComponentA) registry.getComponentInstance(name2); 109 ta2.setName("Hello,Everybody!"); 110 111 assertTrue(registry.countComponents()>=2); 112 assertTrue(registry.countComponentTypes() >= 1); 113 114 TestComponentA ta3 = (TestComponentA) registry.getComponentInstance(name1); 115 assertEquals(ta3.getName(), "Hello,World!"); 116 117 TestComponentA ta4 = (TestComponentA) registry.getComponentInstance(name2); 118 assertEquals(ta4.getName(), "Hello,Everybody!"); 119 120 } 121 122 123 public void testConstructorComponentInjection() throws Exception { 124 registry.registerComponent(TestComponentD.class,new Dependency[]{ 125 new ComponentRefDependency(ComponentName.newInstance(TestComponentC.class)) 126 }); 127 128 TestComponentD td = (TestComponentD)registry.getComponentInstance(TestComponentD.class); 129 130 TestComponentC tc = td.getTestComponent(); 131 assertEquals(tc.getName(), "Hello,Everybody!"); 132 133 } 134 135 public void testRegisterByInterface() throws Exception { 136 registry.registerComponent(ComponentName.newInstance(Component.class,"TestA"), 137 TestComponentA.class, 138 new Dependency[]{}, 139 new DependencyPack[]{ 140 new DependencyPack("name",new ObjectDependency("Hello!")) 141 }, 142 true, 143 false); 144 145 TestComponentA ta = (TestComponentA)registry.getComponentInstance(ComponentName.parseString(Component.class.getName() + "@TestA")); 146 assertEquals(ta.getName(),"Hello!"); 147 } 148 149 } | Popular Tags |