1 10 11 package org.nanocontainer.reflection; 12 13 import junit.framework.TestCase; 14 import org.nanocontainer.DefaultNanoContainer; 15 import org.nanocontainer.NanoContainer; 16 import org.nanocontainer.testmodel.ThingThatTakesParamsInConstructor; 17 import org.nanocontainer.testmodel.WebServerImpl; 18 import org.picocontainer.*; 19 import org.picocontainer.alternatives.AbstractDelegatingMutablePicoContainer; 20 21 import java.io.File ; 22 import java.net.MalformedURLException ; 23 import java.util.Vector ; 24 import java.util.HashMap ; 25 import java.util.ArrayList ; 26 27 public class DefaultNanoContainerTestCase extends TestCase { 28 29 public void testBasic() throws PicoRegistrationException, PicoInitializationException, ClassNotFoundException { 30 NanoContainer nanoContainer = new DefaultNanoContainer(); 31 nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.DefaultWebServerConfig"); 32 nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.WebServer", "org.nanocontainer.testmodel.WebServerImpl"); 33 } 34 35 public void testProvision() throws PicoException, PicoInitializationException, ClassNotFoundException { 36 NanoContainer nanoContainer = new DefaultNanoContainer(); 37 nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.DefaultWebServerConfig"); 38 nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.WebServerImpl"); 39 40 assertNotNull("WebServerImpl should exist", nanoContainer.getPico().getComponentInstance(WebServerImpl.class)); 41 assertTrue("WebServerImpl should exist", nanoContainer.getPico().getComponentInstance(WebServerImpl.class) instanceof WebServerImpl); 42 } 43 44 public void testNoGenerationRegistration() throws PicoRegistrationException, PicoIntrospectionException { 45 NanoContainer nanoContainer = new DefaultNanoContainer(); 46 try { 47 nanoContainer.registerComponentImplementation("Ping"); 48 fail("should have failed"); 49 } catch (ClassNotFoundException e) { 50 } 52 } 53 54 public void testParametersCanBePassedInStringForm() throws ClassNotFoundException , PicoException, PicoInitializationException { 55 NanoContainer nanoContainer = new DefaultNanoContainer(); 56 String className = ThingThatTakesParamsInConstructor.class.getName(); 57 58 nanoContainer.registerComponentImplementation("thing", 59 className, 60 new String []{ 61 "java.lang.String", 62 "java.lang.Integer" 63 }, 64 new String []{ 65 "hello", 66 "22" 67 }); 68 69 ThingThatTakesParamsInConstructor thing = 70 (ThingThatTakesParamsInConstructor) nanoContainer.getPico().getComponentInstance("thing"); 71 assertNotNull("component not present", thing); 72 assertEquals("hello22", thing.getValue()); 73 } 74 75 public void testThatTestCompIsNotNaturallyInTheClassPathForTesting() { 76 77 80 try { 81 DefaultNanoContainer dfca = new DefaultNanoContainer(); 82 dfca.registerComponentImplementation("foo", "TestComp"); 83 Object o = dfca.getPico().getComponentInstance("foo"); 84 System.out.println(""); 85 fail("Should have failed. Class was loaded from " + o.getClass().getProtectionDomain().getCodeSource().getLocation()); 86 } catch (ClassNotFoundException expected) { 87 } 88 89 } 90 91 public void testChildContainerAdapterCanRelyOnParentContainerAdapter() throws MalformedURLException , ClassNotFoundException { 92 93 String testcompJarFileName = System.getProperty("testcomp.jar", "src/test-comp/TestComp.jar"); 94 File testCompJar = new File (testcompJarFileName); 97 assertTrue("The testcomp.jar system property should point to java/nanocontainer/src/test-comp/TestComp.jar", testCompJar.isFile()); 98 99 NanoContainer parentContainer = new DefaultNanoContainer(); 101 parentContainer.addClassLoaderURL(testCompJar.toURL()); 102 parentContainer.registerComponentImplementation("parentTestComp", "TestComp"); 103 parentContainer.registerComponentImplementation("java.lang.StringBuffer"); 104 105 PicoContainer parentContainerAdapterPico = parentContainer.getPico(); 106 Object parentTestComp = parentContainerAdapterPico.getComponentInstance("parentTestComp"); 107 assertEquals("TestComp", parentTestComp.getClass().getName()); 108 109 NanoContainer childContainer = new DefaultNanoContainer(parentContainer); 111 File testCompJar2 = new File (testCompJar.getParentFile(), "TestComp2.jar"); 112 childContainer.addClassLoaderURL(testCompJar2.toURL()); 113 childContainer.registerComponentImplementation("childTestComp", "TestComp2"); 114 115 PicoContainer childContainerAdapterPico = childContainer.getPico(); 116 Object childTestComp = childContainerAdapterPico.getComponentInstance("childTestComp"); 117 118 assertEquals("TestComp2", childTestComp.getClass().getName()); 119 120 assertNotSame(parentTestComp, childTestComp); 121 122 final ClassLoader parentCompClassLoader = parentTestComp.getClass().getClassLoader(); 123 final ClassLoader childCompClassLoader = childTestComp.getClass().getClassLoader(); 124 if(parentCompClassLoader != childCompClassLoader.getParent()) { 125 printClassLoader(parentCompClassLoader); 126 printClassLoader(childCompClassLoader); 127 fail("parentTestComp classloader should be parent of childTestComp classloader"); 128 } 129 assertNotSame(parentContainerAdapterPico, childContainerAdapterPico.getParent()); 131 } 132 133 private void printClassLoader(ClassLoader classLoader) { 134 while(classLoader != null) { 135 System.out.println(classLoader); 136 classLoader = classLoader.getParent(); 137 } 138 System.out.println("--"); 139 } 140 141 public static class AnotherFooComp { 142 143 } 144 145 public void testClassLoaderJugglingIsPossible() throws MalformedURLException , ClassNotFoundException { 146 NanoContainer parentContainer = new DefaultNanoContainer(); 147 148 String testcompJarFileName = System.getProperty("testcomp.jar", "src/test-comp/TestComp.jar"); 149 File testCompJar = new File (testcompJarFileName); 152 assertTrue("The testcomp.jar system property should point to java/nanocontainer/src/test-comp/TestComp.jar", testCompJar.isFile()); 153 154 parentContainer.registerComponentImplementation("foo", "org.nanocontainer.testmodel.DefaultWebServerConfig"); 155 156 Object fooWebServerConfig = parentContainer.getPico().getComponentInstance("foo"); 157 assertEquals("org.nanocontainer.testmodel.DefaultWebServerConfig", fooWebServerConfig.getClass().getName()); 158 159 NanoContainer childContainer = new DefaultNanoContainer(parentContainer); 160 childContainer.addClassLoaderURL(testCompJar.toURL()); 161 childContainer.registerComponentImplementation("bar", "TestComp"); 162 163 Object barTestComp = childContainer.getPico().getComponentInstance("bar"); 164 assertEquals("TestComp", barTestComp.getClass().getName()); 165 166 assertNotSame(fooWebServerConfig.getClass().getClassLoader(), barTestComp.getClass().getClassLoader()); 167 168 ClassLoader fooCL = fooWebServerConfig.getClass().getClassLoader(); 171 ClassLoader barCL1 = barTestComp.getClass().getClassLoader().getParent(); 172 ClassLoader barCL2, barCL3; 173 if (barCL1 != null && barCL1 != fooCL) { 174 barCL2 = barCL1.getParent(); 175 if (barCL2 != null && barCL2 != fooCL) { 176 barCL3 = barCL2.getParent(); 177 if (barCL3 != null && barCL3 != fooCL) { 178 fail("One of the parent classloaders of TestComp, should be that of DefaultWebServerConfig"); 179 } 180 } 181 } 182 } 183 184 public void TODO_testSecurityManagerCanPreventOperations() throws MalformedURLException , ClassNotFoundException { 185 NanoContainer parentContainer = new DefaultNanoContainer(); 186 187 String testcompJarFileName = System.getProperty("testcomp.jar"); 188 assertNotNull("The testcomp.jar system property should point to nano/reflection/src/test-comp/TestComp.jar", testcompJarFileName); 191 File testCompJar = new File (testcompJarFileName); 192 assertTrue(testCompJar.isFile()); 193 194 parentContainer.registerComponentImplementation("foo", "org.nanocontainer.testmodel.DefaultWebServerConfig"); 195 196 Object fooWebServerConfig = parentContainer.getPico().getComponentInstance("foo"); 197 assertEquals("org.nanocontainer.testmodel.DefaultWebServerConfig", fooWebServerConfig.getClass().getName()); 198 199 NanoContainer childContainer = new DefaultNanoContainer(parentContainer); 200 childContainer.addClassLoaderURL(testCompJar.toURL()); 201 childContainer.registerComponentImplementation("bar", "org.nanocontainer.testmodel.FileSystemUsing"); 205 206 try { 207 parentContainer.getPico().getComponentInstance("bar"); 208 fail("Should have barfed"); 209 } catch (java.security.AccessControlException e) { 210 } 212 } 213 214 215 public void testChainOfDecoratingPicoContainersCanDoInterceptionOfMutablePicoContainerMethods() throws ClassNotFoundException { 216 NanoContainer nanoContainer = new DefaultNanoContainer(); 217 MutablePicoContainer decorating = nanoContainer.addDecoratingPicoContainer(FooDecoratingPicoContainer.class); 218 assertTrue(decorating instanceof FooDecoratingPicoContainer); 219 MutablePicoContainer decorating2 = nanoContainer.addDecoratingPicoContainer(BarDecoratingPicoContainer.class); 220 assertTrue(decorating2 instanceof BarDecoratingPicoContainer); 221 nanoContainer.registerComponentImplementation("java.util.Vector"); 222 assertNotNull(nanoContainer.getComponentInstanceOfType("java.util.ArrayList")); 224 assertNull(nanoContainer.getComponentInstanceOfType("java.util.Vector")); 225 assertNotNull(nanoContainer.getPico().getComponentInstanceOfType(ArrayList .class)); 226 assertNull(nanoContainer.getPico().getComponentInstanceOfType(Vector .class)); 227 } 228 229 public static class FooDecoratingPicoContainer extends AbstractDelegatingMutablePicoContainer { 230 public FooDecoratingPicoContainer(MutablePicoContainer delegate) { 231 super(delegate); 232 } 233 public MutablePicoContainer makeChildContainer() { 234 return null; 235 } 236 237 public ComponentAdapter registerComponentImplementation(Class compImpl) throws PicoRegistrationException { 238 assertEquals(HashMap .class, compImpl); 239 return super.registerComponentImplementation(ArrayList .class); 240 } 241 } 242 243 public static class BarDecoratingPicoContainer extends AbstractDelegatingMutablePicoContainer { 244 public BarDecoratingPicoContainer(MutablePicoContainer delegate) { 245 super(delegate); 246 } 247 public MutablePicoContainer makeChildContainer() { 248 return null; 249 } 250 public ComponentAdapter registerComponentImplementation(Class compImpl) throws PicoRegistrationException { 251 assertEquals(Vector .class, compImpl); 252 return super.registerComponentImplementation(HashMap .class); 253 } 254 } 255 256 257 } 258 | Popular Tags |