private void assertContains(StringPrintStream str, String expected) throws Error { if (!str.contains(expected)) { throw new AssertionError("expected '" + expected + "' but got '" + str.toString() + "'"); } }
private void assertNotContains(StringPrintStream str, String expected) throws Error { if (str.contains(expected)) { throw new AssertionError("expected absence of '" + expected + "' but got '" + str.toString() + "'"); } } }
public void testConsolePrintingCanBeDisabledButLinesAreStillRecorded() { StringPrintStream console = StringPrintStream.newStream(); PrintStream old = System.err; System.setErr(console); // << have to interfere with the system for this test try { TestLoggerFactory f = Settings.instance().printingEnabled(false).buildLogging(); TestLogger log = f.getLogger("john"); log.error("anError"); assert (!console.contains("anError")); assert (log.contains("anError")); } finally { System.setErr(old); } }
public void testLoggingDefaults() { StringPrintStream console = StringPrintStream.newStream(); PrintStream old = System.err; System.setErr(console); try { TestLoggerFactory f = new TestLoggerFactory(); TestLogger log = f.getLogger("john"); log.error("anError"); log.info("someInfo"); assert (console.contains("anError")); assert (!console.contains("someInfo")); assert (log.contains("anError")); assert (!log.contains("someInfo")); assert (console.matches(".*anError.*")); assert (!console.matches(".*someInfo.*")); assert (log.matches(".*anError.*")); assert (!log.matches(".*someInfo.*")); } finally { System.setErr(old); } }
public void testPrintStreamCanBeOverridden() { StringPrintStream ps = StringPrintStream.newStream(); TestLoggerFactory f = Settings.instance() .redirectPrintStream(LogLevel.ErrorLevel, ps).buildLogging(); TestLogger log = f.getLogger("john"); log.error("anError"); assert (ps.contains("anError")); assert (log.contains("anError")); }
public void testAssociatedPrintingCanBeDisabledButLinesAreStillRecorded() { StringPrintStream ps = StringPrintStream.newStream(); TestLoggerFactory loggerFactory = Settings.instance(). printingEnabled(false).redirectPrintStream(LogLevel.ErrorLevel, ps). buildLogging(); TestLogger log = loggerFactory.getLogger("john"); log.error("anError"); assert (!ps.contains("anError")); assert (log.contains("anError")); }