1
0

Merge in the allow-flawed branch, as there were no objections.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@190565 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Kitching
2005-06-14 10:03:48 +00:00
parent ca0188c41e
commit a3f8e5302a
4 changed files with 475 additions and 197 deletions

View File

@@ -19,6 +19,8 @@ import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.impl.LogFactoryImpl;
/**
* testcase to emulate container and application isolated from container
* @author baliuka
@@ -97,7 +99,17 @@ public class LoadTest extends TestCase{
}
/**
* Call the static setAllowFlawedContext method on the specified class
* (expected to be a UserClass loaded via a custom classloader), passing
* it the specified state parameter.
*/
private void setAllowFlawedContext(Class c, String state) throws Exception {
Class[] params = {String.class};
java.lang.reflect.Method m = c.getDeclaredMethod("setAllowFlawedContext", params);
m.invoke(null, new Object[] {state});
}
/**
* Test what happens when we play various classloader tricks like those
@@ -120,31 +132,50 @@ public class LoadTest extends TestCase{
Thread.currentThread().setContextClassLoader(cls.getClassLoader());
execute(cls);
// Context classloader is the "bootclassloader"
// Context classloader is the "bootclassloader". This is technically
// bad, but LogFactoryImpl.ALLOW_FLAWED_CONTEXT defaults to true so
// this test should pass.
cls = reload();
Thread.currentThread().setContextClassLoader(null);
execute(cls);
// Context classloader is the "bootclassloader". This is same as above
// except that ALLOW_FLAWED_CONTEXT is set to false; an error should
// now be reported.
cls = reload();
Thread.currentThread().setContextClassLoader(null);
try {
setAllowFlawedContext(cls, "false");
execute(cls);
fail("Logging config succeeded when context classloader was null!");
} catch(LogConfigurationException ex) {
// expected; the boot classloader doesn't *have* JCL available
}
// 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.
// via the child classloader. However as ALLOW_FLAWED_CONTEXT defaults
// to true this test should pass.
cls = reload();
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
execute(cls);
// 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.
// Context classloader is the system classloader. This is the same
// as above except that ALLOW_FLAWED_CONTEXT is set to false; an error
// should now be reported.
cls = reload();
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
execute(cls);
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
try {
setAllowFlawedContext(cls, "false");
execute(cls);
fail("Error: somehow downcast a Logger loaded via system classloader"
+ " to the Log interface loaded via a custom classloader");
} catch(LogConfigurationException ex) {
// expected
}
}
/**
@@ -206,4 +237,15 @@ public class LoadTest extends TestCase{
return suite;
}
public void setUp() {
// save state before test starts so we can restore it when test ends
origContextClassLoader = Thread.currentThread().getContextClassLoader();
}
public void tearDown() {
// restore original state so a test can't stuff up later tests.
Thread.currentThread().setContextClassLoader(origContextClassLoader);
}
private ClassLoader origContextClassLoader;
}

View File

@@ -60,22 +60,8 @@ public class NullClassLoaderTest extends TestCase{
* log object when called multiple times with the same name.
*/
public void testSameLogObject() throws Exception {
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
// emulate an app (not a webapp) running code loaded via the
// "null" classloader (bootclassloader for JDK1.2+, or
// systemclassloader for jdk1.1).
Thread.currentThread().setContextClassLoader(null);
Log log1 = LogFactory.getLog("foo");
Log log2 = LogFactory.getLog("foo");
assertSame(
"Calling getLog twice with the same category " +
"resulted in different objects!",
log1, log2);
} finally {
Thread.currentThread().setContextClassLoader(oldContextClassLoader);
}
// unfortunately, there just isn't any way to emulate JCL being
// accessable via the null classloader in "standard" systems, so
// we can't include this test in our standard unit tests.
}
}

View File

@@ -16,10 +16,25 @@
package org.apache.commons.logging;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.LogFactoryImpl;
public class UserClass {
/**
* Set the ALLOW_FLAWED_CONTEXT feature on the LogFactoryImpl object
* associated with this class' classloader.
* <p>
* Don't forget to set the context classloader to whatever it will be
* when an instance of this class is actually created <i>before</i> calling
* this method!
*/
public static void setAllowFlawedContext(String state) {
LogFactory f = LogFactory.getFactory();
f.setAttribute(LogFactoryImpl.ALLOW_FLAWED_CONTEXT_PROPERTY, state);
}
public UserClass() {
Log log = LogFactory.getLog(LoadTest.class);
}