JUnitLoggerProvider implements ILoggerProvider { public static LoggerEnablement getConsoleLoggerEnablement() { return getInstance().consoleLoggerEnablement.getLoggerEnablementForThread(); } private  JUnitLoggerProvider(); @Override JUnitLoggerComposite get(final String name, final String wrapperFQCN); static void reset(); static JUnitLogger getGlobalLogger(); @SuppressWarnings("unchecked") static JUnitLogger getLogger(final String name, final String wrapperFQCN); static JUnitLogger getLogger(final Class<?> clazz); static LoggerEnablement getConsoleLoggerEnablement(); static JUnitLoggerComposite getLoggerComposite(final String name, final String wrapperFQCN);  }
@Test public void testConsoleLoggerTraceEnablement() { final PrintStream oldOutStream = System.out; ByteArrayOutputStream systemOutStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(systemOutStream)); final String logMessage = "FooMessage4"; loggingClass.logTrace(logMessage); Assert.assertEquals(0, systemOutStream.size()); JUnitLoggerProvider.getConsoleLoggerEnablement().setTraceEnabled(true); loggingClass.logTrace(logMessage); Assert.assertTrue(systemOutStream.toString().contains(logMessage)); systemOutStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(systemOutStream)); JUnitLoggerProvider.getConsoleLoggerEnablement().setTraceEnabled(false); loggingClass.logTrace(logMessage); Assert.assertEquals(0, systemOutStream.size()); System.setOut(oldOutStream); }
@Test public void testConsoleLoggerDebugEnablement() { final PrintStream oldOutStream = System.out; ByteArrayOutputStream systemOutStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(systemOutStream)); final String logMessage = "FooMessage5"; loggingClass.logDebug(logMessage); Assert.assertEquals(0, systemOutStream.size()); JUnitLoggerProvider.getConsoleLoggerEnablement().setDebugEnabled(true); loggingClass.logDebug(logMessage); Assert.assertTrue(systemOutStream.toString().contains(logMessage)); systemOutStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(systemOutStream)); JUnitLoggerProvider.getConsoleLoggerEnablement().setDebugEnabled(false); loggingClass.logDebug(logMessage); Assert.assertEquals(0, systemOutStream.size()); System.setOut(oldOutStream); }
@Test public void testConsoleLoggerInfoEnablement() { final PrintStream oldOutStream = System.out; ByteArrayOutputStream systemOutStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(systemOutStream)); final String logMessage = "FooMessage6"; loggingClass.logInfo(logMessage); Assert.assertEquals(0, systemOutStream.size()); JUnitLoggerProvider.getConsoleLoggerEnablement().setInfoEnabled(true); loggingClass.logInfo(logMessage); Assert.assertTrue(systemOutStream.toString().contains(logMessage)); systemOutStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(systemOutStream)); JUnitLoggerProvider.getConsoleLoggerEnablement().setInfoEnabled(false); loggingClass.logInfo(logMessage); Assert.assertEquals(0, systemOutStream.size()); System.setOut(oldOutStream); }
@Test public void testConsoleLoggerWarnEnablement() { final PrintStream oldOutStream = System.out; ByteArrayOutputStream systemOutStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(systemOutStream)); final String logMessage = "FooMessage7"; loggingClass.logWarning(logMessage); Assert.assertTrue(systemOutStream.toString().contains(logMessage)); systemOutStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(systemOutStream)); JUnitLoggerProvider.getConsoleLoggerEnablement().setWarnEnabled(false); loggingClass.logWarning(logMessage); Assert.assertEquals(0, systemOutStream.size()); systemOutStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(systemOutStream)); JUnitLoggerProvider.getConsoleLoggerEnablement().setWarnEnabled(true); loggingClass.logWarning(logMessage); Assert.assertTrue(systemOutStream.toString().contains(logMessage)); System.setOut(oldOutStream); }
@Test public void testConsoleLoggerErrorEnablement() { final PrintStream oldErrStream = System.err; ByteArrayOutputStream systemErrorStream = new ByteArrayOutputStream(); System.setErr(new PrintStream(systemErrorStream)); final String logMessage = "FooMessage8"; loggingClass.logError(logMessage); Assert.assertTrue(systemErrorStream.toString().contains(logMessage)); systemErrorStream = new ByteArrayOutputStream(); System.setErr(new PrintStream(systemErrorStream)); JUnitLoggerProvider.getConsoleLoggerEnablement().setErrorEnabled(false); loggingClass.logError(logMessage); Assert.assertEquals(0, systemErrorStream.size()); systemErrorStream = new ByteArrayOutputStream(); System.setErr(new PrintStream(systemErrorStream)); JUnitLoggerProvider.getConsoleLoggerEnablement().setErrorEnabled(true); loggingClass.logError(logMessage); Assert.assertTrue(systemErrorStream.toString().contains(logMessage)); System.setErr(oldErrStream); }
@Test public void testConsoleLoggerEnablementForMultipleThread() throws InterruptedException { final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); final LogEnablementRunnable runnable1 = new LogEnablementRunnable(latch1); final LogEnablementRunnable runnable2 = new LogEnablementRunnable(latch2); final LoggerEnablement enablement = JUnitLoggerProvider.getConsoleLoggerEnablement(); enablement.setTraceEnabled(true); new Thread(runnable1).start(); latch1.await(); final LoggerEnablement enablement1 = runnable1.enablement; Assert.assertNotSame(enablement, enablement1); new Thread(runnable2).start(); latch2.await(); final LoggerEnablement enablement2 = runnable2.enablement; Assert.assertNotSame(enablement, enablement2); Assert.assertNotSame(enablement1, enablement2); Assert.assertTrue(enablement.isTraceEnabled()); Assert.assertFalse(enablement1.isTraceEnabled()); Assert.assertFalse(enablement2.isTraceEnabled()); enablement.setTraceEnabled(false); enablement1.setTraceEnabled(true); enablement2.setTraceEnabled(true); Assert.assertFalse(enablement.isTraceEnabled()); Assert.assertTrue(enablement1.isTraceEnabled()); Assert.assertTrue(enablement2.isTraceEnabled()); }