Making fixes to StdErrLogTest

This commit is contained in:
Joakim Erdfelt 2011-10-28 09:07:03 -07:00
parent 824e6ebc52
commit f510e0daf9
4 changed files with 92 additions and 55 deletions

View File

@ -37,6 +37,7 @@ public class Slf4jLog implements Logger
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace(System.err);
throw new NoClassDefFoundError("org.slf4j.impl.StaticLoggerBinder");
}

View File

@ -15,7 +15,9 @@ package org.eclipse.jetty.util.log;
import static org.hamcrest.Matchers.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -23,6 +25,21 @@ import org.junit.runner.RunWith;
@RunWith(Slf4jTestJarsRunner.class)
public class LogTest
{
private Logger originalLogger;
@SuppressWarnings("deprecation")
@Before
public void rememberOriginalLogger()
{
originalLogger = Log.getLog();
}
@After
public void restoreOriginalLogger()
{
Log.setLog(originalLogger);
}
@Test
public void testNamedLogNamed_StdErrLog()
{

View File

@ -9,7 +9,9 @@ import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
@ -23,24 +25,25 @@ public class Slf4jTestJarsRunner extends BlockJUnit4ClassRunner
{
super(urls);
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException
{
System.err.printf("[slf4j.cl] loadClass(%s)%n", name);
System.err.printf("[slf4j.cl] loadClass(%s)%n",name);
return super.loadClass(name);
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException
{
System.err.printf("[slf4j.cl] findClass(%s)%n", name);
System.err.printf("[slf4j.cl] findClass(%s)%n",name);
return super.findClass(name);
}
}
private ClassLoader original;
private URLClassLoader slf4jClassLoader;
private boolean foundSlf4jJars = false;
public Slf4jTestJarsRunner(Class<?> klass) throws InitializationError
{
@ -48,7 +51,11 @@ public class Slf4jTestJarsRunner extends BlockJUnit4ClassRunner
original = Thread.currentThread().getContextClassLoader();
File testJarDir = MavenTestingUtils.getTargetFile("test-jars");
Assume.assumeTrue(testJarDir.exists()); // trigger @Ignore if dir not there
if (!testJarDir.exists())
{
System.out.println("Directory not found: " + testJarDir.getAbsolutePath());
return;
}
File jarfiles[] = testJarDir.listFiles(new FileFilter()
{
@ -62,7 +69,13 @@ public class Slf4jTestJarsRunner extends BlockJUnit4ClassRunner
}
});
Assume.assumeTrue(jarfiles.length > 0); // trigger @Ignore if no jar files.
if (jarfiles.length < 0)
{
System.out.println("No slf4j test-jars found");
return;
}
foundSlf4jJars = true;
try
{
@ -75,13 +88,17 @@ public class Slf4jTestJarsRunner extends BlockJUnit4ClassRunner
for (String entry : System.getProperty("java.class.path").split(File.pathSeparator))
{
File path = new File(entry);
if (path.exists())
if (path.exists() && !path.getName().contains("slf4j-api"))
{
urlist.add(path.toURI().toURL());
}
}
URL urls[] = urlist.toArray(new URL[urlist.size()]);
for (URL url : urls)
{
System.out.println("Classpath entry: " + url);
}
slf4jClassLoader = new Slf4jTestClassLoader(urls);
}
@ -91,9 +108,25 @@ public class Slf4jTestJarsRunner extends BlockJUnit4ClassRunner
}
}
private EachTestNotifier makeNotifier(FrameworkMethod method, RunNotifier notifier)
{
Description description = describeChild(method);
return new EachTestNotifier(notifier,description);
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier)
{
if (!foundSlf4jJars)
{
EachTestNotifier eachNotifier = makeNotifier(method,notifier);
if (method.getAnnotation(Ignore.class) != null)
{
eachNotifier.fireTestIgnored();
return;
}
}
try
{
Thread.currentThread().setContextClassLoader(slf4jClassLoader);

View File

@ -54,7 +54,6 @@ public class StdErrLogTest
output.assertContains("INFO:oejul.LogTest:testing");
}
@SuppressWarnings("deprecation")
@Test
public void testStdErrLogDebug()
{
@ -283,10 +282,8 @@ public class StdErrLogTest
{
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setHideStacks(false);
ByteArrayOutputStream test = new ByteArrayOutputStream();
PrintStream err = new PrintStream(test);
log.setStdErrStream(err);
StdErrCapture output = new StdErrCapture(log);
// Start with default level
log.warn("See Me");
@ -303,16 +300,15 @@ public class StdErrLogTest
log.warn(new Throwable("scene lost"));
// Validate Output
String output = new String(test.toByteArray(),"UTF-8");
// System.err.print(output);
Assert.assertThat(output,containsString("See Me"));
Assert.assertThat(output,containsString("Hear Me"));
Assert.assertThat(output,containsString("Cheer Me"));
output.assertContains("See Me");
output.assertContains("Hear Me");
output.assertContains("Cheer Me");
// Validate Stack Traces
Assert.assertThat(output,containsString(".StdErrLogTest:<zoom>"));
Assert.assertThat(output,containsString("java.lang.Throwable: out of focus"));
Assert.assertThat(output,containsString("java.lang.Throwable: scene lost"));
output.assertContains(".StdErrLogTest:<zoom>");
output.assertContains("java.lang.Throwable: out of focus");
output.assertContains("java.lang.Throwable: scene lost");
}
/**
@ -326,9 +322,7 @@ public class StdErrLogTest
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setHideStacks(false);
ByteArrayOutputStream test = new ByteArrayOutputStream();
PrintStream err = new PrintStream(test);
log.setStdErrStream(err);
StdErrCapture output = new StdErrCapture(log);
// Normal/Default behavior
log.info("I will not buy");
@ -350,20 +344,18 @@ public class StdErrLogTest
log.info("<spoken line>", new Throwable("on editing room floor"));
// Validate Output
String output = new String(test.toByteArray(),"UTF-8");
// System.err.print(output);
Assert.assertThat(output,containsString("I will not buy"));
Assert.assertThat(output,containsString("this record"));
Assert.assertThat(output,containsString("it is scratched."));
Assert.assertThat(output,not(containsString("sorry?")));
output.assertContains("I will not buy");
output.assertContains("this record");
output.assertContains("it is scratched.");
output.assertNotContains("sorry?");
// Validate Stack Traces
Assert.assertThat(output,not(containsString("<spoken line>")));
Assert.assertThat(output,not(containsString("on editing room floor")));
output.assertNotContains("<spoken line>");
output.assertNotContains("on editing room floor");
Assert.assertThat(output,containsString(".StdErrLogTest:<zoom>"));
Assert.assertThat(output,containsString("java.lang.Throwable: out of focus"));
Assert.assertThat(output,containsString("java.lang.Throwable: scene lost"));
output.assertContains(".StdErrLogTest:<zoom>");
output.assertContains("java.lang.Throwable: out of focus");
output.assertContains("java.lang.Throwable: scene lost");
}
/**
@ -377,9 +369,7 @@ public class StdErrLogTest
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setHideStacks(true);
ByteArrayOutputStream test = new ByteArrayOutputStream();
PrintStream err = new PrintStream(test);
log.setStdErrStream(err);
StdErrCapture output = new StdErrCapture(log);
// Normal/Default behavior
log.debug("Tobacconist");
@ -401,20 +391,19 @@ public class StdErrLogTest
log.debug("what?");
// Validate Output
String output = new String(test.toByteArray(),"UTF-8");
// System.err.print(output);
Assert.assertThat(output,not(containsString("Tobacconist")));
Assert.assertThat(output,containsString("my hovercraft is"));
Assert.assertThat(output,containsString("full of eels."));
Assert.assertThat(output,not(containsString("what?")));
output.assertNotContains("Tobacconist");
output.assertContains("my hovercraft is");
output.assertContains("full of eels.");
output.assertNotContains("what?");
// Validate Stack Traces
Assert.assertThat(output,not(containsString("<spoken line>")));
Assert.assertThat(output,not(containsString("on editing room floor")));
output.assertNotContains("<spoken line>");
output.assertNotContains("on editing room floor");
Assert.assertThat(output,containsString(".StdErrLogTest:<zoom>"));
Assert.assertThat(output,containsString("java.lang.Throwable: out of focus"));
Assert.assertThat(output,containsString("java.lang.Throwable: scene lost"));
output.assertContains(".StdErrLogTest:<zoom>");
output.assertContains("java.lang.Throwable: out of focus");
output.assertContains("java.lang.Throwable: scene lost");
}
/**
@ -428,9 +417,7 @@ public class StdErrLogTest
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setHideStacks(true);
ByteArrayOutputStream test = new ByteArrayOutputStream();
PrintStream err = new PrintStream(test);
log.setStdErrStream(err);
StdErrCapture output = new StdErrCapture(log);
// Normal/Default behavior
log.ignore(new Throwable("IGNORE ME"));
@ -444,11 +431,10 @@ public class StdErrLogTest
log.ignore(new Throwable("Debug me"));
// Validate Output
String output = new String(test.toByteArray(),"UTF-8");
// System.err.print(output);
Assert.assertThat(output,not(containsString("IGNORE ME")));
Assert.assertThat(output,containsString("Don't ignore me"));
Assert.assertThat(output,not(containsString("Debug me")));
output.assertNotContains("IGNORE ME");
output.assertContains("Don't ignore me");
output.assertNotContains("Debug me");
}
@Test