Clean up LoadTest testcase and include in standard test suite.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@179566 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -29,6 +29,15 @@ public class LoadTest extends TestCase{
|
|||||||
static private String LOG_PCKG[] = {"org.apache.commons.logging",
|
static private String LOG_PCKG[] = {"org.apache.commons.logging",
|
||||||
"org.apache.commons.logging.impl"};
|
"org.apache.commons.logging.impl"};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A custom classloader which "duplicates" logging classes available
|
||||||
|
* in the parent classloader into itself.
|
||||||
|
* <p>
|
||||||
|
* When asked to load a class that is in one of the LOG_PCKG packages,
|
||||||
|
* it loads the class itself (child-first). This class doesn't need
|
||||||
|
* to be set up with a classpath, as it simply uses the same classpath
|
||||||
|
* as the classloader that loaded it.
|
||||||
|
*/
|
||||||
static class AppClassLoader extends ClassLoader{
|
static class AppClassLoader extends ClassLoader{
|
||||||
|
|
||||||
java.util.Map classes = new java.util.HashMap();
|
java.util.Map classes = new java.util.HashMap();
|
||||||
@@ -46,8 +55,9 @@ public class LoadTest extends TestCase{
|
|||||||
|
|
||||||
try{
|
try{
|
||||||
|
|
||||||
java.io.InputStream is = this.getClass().getClassLoader().
|
ClassLoader cl = this.getClass().getClassLoader();
|
||||||
getResourceAsStream( name.replace('.','\\') + ".class" );
|
String classFileName = name.replace('.','/') + ".class";
|
||||||
|
java.io.InputStream is = cl.getResourceAsStream(classFileName);
|
||||||
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
|
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
|
||||||
|
|
||||||
while(is.available() > 0){
|
while(is.available() > 0){
|
||||||
@@ -89,6 +99,13 @@ public class LoadTest extends TestCase{
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test what happens when we play various classloader tricks like those
|
||||||
|
* that happen in web and j2ee containers.
|
||||||
|
* <p>
|
||||||
|
* Note that this test assumes that commons-logging.jar and log4j.jar
|
||||||
|
* are available via the system classpath.
|
||||||
|
*/
|
||||||
public void testInContainer()throws Exception{
|
public void testInContainer()throws Exception{
|
||||||
|
|
||||||
//problem can be in this step (broken app container or missconfiguration)
|
//problem can be in this step (broken app container or missconfiguration)
|
||||||
@@ -98,44 +115,48 @@ public class LoadTest extends TestCase{
|
|||||||
// 1. Thread.currentThread().setContextClassLoader(appLoader);
|
// 1. Thread.currentThread().setContextClassLoader(appLoader);
|
||||||
// 2. Thread.currentThread().setContextClassLoader(null);
|
// 2. Thread.currentThread().setContextClassLoader(null);
|
||||||
|
|
||||||
|
// Context classloader is same as class calling into log
|
||||||
Class cls = reload();
|
Class cls = reload();
|
||||||
Thread.currentThread().setContextClassLoader(cls.getClassLoader());
|
Thread.currentThread().setContextClassLoader(cls.getClassLoader());
|
||||||
execute(cls);
|
execute(cls);
|
||||||
|
|
||||||
|
// Context classloader is the "bootclassloader"
|
||||||
cls = reload();
|
cls = reload();
|
||||||
Thread.currentThread().setContextClassLoader(null);
|
Thread.currentThread().setContextClassLoader(null);
|
||||||
execute(cls);
|
execute(cls);
|
||||||
|
|
||||||
|
// Context classloader is the system classloader.
|
||||||
|
//
|
||||||
|
// This is expected to cause problems, as LogFactoryImpl will attempt
|
||||||
|
// to use the system classloader to load the Log4JLogger class, which
|
||||||
|
// will then be unable to cast that object to the Log interface loaded
|
||||||
|
// via the child classloader. JCL 1.0.4 and earlier will fail with
|
||||||
|
// this setup. Later versions of JCL should fail to load Log4J, but
|
||||||
|
// then fall back to jdk14 logging.
|
||||||
cls = reload();
|
cls = reload();
|
||||||
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
|
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
|
||||||
try{
|
execute(cls);
|
||||||
execute(cls);
|
|
||||||
fail("SystemClassLoader");
|
|
||||||
}catch( LogConfigurationException ok ){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
// Context classloader is the classloader of this class, ie the
|
||||||
|
// parent classloader of the UserClass that will make the logging call.
|
||||||
|
//
|
||||||
|
// Actually, as the system classloader is expected to load this class,
|
||||||
|
// this test is identical to the preceding one.
|
||||||
cls = reload();
|
cls = reload();
|
||||||
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
|
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
|
||||||
try{
|
execute(cls);
|
||||||
execute(cls);
|
|
||||||
fail("ContainerClassLoader");
|
|
||||||
}catch( LogConfigurationException ok ){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load class UserClass via a temporary classloader which is a child of
|
||||||
|
* the classloader used to load this test class.
|
||||||
|
*/
|
||||||
private Class reload()throws Exception{
|
private Class reload()throws Exception{
|
||||||
|
|
||||||
Class testObjCls = null;
|
Class testObjCls = null;
|
||||||
|
|
||||||
AppClassLoader appLoader = new AppClassLoader( this.getClass().
|
AppClassLoader appLoader = new AppClassLoader(
|
||||||
getClassLoader()
|
this.getClass().getClassLoader());
|
||||||
|
|
||||||
);
|
|
||||||
try{
|
try{
|
||||||
|
|
||||||
testObjCls = appLoader.loadClass(UserClass.class.getName());
|
testObjCls = appLoader.loadClass(UserClass.class.getName());
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ public class TestAll extends TestCase {
|
|||||||
suite.addTest(NoOpLogTest.suite());
|
suite.addTest(NoOpLogTest.suite());
|
||||||
suite.addTest(LogTest.suite());
|
suite.addTest(LogTest.suite());
|
||||||
suite.addTest(NullClassLoaderTest.suite());
|
suite.addTest(NullClassLoaderTest.suite());
|
||||||
|
suite.addTest(LoadTest.suite());
|
||||||
|
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user