/*
 * This is free and unencumbered software released into the public domain.
 *
 * Please see https://github.com/binkley/binkley/blob/master/LICENSE.md.
 */

package hm.binkley.util.logging.osi;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
import org.junit.contrib.java.lang.system.StandardErrorStreamLog;
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;

import static hm.binkley.util.logging.LoggerUtil.refreshLogback;
import static hm.binkley.util.logging.osi.OSI.SystemProperty.LOGBACK_CONFIGURATION_FILE;
import static hm.binkley.util.logging.osi.OSI.SystemProperty.LOGBACK_INCLUDED_RESOURCE;
import static hm.binkley.util.logging.osi.SupportLoggers.ALERT;
import static hm.binkley.util.logging.osi.SupportLoggers.APPLICATION;
import static hm.binkley.util.logging.osi.SupportLoggers.AUDIT;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.isEmptyString;
import static org.junit.Assert.assertThat;

/**
 * {@code ITSupportLoggers} integration tests {@link SupportLoggers}.
 *
 * @author <a href="mailto:binkley@alumni.rice.edu">B. K. Oxley</a>
 */
public final class ITSupportLoggers {
    @Rule
    public final StandardOutputStreamLog sout = new StandardOutputStreamLog();
    @Rule
    public final StandardErrorStreamLog serr = new StandardErrorStreamLog();
    @Rule
    public final ProvideSystemProperty osi = new ProvideSystemProperty(
            LOGBACK_CONFIGURATION_FILE.key(), "osi-logback.xml");
    @Rule
    public final ProvideSystemProperty included = new ProvideSystemProperty(
            LOGBACK_INCLUDED_RESOURCE.key(), "osi-support-loggers-included.xml");

    @Before
    public void setUp() {
        refreshLogback();
    }

    @Test(expected = IllegalStateException.class)
    public void alertShouldRejectTrivialMessages() {
        ALERT.getLogger("test").info("Ignored");
    }

    @Test
    public void applicationShouldLogNormally() {
        APPLICATION.getLogger("test").error("Ignored");

        assertThat(sout.getLog(), containsString("Ignored"));
    }

    @Test
    public void alertShouldSayWarnOnStderr() {
        ALERT.getLogger("alert").warn("Ignored");

        assertThat(serr.getLog(), containsString("ALERT/WARN"));
    }

    @Test
    public void alertShouldIncludeNonAlertLoggerName() {
        ALERT.getLogger("test").warn("Ignored");

        assertThat(serr.getLog(), containsString("ALERT/WARN"));
    }

    @Test
    public void alertShouldSayNothingOnStdout() {
        ALERT.getLogger("test").warn("Ignored");

        assertThat(sout.getLog(), isEmptyString());
    }

    @Test(expected = IllegalStateException.class)
    public void alertShouldComplainWithInfo() {
        ALERT.getLogger("test").info("Ignored");
    }

    @Test
    public void auditShouldSayInfoOnStdout() {
        AUDIT.getLogger("audit").info("Ignored");

        assertThat(sout.getLog(), containsString("AUDIT/INFO"));
    }

    @Test
    public void auditShouldIncludeNonAuditLoggerName() {
        AUDIT.getLogger("test").info("Ignored");

        assertThat(sout.getLog(), containsString("AUDIT/INFO"));
    }

    @Test
    public void auditShouldSayNothingOnStderr() {
        AUDIT.getLogger("test").info("Ignored");

        assertThat(serr.getLog(), isEmptyString());
    }

    @Test(expected = IllegalStateException.class)
    public void auditShouldComplainWithDebug() {
        AUDIT.getLogger("test").debug("Ignored");
    }
}

@@ -1803,167 +1803,8 @@
  %7D%0A%0A
-    @Test(expected = IllegalStateException.class)%0A    public void alertShouldRejectTrivialMessages() %7B%0A        ALERT.getLogger(%22test%22).info(%22Ignored%22);%0A    %7D%0A%0A
     
@@ -2386,34 +2386,33 @@
  alertShould
-SayNothing
+Duplicate
 OnStdout() %7B
@@ -2429,35 +2429,36 @@
 LERT.getLogger(%22
-tes
+aler
 t%22).warn(%22Ignore
@@ -2493,38 +2493,51 @@
 t.getLog(), 
-isEmptyString(
+containsString(%22ALERT/WARN%22
 ));%0A    %7D%0A%0A 

Fix support logger test.