@SuppressWarnings("unchecked") @Test public void testOverrideLoad() throws ClassNotFoundException, SecurityException, InstantiationException, IllegalAccessException { pluginLoader.setOverrideLoad(true); String className = "org.pentaho.test.platform.plugin.pluginmgr.ClassToOverride"; Class clazz = Class.forName(className, true, getClass().getClassLoader()); Object o = clazz.newInstance(); assertEquals("I am the original class from the parent class loader", o.toString()); Class overridenClazz = Class.forName(className, true, pluginLoader); Object o2 = overridenClazz.newInstance(); assertEquals("I am the overridden class from the plugin class loader", o2.toString()); }
@Test public void testImplicitLoad() throws ClassNotFoundException, InstantiationException, IllegalAccessException { pluginLoader.setOverrideLoad(true); // // 1. Implicitly create an instance of ClassToOverride and check that it was loaded by the // default loader. // ClassToOverride cto = new ClassToOverride(); assertNotSame( PluginClassLoader.class.getName(), cto.getClass().getClassLoader().getClass().getName()); assertEquals( "failed to load original class", "I am the original class from the parent class loader", cto.toString()); // NOTE: implicit loading of a class (i.e. "new" or static method access) is always performed by // the // classloader of the enclosing class (the "current" classloader), so if we set // Thread.currentThread().setContextClassLoader(pluginLoader) and then did a new // ClassToOverride(), // the pluginLoader would *not* be asked to load ClassToOverride, rather the current classloader // will be asked to load it. // // 2. Ask a class that we are certain was loaded by the private loader to implicitly create an // instance // of ClassToOverride and check that the class was loaded by the private loader // Object explicitlyLoadedContainer = getContainerFromPrivateClassLoader(); assertEquals( PluginClassLoader.class.getName(), explicitlyLoadedContainer.getClass().getClassLoader().getClass().getName()); assertEquals( "failed to load override class", "I am the overridden class from the plugin class loader", explicitlyLoadedContainer.toString()); }