Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java')
-rw-r--r--jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java201
1 files changed, 78 insertions, 123 deletions
diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java
index 5386e09eec..d51099d239 100644
--- a/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java
+++ b/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java
@@ -29,7 +29,6 @@ import java.io.UnsupportedEncodingException;
import java.util.Properties;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.Test;
/**
@@ -37,12 +36,6 @@ import org.junit.Test;
*/
public class StdErrLogTest
{
- @Before
- public void before()
- {
- Thread.currentThread().setName("tname");
- }
-
@Test
public void testStdErrLogFormat() throws UnsupportedEncodingException
{
@@ -57,15 +50,14 @@ public class StdErrLogTest
log.info("testing:{}",null,null);
log.info("testing",null,null);
- System.err.println(output);
- output.assertContains("INFO:oejul.LogTest:tname: testing:test,format1");
- output.assertContains("INFO:oejul.LogTest:tname: testing:test,format1");
- output.assertContains("INFO:oejul.LogTest:tname: testing:test format2");
- output.assertContains("INFO:oejul.LogTest:tname: testing test format3");
- output.assertContains("INFO:oejul.LogTest:tname: testing:test,null");
- output.assertContains("INFO:oejul.LogTest:tname: testing null null");
- output.assertContains("INFO:oejul.LogTest:tname: testing:null");
- output.assertContains("INFO:oejul.LogTest:tname: testing");
+ output.assertContains("INFO:oejul.LogTest:testing:test,format1");
+ output.assertContains("INFO:oejul.LogTest:testing:test,format1");
+ output.assertContains("INFO:oejul.LogTest:testing:test format2");
+ output.assertContains("INFO:oejul.LogTest:testing test format3");
+ output.assertContains("INFO:oejul.LogTest:testing:test,null");
+ output.assertContains("INFO:oejul.LogTest:testing null null");
+ output.assertContains("INFO:oejul.LogTest:testing:null");
+ output.assertContains("INFO:oejul.LogTest:testing");
}
@Test
@@ -73,14 +65,14 @@ public class StdErrLogTest
{
StdErrLog log = new StdErrLog("xxx",new Properties());
StdErrCapture output = new StdErrCapture(log);
-
+
log.setLevel(StdErrLog.LEVEL_DEBUG);
log.debug("testing {} {}","test","debug");
log.info("testing {} {}","test","info");
log.warn("testing {} {}","test","warn");
log.setLevel(StdErrLog.LEVEL_INFO);
log.debug("YOU SHOULD NOT SEE THIS!",null,null);
-
+
// Test for backward compat with old (now deprecated) method
Logger before = log.getLogger("before");
log.setDebugEnabled(true);
@@ -93,59 +85,59 @@ public class StdErrLogTest
before.debug("testing {} {}","test","debug-before-false");
log.debug("testing {} {}","test","debug-deprecated-false");
after.debug("testing {} {}","test","debug-after-false");
-
- output.assertContains("DBUG:xxx:tname: testing test debug");
- output.assertContains("INFO:xxx:tname: testing test info");
- output.assertContains("WARN:xxx:tname: testing test warn");
+
+ output.assertContains("DBUG:xxx:testing test debug");
+ output.assertContains("INFO:xxx:testing test info");
+ output.assertContains("WARN:xxx:testing test warn");
output.assertNotContains("YOU SHOULD NOT SEE THIS!");
- output.assertContains("DBUG:x.before:tname: testing test debug-before");
- output.assertContains("DBUG:xxx:tname: testing test debug-deprecated");
- output.assertContains("DBUG:x.after:tname: testing test debug-after");
- output.assertNotContains("DBUG:x.before:tname: testing test debug-before-false");
- output.assertNotContains("DBUG:xxx:tname: testing test debug-deprecated-false");
- output.assertNotContains("DBUG:x.after:tname: testing test debug-after-false");
+ output.assertContains("DBUG:x.before:testing test debug-before");
+ output.assertContains("DBUG:xxx:testing test debug-deprecated");
+ output.assertContains("DBUG:x.after:testing test debug-after");
+ output.assertNotContains("DBUG:x.before:testing test debug-before-false");
+ output.assertNotContains("DBUG:xxx:testing test debug-deprecated-false");
+ output.assertNotContains("DBUG:x.after:testing test debug-after-false");
}
-
+
@Test
public void testStdErrLogName()
{
StdErrLog log = new StdErrLog("test",new Properties());
log.setPrintLongNames(true);
StdErrCapture output = new StdErrCapture(log);
-
+
Assert.assertThat("Log.name", log.getName(), is("test"));
Logger next=log.getLogger("next");
Assert.assertThat("Log.name(child)", next.getName(), is("test.next"));
next.info("testing {} {}","next","info");
-
- output.assertContains(":test.next:tname: testing next info");
+
+ output.assertContains(":test.next:testing next info");
}
-
+
@Test
public void testStdErrThrowable()
{
// Common Throwable (for test)
Throwable th = new Throwable("Message");
-
+
// Capture raw string form
StringWriter tout = new StringWriter();
th.printStackTrace(new PrintWriter(tout));
String ths = tout.toString();
-
+
// Start test
StdErrLog log = new StdErrLog("test",new Properties());
StdErrCapture output = new StdErrCapture(log);
log.warn("ex",th);
output.assertContains(ths);
-
+
th = new Throwable("Message with \033 escape");
log.warn("ex",th);
output.assertNotContains("Message with \033 escape");
log.info(th.toString());
output.assertNotContains("Message with \033 escape");
-
+
log.warn("ex",th);
output.assertContains("Message with ? escape");
log.info(th.toString());
@@ -194,7 +186,7 @@ public class StdErrLogTest
public void testGetLoggingLevel_Default()
{
Properties props = new Properties();
-
+
// Default Levels
Assert.assertEquals("Default Logging Level",StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,null));
Assert.assertEquals("Default Logging Level",StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,""));
@@ -207,8 +199,8 @@ public class StdErrLogTest
{
Properties props = new Properties();
props.setProperty("log.LEVEL", "WARN");
- props.setProperty("org.eclipse.jetty.bad.LEVEL","EXPECTED_BAD_LEVEL");
-
+ props.setProperty("org.eclipse.jetty.bad.LEVEL","FRUIT");
+
// Default Level (because of bad level value)
Assert.assertEquals("Bad Logging Level",StdErrLog.LEVEL_WARN,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.bad"));
}
@@ -219,7 +211,7 @@ public class StdErrLogTest
Properties props = new Properties();
props.setProperty("log.LEVEL", "warn");
props.setProperty("org.eclipse.jetty.util.LEVEL","info");
-
+
// Default Level
Assert.assertEquals("Lowercase Level",StdErrLog.LEVEL_WARN,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty"));
// Specific Level
@@ -231,7 +223,7 @@ public class StdErrLogTest
{
Properties props = new Properties();
props.setProperty("log.LEVEL","DEBUG");
-
+
// Default Levels
Assert.assertEquals("Default Logging Level",StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,null));
Assert.assertEquals("Default Logging Level",StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,""));
@@ -250,7 +242,7 @@ public class StdErrLogTest
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,null));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,""));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty"));
-
+
// Specified Level
Assert.assertEquals(StdErrLog.LEVEL_ALL,StdErrLog.getLoggingLevel(props,name));
}
@@ -266,7 +258,7 @@ public class StdErrLogTest
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,""));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty"));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.server.BogusObject"));
-
+
// Configured Level
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,StdErrLogTest.class.getName()));
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.util.Bogus"));
@@ -287,7 +279,7 @@ public class StdErrLogTest
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,""));
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty"));
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.server.ServerObject"));
-
+
// Configured Level
Assert.assertEquals(StdErrLog.LEVEL_WARN,StdErrLog.getLoggingLevel(props,StdErrLogTest.class.getName()));
Assert.assertEquals(StdErrLog.LEVEL_WARN,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.util.MagicUtil"));
@@ -306,7 +298,7 @@ public class StdErrLogTest
{
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(),new Properties());
log.setHideStacks(false);
-
+
StdErrCapture output = new StdErrCapture(log);
// Start with default level
@@ -330,7 +322,7 @@ public class StdErrLogTest
output.assertContains("Cheer Me");
// Validate Stack Traces
- output.assertContains(".StdErrLogTest:tname: <zoom>");
+ output.assertContains(".StdErrLogTest:<zoom>");
output.assertContains("java.lang.Throwable: out of focus");
output.assertContains("java.lang.Throwable: scene lost");
}
@@ -372,42 +364,17 @@ public class StdErrLogTest
output.assertContains("this record");
output.assertContains("it is scratched.");
output.assertNotContains("sorry?");
-
+
// Validate Stack Traces
output.assertNotContains("<spoken line>");
output.assertNotContains("on editing room floor");
- output.assertContains(".StdErrLogTest:tname: <zoom>");
+ output.assertContains(".StdErrLogTest:<zoom>");
output.assertContains("java.lang.Throwable: out of focus");
output.assertContains("java.lang.Throwable: scene lost");
}
/**
- * Tests {@link StdErrLog#LEVEL_OFF} filtering.
- */
- @Test
- public void testOffFiltering() throws UnsupportedEncodingException
- {
- StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(),new Properties());
- log.setHideStacks(false);
- log.setLevel(StdErrLog.LEVEL_OFF);
-
- StdErrCapture output = new StdErrCapture(log);
-
- // Various logging events
- log.debug("Squelch");
- log.debug("Squelch", new RuntimeException("Squelch"));
- log.info("Squelch");
- log.info("Squelch", new IllegalStateException("Squelch"));
- log.warn("Squelch");
- log.warn("Squelch", new Exception("Squelch"));
- log.ignore(new Throwable("Squelch"));
-
- // Validate Output
- output.assertNotContains("Squelch");
- }
-
- /**
* Tests StdErrLog.debug() methods with level filtering.
* <p>
* Should only see DEBUG level messages when level is set to {@link StdErrLog#LEVEL_DEBUG} and below.
@@ -427,7 +394,7 @@ public class StdErrLogTest
// Level Debug
log.setLevel(StdErrLog.LEVEL_DEBUG);
log.debug("my hovercraft is");
-
+
log.debug("<zoom>", new Throwable("out of focus"));
log.debug(new Throwable("scene lost"));
@@ -449,8 +416,8 @@ public class StdErrLogTest
// Validate Stack Traces
output.assertNotContains("<spoken line>");
output.assertNotContains("on editing room floor");
-
- output.assertContains(".StdErrLogTest:tname: <zoom>");
+
+ output.assertContains(".StdErrLogTest:<zoom>");
output.assertContains("java.lang.Throwable: out of focus");
output.assertContains("java.lang.Throwable: scene lost");
}
@@ -474,7 +441,7 @@ public class StdErrLogTest
// Show Ignored
log.setLevel(StdErrLog.LEVEL_ALL);
log.ignore(new Throwable("Don't ignore me"));
-
+
// Set to Debug level
log.setLevel(StdErrLog.LEVEL_DEBUG);
log.ignore(new Throwable("Debug me"));
@@ -485,12 +452,12 @@ public class StdErrLogTest
output.assertContains("Don't ignore me");
output.assertNotContains("Debug me");
}
-
+
@Test
public void testIsDebugEnabled() {
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(),new Properties());
log.setHideStacks(true);
-
+
log.setLevel(StdErrLog.LEVEL_ALL);
Assert.assertThat("log.level(all).isDebugEnabled", log.isDebugEnabled(), is(true));
@@ -502,17 +469,14 @@ public class StdErrLogTest
log.setLevel(StdErrLog.LEVEL_WARN);
Assert.assertThat("log.level(warn).isDebugEnabled", log.isDebugEnabled(), is(false));
-
- log.setLevel(StdErrLog.LEVEL_OFF);
- Assert.assertThat("log.level(off).isDebugEnabled", log.isDebugEnabled(), is(false));
}
-
+
@Test
public void testSetGetLevel()
{
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(),new Properties());
log.setHideStacks(true);
-
+
log.setLevel(StdErrLog.LEVEL_ALL);
Assert.assertThat("log.level(all).getLevel()", log.getLevel(), is(StdErrLog.LEVEL_ALL));
@@ -524,33 +488,30 @@ public class StdErrLogTest
log.setLevel(StdErrLog.LEVEL_WARN);
Assert.assertThat("log.level(warn).getLevel()", log.getLevel(), is(StdErrLog.LEVEL_WARN));
-
- log.setLevel(StdErrLog.LEVEL_OFF);
- Assert.assertThat("log.level(off).getLevel()", log.getLevel(), is(StdErrLog.LEVEL_OFF));
}
-
+
@Test
public void testGetChildLogger_Simple()
{
String baseName = "jetty";
StdErrLog log = new StdErrLog(baseName,new Properties());
log.setHideStacks(true);
-
+
Assert.assertThat("Logger.name", log.getName(), is("jetty"));
-
+
Logger log2 = log.getLogger("child");
Assert.assertThat("Logger.child.name", log2.getName(), is("jetty.child"));
}
-
+
@Test
public void testGetChildLogger_Deep()
{
String baseName = "jetty";
StdErrLog log = new StdErrLog(baseName,new Properties());
log.setHideStacks(true);
-
+
Assert.assertThat("Logger.name", log.getName(), is("jetty"));
-
+
Logger log2 = log.getLogger("child.of.the.sixties");
Assert.assertThat("Logger.child.name", log2.getName(), is("jetty.child.of.the.sixties"));
}
@@ -561,11 +522,11 @@ public class StdErrLogTest
String baseName = "jetty";
StdErrLog log = new StdErrLog(baseName,new Properties());
log.setHideStacks(true);
-
+
Assert.assertThat("Logger.name", log.getName(), is("jetty"));
-
+
// Pass null as child reference, should return parent logger
- Logger log2 = log.getLogger((String)null);
+ Logger log2 = log.getLogger(null);
Assert.assertThat("Logger.child.name", log2.getName(), is("jetty"));
Assert.assertSame("Should have returned same logger", log2, log);
}
@@ -576,9 +537,9 @@ public class StdErrLogTest
String baseName = "jetty";
StdErrLog log = new StdErrLog(baseName,new Properties());
log.setHideStacks(true);
-
+
Assert.assertThat("Logger.name", log.getName(), is("jetty"));
-
+
// Pass empty name as child reference, should return parent logger
Logger log2 = log.getLogger("");
Assert.assertThat("Logger.child.name", log2.getName(), is("jetty"));
@@ -591,9 +552,9 @@ public class StdErrLogTest
String baseName = "jetty";
StdErrLog log = new StdErrLog(baseName,new Properties());
log.setHideStacks(true);
-
+
Assert.assertThat("Logger.name", log.getName(), is("jetty"));
-
+
// Pass empty name as child reference, should return parent logger
Logger log2 = log.getLogger(" ");
Assert.assertThat("Logger.child.name", log2.getName(), is("jetty"));
@@ -604,14 +565,14 @@ public class StdErrLogTest
public void testGetChildLogger_NullParent()
{
StdErrLog log = new StdErrLog(null,new Properties());
-
+
Assert.assertThat("Logger.name", log.getName(), is(""));
-
+
Logger log2 = log.getLogger("jetty");
Assert.assertThat("Logger.child.name", log2.getName(), is("jetty"));
Assert.assertNotSame("Should have returned same logger", log2, log);
}
-
+
@Test
public void testToString()
{
@@ -622,39 +583,34 @@ public class StdErrLogTest
log.setLevel(StdErrLog.LEVEL_DEBUG);
Assert.assertThat("Logger.toString", log.toString(), is("StdErrLog:jetty:LEVEL=DEBUG"));
-
+
log.setLevel(StdErrLog.LEVEL_INFO);
Assert.assertThat("Logger.toString", log.toString(), is("StdErrLog:jetty:LEVEL=INFO"));
-
+
log.setLevel(StdErrLog.LEVEL_WARN);
Assert.assertThat("Logger.toString", log.toString(), is("StdErrLog:jetty:LEVEL=WARN"));
-
+
log.setLevel(99); // intentionally bogus level
Assert.assertThat("Logger.toString", log.toString(), is("StdErrLog:jetty:LEVEL=?"));
}
-
+
@Test
public void testPrintSource() throws UnsupportedEncodingException
{
- Properties props=new Properties();
- props.put("test.SOURCE","true");
- StdErrLog log = new StdErrLog("test",props);
+ StdErrLog log = new StdErrLog("test",new Properties());
log.setLevel(StdErrLog.LEVEL_DEBUG);
+ log.setSource(true);
ByteArrayOutputStream test = new ByteArrayOutputStream();
PrintStream err = new PrintStream(test);
log.setStdErrStream(err);
-
+
log.debug("Show me the source!");
-
+
String output = new String(test.toByteArray(),"UTF-8");
- // System.err.print(output);
-
- Assert.assertThat(output, containsString(".StdErrLogTest#testPrintSource(StdErrLogTest.java:"));
+ // System.err.print(output);
-
- props.put("test.SOURCE","false");
- log=new StdErrLog("other",props);
+ Assert.assertThat(output, containsString(".StdErrLogTest#testPrintSource(StdErrLogTest.java:"));
}
@Test
@@ -663,14 +619,14 @@ public class StdErrLogTest
Properties props = new Properties();
props.setProperty("org.eclipse.jetty.util.LEVEL","WARN");
props.setProperty("org.eclipse.jetty.io.LEVEL", "WARN");
-
+
StdErrLog root = new StdErrLog("", props);
assertLevel(root,StdErrLog.LEVEL_INFO); // default
StdErrLog log = (StdErrLog)root.getLogger(StdErrLogTest.class.getName());
Assert.assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(false));
assertLevel(log,StdErrLog.LEVEL_WARN); // as configured
-
+
// Boot stomp it all to debug
root.setDebugEnabled(true);
Assert.assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(true));
@@ -681,13 +637,12 @@ public class StdErrLogTest
Assert.assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(false));
assertLevel(log,StdErrLog.LEVEL_WARN); // as configured
}
-
private void assertLevel(StdErrLog log, int expectedLevel)
{
Assert.assertThat("Log[" + log.getName() + "].level",levelToString(log.getLevel()),is(levelToString(expectedLevel)));
}
-
+
private String levelToString(int level)
{
switch (level)

Back to the top