Sort members
This commit is contained in:
@@ -60,21 +60,6 @@ public class AltHashtableTestCase extends TestCase {
|
||||
AltHashtable.class.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that initialising the LogFactory class will cause it
|
||||
* to instantiate an object of type specified in system property
|
||||
* "org.apache.commons.logging.LogFactory.HashtableImpl".
|
||||
*/
|
||||
public void testType() {
|
||||
// Here, the reference to the LogFactory class should cause the
|
||||
// class to be loaded and initialized. It will see the property
|
||||
// set and use the AltHashtable class. If other tests in this
|
||||
// class have already been run within the same classloader then
|
||||
// LogFactory will already have been initialized, but that
|
||||
// doesn't change the effectiveness of this test.
|
||||
assertTrue(LogFactory.factories instanceof AltHashtable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that when LogFactory sees a context-classloader for the
|
||||
* first time that it creates a new entry in the LogFactory.factories
|
||||
@@ -91,4 +76,19 @@ public class AltHashtableTestCase extends TestCase {
|
||||
assertEquals(contextLoader, AltHashtable.lastKey);
|
||||
assertNotNull(AltHashtable.lastValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that initialising the LogFactory class will cause it
|
||||
* to instantiate an object of type specified in system property
|
||||
* "org.apache.commons.logging.LogFactory.HashtableImpl".
|
||||
*/
|
||||
public void testType() {
|
||||
// Here, the reference to the LogFactory class should cause the
|
||||
// class to be loaded and initialized. It will see the property
|
||||
// set and use the AltHashtable class. If other tests in this
|
||||
// class have already been run within the same classloader then
|
||||
// LogFactory will already have been initialized, but that
|
||||
// doesn't change the effectiveness of this test.
|
||||
assertTrue(LogFactory.factories instanceof AltHashtable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,11 +25,11 @@ import java.util.Hashtable;
|
||||
*/
|
||||
public class BadHashtablePropertyTestCase extends TestCase {
|
||||
|
||||
public void testType() {
|
||||
assertTrue(LogFactory.factories instanceof Hashtable);
|
||||
}
|
||||
|
||||
public void testPutCalled() throws Exception {
|
||||
LogFactory.getLog(BadHashtablePropertyTestCase.class);
|
||||
}
|
||||
|
||||
public void testType() {
|
||||
assertTrue(LogFactory.factories instanceof Hashtable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,18 +27,6 @@ import junit.framework.TestCase;
|
||||
*/
|
||||
public class BasicOperationsTestCase extends TestCase
|
||||
{
|
||||
public void testIsEnabledClassLog()
|
||||
{
|
||||
final Log log = LogFactory.getLog(BasicOperationsTestCase.class);
|
||||
executeIsEnabledTest(log);
|
||||
}
|
||||
|
||||
public void testIsEnabledNamedLog()
|
||||
{
|
||||
final Log log = LogFactory.getLog(BasicOperationsTestCase.class.getName());
|
||||
executeIsEnabledTest(log);
|
||||
}
|
||||
|
||||
public void executeIsEnabledTest(final Log log)
|
||||
{
|
||||
try
|
||||
@@ -57,16 +45,22 @@ public class BasicOperationsTestCase extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public void testMessageWithoutExceptionClassLog()
|
||||
public void executeMessageWithExceptionTest(final Log log)
|
||||
{
|
||||
final Log log = LogFactory.getLog(BasicOperationsTestCase.class);
|
||||
executeMessageWithoutExceptionTest(log);
|
||||
}
|
||||
|
||||
public void testMessageWithoutExceptionNamedLog()
|
||||
{
|
||||
final Log log = LogFactory.getLog(BasicOperationsTestCase.class.getName());
|
||||
executeMessageWithoutExceptionTest(log);
|
||||
try
|
||||
{
|
||||
log.trace("Hello, Mum", new ArithmeticException());
|
||||
log.debug("Hello, Mum", new ArithmeticException());
|
||||
log.info("Hello, Mum", new ArithmeticException());
|
||||
log.warn("Hello, Mum", new ArithmeticException());
|
||||
log.error("Hello, Mum", new ArithmeticException());
|
||||
log.fatal("Hello, Mum", new ArithmeticException());
|
||||
}
|
||||
catch (final Throwable t)
|
||||
{
|
||||
t.printStackTrace();
|
||||
fail("Exception thrown: " + t);
|
||||
}
|
||||
}
|
||||
|
||||
public void executeMessageWithoutExceptionTest(final Log log)
|
||||
@@ -87,6 +81,18 @@ public class BasicOperationsTestCase extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public void testIsEnabledClassLog()
|
||||
{
|
||||
final Log log = LogFactory.getLog(BasicOperationsTestCase.class);
|
||||
executeIsEnabledTest(log);
|
||||
}
|
||||
|
||||
public void testIsEnabledNamedLog()
|
||||
{
|
||||
final Log log = LogFactory.getLog(BasicOperationsTestCase.class.getName());
|
||||
executeIsEnabledTest(log);
|
||||
}
|
||||
|
||||
public void testMessageWithExceptionClassLog()
|
||||
{
|
||||
final Log log = LogFactory.getLog(BasicOperationsTestCase.class);
|
||||
@@ -99,21 +105,15 @@ public class BasicOperationsTestCase extends TestCase
|
||||
executeMessageWithExceptionTest(log);
|
||||
}
|
||||
|
||||
public void executeMessageWithExceptionTest(final Log log)
|
||||
public void testMessageWithoutExceptionClassLog()
|
||||
{
|
||||
try
|
||||
{
|
||||
log.trace("Hello, Mum", new ArithmeticException());
|
||||
log.debug("Hello, Mum", new ArithmeticException());
|
||||
log.info("Hello, Mum", new ArithmeticException());
|
||||
log.warn("Hello, Mum", new ArithmeticException());
|
||||
log.error("Hello, Mum", new ArithmeticException());
|
||||
log.fatal("Hello, Mum", new ArithmeticException());
|
||||
}
|
||||
catch (final Throwable t)
|
||||
{
|
||||
t.printStackTrace();
|
||||
fail("Exception thrown: " + t);
|
||||
}
|
||||
final Log log = LogFactory.getLog(BasicOperationsTestCase.class);
|
||||
executeMessageWithoutExceptionTest(log);
|
||||
}
|
||||
|
||||
public void testMessageWithoutExceptionNamedLog()
|
||||
{
|
||||
final Log log = LogFactory.getLog(BasicOperationsTestCase.class.getName());
|
||||
executeMessageWithoutExceptionTest(log);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,23 +27,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
// after: https://github.com/apache/logging-log4j2/blob/c47e98423b461731f7791fcb9ea1079cd451f365/log4j-core/src/test/java/org/apache/logging/log4j/core/GarbageCollectionHelper.java
|
||||
public final class GarbageCollectionHelper implements Closeable, Runnable {
|
||||
private static final OutputStream SINK = new OutputStream() {
|
||||
@Override
|
||||
public void write(int b) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) {
|
||||
}
|
||||
};
|
||||
private final AtomicBoolean running = new AtomicBoolean();
|
||||
private final CountDownLatch latch = new CountDownLatch(1);
|
||||
private final Thread gcThread = new Thread(new GcTask());
|
||||
|
||||
class GcTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -64,13 +47,23 @@ public final class GarbageCollectionHelper implements Closeable, Runnable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (running.compareAndSet(false, true)) {
|
||||
gcThread.start();
|
||||
private static final OutputStream SINK = new OutputStream() {
|
||||
@Override
|
||||
public void write(byte[] b) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
}
|
||||
};
|
||||
private final AtomicBoolean running = new AtomicBoolean();
|
||||
private final CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
private final Thread gcThread = new Thread(new GcTask());
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
@@ -82,5 +75,12 @@ public final class GarbageCollectionHelper implements Closeable, Runnable {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (running.compareAndSet(false, true)) {
|
||||
gcThread.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,10 +22,6 @@ import junit.framework.TestCase;
|
||||
* test to emulate container and application isolated from container
|
||||
*/
|
||||
public class LoadTestCase extends TestCase{
|
||||
//TODO: need some way to add service provider packages
|
||||
static private String[] LOG_PCKG = {"org.apache.commons.logging",
|
||||
"org.apache.commons.logging.impl"};
|
||||
|
||||
/**
|
||||
* A custom classloader which "duplicates" logging classes available
|
||||
* in the parent classloader into itself.
|
||||
@@ -92,6 +88,41 @@ public class LoadTestCase extends TestCase{
|
||||
|
||||
}
|
||||
|
||||
//TODO: need some way to add service provider packages
|
||||
static private String[] LOG_PCKG = {"org.apache.commons.logging",
|
||||
"org.apache.commons.logging.impl"};
|
||||
|
||||
|
||||
private ClassLoader origContextClassLoader;
|
||||
|
||||
private void execute(final Class cls) throws Exception {
|
||||
cls.getConstructor().newInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
Class testObjCls = null;
|
||||
final AppClassLoader appLoader = new AppClassLoader(this.getClass().getClassLoader());
|
||||
try {
|
||||
|
||||
testObjCls = appLoader.loadClass(UserClass.class.getName());
|
||||
|
||||
} catch (final ClassNotFoundException cnfe) {
|
||||
throw cnfe;
|
||||
} catch (final Throwable t) {
|
||||
t.printStackTrace();
|
||||
fail("AppClassLoader failed ");
|
||||
}
|
||||
|
||||
assertSame("app isolated", testObjCls.getClassLoader(), appLoader);
|
||||
|
||||
return testObjCls;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Call the static setAllowFlawedContext method on the specified class
|
||||
@@ -104,6 +135,18 @@ public class LoadTestCase extends TestCase{
|
||||
m.invoke(null, state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
// save state before test starts so we can restore it when test ends
|
||||
origContextClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown() {
|
||||
// restore original state so a test can't stuff up later tests.
|
||||
Thread.currentThread().setContextClassLoader(origContextClassLoader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test what happens when we play various classloader tricks like those
|
||||
* that happen in web and j2ee containers.
|
||||
@@ -170,47 +213,4 @@ public class LoadTestCase extends TestCase{
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
Class testObjCls = null;
|
||||
final AppClassLoader appLoader = new AppClassLoader(this.getClass().getClassLoader());
|
||||
try {
|
||||
|
||||
testObjCls = appLoader.loadClass(UserClass.class.getName());
|
||||
|
||||
} catch (final ClassNotFoundException cnfe) {
|
||||
throw cnfe;
|
||||
} catch (final Throwable t) {
|
||||
t.printStackTrace();
|
||||
fail("AppClassLoader failed ");
|
||||
}
|
||||
|
||||
assertSame("app isolated", testObjCls.getClassLoader(), appLoader);
|
||||
|
||||
return testObjCls;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void execute(final Class cls) throws Exception {
|
||||
cls.getConstructor().newInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
// save state before test starts so we can restore it when test ends
|
||||
origContextClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown() {
|
||||
// restore original state so a test can't stuff up later tests.
|
||||
Thread.currentThread().setContextClassLoader(origContextClassLoader);
|
||||
}
|
||||
|
||||
private ClassLoader origContextClassLoader;
|
||||
}
|
||||
|
||||
@@ -96,94 +96,6 @@ public class PathableClassLoader extends URLClassLoader {
|
||||
super(NO_URLS, parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow caller to explicitly add paths. Generally this not a good idea;
|
||||
* use addLogicalLib instead, then define the location for that logical
|
||||
* library in the build.xml file.
|
||||
*/
|
||||
@Override
|
||||
public void addURL(final URL url) {
|
||||
super.addURL(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify whether this classloader should ask the parent classloader
|
||||
* to resolve a class first, before trying to resolve it via its own
|
||||
* classpath.
|
||||
* <p>
|
||||
* Checking with the parent first is the normal approach for java, but
|
||||
* components within containers such as servlet engines can use
|
||||
* child-first lookup instead, to allow the components to override libs
|
||||
* which are visible in shared classloaders provided by the container.
|
||||
* <p>
|
||||
* Note that the method getResources always behaves as if parentFirst=true,
|
||||
* because of limitations in java 1.4; see the javadoc for method
|
||||
* getResourcesInOrder for details.
|
||||
* <p>
|
||||
* This value defaults to true.
|
||||
*/
|
||||
public void setParentFirst(final boolean state) {
|
||||
parentFirst = state;
|
||||
}
|
||||
|
||||
/**
|
||||
* For classes with the specified prefix, get them from the system
|
||||
* classpath <i>which is active at the point this method is called</i>.
|
||||
* <p>
|
||||
* This method is just a shortcut for
|
||||
* <pre>
|
||||
* useExplicitLoader(prefix, ClassLoader.getSystemClassLoader());
|
||||
* </pre>
|
||||
* <p>
|
||||
* Of course, this assumes that the classes of interest are already
|
||||
* in the classpath of the system classloader.
|
||||
*/
|
||||
public void useSystemLoader(final String prefix) {
|
||||
useExplicitLoader(prefix, ClassLoader.getSystemClassLoader());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a classloader to use for specific java packages.
|
||||
* <p>
|
||||
* The specified classloader is normally a loader that is NOT
|
||||
* an ancestor of this classloader. In particular, this loader
|
||||
* may have the bootloader as its parent, but be configured to
|
||||
* see specific other classes (eg the junit library loaded
|
||||
* via the system classloader).
|
||||
* <p>
|
||||
* The differences between using this method, and using
|
||||
* addLogicalLib are:
|
||||
* <ul>
|
||||
* <li>If code calls getClassLoader on a class loaded via
|
||||
* "lookaside", then traces up its inheritance chain, it
|
||||
* will see the "real" classloaders. When the class is remapped
|
||||
* into this classloader via addLogicalLib, the classloader
|
||||
* chain seen is this object plus ancestors.
|
||||
* <li>If two different jars contain classes in the same
|
||||
* package, then it is not possible to load both jars into
|
||||
* the same "lookaside" classloader (eg the system classloader)
|
||||
* then map one of those subsets from here. Of course they could
|
||||
* be loaded into two different "lookaside" classloaders and
|
||||
* then a prefix used to map from here to one of those classloaders.
|
||||
* </ul>
|
||||
*/
|
||||
public void useExplicitLoader(final String prefix, final ClassLoader loader) {
|
||||
if (lookasides == null) {
|
||||
lookasides = new HashMap();
|
||||
}
|
||||
lookasides.put(prefix, loader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a collection of logical libraries. See addLogicalLib.
|
||||
*/
|
||||
public void addLogicalLib(final String[] logicalLibs) {
|
||||
for (final String logicalLib : logicalLibs) {
|
||||
addLogicalLib(logicalLib);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a logical library to be included in the classpath used to
|
||||
* locate classes.
|
||||
@@ -231,6 +143,101 @@ public class PathableClassLoader extends URLClassLoader {
|
||||
+ " as a System property.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a collection of logical libraries. See addLogicalLib.
|
||||
*/
|
||||
public void addLogicalLib(final String[] logicalLibs) {
|
||||
for (final String logicalLib : logicalLibs) {
|
||||
addLogicalLib(logicalLib);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow caller to explicitly add paths. Generally this not a good idea;
|
||||
* use addLogicalLib instead, then define the location for that logical
|
||||
* library in the build.xml file.
|
||||
*/
|
||||
@Override
|
||||
public void addURL(final URL url) {
|
||||
super.addURL(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as parent class method except that when parentFirst is false
|
||||
* the resource is looked for in the local classpath before the parent
|
||||
* loader is consulted.
|
||||
*/
|
||||
@Override
|
||||
public URL getResource(final String name) {
|
||||
if (parentFirst) {
|
||||
return super.getResource(name);
|
||||
}
|
||||
final URL local = super.findResource(name);
|
||||
if (local != null) {
|
||||
return local;
|
||||
}
|
||||
return super.getResource(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as parent class method except that when parentFirst is false
|
||||
* the resource is looked for in the local classpath before the parent
|
||||
* loader is consulted.
|
||||
*/
|
||||
@Override
|
||||
public InputStream getResourceAsStream(final String name) {
|
||||
if (parentFirst) {
|
||||
return super.getResourceAsStream(name);
|
||||
}
|
||||
final URL local = super.findResource(name);
|
||||
if (local != null) {
|
||||
try {
|
||||
return local.openStream();
|
||||
} catch (final IOException e) {
|
||||
// TODO: check if this is right or whether we should
|
||||
// fall back to trying parent. The javadoc doesn't say...
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return super.getResourceAsStream(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emulate a proper implementation of getResources which respects the
|
||||
* setting for parentFirst.
|
||||
* <p>
|
||||
* Note that it's not possible to override the inherited getResources, as
|
||||
* it's declared final in java1.4 (thought that's been removed for 1.5).
|
||||
* The inherited implementation always behaves as if parentFirst=true.
|
||||
*/
|
||||
public Enumeration getResourcesInOrder(final String name) throws IOException {
|
||||
if (parentFirst) {
|
||||
return super.getResources(name);
|
||||
}
|
||||
final Enumeration localUrls = super.findResources(name);
|
||||
|
||||
final ClassLoader parent = getParent();
|
||||
if (parent == null) {
|
||||
// Alas, there is no method to get matching resources
|
||||
// from a null (BOOT) parent classloader. Calling
|
||||
// ClassLoader.getSystemClassLoader isn't right. Maybe
|
||||
// calling Class.class.getResources(name) would do?
|
||||
//
|
||||
// However for the purposes of unit tests, we can
|
||||
// simply assume that no relevant resources are
|
||||
// loadable from the parent; unit tests will never be
|
||||
// putting any of their resources in a "boot" classloader
|
||||
// path!
|
||||
return localUrls;
|
||||
}
|
||||
final Enumeration parentUrls = parent.getResources(name);
|
||||
|
||||
final ArrayList localItems = toList(localUrls);
|
||||
final ArrayList parentItems = toList(parentUrls);
|
||||
localItems.addAll(parentItems);
|
||||
return Collections.enumeration(localItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the classloader that loaded this class has this logical lib in its
|
||||
* path, then return the matching URL otherwise return null.
|
||||
@@ -328,56 +335,23 @@ public class PathableClassLoader extends URLClassLoader {
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as parent class method except that when parentFirst is false
|
||||
* the resource is looked for in the local classpath before the parent
|
||||
* loader is consulted.
|
||||
*/
|
||||
@Override
|
||||
public URL getResource(final String name) {
|
||||
if (parentFirst) {
|
||||
return super.getResource(name);
|
||||
}
|
||||
final URL local = super.findResource(name);
|
||||
if (local != null) {
|
||||
return local;
|
||||
}
|
||||
return super.getResource(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emulate a proper implementation of getResources which respects the
|
||||
* setting for parentFirst.
|
||||
* Specify whether this classloader should ask the parent classloader
|
||||
* to resolve a class first, before trying to resolve it via its own
|
||||
* classpath.
|
||||
* <p>
|
||||
* Note that it's not possible to override the inherited getResources, as
|
||||
* it's declared final in java1.4 (thought that's been removed for 1.5).
|
||||
* The inherited implementation always behaves as if parentFirst=true.
|
||||
* Checking with the parent first is the normal approach for java, but
|
||||
* components within containers such as servlet engines can use
|
||||
* child-first lookup instead, to allow the components to override libs
|
||||
* which are visible in shared classloaders provided by the container.
|
||||
* <p>
|
||||
* Note that the method getResources always behaves as if parentFirst=true,
|
||||
* because of limitations in java 1.4; see the javadoc for method
|
||||
* getResourcesInOrder for details.
|
||||
* <p>
|
||||
* This value defaults to true.
|
||||
*/
|
||||
public Enumeration getResourcesInOrder(final String name) throws IOException {
|
||||
if (parentFirst) {
|
||||
return super.getResources(name);
|
||||
}
|
||||
final Enumeration localUrls = super.findResources(name);
|
||||
|
||||
final ClassLoader parent = getParent();
|
||||
if (parent == null) {
|
||||
// Alas, there is no method to get matching resources
|
||||
// from a null (BOOT) parent classloader. Calling
|
||||
// ClassLoader.getSystemClassLoader isn't right. Maybe
|
||||
// calling Class.class.getResources(name) would do?
|
||||
//
|
||||
// However for the purposes of unit tests, we can
|
||||
// simply assume that no relevant resources are
|
||||
// loadable from the parent; unit tests will never be
|
||||
// putting any of their resources in a "boot" classloader
|
||||
// path!
|
||||
return localUrls;
|
||||
}
|
||||
final Enumeration parentUrls = parent.getResources(name);
|
||||
|
||||
final ArrayList localItems = toList(localUrls);
|
||||
final ArrayList parentItems = toList(parentUrls);
|
||||
localItems.addAll(parentItems);
|
||||
return Collections.enumeration(localItems);
|
||||
public void setParentFirst(final boolean state) {
|
||||
parentFirst = state;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -400,25 +374,51 @@ public class PathableClassLoader extends URLClassLoader {
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as parent class method except that when parentFirst is false
|
||||
* the resource is looked for in the local classpath before the parent
|
||||
* loader is consulted.
|
||||
* Specify a classloader to use for specific java packages.
|
||||
* <p>
|
||||
* The specified classloader is normally a loader that is NOT
|
||||
* an ancestor of this classloader. In particular, this loader
|
||||
* may have the bootloader as its parent, but be configured to
|
||||
* see specific other classes (eg the junit library loaded
|
||||
* via the system classloader).
|
||||
* <p>
|
||||
* The differences between using this method, and using
|
||||
* addLogicalLib are:
|
||||
* <ul>
|
||||
* <li>If code calls getClassLoader on a class loaded via
|
||||
* "lookaside", then traces up its inheritance chain, it
|
||||
* will see the "real" classloaders. When the class is remapped
|
||||
* into this classloader via addLogicalLib, the classloader
|
||||
* chain seen is this object plus ancestors.
|
||||
* <li>If two different jars contain classes in the same
|
||||
* package, then it is not possible to load both jars into
|
||||
* the same "lookaside" classloader (eg the system classloader)
|
||||
* then map one of those subsets from here. Of course they could
|
||||
* be loaded into two different "lookaside" classloaders and
|
||||
* then a prefix used to map from here to one of those classloaders.
|
||||
* </ul>
|
||||
*/
|
||||
@Override
|
||||
public InputStream getResourceAsStream(final String name) {
|
||||
if (parentFirst) {
|
||||
return super.getResourceAsStream(name);
|
||||
public void useExplicitLoader(final String prefix, final ClassLoader loader) {
|
||||
if (lookasides == null) {
|
||||
lookasides = new HashMap();
|
||||
}
|
||||
final URL local = super.findResource(name);
|
||||
if (local != null) {
|
||||
try {
|
||||
return local.openStream();
|
||||
} catch (final IOException e) {
|
||||
// TODO: check if this is right or whether we should
|
||||
// fall back to trying parent. The javadoc doesn't say...
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return super.getResourceAsStream(name);
|
||||
lookasides.put(prefix, loader);
|
||||
}
|
||||
|
||||
/**
|
||||
* For classes with the specified prefix, get them from the system
|
||||
* classpath <i>which is active at the point this method is called</i>.
|
||||
* <p>
|
||||
* This method is just a shortcut for
|
||||
* <pre>
|
||||
* useExplicitLoader(prefix, ClassLoader.getSystemClassLoader());
|
||||
* </pre>
|
||||
* <p>
|
||||
* Of course, this assumes that the classes of interest are already
|
||||
* in the classpath of the system classloader.
|
||||
*/
|
||||
public void useSystemLoader(final String prefix) {
|
||||
useExplicitLoader(prefix, ClassLoader.getSystemClassLoader());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,22 +31,39 @@ import junit.framework.TestCase;
|
||||
|
||||
public class WeakHashtableTestCase extends TestCase {
|
||||
|
||||
public static class StupidThread extends Thread {
|
||||
|
||||
public StupidThread(final String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < RUN_LOOPS; i++) {
|
||||
hashtable.put("key" + ":" + i % 10, Boolean.TRUE);
|
||||
if (i % 50 == 0) {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private static final int WAIT_FOR_THREAD_COMPLETION = 5000; // 5 seconds
|
||||
private static final int RUN_LOOPS = 3000;
|
||||
private static final int OUTER_LOOP = 400;
|
||||
|
||||
private static final int THREAD_COUNT = 10;
|
||||
|
||||
private static WeakHashtable hashtable;
|
||||
|
||||
/** Maximum number of iterations before our test fails */
|
||||
private static final int MAX_GC_ITERATIONS = 50;
|
||||
|
||||
private WeakHashtable weakHashtable;
|
||||
private Long keyOne;
|
||||
private Long keyTwo;
|
||||
private Long keyThree;
|
||||
private Long valueOne;
|
||||
private Long valueTwo;
|
||||
|
||||
private Long valueThree;
|
||||
|
||||
public WeakHashtableTestCase(final String testName) {
|
||||
@@ -162,6 +179,33 @@ public class WeakHashtableTestCase extends TestCase {
|
||||
assertTrue(keySet.contains(keyThree));
|
||||
}
|
||||
|
||||
public void testLOGGING_119() throws Exception {
|
||||
final Thread [] t = new Thread[THREAD_COUNT];
|
||||
for (int j=1; j <= OUTER_LOOP; j++) {
|
||||
hashtable = new WeakHashtable();
|
||||
for (int i = 0; i < t.length; i++) {
|
||||
t[i] = new StupidThread("Thread:" + i);
|
||||
t[i].setDaemon(true); // Otherwise we cannot exit
|
||||
t[i].start();
|
||||
}
|
||||
for (final Thread element : t) {
|
||||
element.join(WAIT_FOR_THREAD_COMPLETION);
|
||||
if (element.isAlive()) {
|
||||
break; // at least one thread is stuck
|
||||
}
|
||||
}
|
||||
int active=0;
|
||||
for (final Thread element : t) {
|
||||
if (element.isAlive()) {
|
||||
active++;
|
||||
}
|
||||
}
|
||||
if (active > 0) {
|
||||
fail("Attempt: " + j + " Stuck threads: " + active);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Tests public Object put(Object key, Object value) */
|
||||
public void testPut() throws Exception {
|
||||
final Long anotherKey = new Long(2004);
|
||||
@@ -266,48 +310,4 @@ public class WeakHashtableTestCase extends TestCase {
|
||||
// Test that the released objects are not taking space in the table
|
||||
assertEquals("underlying table not emptied", 0, weakHashtable.size());
|
||||
}
|
||||
|
||||
public static class StupidThread extends Thread {
|
||||
|
||||
public StupidThread(final String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < RUN_LOOPS; i++) {
|
||||
hashtable.put("key" + ":" + i % 10, Boolean.TRUE);
|
||||
if (i % 50 == 0) {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testLOGGING_119() throws Exception {
|
||||
final Thread [] t = new Thread[THREAD_COUNT];
|
||||
for (int j=1; j <= OUTER_LOOP; j++) {
|
||||
hashtable = new WeakHashtable();
|
||||
for (int i = 0; i < t.length; i++) {
|
||||
t[i] = new StupidThread("Thread:" + i);
|
||||
t[i].setDaemon(true); // Otherwise we cannot exit
|
||||
t[i].start();
|
||||
}
|
||||
for (final Thread element : t) {
|
||||
element.join(WAIT_FOR_THREAD_COMPLETION);
|
||||
if (element.isAlive()) {
|
||||
break; // at least one thread is stuck
|
||||
}
|
||||
}
|
||||
int active=0;
|
||||
for (final Thread element : t) {
|
||||
if (element.isAlive()) {
|
||||
active++;
|
||||
}
|
||||
}
|
||||
if (active > 0) {
|
||||
fail("Attempt: " + j + " Stuck threads: " + active);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +30,6 @@ import org.apache.commons.logging.PathableTestSuite;
|
||||
|
||||
public class CustomConfigAPITestCase extends CustomConfigTestCase {
|
||||
|
||||
public CustomConfigAPITestCase(final String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the tests included in this test suite.
|
||||
*/
|
||||
@@ -58,4 +54,8 @@ public class CustomConfigAPITestCase extends CustomConfigTestCase {
|
||||
final Class testClass = child.loadClass(CustomConfigAPITestCase.class.getName());
|
||||
return new PathableTestSuite(testClass, child);
|
||||
}
|
||||
|
||||
public CustomConfigAPITestCase(final String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,11 +32,6 @@ import org.apache.commons.logging.PathableClassLoader;
|
||||
public class CustomConfigFullTestCase extends CustomConfigTestCase {
|
||||
|
||||
|
||||
public CustomConfigFullTestCase(final String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the tests included in this test suite.
|
||||
*/
|
||||
@@ -60,4 +55,9 @@ public class CustomConfigFullTestCase extends CustomConfigTestCase {
|
||||
final Class testClass = child.loadClass(CustomConfigFullTestCase.class.getName());
|
||||
return new PathableTestSuite(testClass, child);
|
||||
}
|
||||
|
||||
|
||||
public CustomConfigFullTestCase(final String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,82 +48,6 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
// ----------------------------------------------------------- Constructors
|
||||
|
||||
|
||||
/**
|
||||
* <p>Construct a new instance of this test case.</p>
|
||||
*
|
||||
* @param name Name of the test case
|
||||
*/
|
||||
public CustomConfigTestCase(final String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------- Instance Variables
|
||||
|
||||
|
||||
/**
|
||||
* <p>The customized {@code Handler} we will be using.</p>
|
||||
*/
|
||||
protected TestHandler handler;
|
||||
|
||||
|
||||
/**
|
||||
* <p>The underlying {@code Handler}s we will be using.</p>
|
||||
*/
|
||||
protected Handler handlers[];
|
||||
|
||||
|
||||
/**
|
||||
* <p>The underlying {@code Logger} we will be using.</p>
|
||||
*/
|
||||
protected Logger logger;
|
||||
|
||||
|
||||
/**
|
||||
* <p>The underlying {@code LogManager} we will be using.</p>
|
||||
*/
|
||||
protected LogManager manager;
|
||||
|
||||
|
||||
/**
|
||||
* <p>The message levels that should have been logged.</p>
|
||||
*/
|
||||
protected Level[] testLevels =
|
||||
{ Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE };
|
||||
|
||||
|
||||
/**
|
||||
* <p>The message strings that should have been logged.</p>
|
||||
*/
|
||||
protected String[] testMessages =
|
||||
{ "debug", "info", "warn", "error", "fatal" };
|
||||
|
||||
|
||||
// ------------------------------------------- JUnit Infrastructure Methods
|
||||
|
||||
|
||||
/**
|
||||
* Given the name of a class that is somewhere in the classpath of the provided
|
||||
* classloader, return the contents of the corresponding .class file.
|
||||
*/
|
||||
protected static byte[] readClass(final String name, final ClassLoader srcCL) throws Exception {
|
||||
final String resName = name.replace('.', '/') + ".class";
|
||||
System.err.println("Trying to load resource [" + resName + "]");
|
||||
final InputStream is = srcCL.getResourceAsStream(resName);
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
System.err.println("Reading resource [" + resName + "]");
|
||||
final byte[] buf = new byte[1000];
|
||||
for(;;) {
|
||||
final int read = is.read(buf);
|
||||
if (read <= 0) {
|
||||
break;
|
||||
}
|
||||
baos.write(buf, 0, read);
|
||||
}
|
||||
is.close();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a class available in the system classloader even when its classfile is
|
||||
* not present in the classpath configured for that classloader. This only
|
||||
@@ -159,17 +83,30 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------- Instance Variables
|
||||
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
* Given the name of a class that is somewhere in the classpath of the provided
|
||||
* classloader, return the contents of the corresponding .class file.
|
||||
*/
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
setUpManager
|
||||
("org/apache/commons/logging/jdk14/CustomConfig.properties");
|
||||
setUpLogger(this.getClass().getName());
|
||||
setUpHandlers();
|
||||
setUpFactory();
|
||||
setUpLog(this.getClass().getName());
|
||||
protected static byte[] readClass(final String name, final ClassLoader srcCL) throws Exception {
|
||||
final String resName = name.replace('.', '/') + ".class";
|
||||
System.err.println("Trying to load resource [" + resName + "]");
|
||||
final InputStream is = srcCL.getResourceAsStream(resName);
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
System.err.println("Reading resource [" + resName + "]");
|
||||
final byte[] buf = new byte[1000];
|
||||
for(;;) {
|
||||
final int read = is.read(buf);
|
||||
if (read <= 0) {
|
||||
break;
|
||||
}
|
||||
baos.write(buf, 0, read);
|
||||
}
|
||||
is.close();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
@@ -195,6 +132,198 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
return new PathableTestSuite(testClass, cl);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <p>The customized {@code Handler} we will be using.</p>
|
||||
*/
|
||||
protected TestHandler handler;
|
||||
|
||||
|
||||
/**
|
||||
* <p>The underlying {@code Handler}s we will be using.</p>
|
||||
*/
|
||||
protected Handler handlers[];
|
||||
|
||||
|
||||
/**
|
||||
* <p>The underlying {@code Logger} we will be using.</p>
|
||||
*/
|
||||
protected Logger logger;
|
||||
|
||||
|
||||
/**
|
||||
* <p>The underlying {@code LogManager} we will be using.</p>
|
||||
*/
|
||||
protected LogManager manager;
|
||||
|
||||
|
||||
// ------------------------------------------- JUnit Infrastructure Methods
|
||||
|
||||
|
||||
/**
|
||||
* <p>The message levels that should have been logged.</p>
|
||||
*/
|
||||
protected Level[] testLevels =
|
||||
{ Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE };
|
||||
|
||||
/**
|
||||
* <p>The message strings that should have been logged.</p>
|
||||
*/
|
||||
protected String[] testMessages =
|
||||
{ "debug", "info", "warn", "error", "fatal" };
|
||||
|
||||
/**
|
||||
* <p>Construct a new instance of this test case.</p>
|
||||
*
|
||||
* @param name Name of the test case
|
||||
*/
|
||||
public CustomConfigTestCase(final String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
|
||||
// Check the log instance
|
||||
@Override
|
||||
protected void checkLog() {
|
||||
|
||||
assertNotNull("Log exists", log);
|
||||
assertEquals("Log class",
|
||||
"org.apache.commons.logging.impl.Jdk14Logger",
|
||||
log.getClass().getName());
|
||||
|
||||
// Assert which logging levels have been enabled
|
||||
assertTrue(log.isFatalEnabled());
|
||||
assertTrue(log.isErrorEnabled());
|
||||
assertTrue(log.isWarnEnabled());
|
||||
assertTrue(log.isInfoEnabled());
|
||||
assertTrue(log.isDebugEnabled());
|
||||
assertFalse(log.isTraceEnabled());
|
||||
|
||||
}
|
||||
|
||||
// Check the recorded messages
|
||||
protected void checkLogRecords(final boolean thrown) {
|
||||
final Iterator records = handler.records();
|
||||
for (int i = 0; i < testMessages.length; i++) {
|
||||
assertTrue(records.hasNext());
|
||||
final LogRecord record = (LogRecord) records.next();
|
||||
assertEquals("LogRecord level",
|
||||
testLevels[i], record.getLevel());
|
||||
assertEquals("LogRecord message",
|
||||
testMessages[i], record.getMessage());
|
||||
assertTrue("LogRecord class",
|
||||
record.getSourceClassName().startsWith(
|
||||
"org.apache.commons.logging.jdk14.CustomConfig"));
|
||||
if (thrown) {
|
||||
assertEquals("LogRecord method",
|
||||
"logExceptionMessages",
|
||||
record.getSourceMethodName());
|
||||
} else {
|
||||
assertEquals("LogRecord method",
|
||||
"logPlainMessages",
|
||||
record.getSourceMethodName());
|
||||
}
|
||||
if (thrown) {
|
||||
assertNotNull("LogRecord thrown", record.getThrown());
|
||||
assertTrue("LogRecord thrown type",
|
||||
record.getThrown() instanceof DummyException);
|
||||
} else {
|
||||
assertNull("LogRecord thrown",
|
||||
record.getThrown());
|
||||
}
|
||||
}
|
||||
assertFalse(records.hasNext());
|
||||
handler.flush();
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------- Test Methods
|
||||
|
||||
|
||||
// Log the messages with exceptions
|
||||
protected void logExceptionMessages() {
|
||||
final Throwable t = new DummyException();
|
||||
log.trace("trace", t); // Should not actually get logged
|
||||
log.debug("debug", t);
|
||||
log.info("info", t);
|
||||
log.warn("warn", t);
|
||||
log.error("error", t);
|
||||
log.fatal("fatal", t);
|
||||
}
|
||||
|
||||
|
||||
// Log the plain messages
|
||||
protected void logPlainMessages() {
|
||||
log.trace("trace"); // Should not actually get logged
|
||||
log.debug("debug");
|
||||
log.info("info");
|
||||
log.warn("warn");
|
||||
log.error("error");
|
||||
log.fatal("fatal");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
setUpManager
|
||||
("org/apache/commons/logging/jdk14/CustomConfig.properties");
|
||||
setUpLogger(this.getClass().getName());
|
||||
setUpHandlers();
|
||||
setUpFactory();
|
||||
setUpLog(this.getClass().getName());
|
||||
}
|
||||
|
||||
|
||||
// Set up handlers instance
|
||||
protected void setUpHandlers() throws Exception {
|
||||
Logger parent = logger;
|
||||
while (parent.getParent() != null) {
|
||||
parent = parent.getParent();
|
||||
}
|
||||
handlers = parent.getHandlers();
|
||||
|
||||
// The CustomConfig.properties file explicitly defines one handler class
|
||||
// to be attached to the root logger, so if it isn't there then
|
||||
// something is badly wrong...
|
||||
//
|
||||
// Yes this testing is also done in testPristineHandlers but
|
||||
// unfortunately:
|
||||
// * we need to set up the handlers variable here,
|
||||
// * we don't want that to be set up incorrectly, as that can
|
||||
// produce weird error messages in other tests, and
|
||||
// * we can't rely on testPristineHandlers being the first
|
||||
// test to run.
|
||||
// so we need to test things here too.
|
||||
assertNotNull("No Handlers defined for JDK14 logging", handlers);
|
||||
assertEquals("Unexpected number of handlers for JDK14 logging", 1, handlers.length);
|
||||
assertNotNull("Handler is null", handlers[0]);
|
||||
assertTrue("Handler not of expected type", handlers[0] instanceof TestHandler);
|
||||
handler = (TestHandler) handlers[0];
|
||||
}
|
||||
|
||||
|
||||
// Set up logger instance
|
||||
protected void setUpLogger(final String name) throws Exception {
|
||||
logger = Logger.getLogger(name);
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------------- Support Methods
|
||||
|
||||
|
||||
// Set up LogManager instance
|
||||
protected void setUpManager(final String config) throws Exception {
|
||||
manager = LogManager.getLogManager();
|
||||
final InputStream is =
|
||||
this.getClass().getClassLoader().getResourceAsStream(config);
|
||||
manager.readConfiguration(is);
|
||||
is.close();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tear down instance variables required by this test case.
|
||||
*/
|
||||
@@ -207,9 +336,6 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------- Test Methods
|
||||
|
||||
|
||||
// Test logging message strings with exceptions
|
||||
public void testExceptionMessages() throws Exception {
|
||||
|
||||
@@ -267,130 +393,4 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------------- Support Methods
|
||||
|
||||
|
||||
// Check the log instance
|
||||
@Override
|
||||
protected void checkLog() {
|
||||
|
||||
assertNotNull("Log exists", log);
|
||||
assertEquals("Log class",
|
||||
"org.apache.commons.logging.impl.Jdk14Logger",
|
||||
log.getClass().getName());
|
||||
|
||||
// Assert which logging levels have been enabled
|
||||
assertTrue(log.isFatalEnabled());
|
||||
assertTrue(log.isErrorEnabled());
|
||||
assertTrue(log.isWarnEnabled());
|
||||
assertTrue(log.isInfoEnabled());
|
||||
assertTrue(log.isDebugEnabled());
|
||||
assertFalse(log.isTraceEnabled());
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Check the recorded messages
|
||||
protected void checkLogRecords(final boolean thrown) {
|
||||
final Iterator records = handler.records();
|
||||
for (int i = 0; i < testMessages.length; i++) {
|
||||
assertTrue(records.hasNext());
|
||||
final LogRecord record = (LogRecord) records.next();
|
||||
assertEquals("LogRecord level",
|
||||
testLevels[i], record.getLevel());
|
||||
assertEquals("LogRecord message",
|
||||
testMessages[i], record.getMessage());
|
||||
assertTrue("LogRecord class",
|
||||
record.getSourceClassName().startsWith(
|
||||
"org.apache.commons.logging.jdk14.CustomConfig"));
|
||||
if (thrown) {
|
||||
assertEquals("LogRecord method",
|
||||
"logExceptionMessages",
|
||||
record.getSourceMethodName());
|
||||
} else {
|
||||
assertEquals("LogRecord method",
|
||||
"logPlainMessages",
|
||||
record.getSourceMethodName());
|
||||
}
|
||||
if (thrown) {
|
||||
assertNotNull("LogRecord thrown", record.getThrown());
|
||||
assertTrue("LogRecord thrown type",
|
||||
record.getThrown() instanceof DummyException);
|
||||
} else {
|
||||
assertNull("LogRecord thrown",
|
||||
record.getThrown());
|
||||
}
|
||||
}
|
||||
assertFalse(records.hasNext());
|
||||
handler.flush();
|
||||
}
|
||||
|
||||
|
||||
// Log the messages with exceptions
|
||||
protected void logExceptionMessages() {
|
||||
final Throwable t = new DummyException();
|
||||
log.trace("trace", t); // Should not actually get logged
|
||||
log.debug("debug", t);
|
||||
log.info("info", t);
|
||||
log.warn("warn", t);
|
||||
log.error("error", t);
|
||||
log.fatal("fatal", t);
|
||||
}
|
||||
|
||||
|
||||
// Log the plain messages
|
||||
protected void logPlainMessages() {
|
||||
log.trace("trace"); // Should not actually get logged
|
||||
log.debug("debug");
|
||||
log.info("info");
|
||||
log.warn("warn");
|
||||
log.error("error");
|
||||
log.fatal("fatal");
|
||||
}
|
||||
|
||||
|
||||
// Set up handlers instance
|
||||
protected void setUpHandlers() throws Exception {
|
||||
Logger parent = logger;
|
||||
while (parent.getParent() != null) {
|
||||
parent = parent.getParent();
|
||||
}
|
||||
handlers = parent.getHandlers();
|
||||
|
||||
// The CustomConfig.properties file explicitly defines one handler class
|
||||
// to be attached to the root logger, so if it isn't there then
|
||||
// something is badly wrong...
|
||||
//
|
||||
// Yes this testing is also done in testPristineHandlers but
|
||||
// unfortunately:
|
||||
// * we need to set up the handlers variable here,
|
||||
// * we don't want that to be set up incorrectly, as that can
|
||||
// produce weird error messages in other tests, and
|
||||
// * we can't rely on testPristineHandlers being the first
|
||||
// test to run.
|
||||
// so we need to test things here too.
|
||||
assertNotNull("No Handlers defined for JDK14 logging", handlers);
|
||||
assertEquals("Unexpected number of handlers for JDK14 logging", 1, handlers.length);
|
||||
assertNotNull("Handler is null", handlers[0]);
|
||||
assertTrue("Handler not of expected type", handlers[0] instanceof TestHandler);
|
||||
handler = (TestHandler) handlers[0];
|
||||
}
|
||||
|
||||
|
||||
// Set up logger instance
|
||||
protected void setUpLogger(final String name) throws Exception {
|
||||
logger = Logger.getLogger(name);
|
||||
}
|
||||
|
||||
|
||||
// Set up LogManager instance
|
||||
protected void setUpManager(final String config) throws Exception {
|
||||
manager = LogManager.getLogManager();
|
||||
final InputStream is =
|
||||
this.getClass().getClassLoader().getResourceAsStream(config);
|
||||
manager.readConfiguration(is);
|
||||
is.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -39,34 +39,6 @@ import org.apache.commons.logging.PathableTestSuite;
|
||||
*/
|
||||
public class DefaultConfigTestCase extends TestCase {
|
||||
|
||||
/**
|
||||
* <p>Construct a new instance of this test case.</p>
|
||||
*
|
||||
* @param name Name of the test case
|
||||
*/
|
||||
public DefaultConfigTestCase(final String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>The {@link LogFactory} implementation we have selected.</p>
|
||||
*/
|
||||
protected LogFactory factory;
|
||||
|
||||
/**
|
||||
* <p>The {@link Log} implementation we have selected.</p>
|
||||
*/
|
||||
protected Log log;
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
setUpFactory();
|
||||
setUpLog("TestLogger");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the tests included in this test suite.
|
||||
*/
|
||||
@@ -81,50 +53,22 @@ public class DefaultConfigTestCase extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down instance variables required by this test case.
|
||||
* <p>The {@link LogFactory} implementation we have selected.</p>
|
||||
*/
|
||||
@Override
|
||||
public void tearDown() {
|
||||
log = null;
|
||||
factory = null;
|
||||
LogFactory.releaseAll();
|
||||
}
|
||||
protected LogFactory factory;
|
||||
|
||||
// Test pristine Log instance
|
||||
public void testPristineLog() {
|
||||
checkLog();
|
||||
}
|
||||
|
||||
// Test pristine LogFactory instance
|
||||
public void testPristineFactory() {
|
||||
assertNotNull("LogFactory exists", factory);
|
||||
assertEquals("LogFactory class",
|
||||
"org.apache.commons.logging.impl.LogFactoryImpl",
|
||||
factory.getClass().getName());
|
||||
|
||||
final String[] names = factory.getAttributeNames();
|
||||
assertNotNull("Names exists", names);
|
||||
assertEquals("Names empty", 0, names.length);
|
||||
}
|
||||
|
||||
|
||||
// Test Serializability of Log instance
|
||||
public void testSerializable() throws Exception {
|
||||
|
||||
// Serialize and deserialize the instance
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(log);
|
||||
oos.close();
|
||||
final ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream(baos.toByteArray());
|
||||
final ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
log = (Log) ois.readObject();
|
||||
ois.close();
|
||||
|
||||
// Check the characteristics of the resulting object
|
||||
checkLog();
|
||||
/**
|
||||
* <p>The {@link Log} implementation we have selected.</p>
|
||||
*/
|
||||
protected Log log;
|
||||
|
||||
/**
|
||||
* <p>Construct a new instance of this test case.</p>
|
||||
*
|
||||
* @param name Name of the test case
|
||||
*/
|
||||
public DefaultConfigTestCase(final String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
// Check the log instance
|
||||
@@ -145,6 +89,15 @@ public class DefaultConfigTestCase extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
setUpFactory();
|
||||
setUpLog("TestLogger");
|
||||
}
|
||||
|
||||
// Set up factory instance
|
||||
protected void setUpFactory() throws Exception {
|
||||
factory = LogFactory.getFactory();
|
||||
@@ -155,4 +108,51 @@ public class DefaultConfigTestCase extends TestCase {
|
||||
log = LogFactory.getLog(name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tear down instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void tearDown() {
|
||||
log = null;
|
||||
factory = null;
|
||||
LogFactory.releaseAll();
|
||||
}
|
||||
|
||||
// Test pristine LogFactory instance
|
||||
public void testPristineFactory() {
|
||||
assertNotNull("LogFactory exists", factory);
|
||||
assertEquals("LogFactory class",
|
||||
"org.apache.commons.logging.impl.LogFactoryImpl",
|
||||
factory.getClass().getName());
|
||||
|
||||
final String[] names = factory.getAttributeNames();
|
||||
assertNotNull("Names exists", names);
|
||||
assertEquals("Names empty", 0, names.length);
|
||||
}
|
||||
|
||||
// Test pristine Log instance
|
||||
public void testPristineLog() {
|
||||
checkLog();
|
||||
}
|
||||
|
||||
// Test Serializability of Log instance
|
||||
public void testSerializable() throws Exception {
|
||||
|
||||
// Serialize and deserialize the instance
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(log);
|
||||
oos.close();
|
||||
final ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream(baos.toByteArray());
|
||||
final ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
log = (Log) ois.readObject();
|
||||
ois.close();
|
||||
|
||||
// Check the characteristics of the resulting object
|
||||
checkLog();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,10 +32,6 @@ public class TestHandler extends Handler {
|
||||
// The set of logged records for this handler
|
||||
private final List records = new ArrayList();
|
||||
|
||||
public Iterator records() {
|
||||
return records.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
@@ -50,4 +46,8 @@ public class TestHandler extends Handler {
|
||||
records.add(record);
|
||||
}
|
||||
|
||||
public Iterator records() {
|
||||
return records.iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -51,91 +51,6 @@ public abstract class StandardTests extends TestCase {
|
||||
public Throwable throwable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
LogFactory.releaseAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void tearDown() {
|
||||
LogFactory.releaseAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify log4j's setup so that all messages actually logged get redirected
|
||||
* into the specified list.
|
||||
* <p>
|
||||
* This method also sets the logging level to INFO so that we
|
||||
* can test whether messages are getting properly filtered.
|
||||
*/
|
||||
public abstract void setUpTestAppender(List logEvents) throws Exception;
|
||||
|
||||
/**
|
||||
* Test that a LogFactory gets created as expected.
|
||||
*/
|
||||
public void testCreateFactory() {
|
||||
final LogFactory factory = LogFactory.getFactory();
|
||||
assertNotNull("LogFactory exists", factory);
|
||||
assertEquals("LogFactory class",
|
||||
"org.apache.commons.logging.impl.LogFactoryImpl",
|
||||
factory.getClass().getName());
|
||||
|
||||
final String[] names = factory.getAttributeNames();
|
||||
assertNotNull("Names exists", names);
|
||||
assertEquals("Names empty", 0, names.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that we can log messages without exceptions.
|
||||
*/
|
||||
public void testPlainMessages() throws Exception {
|
||||
final List logEvents = new ArrayList();
|
||||
setUpTestAppender(logEvents);
|
||||
final Log log = LogFactory.getLog("test-category");
|
||||
logPlainMessages(log);
|
||||
checkLoggingEvents(logEvents, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that we can log exception messages.
|
||||
*/
|
||||
public void testExceptionMessages() throws Exception {
|
||||
final List logEvents = new ArrayList();
|
||||
setUpTestAppender(logEvents);
|
||||
final Log log = LogFactory.getLog("test-category");
|
||||
logExceptionMessages(log);
|
||||
checkLoggingEvents(logEvents, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Serializability of Log instance
|
||||
*/
|
||||
public void testSerializable() throws Exception {
|
||||
final List logEvents = new ArrayList();
|
||||
setUpTestAppender(logEvents);
|
||||
final Log log = LogFactory.getLog("test-category");
|
||||
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(log);
|
||||
oos.close();
|
||||
final ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream(baos.toByteArray());
|
||||
final ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
final Log newLog = (Log) ois.readObject();
|
||||
ois.close();
|
||||
|
||||
// Check the characteristics of the resulting object
|
||||
logExceptionMessages(newLog);
|
||||
checkLoggingEvents(logEvents, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the TestAppender has received the expected
|
||||
* number of messages. This assumes that:
|
||||
@@ -178,6 +93,18 @@ public abstract class StandardTests extends TestCase {
|
||||
assertEquals("Exception data incorrect", ev.throwable!=null, thrown);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log messages with exceptions
|
||||
*/
|
||||
private void logExceptionMessages(final Log log) {
|
||||
final Throwable t = new DummyException();
|
||||
log.trace("trace", t); // Should not actually get logged
|
||||
log.debug("debug", t); // Should not actually get logged
|
||||
log.info("info", t);
|
||||
log.warn("warn", t);
|
||||
log.error("error", t);
|
||||
log.fatal("fatal", t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log plain messages.
|
||||
@@ -192,15 +119,88 @@ public abstract class StandardTests extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Log messages with exceptions
|
||||
* Set up instance variables required by this test case.
|
||||
*/
|
||||
private void logExceptionMessages(final Log log) {
|
||||
final Throwable t = new DummyException();
|
||||
log.trace("trace", t); // Should not actually get logged
|
||||
log.debug("debug", t); // Should not actually get logged
|
||||
log.info("info", t);
|
||||
log.warn("warn", t);
|
||||
log.error("error", t);
|
||||
log.fatal("fatal", t);
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
LogFactory.releaseAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify log4j's setup so that all messages actually logged get redirected
|
||||
* into the specified list.
|
||||
* <p>
|
||||
* This method also sets the logging level to INFO so that we
|
||||
* can test whether messages are getting properly filtered.
|
||||
*/
|
||||
public abstract void setUpTestAppender(List logEvents) throws Exception;
|
||||
|
||||
/**
|
||||
* Tear down instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void tearDown() {
|
||||
LogFactory.releaseAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a LogFactory gets created as expected.
|
||||
*/
|
||||
public void testCreateFactory() {
|
||||
final LogFactory factory = LogFactory.getFactory();
|
||||
assertNotNull("LogFactory exists", factory);
|
||||
assertEquals("LogFactory class",
|
||||
"org.apache.commons.logging.impl.LogFactoryImpl",
|
||||
factory.getClass().getName());
|
||||
|
||||
final String[] names = factory.getAttributeNames();
|
||||
assertNotNull("Names exists", names);
|
||||
assertEquals("Names empty", 0, names.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that we can log exception messages.
|
||||
*/
|
||||
public void testExceptionMessages() throws Exception {
|
||||
final List logEvents = new ArrayList();
|
||||
setUpTestAppender(logEvents);
|
||||
final Log log = LogFactory.getLog("test-category");
|
||||
logExceptionMessages(log);
|
||||
checkLoggingEvents(logEvents, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Verify that we can log messages without exceptions.
|
||||
*/
|
||||
public void testPlainMessages() throws Exception {
|
||||
final List logEvents = new ArrayList();
|
||||
setUpTestAppender(logEvents);
|
||||
final Log log = LogFactory.getLog("test-category");
|
||||
logPlainMessages(log);
|
||||
checkLoggingEvents(logEvents, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Serializability of Log instance
|
||||
*/
|
||||
public void testSerializable() throws Exception {
|
||||
final List logEvents = new ArrayList();
|
||||
setUpTestAppender(logEvents);
|
||||
final Log log = LogFactory.getLog("test-category");
|
||||
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(log);
|
||||
oos.close();
|
||||
final ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream(baos.toByteArray());
|
||||
final ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
final Log newLog = (Log) ois.readObject();
|
||||
ois.close();
|
||||
|
||||
// Check the characteristics of the resulting object
|
||||
logExceptionMessages(newLog);
|
||||
checkLoggingEvents(logEvents, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,12 @@ import org.apache.log4j.spi.LoggingEvent;
|
||||
|
||||
public class TestAppender extends AppenderSkeleton {
|
||||
|
||||
// The set of logged events for this appender
|
||||
private final List events;
|
||||
|
||||
// ----------------------------------------------------- Instance Variables
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
@@ -40,12 +46,6 @@ public class TestAppender extends AppenderSkeleton {
|
||||
events = logEvents;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------- Instance Variables
|
||||
|
||||
|
||||
// The set of logged events for this appender
|
||||
private final List events;
|
||||
|
||||
|
||||
// ------------------------------------------------------- Appender Methods
|
||||
|
||||
|
||||
@@ -42,21 +42,6 @@ public class StandardTestCase extends AbstractLogTest {
|
||||
// ----------------------------------------------------- Instance Variables
|
||||
|
||||
|
||||
/**
|
||||
* <p>The {@link LogFactory} implementation we have selected.</p>
|
||||
*/
|
||||
protected LogFactory factory;
|
||||
|
||||
|
||||
/**
|
||||
* <p>The {@link Log} implementation we have selected.</p>
|
||||
*/
|
||||
protected Log log;
|
||||
|
||||
|
||||
// ------------------------------------------- JUnit Infrastructure Methods
|
||||
|
||||
|
||||
/**
|
||||
* Return the tests included in this test suite.
|
||||
*/
|
||||
@@ -73,6 +58,51 @@ public class StandardTestCase extends AbstractLogTest {
|
||||
return new PathableTestSuite(testClass, loader);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <p>The {@link LogFactory} implementation we have selected.</p>
|
||||
*/
|
||||
protected LogFactory factory;
|
||||
|
||||
|
||||
// ------------------------------------------- JUnit Infrastructure Methods
|
||||
|
||||
|
||||
/**
|
||||
* <p>The {@link Log} implementation we have selected.</p>
|
||||
*/
|
||||
protected Log log;
|
||||
|
||||
// Check the standard log instance
|
||||
protected void checkStandard() {
|
||||
|
||||
assertNotNull("Log exists", log);
|
||||
assertEquals("Log class",
|
||||
"org.apache.commons.logging.impl.LogKitLogger",
|
||||
log.getClass().getName());
|
||||
|
||||
// Can we call level checkers with no exceptions?
|
||||
// Note that by default *everything* is enabled for LogKit
|
||||
assertTrue(log.isTraceEnabled());
|
||||
assertTrue(log.isDebugEnabled());
|
||||
assertTrue(log.isInfoEnabled());
|
||||
assertTrue(log.isWarnEnabled());
|
||||
assertTrue(log.isErrorEnabled());
|
||||
assertTrue(log.isFatalEnabled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the abstract method from the parent class so that the
|
||||
* inherited tests can access the right Log object type.
|
||||
*/
|
||||
@Override
|
||||
public Log getLogObject()
|
||||
{
|
||||
return new LogKitLogger(this.getClass().getName());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------- Test Methods
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
*/
|
||||
@@ -98,18 +128,6 @@ public class StandardTestCase extends AbstractLogTest {
|
||||
LogFactory.releaseAll();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------- Test Methods
|
||||
|
||||
/**
|
||||
* Override the abstract method from the parent class so that the
|
||||
* inherited tests can access the right Log object type.
|
||||
*/
|
||||
@Override
|
||||
public Log getLogObject()
|
||||
{
|
||||
return new LogKitLogger(this.getClass().getName());
|
||||
}
|
||||
|
||||
// Test pristine LogFactory instance
|
||||
public void testPristineFactory() {
|
||||
|
||||
@@ -128,6 +146,9 @@ public class StandardTestCase extends AbstractLogTest {
|
||||
checkStandard();
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------------- Support Methods
|
||||
|
||||
// Test Serializability of standard instance
|
||||
public void testSerializable() throws Exception {
|
||||
checkStandard();
|
||||
@@ -145,25 +166,4 @@ public class StandardTestCase extends AbstractLogTest {
|
||||
|
||||
checkStandard();
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------------- Support Methods
|
||||
|
||||
// Check the standard log instance
|
||||
protected void checkStandard() {
|
||||
|
||||
assertNotNull("Log exists", log);
|
||||
assertEquals("Log class",
|
||||
"org.apache.commons.logging.impl.LogKitLogger",
|
||||
log.getClass().getName());
|
||||
|
||||
// Can we call level checkers with no exceptions?
|
||||
// Note that by default *everything* is enabled for LogKit
|
||||
assertTrue(log.isTraceEnabled());
|
||||
assertTrue(log.isDebugEnabled());
|
||||
assertTrue(log.isInfoEnabled());
|
||||
assertTrue(log.isWarnEnabled());
|
||||
assertTrue(log.isErrorEnabled());
|
||||
assertTrue(log.isFatalEnabled());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,33 @@ import org.apache.commons.logging.AbstractLogTest;
|
||||
*/
|
||||
public class NoOpLogTestCase extends AbstractLogTest
|
||||
{
|
||||
private void checkLog(final Log log) {
|
||||
|
||||
assertNotNull("Log exists", log);
|
||||
assertEquals("Log class",
|
||||
"org.apache.commons.logging.impl.NoOpLog",
|
||||
log.getClass().getName());
|
||||
|
||||
// Can we call level checkers with no exceptions?
|
||||
// Note that *everything* is permanently disabled for NoOpLog
|
||||
assertFalse(log.isTraceEnabled());
|
||||
assertFalse(log.isDebugEnabled());
|
||||
assertFalse(log.isInfoEnabled());
|
||||
assertFalse(log.isWarnEnabled());
|
||||
assertFalse(log.isErrorEnabled());
|
||||
assertFalse(log.isFatalEnabled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the abstract method from the parent class so that the
|
||||
* inherited tests can access the right Log object type.
|
||||
*/
|
||||
@Override
|
||||
public Log getLogObject()
|
||||
{
|
||||
return new NoOpLog(this.getClass().getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
*/
|
||||
@@ -55,15 +82,8 @@ public class NoOpLogTestCase extends AbstractLogTest
|
||||
System.getProperties().remove("org.apache.commons.logging.Log");
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the abstract method from the parent class so that the
|
||||
* inherited tests can access the right Log object type.
|
||||
*/
|
||||
@Override
|
||||
public Log getLogObject()
|
||||
{
|
||||
return new NoOpLog(this.getClass().getName());
|
||||
}
|
||||
|
||||
// -------------------------------------------------------- Support Methods
|
||||
|
||||
// Test Serializability of standard instance
|
||||
public void testSerializable() throws Exception {
|
||||
@@ -83,24 +103,4 @@ public class NoOpLogTestCase extends AbstractLogTest
|
||||
|
||||
checkLog(log);
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------------- Support Methods
|
||||
|
||||
private void checkLog(final Log log) {
|
||||
|
||||
assertNotNull("Log exists", log);
|
||||
assertEquals("Log class",
|
||||
"org.apache.commons.logging.impl.NoOpLog",
|
||||
log.getClass().getName());
|
||||
|
||||
// Can we call level checkers with no exceptions?
|
||||
// Note that *everything* is permanently disabled for NoOpLog
|
||||
assertFalse(log.isTraceEnabled());
|
||||
assertFalse(log.isDebugEnabled());
|
||||
assertFalse(log.isInfoEnabled());
|
||||
assertFalse(log.isWarnEnabled());
|
||||
assertFalse(log.isErrorEnabled());
|
||||
assertFalse(log.isFatalEnabled());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +90,19 @@ public class ChildFirstTestCase extends TestCase {
|
||||
return new PathableTestSuite(testClass, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to convert an enumeration-of-URLs into an array of URLs.
|
||||
*/
|
||||
private static URL[] toURLArray(final Enumeration e) {
|
||||
final ArrayList l = new ArrayList();
|
||||
while (e.hasMoreElements()) {
|
||||
final URL u = (URL) e.nextElement();
|
||||
l.add(u);
|
||||
}
|
||||
final URL[] tmp = new URL[l.size()];
|
||||
return (URL[]) l.toArray(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to return the set of all classloaders in the
|
||||
* parent chain starting from the one that loaded the class for
|
||||
@@ -220,6 +233,34 @@ public class ChildFirstTestCase extends TestCase {
|
||||
resource.toString().indexOf("/commons-logging-adapters-1.") > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that getResourceAsStream works.
|
||||
*/
|
||||
public void testResourceAsStream() throws Exception {
|
||||
java.io.InputStream is;
|
||||
|
||||
// verify the classloader hierarchy
|
||||
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||
final ClassLoader childLoader = contextLoader.getParent();
|
||||
final ClassLoader parentLoader = childLoader.getParent();
|
||||
final ClassLoader bootLoader = parentLoader.getParent();
|
||||
assertNull("Unexpected classloader hierarchy", bootLoader);
|
||||
|
||||
// getResourceAsStream where no instances exist
|
||||
is = childLoader.getResourceAsStream("nosuchfile");
|
||||
assertNull("Invalid resource returned non-null stream", is);
|
||||
|
||||
// getResourceAsStream where resource does exist
|
||||
is = childLoader.getResourceAsStream("org/apache/commons/logging/Log.class");
|
||||
assertNotNull("Null returned for valid resource", is);
|
||||
is.close();
|
||||
|
||||
// It would be nice to test parent-first ordering here, but that would require
|
||||
// having a resource with the same name in both the parent and child loaders,
|
||||
// but with different contents. That's a little tricky to set up so we'll
|
||||
// skip that for now.
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the various flavours of ClassLoader.getResources work as expected.
|
||||
*/
|
||||
@@ -273,45 +314,4 @@ public class ChildFirstTestCase extends TestCase {
|
||||
assertTrue("Incorrect source for Log4JLogger class",
|
||||
urlsToStrings[1].indexOf("/commons-logging-adapters-1.") > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to convert an enumeration-of-URLs into an array of URLs.
|
||||
*/
|
||||
private static URL[] toURLArray(final Enumeration e) {
|
||||
final ArrayList l = new ArrayList();
|
||||
while (e.hasMoreElements()) {
|
||||
final URL u = (URL) e.nextElement();
|
||||
l.add(u);
|
||||
}
|
||||
final URL[] tmp = new URL[l.size()];
|
||||
return (URL[]) l.toArray(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that getResourceAsStream works.
|
||||
*/
|
||||
public void testResourceAsStream() throws Exception {
|
||||
java.io.InputStream is;
|
||||
|
||||
// verify the classloader hierarchy
|
||||
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||
final ClassLoader childLoader = contextLoader.getParent();
|
||||
final ClassLoader parentLoader = childLoader.getParent();
|
||||
final ClassLoader bootLoader = parentLoader.getParent();
|
||||
assertNull("Unexpected classloader hierarchy", bootLoader);
|
||||
|
||||
// getResourceAsStream where no instances exist
|
||||
is = childLoader.getResourceAsStream("nosuchfile");
|
||||
assertNull("Invalid resource returned non-null stream", is);
|
||||
|
||||
// getResourceAsStream where resource does exist
|
||||
is = childLoader.getResourceAsStream("org/apache/commons/logging/Log.class");
|
||||
assertNotNull("Null returned for valid resource", is);
|
||||
is.close();
|
||||
|
||||
// It would be nice to test parent-first ordering here, but that would require
|
||||
// having a resource with the same name in both the parent and child loaders,
|
||||
// but with different contents. That's a little tricky to set up so we'll
|
||||
// skip that for now.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,31 @@ import org.apache.commons.logging.PathableTestSuite;
|
||||
|
||||
public class GeneralTestCase extends TestCase {
|
||||
|
||||
/**
|
||||
* Verify that the context classloader is a custom one, then reset it to
|
||||
* a non-custom one.
|
||||
*/
|
||||
private static void checkAndSetContext() {
|
||||
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||
assertEquals("ContextLoader is of unexpected type",
|
||||
contextLoader.getClass().getName(),
|
||||
PathableClassLoader.class.getName());
|
||||
|
||||
final URL[] noUrls = new URL[0];
|
||||
Thread.currentThread().setContextClassLoader(new URLClassLoader(noUrls));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a certain system property is not set, then set it.
|
||||
*/
|
||||
private static void checkAndSetProperties() {
|
||||
String prop = System.getProperty("no.such.property");
|
||||
assertNull("no.such.property is unexpectedly defined", prop);
|
||||
System.setProperty("no.such.property", "dummy value");
|
||||
prop = System.getProperty("no.such.property");
|
||||
assertNotNull("no.such.property is unexpectedly undefined", prop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up a custom classloader hierarchy for this test case.
|
||||
*/
|
||||
@@ -49,53 +74,6 @@ public class GeneralTestCase extends TestCase {
|
||||
return new PathableTestSuite(testClass, loader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a certain system property is not set, then set it.
|
||||
*/
|
||||
private static void checkAndSetProperties() {
|
||||
String prop = System.getProperty("no.such.property");
|
||||
assertNull("no.such.property is unexpectedly defined", prop);
|
||||
System.setProperty("no.such.property", "dummy value");
|
||||
prop = System.getProperty("no.such.property");
|
||||
assertNotNull("no.such.property is unexpectedly undefined", prop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that when a test method modifies the system properties they are
|
||||
* reset before the next test is run.
|
||||
* <p>
|
||||
* This method works in conjunction with testResetProps2. There is no
|
||||
* way of knowing which test method junit will run first, but it doesn't
|
||||
* matter; whichever one of them runs first will modify the system properties.
|
||||
* If the PathableTestSuite isn't resetting the system properties then whichever
|
||||
* of them runs second will fail. Of course if other methods are run in-between
|
||||
* then those methods might also fail...
|
||||
*/
|
||||
public void testResetProps1() {
|
||||
checkAndSetProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* See testResetProps1.
|
||||
*/
|
||||
public void testResetProps2() {
|
||||
checkAndSetProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the context classloader is a custom one, then reset it to
|
||||
* a non-custom one.
|
||||
*/
|
||||
private static void checkAndSetContext() {
|
||||
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||
assertEquals("ContextLoader is of unexpected type",
|
||||
contextLoader.getClass().getName(),
|
||||
PathableClassLoader.class.getName());
|
||||
|
||||
final URL[] noUrls = new URL[0];
|
||||
Thread.currentThread().setContextClassLoader(new URLClassLoader(noUrls));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that when a test method modifies the context classloader it is
|
||||
* reset before the next test is run.
|
||||
@@ -117,4 +95,26 @@ public class GeneralTestCase extends TestCase {
|
||||
public void testResetContext2() {
|
||||
checkAndSetContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that when a test method modifies the system properties they are
|
||||
* reset before the next test is run.
|
||||
* <p>
|
||||
* This method works in conjunction with testResetProps2. There is no
|
||||
* way of knowing which test method junit will run first, but it doesn't
|
||||
* matter; whichever one of them runs first will modify the system properties.
|
||||
* If the PathableTestSuite isn't resetting the system properties then whichever
|
||||
* of them runs second will fail. Of course if other methods are run in-between
|
||||
* then those methods might also fail...
|
||||
*/
|
||||
public void testResetProps1() {
|
||||
checkAndSetProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* See testResetProps1.
|
||||
*/
|
||||
public void testResetProps2() {
|
||||
checkAndSetProperties();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,6 +89,19 @@ public class ParentFirstTestCase extends TestCase {
|
||||
return new PathableTestSuite(testClass, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to convert an enumeration-of-URLs into an array of URLs.
|
||||
*/
|
||||
private static URL[] toURLArray(final Enumeration e) {
|
||||
final ArrayList l = new ArrayList();
|
||||
while (e.hasMoreElements()) {
|
||||
final URL u = (URL) e.nextElement();
|
||||
l.add(u);
|
||||
}
|
||||
final URL[] tmp = new URL[l.size()];
|
||||
return (URL[]) l.toArray(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to return the set of all classloaders in the
|
||||
* parent chain starting from the one that loaded the class for
|
||||
@@ -218,6 +231,34 @@ public class ParentFirstTestCase extends TestCase {
|
||||
resource.toString().indexOf("/commons-logging-1.") > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that getResourceAsStream works.
|
||||
*/
|
||||
public void testResourceAsStream() throws Exception {
|
||||
java.io.InputStream is;
|
||||
|
||||
// verify the classloader hierarchy
|
||||
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||
final ClassLoader childLoader = contextLoader.getParent();
|
||||
final ClassLoader parentLoader = childLoader.getParent();
|
||||
final ClassLoader bootLoader = parentLoader.getParent();
|
||||
assertNull("Unexpected classloader hierarchy", bootLoader);
|
||||
|
||||
// getResourceAsStream where no instances exist
|
||||
is = childLoader.getResourceAsStream("nosuchfile");
|
||||
assertNull("Invalid resource returned non-null stream", is);
|
||||
|
||||
// getResourceAsStream where resource does exist
|
||||
is = childLoader.getResourceAsStream("org/apache/commons/logging/Log.class");
|
||||
assertNotNull("Null returned for valid resource", is);
|
||||
is.close();
|
||||
|
||||
// It would be nice to test parent-first ordering here, but that would require
|
||||
// having a resource with the same name in both the parent and child loaders,
|
||||
// but with different contents. That's a little tricky to set up so we'll
|
||||
// skip that for now.
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the various flavours of ClassLoader.getResources work as expected.
|
||||
*/
|
||||
@@ -265,45 +306,4 @@ public class ParentFirstTestCase extends TestCase {
|
||||
urlsToStrings[1].indexOf("/commons-logging-adapters-1.") > 0);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to convert an enumeration-of-URLs into an array of URLs.
|
||||
*/
|
||||
private static URL[] toURLArray(final Enumeration e) {
|
||||
final ArrayList l = new ArrayList();
|
||||
while (e.hasMoreElements()) {
|
||||
final URL u = (URL) e.nextElement();
|
||||
l.add(u);
|
||||
}
|
||||
final URL[] tmp = new URL[l.size()];
|
||||
return (URL[]) l.toArray(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that getResourceAsStream works.
|
||||
*/
|
||||
public void testResourceAsStream() throws Exception {
|
||||
java.io.InputStream is;
|
||||
|
||||
// verify the classloader hierarchy
|
||||
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||
final ClassLoader childLoader = contextLoader.getParent();
|
||||
final ClassLoader parentLoader = childLoader.getParent();
|
||||
final ClassLoader bootLoader = parentLoader.getParent();
|
||||
assertNull("Unexpected classloader hierarchy", bootLoader);
|
||||
|
||||
// getResourceAsStream where no instances exist
|
||||
is = childLoader.getResourceAsStream("nosuchfile");
|
||||
assertNull("Invalid resource returned non-null stream", is);
|
||||
|
||||
// getResourceAsStream where resource does exist
|
||||
is = childLoader.getResourceAsStream("org/apache/commons/logging/Log.class");
|
||||
assertNotNull("Null returned for valid resource", is);
|
||||
is.close();
|
||||
|
||||
// It would be nice to test parent-first ordering here, but that would require
|
||||
// having a resource with the same name in both the parent and child loaders,
|
||||
// but with different contents. That's a little tricky to set up so we'll
|
||||
// skip that for now.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ import java.security.Permissions;
|
||||
*/
|
||||
public class MockSecurityManager extends SecurityManager {
|
||||
|
||||
private final Permissions permissions = new Permissions();
|
||||
private static final Permission setSecurityManagerPerm =
|
||||
new RuntimePermission("setSecurityManager");
|
||||
private final Permissions permissions = new Permissions();
|
||||
|
||||
private int untrustedCodeCount;
|
||||
|
||||
@@ -46,18 +46,6 @@ public class MockSecurityManager extends SecurityManager {
|
||||
permissions.add(p);
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the number of times that a check of a permission failed
|
||||
* due to stack-walking tracing up into untrusted code. Any non-zero
|
||||
* value indicates a bug in JCL, ie a situation where code was not
|
||||
* correctly wrapped in an AccessController block. The result of such a
|
||||
* bug is that signing JCL is not sufficient to allow JCL to perform
|
||||
* the operation; the caller would need to be signed too.
|
||||
*/
|
||||
public int getUntrustedCodeCount() {
|
||||
return untrustedCodeCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkPermission(final Permission p) throws SecurityException {
|
||||
if (setSecurityManagerPerm.implies(p)) {
|
||||
@@ -147,4 +135,16 @@ public class MockSecurityManager extends SecurityManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the number of times that a check of a permission failed
|
||||
* due to stack-walking tracing up into untrusted code. Any non-zero
|
||||
* value indicates a bug in JCL, ie a situation where code was not
|
||||
* correctly wrapped in an AccessController block. The result of such a
|
||||
* bug is that signing JCL is not sufficient to allow JCL to perform
|
||||
* the operation; the caller would need to be signed too.
|
||||
*/
|
||||
public int getUntrustedCodeCount() {
|
||||
return untrustedCodeCount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,8 +43,6 @@ import org.apache.commons.logging.PathableTestSuite;
|
||||
*/
|
||||
public class SecurityAllowedTestCase extends TestCase
|
||||
{
|
||||
private SecurityManager oldSecMgr;
|
||||
|
||||
// Dummy special hashtable, so we can tell JCL to use this instead of
|
||||
// the standard one.
|
||||
public static class CustomHashtable extends Hashtable {
|
||||
@@ -69,6 +67,8 @@ public class SecurityAllowedTestCase extends TestCase
|
||||
return new PathableTestSuite(testClass, parent);
|
||||
}
|
||||
|
||||
private SecurityManager oldSecMgr;
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
// save security manager so it can be restored in tearDown
|
||||
|
||||
@@ -48,9 +48,6 @@ import org.apache.commons.logging.PathableTestSuite;
|
||||
*/
|
||||
public class SecurityForbiddenTestCase extends TestCase
|
||||
{
|
||||
private SecurityManager oldSecMgr;
|
||||
private ClassLoader otherClassLoader;
|
||||
|
||||
// Dummy special hashtable, so we can tell JCL to use this instead of
|
||||
// the standard one.
|
||||
public static class CustomHashtable extends Hashtable {
|
||||
@@ -60,7 +57,6 @@ public class SecurityForbiddenTestCase extends TestCase
|
||||
*/
|
||||
private static final long serialVersionUID = 7224652794746236024L;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the tests included in this test suite.
|
||||
*/
|
||||
@@ -75,6 +71,27 @@ public class SecurityForbiddenTestCase extends TestCase
|
||||
return new PathableTestSuite(testClass, parent);
|
||||
}
|
||||
|
||||
private SecurityManager oldSecMgr;
|
||||
|
||||
private ClassLoader otherClassLoader;
|
||||
|
||||
/**
|
||||
* Loads a class with the given classloader.
|
||||
*/
|
||||
private Object loadClass(final String name, final ClassLoader classLoader) {
|
||||
try {
|
||||
final Class clazz = classLoader.loadClass(name);
|
||||
final Object obj = clazz.getConstructor().newInstance();
|
||||
return obj;
|
||||
} catch ( final Exception e ) {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final PrintWriter pw = new PrintWriter(sw);
|
||||
e.printStackTrace(pw);
|
||||
fail("Unexpected exception:" + e.getMessage() + ":" + sw.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
// save security manager so it can be restored in tearDown
|
||||
@@ -175,21 +192,4 @@ public class SecurityForbiddenTestCase extends TestCase
|
||||
fail("Unexpected exception:" + t.getMessage() + ":" + sw.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a class with the given classloader.
|
||||
*/
|
||||
private Object loadClass(final String name, final ClassLoader classLoader) {
|
||||
try {
|
||||
final Class clazz = classLoader.loadClass(name);
|
||||
final Object obj = clazz.getConstructor().newInstance();
|
||||
return obj;
|
||||
} catch ( final Exception e ) {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final PrintWriter pw = new PrintWriter(sw);
|
||||
e.printStackTrace(pw);
|
||||
fail("Unexpected exception:" + e.getMessage() + ":" + sw.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,52 +37,6 @@ import org.apache.commons.logging.impl.SimpleLog;
|
||||
*/
|
||||
public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
|
||||
/**
|
||||
* <p>The expected log records.</p>
|
||||
*/
|
||||
protected List expected;
|
||||
|
||||
/**
|
||||
* <p>The message levels that should have been logged.</p>
|
||||
*/
|
||||
/*
|
||||
protected Level[] testLevels =
|
||||
{ Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE };
|
||||
*/
|
||||
|
||||
/**
|
||||
* <p>The message strings that should have been logged.</p>
|
||||
*/
|
||||
protected String[] testMessages =
|
||||
{ "debug", "info", "warn", "error", "fatal" };
|
||||
|
||||
/**
|
||||
* Set system properties that will control the LogFactory/Log objects
|
||||
* when they are created. Subclasses can override this method to
|
||||
* define properties that suit them.
|
||||
*/
|
||||
@Override
|
||||
public void setProperties() {
|
||||
System.setProperty(
|
||||
"org.apache.commons.logging.Log",
|
||||
"org.apache.commons.logging.simple.DecoratedSimpleLog");
|
||||
System.setProperty(
|
||||
"org.apache.commons.logging.simplelog.defaultlog",
|
||||
"debug");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
LogFactory.releaseAll();
|
||||
setProperties();
|
||||
expected = new ArrayList();
|
||||
setUpFactory();
|
||||
setUpLog("DecoratedLogger");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the tests included in this test suite.
|
||||
* <p>
|
||||
@@ -108,37 +62,23 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down instance variables required by this test case.
|
||||
* <p>The message levels that should have been logged.</p>
|
||||
*/
|
||||
@Override
|
||||
public void tearDown() {
|
||||
super.tearDown();
|
||||
expected = null;
|
||||
}
|
||||
/*
|
||||
protected Level[] testLevels =
|
||||
{ Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE };
|
||||
*/
|
||||
|
||||
// Test logging message strings with exceptions
|
||||
public void testExceptionMessages() throws Exception {
|
||||
((DecoratedSimpleLog) log).clearCache();
|
||||
logExceptionMessages();
|
||||
checkExpected();
|
||||
}
|
||||
/**
|
||||
* <p>The expected log records.</p>
|
||||
*/
|
||||
protected List expected;
|
||||
|
||||
// Test logging plain message strings
|
||||
public void testPlainMessages() throws Exception {
|
||||
((DecoratedSimpleLog) log).clearCache();
|
||||
logPlainMessages();
|
||||
checkExpected();
|
||||
}
|
||||
|
||||
// Test Serializability of standard instance
|
||||
@Override
|
||||
public void testSerializable() throws Exception {
|
||||
((DecoratedSimpleLog) log).clearCache();
|
||||
logPlainMessages();
|
||||
super.testSerializable();
|
||||
logExceptionMessages();
|
||||
checkExpected();
|
||||
}
|
||||
/**
|
||||
* <p>The message strings that should have been logged.</p>
|
||||
*/
|
||||
protected String[] testMessages =
|
||||
{ "debug", "info", "warn", "error", "fatal" };
|
||||
|
||||
// Check the decorated log instance
|
||||
@Override
|
||||
@@ -169,19 +109,12 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
|
||||
}
|
||||
|
||||
/** Hook for subclassses */
|
||||
protected void checkShowDateTime() {
|
||||
assertFalse(((DecoratedSimpleLog) log).getShowDateTime());
|
||||
}
|
||||
|
||||
/** Hook for subclasses */
|
||||
protected void checkDecoratedDateTime() {
|
||||
assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
|
||||
((DecoratedSimpleLog) log).getDateTimeFormat());
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Check the actual log records against the expected ones
|
||||
protected void checkExpected() {
|
||||
final List acts = ((DecoratedSimpleLog) log).getCache();
|
||||
@@ -196,6 +129,10 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/** Hook for subclassses */
|
||||
protected void checkShowDateTime() {
|
||||
assertFalse(((DecoratedSimpleLog) log).getShowDateTime());
|
||||
}
|
||||
|
||||
// Check the standard log instance
|
||||
@Override
|
||||
@@ -203,7 +140,6 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
checkDecorated();
|
||||
}
|
||||
|
||||
|
||||
// Log the messages with exceptions
|
||||
protected void logExceptionMessages() {
|
||||
// Generate log records
|
||||
@@ -223,7 +159,6 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", t));
|
||||
}
|
||||
|
||||
|
||||
// Log the plain messages
|
||||
protected void logPlainMessages() {
|
||||
// Generate log records
|
||||
@@ -241,4 +176,69 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||
expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", null));
|
||||
expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set system properties that will control the LogFactory/Log objects
|
||||
* when they are created. Subclasses can override this method to
|
||||
* define properties that suit them.
|
||||
*/
|
||||
@Override
|
||||
public void setProperties() {
|
||||
System.setProperty(
|
||||
"org.apache.commons.logging.Log",
|
||||
"org.apache.commons.logging.simple.DecoratedSimpleLog");
|
||||
System.setProperty(
|
||||
"org.apache.commons.logging.simplelog.defaultlog",
|
||||
"debug");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
LogFactory.releaseAll();
|
||||
setProperties();
|
||||
expected = new ArrayList();
|
||||
setUpFactory();
|
||||
setUpLog("DecoratedLogger");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Tear down instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void tearDown() {
|
||||
super.tearDown();
|
||||
expected = null;
|
||||
}
|
||||
|
||||
|
||||
// Test logging message strings with exceptions
|
||||
public void testExceptionMessages() throws Exception {
|
||||
((DecoratedSimpleLog) log).clearCache();
|
||||
logExceptionMessages();
|
||||
checkExpected();
|
||||
}
|
||||
|
||||
|
||||
// Test logging plain message strings
|
||||
public void testPlainMessages() throws Exception {
|
||||
((DecoratedSimpleLog) log).clearCache();
|
||||
logPlainMessages();
|
||||
checkExpected();
|
||||
}
|
||||
|
||||
|
||||
// Test Serializability of standard instance
|
||||
@Override
|
||||
public void testSerializable() throws Exception {
|
||||
((DecoratedSimpleLog) log).clearCache();
|
||||
logPlainMessages();
|
||||
super.testSerializable();
|
||||
logExceptionMessages();
|
||||
checkExpected();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,28 @@ public class DateTimeCustomConfigTestCase extends CustomConfigTestCase {
|
||||
}
|
||||
|
||||
|
||||
/** Checks that the date time format has been successfully set */
|
||||
@Override
|
||||
protected void checkDecoratedDateTime() {
|
||||
assertEquals("Expected date format to be set", "dd.mm.yyyy",
|
||||
((DecoratedSimpleLog) log).getDateTimeFormat());
|
||||
// try the formatter
|
||||
final Date now = new Date();
|
||||
final DateFormat formatter = ((DecoratedSimpleLog) log).getDateTimeFormatter();
|
||||
final SimpleDateFormat sampleFormatter = new SimpleDateFormat("dd.mm.yyyy");
|
||||
assertEquals("Date should be formatters to pattern dd.mm.yyyy",
|
||||
sampleFormatter.format(now), formatter.format(now));
|
||||
}
|
||||
|
||||
/** Hook for subclassses */
|
||||
@Override
|
||||
protected void checkShowDateTime() {
|
||||
assertTrue(((DecoratedSimpleLog) log).getShowDateTime());
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------- Methods
|
||||
|
||||
/**
|
||||
* Set up system properties required by this unit test. Here, we
|
||||
* set up the props defined in the parent class setProperties method,
|
||||
@@ -84,26 +106,4 @@ public class DateTimeCustomConfigTestCase extends CustomConfigTestCase {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------- Methods
|
||||
|
||||
/** Checks that the date time format has been successfully set */
|
||||
@Override
|
||||
protected void checkDecoratedDateTime() {
|
||||
assertEquals("Expected date format to be set", "dd.mm.yyyy",
|
||||
((DecoratedSimpleLog) log).getDateTimeFormat());
|
||||
// try the formatter
|
||||
final Date now = new Date();
|
||||
final DateFormat formatter = ((DecoratedSimpleLog) log).getDateTimeFormatter();
|
||||
final SimpleDateFormat sampleFormatter = new SimpleDateFormat("dd.mm.yyyy");
|
||||
assertEquals("Date should be formatters to pattern dd.mm.yyyy",
|
||||
sampleFormatter.format(now), formatter.format(now));
|
||||
}
|
||||
|
||||
/** Hook for subclassses */
|
||||
@Override
|
||||
protected void checkShowDateTime() {
|
||||
assertTrue(((DecoratedSimpleLog) log).getShowDateTime());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,58 +40,17 @@ public class DecoratedSimpleLog extends SimpleLog {
|
||||
*/
|
||||
private static final long serialVersionUID = 196544280770017153L;
|
||||
|
||||
public DecoratedSimpleLog(final String name) {
|
||||
super(name);
|
||||
}
|
||||
// Cache of logged records
|
||||
protected ArrayList cache = new ArrayList();
|
||||
|
||||
|
||||
// ------------------------------------------------------------- Properties
|
||||
|
||||
public DateFormat getDateTimeFormatter() {
|
||||
return dateFormatter;
|
||||
public DecoratedSimpleLog(final String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
|
||||
public String getDateTimeFormat() {
|
||||
return dateTimeFormat;
|
||||
}
|
||||
|
||||
|
||||
public String getLogName() {
|
||||
return logName;
|
||||
}
|
||||
|
||||
|
||||
public boolean getShowDateTime() {
|
||||
return showDateTime;
|
||||
}
|
||||
|
||||
|
||||
public boolean getShowShortName() {
|
||||
return showShortName;
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------- Protected Methods
|
||||
|
||||
|
||||
// Cache logged messages
|
||||
@Override
|
||||
protected void log(final int type, final Object message, final Throwable t) {
|
||||
|
||||
super.log(type, message, t);
|
||||
cache.add(new LogRecord(type, message, t));
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------- Public Methods
|
||||
|
||||
|
||||
// Cache of logged records
|
||||
protected ArrayList cache = new ArrayList();
|
||||
|
||||
|
||||
// Clear cache
|
||||
public void clearCache() {
|
||||
cache.clear();
|
||||
@@ -104,4 +63,45 @@ public class DecoratedSimpleLog extends SimpleLog {
|
||||
}
|
||||
|
||||
|
||||
public String getDateTimeFormat() {
|
||||
return dateTimeFormat;
|
||||
}
|
||||
|
||||
|
||||
public DateFormat getDateTimeFormatter() {
|
||||
return dateFormatter;
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------- Protected Methods
|
||||
|
||||
|
||||
public String getLogName() {
|
||||
return logName;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------- Public Methods
|
||||
|
||||
|
||||
public boolean getShowDateTime() {
|
||||
return showDateTime;
|
||||
}
|
||||
|
||||
|
||||
public boolean getShowShortName() {
|
||||
return showShortName;
|
||||
}
|
||||
|
||||
|
||||
// Cache logged messages
|
||||
@Override
|
||||
protected void log(final int type, final Object message, final Throwable t) {
|
||||
|
||||
super.log(type, message, t);
|
||||
cache.add(new LogRecord(type, message, t));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -40,16 +40,6 @@ import org.apache.commons.logging.impl.SimpleLog;
|
||||
|
||||
public class DefaultConfigTestCase extends TestCase {
|
||||
|
||||
/**
|
||||
* <p>The {@link LogFactory} implementation we have selected.</p>
|
||||
*/
|
||||
protected LogFactory factory;
|
||||
|
||||
/**
|
||||
* <p>The {@link Log} implementation we have selected.</p>
|
||||
*/
|
||||
protected Log log;
|
||||
|
||||
/**
|
||||
* Return the tests included in this test suite.
|
||||
* <p>
|
||||
@@ -75,78 +65,14 @@ public class DefaultConfigTestCase extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set system properties that will control the LogFactory/Log objects
|
||||
* when they are created. Subclasses can override this method to
|
||||
* define properties that suit them.
|
||||
* <p>The {@link LogFactory} implementation we have selected.</p>
|
||||
*/
|
||||
public void setProperties() {
|
||||
System.setProperty(
|
||||
"org.apache.commons.logging.Log",
|
||||
"org.apache.commons.logging.impl.SimpleLog");
|
||||
}
|
||||
protected LogFactory factory;
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
* <p>The {@link Log} implementation we have selected.</p>
|
||||
*/
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
LogFactory.releaseAll();
|
||||
setProperties();
|
||||
setUpFactory();
|
||||
setUpLog("TestLogger");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void tearDown() {
|
||||
log = null;
|
||||
factory = null;
|
||||
LogFactory.releaseAll();
|
||||
}
|
||||
|
||||
// Test pristine DecoratedSimpleLog instance
|
||||
public void testPristineDecorated() {
|
||||
setUpDecorated("DecoratedLogger");
|
||||
checkDecorated();
|
||||
}
|
||||
|
||||
// Test pristine Log instance
|
||||
public void testPristineLog() {
|
||||
checkStandard();
|
||||
}
|
||||
|
||||
// Test pristine LogFactory instance
|
||||
public void testPristineFactory() {
|
||||
assertNotNull("LogFactory exists", factory);
|
||||
assertEquals("LogFactory class",
|
||||
"org.apache.commons.logging.impl.LogFactoryImpl",
|
||||
factory.getClass().getName());
|
||||
|
||||
final String[] names = factory.getAttributeNames();
|
||||
assertNotNull("Names exists", names);
|
||||
assertEquals("Names empty", 0, names.length);
|
||||
}
|
||||
|
||||
// Test Serializability of standard instance
|
||||
public void testSerializable() throws Exception {
|
||||
|
||||
// Serialize and deserialize the instance
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(log);
|
||||
oos.close();
|
||||
final ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream(baos.toByteArray());
|
||||
final ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
log = (Log) ois.readObject();
|
||||
ois.close();
|
||||
|
||||
// Check the characteristics of the resulting object
|
||||
checkStandard();
|
||||
|
||||
}
|
||||
protected Log log;
|
||||
|
||||
// Check the decorated log instance
|
||||
protected void checkDecorated() {
|
||||
@@ -175,7 +101,6 @@ public class DefaultConfigTestCase extends TestCase {
|
||||
assertTrue(((DecoratedSimpleLog) log).getShowShortName());
|
||||
}
|
||||
|
||||
|
||||
// Check the standard log instance
|
||||
protected void checkStandard() {
|
||||
assertNotNull("Log exists", log);
|
||||
@@ -195,22 +120,97 @@ public class DefaultConfigTestCase extends TestCase {
|
||||
assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set system properties that will control the LogFactory/Log objects
|
||||
* when they are created. Subclasses can override this method to
|
||||
* define properties that suit them.
|
||||
*/
|
||||
public void setProperties() {
|
||||
System.setProperty(
|
||||
"org.apache.commons.logging.Log",
|
||||
"org.apache.commons.logging.impl.SimpleLog");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
LogFactory.releaseAll();
|
||||
setProperties();
|
||||
setUpFactory();
|
||||
setUpLog("TestLogger");
|
||||
}
|
||||
|
||||
// Set up decorated log instance
|
||||
protected void setUpDecorated(final String name) {
|
||||
log = new DecoratedSimpleLog(name);
|
||||
}
|
||||
|
||||
|
||||
// Set up factory instance
|
||||
protected void setUpFactory() throws Exception {
|
||||
factory = LogFactory.getFactory();
|
||||
}
|
||||
|
||||
|
||||
// Set up log instance
|
||||
protected void setUpLog(final String name) throws Exception {
|
||||
log = LogFactory.getLog(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down instance variables required by this test case.
|
||||
*/
|
||||
@Override
|
||||
public void tearDown() {
|
||||
log = null;
|
||||
factory = null;
|
||||
LogFactory.releaseAll();
|
||||
}
|
||||
|
||||
|
||||
// Test pristine DecoratedSimpleLog instance
|
||||
public void testPristineDecorated() {
|
||||
setUpDecorated("DecoratedLogger");
|
||||
checkDecorated();
|
||||
}
|
||||
|
||||
|
||||
// Test pristine LogFactory instance
|
||||
public void testPristineFactory() {
|
||||
assertNotNull("LogFactory exists", factory);
|
||||
assertEquals("LogFactory class",
|
||||
"org.apache.commons.logging.impl.LogFactoryImpl",
|
||||
factory.getClass().getName());
|
||||
|
||||
final String[] names = factory.getAttributeNames();
|
||||
assertNotNull("Names exists", names);
|
||||
assertEquals("Names empty", 0, names.length);
|
||||
}
|
||||
|
||||
|
||||
// Test pristine Log instance
|
||||
public void testPristineLog() {
|
||||
checkStandard();
|
||||
}
|
||||
|
||||
|
||||
// Test Serializability of standard instance
|
||||
public void testSerializable() throws Exception {
|
||||
|
||||
// Serialize and deserialize the instance
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(log);
|
||||
oos.close();
|
||||
final ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream(baos.toByteArray());
|
||||
final ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
log = (Log) ois.readObject();
|
||||
ois.close();
|
||||
|
||||
// Check the characteristics of the resulting object
|
||||
checkStandard();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,14 +30,14 @@ public class LogRecord implements Serializable {
|
||||
*/
|
||||
private static final long serialVersionUID = -5254831759209770665L;
|
||||
|
||||
public int type;
|
||||
|
||||
public Object message;
|
||||
public Throwable t;
|
||||
public LogRecord(final int type, final Object message, final Throwable t) {
|
||||
this.type = type;
|
||||
this.message = message;
|
||||
this.t = t;
|
||||
}
|
||||
|
||||
public int type;
|
||||
public Object message;
|
||||
public Throwable t;
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,23 @@ public class MyLog implements Log {
|
||||
|
||||
public MyLog(final String category) {}
|
||||
|
||||
@Override
|
||||
public void debug(final Object message) {}
|
||||
@Override
|
||||
public void debug(final Object message, final Throwable t) {}
|
||||
@Override
|
||||
public void error(final Object message) {}
|
||||
@Override
|
||||
public void error(final Object message, final Throwable t) {}
|
||||
@Override
|
||||
public void fatal(final Object message) {}
|
||||
@Override
|
||||
public void fatal(final Object message, final Throwable t) {}
|
||||
|
||||
@Override
|
||||
public void info(final Object message) {}
|
||||
@Override
|
||||
public void info(final Object message, final Throwable t) {}
|
||||
@Override
|
||||
public boolean isDebugEnabled() { return false; }
|
||||
@Override
|
||||
@@ -34,29 +51,12 @@ public class MyLog implements Log {
|
||||
public boolean isTraceEnabled() { return false; }
|
||||
@Override
|
||||
public boolean isWarnEnabled() { return false; }
|
||||
|
||||
@Override
|
||||
public void trace(final Object message) {}
|
||||
@Override
|
||||
public void trace(final Object message, final Throwable t) {}
|
||||
@Override
|
||||
public void debug(final Object message) {}
|
||||
@Override
|
||||
public void debug(final Object message, final Throwable t) {}
|
||||
@Override
|
||||
public void info(final Object message) {}
|
||||
@Override
|
||||
public void info(final Object message, final Throwable t) {}
|
||||
@Override
|
||||
public void warn(final Object message) {}
|
||||
@Override
|
||||
public void warn(final Object message, final Throwable t) {}
|
||||
@Override
|
||||
public void error(final Object message) {}
|
||||
@Override
|
||||
public void error(final Object message, final Throwable t) {}
|
||||
@Override
|
||||
public void fatal(final Object message) {}
|
||||
@Override
|
||||
public void fatal(final Object message, final Throwable t) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user