Making fixes to StdErrLogTest

This commit is contained in:
Joakim Erdfelt 2011-10-28 09:11:21 -07:00
parent f510e0daf9
commit c174304996
3 changed files with 44 additions and 51 deletions

View File

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

View File

@ -1,43 +1,24 @@
package org.eclipse.jetty.util.log;
import static org.hamcrest.Matchers.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class NamedLogTest
{
private PrintStream orig;
private ByteArrayOutputStream logstream;
private PrintStream perr;
private Logger origLogger;
private static Logger originalLogger;
@Before
public void setUp()
@SuppressWarnings("deprecation")
@BeforeClass
public static void rememberOriginalLogger()
{
origLogger = Log.getRootLogger();
orig = System.err;
logstream = new ByteArrayOutputStream();
perr = new PrintStream(logstream);
System.setErr(perr);
StdErrLog logger = new StdErrLog();
Log.setLog(logger);
originalLogger = Log.getLog();
}
@After
public void tearDown()
@AfterClass
public static void restoreOriginalLogger()
{
System.out.println(logstream.toString());
System.setErr(orig);
Log.setLog(origLogger);
Log.setLog(originalLogger);
}
@Test
@ -47,28 +28,30 @@ public class NamedLogTest
Green green = new Green();
Blue blue = new Blue();
setLoggerOptions(Red.class);
setLoggerOptions(Green.class);
setLoggerOptions(Blue.class);
StdErrCapture output = new StdErrCapture();
setLoggerOptions(Red.class, output);
setLoggerOptions(Green.class, output);
setLoggerOptions(Blue.class, output);
red.generateLogs();
green.generateLogs();
blue.generateLogs();
String rawlog = logstream.toString();
Assert.assertThat(rawlog,containsString(Red.class.getName()));
Assert.assertThat(rawlog,containsString(Green.class.getName()));
Assert.assertThat(rawlog,containsString(Blue.class.getName()));
output.assertContains(Red.class.getName());
output.assertContains(Green.class.getName());
output.assertContains(Blue.class.getName());
}
private void setLoggerOptions(Class<?> clazz)
private void setLoggerOptions(Class<?> clazz, StdErrCapture output)
{
Logger logger = Log.getLogger(clazz);
logger.setDebugEnabled(true);
if(logger instanceof StdErrLog) {
((StdErrLog)logger).setPrintLongNames(true);
StdErrLog sel = (StdErrLog)logger;
sel.setPrintLongNames(true);
output.capture(sel);
}
}
}

View File

@ -14,22 +14,32 @@ public class StdErrCapture
public StdErrCapture(StdErrLog log)
{
test = new ByteArrayOutputStream();
err = new PrintStream(test);
this();
log.setStdErrStream(err);
}
public StdErrCapture()
{
test = new ByteArrayOutputStream();
err = new PrintStream(test);
}
public void capture(StdErrLog log)
{
log.setStdErrStream(err);
}
public void assertContains(String expectedString)
{
err.flush();
String output = new String(test.toByteArray());
Assert.assertThat(output, containsString(expectedString));
Assert.assertThat(output,containsString(expectedString));
}
public void assertNotContains(String unexpectedString)
{
err.flush();
String output = new String(test.toByteArray());
Assert.assertThat(output, not(containsString(unexpectedString)));
Assert.assertThat(output,not(containsString(unexpectedString)));
}
}