diff --git a/sentinel-logging/sentinel-logging-slf4j/pom.xml b/sentinel-logging/sentinel-logging-slf4j/pom.xml index 645900a3..a02f67ff 100644 --- a/sentinel-logging/sentinel-logging-slf4j/pom.xml +++ b/sentinel-logging/sentinel-logging-slf4j/pom.xml @@ -16,7 +16,7 @@ 1.7 1.7 1.7.25 - 2.12.1 + 1.2.0 @@ -30,31 +30,22 @@ ${slf4j.version} provided + - org.apache.logging.log4j - log4j-core - ${log4j2.version} + uk.org.lidalia + slf4j-test + ${slf4j-test.version} test - - org.apache.logging.log4j - log4j-slf4j-impl - ${log4j2.version} - test - - junit junit test - - com.github.stefanbirkner - system-rules - 1.16.1 + org.mockito + mockito-core test - diff --git a/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/AbstraceSlf4jLogTest.java b/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/AbstraceSlf4jLogTest.java new file mode 100644 index 00000000..0c02f25e --- /dev/null +++ b/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/AbstraceSlf4jLogTest.java @@ -0,0 +1,185 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.csp.sentinel.logging.slf4j; + +import uk.org.lidalia.slf4jext.Level; +import uk.org.lidalia.slf4jtest.TestLoggerFactory; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.mockito.Mockito.*; + +import java.io.PrintStream; + +/** + * @author xue8 + * @author jason + */ +public abstract class AbstraceSlf4jLogTest { + private static final class TestException extends Exception { + private static final long serialVersionUID = 1L; + + } + PrintStream mockStream; + PrintStream oldOutStream; + PrintStream oldErrStream; + + protected abstract String getLoggerName(); + protected abstract void debug(String msg, Object... args); + protected abstract void trace(String msg, Object... args); + protected abstract void info(String msg, Object... args); + protected abstract void warn(String msg, Object... args); + protected abstract void error(String msg, Object... args); + + @Before + public void mockOutput() { + System.out.println("Try to mock System.out and System.err"); + mockStream = mock(PrintStream.class); + oldOutStream = System.out; + oldOutStream = System.out; + System.setOut(mockStream); + System.setErr(mockStream); + TestLoggerFactory.getInstance().setPrintLevel(Level.TRACE); + } + + @After + public void restore() { + if (oldOutStream != null) { + System.setOut(oldOutStream); + System.setErr(oldErrStream); + oldOutStream = null; + oldErrStream = null; + System.out.println("Restore System.out and System.err"); + } + } + + @Test + public void testRecordLog() { + info("init"); + verify(mockStream).println(contains("init")); + clearInvocations(mockStream); + int count = 0; + + // info test + while (count < 1000) { // 0~999 + info("Count {}.", count); + count ++; + } + verify(mockStream).println(contains("Count 0.")); + verify(mockStream).println(contains("Count 1.")); + verify(mockStream).println(contains("Count 99.")); + verify(mockStream).println(contains("Count 123.")); + verify(mockStream).println(contains("Count 888.")); + verify(mockStream).println(contains("Count 999.")); + verify(mockStream, times(1000)).println(contains(" INFO ")); + verify(mockStream, times(1000)).println(contains(getLoggerName())); + clearInvocations(mockStream); + + // warn test + while (count < 2000) { // 1000~1999 + warn("Count {}.", count); + count ++; + } + verify(mockStream).println(contains("Count 1000.")); + verify(mockStream).println(contains("Count 1223.")); + verify(mockStream).println(contains("Count 1888.")); + verify(mockStream).println(contains("Count 1999.")); + verify(mockStream, times(1000)).println(contains(" WARN ")); + verify(mockStream, times(1000)).println(contains(getLoggerName())); + clearInvocations(mockStream); + + // trace test + while (count < 3000) { // 2000~2999 + trace("Count {}.", count); + count ++; + } + verify(mockStream).println(contains("Count 2000.")); + verify(mockStream).println(contains("Count 2999.")); + verify(mockStream, times(1000)).println(contains(" TRACE ")); + verify(mockStream, times(1000)).println(contains(getLoggerName())); + clearInvocations(mockStream); + + // debug test + while (count < 4000) { // 3000~3999 + debug("Count {}.", count); + count ++; + } + verify(mockStream).println(contains("Count 3000.")); + verify(mockStream).println(contains("Count 3999.")); + verify(mockStream, times(1000)).println(contains(" DEBUG ")); + verify(mockStream, times(1000)).println(contains(getLoggerName())); + clearInvocations(mockStream); + + // test error + while (count < 5000) { // 4000~4999 + error("Count {}.", count); + count ++; + } + verify(mockStream).println(contains("Count 4000.")); + verify(mockStream).println(contains("Count 4999.")); + verify(mockStream, times(1000)).println(contains(" ERROR ")); + verify(mockStream, times(1000)).println(contains(getLoggerName())); + clearInvocations(mockStream); + } + + + @Test + public void testLogException() { + info("init"); + verify(mockStream).println(contains("init")); + verify(mockStream, atLeastOnce()).println(contains(getLoggerName())); + clearInvocations(mockStream); + + Exception e = new TestException(); + + // info test + info("some log", e); + verify(mockStream).println(contains("INFO")); + verify(mockStream).println(isA(TestException.class)); + verify(mockStream).println(contains(getLoggerName())); + clearInvocations(mockStream); + + // warn test + warn("some log", e); + verify(mockStream).println(contains("WARN")); + verify(mockStream).println(isA(TestException.class)); + verify(mockStream).println(contains(getLoggerName())); + clearInvocations(mockStream); + + // trace test + trace("some log", e); + verify(mockStream).println(contains("TRACE")); + verify(mockStream).println(isA(TestException.class)); + verify(mockStream).println(contains(getLoggerName())); + clearInvocations(mockStream); + + // debug test + debug("some log", e); + verify(mockStream).println(contains("DEBUG")); + verify(mockStream).println(isA(TestException.class)); + verify(mockStream).println(contains(getLoggerName())); + clearInvocations(mockStream); + + // error test + error("some log", e); + verify(mockStream).println(contains("ERROR")); + verify(mockStream).println(isA(TestException.class)); + verify(mockStream).println(contains(getLoggerName())); + clearInvocations(mockStream); + } +} diff --git a/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/CommandCenterLogTest.java b/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/CommandCenterLogTest.java index 4df72733..cf364e7c 100644 --- a/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/CommandCenterLogTest.java +++ b/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/CommandCenterLogTest.java @@ -1,11 +1,11 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -13,103 +13,43 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.csp.sentinel.logging.slf4j; import com.alibaba.csp.sentinel.transport.log.CommandCenterLog; -import org.junit.*; -import org.junit.contrib.java.lang.system.SystemOutRule; /** - * @author xue8 + * @author jason */ -@Ignore("https://github.com/stefanbirkner/system-rules/issues/45. You can only run one @Test at a time") -public class CommandCenterLogTest { - @Rule - public SystemOutRule log = new SystemOutRule().enableLog(); - - @Test - public void testLog() { - CommandCenterLog.info("init"); - log.clearLog(); - int count = 0; - - // info test - while (count++ < 1000) { - log.clearLog(); - CommandCenterLog.info("Count {}", count); - String str = String.format("INFO sentinelCommandCenterLogger - Count %d" + System.lineSeparator(), count); - Assert.assertEquals(str, log.getLog()); - } - - // warn test - while (count++ < 2000) { - log.clearLog(); - CommandCenterLog.warn("Count {}", count); - String str = String.format("WARN sentinelCommandCenterLogger - Count %d" + System.lineSeparator(), count); - Assert.assertEquals(str, log.getLog()); - } +public class CommandCenterLogTest extends AbstraceSlf4jLogTest { - // trace test - while (count++ < 3000) { - log.clearLog(); - CommandCenterLog.trace("Count {}", count); - String str = String.format("TRACE sentinelCommandCenterLogger - Count %d" + System.lineSeparator(), count); - Assert.assertEquals(str, log.getLog()); - } - - // debug test - while (count++ < 4000) { - log.clearLog(); - CommandCenterLog.debug("Count {}", count); - String str = String.format("DEBUG sentinelCommandCenterLogger - Count %d" + System.lineSeparator(), count); - Assert.assertEquals(str, log.getLog()); - } - - // test error - while (count++ < 5000) { - log.clearLog(); - CommandCenterLog.error("Count {}", count); - String str = String.format("ERROR sentinelCommandCenterLogger - Count %d" + System.lineSeparator(), count); - Assert.assertEquals(str, log.getLog()); - } + @Override + protected String getLoggerName() { + return CommandCenterLog.LOGGER_NAME; } - @Test - public void testLogException() { - CommandCenterLog.info("init"); - log.clearLog(); - Exception e = new Exception("ex"); - - // info test - CommandCenterLog.info("Error", e); - // split the log for test - String[] logSplit = log.getLog().split(System.lineSeparator()); - Assert.assertEquals("INFO sentinelCommandCenterLogger - Error", logSplit[0]); - - // warn test - log.clearLog(); - CommandCenterLog.warn("Error", e); - logSplit = log.getLog().split(System.lineSeparator()); - Assert.assertEquals("WARN sentinelCommandCenterLogger - Error", logSplit[0]); - - // trace test - log.clearLog(); - CommandCenterLog.trace("Error", e); - logSplit = log.getLog().split(System.lineSeparator()); - Assert.assertEquals("TRACE sentinelCommandCenterLogger - Error", logSplit[0]); + @Override + protected void debug(String msg, Object... args) { + CommandCenterLog.debug(msg, args); + } - // debug test - log.clearLog(); - CommandCenterLog.debug("Error", e); - logSplit = log.getLog().split(System.lineSeparator()); - Assert.assertEquals("DEBUG sentinelCommandCenterLogger - Error", logSplit[0]); + @Override + protected void trace(String msg, Object... args) { + CommandCenterLog.trace(msg, args); + } - // error test - log.clearLog(); - CommandCenterLog.error("Error", e); - logSplit = log.getLog().split(System.lineSeparator()); - Assert.assertEquals("ERROR sentinelCommandCenterLogger - Error", logSplit[0]); + @Override + protected void info(String msg, Object... args) { + CommandCenterLog.info(msg, args); } + @Override + protected void warn(String msg, Object... args) { + CommandCenterLog.warn(msg, args); + } + @Override + protected void error(String msg, Object... args) { + CommandCenterLog.error(msg, args); + } } diff --git a/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/RecordLogTest.java b/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/RecordLogTest.java index 735691cd..39534784 100644 --- a/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/RecordLogTest.java +++ b/sentinel-logging/sentinel-logging-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/RecordLogTest.java @@ -17,102 +17,39 @@ package com.alibaba.csp.sentinel.logging.slf4j; import com.alibaba.csp.sentinel.log.RecordLog; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.contrib.java.lang.system.SystemOutRule; /** - * @author xue8 + * @author jason */ -@Ignore("https://github.com/stefanbirkner/system-rules/issues/45. You can only run one @Test at a time") -public class RecordLogTest { - @Rule - public SystemOutRule log = new SystemOutRule().enableLog(); +public class RecordLogTest extends AbstraceSlf4jLogTest { - @Test - public void testLog() { - RecordLog.info("init"); - log.clearLog(); - int count = 0; - - // info test - while (count++ < 1000) { - log.clearLog(); - RecordLog.info("Count {}", count); - String str = String.format("INFO sentinelRecordLogger - Count %d" + System.lineSeparator(), count); - Assert.assertEquals(str, log.getLog()); - } - - // warn test - while (count++ < 2000) { - log.clearLog(); - RecordLog.warn("Count {}", count); - String str = String.format("WARN sentinelRecordLogger - Count %d" + System.lineSeparator(), count); - Assert.assertEquals(str, log.getLog()); - } - - // trace test - while (count++ < 3000) { - log.clearLog(); - RecordLog.trace("Count {}", count); - String str = String.format("TRACE sentinelRecordLogger - Count %d" + System.lineSeparator(), count); - Assert.assertEquals(str, log.getLog()); - } - - // debug test - while (count++ < 4000) { - log.clearLog(); - RecordLog.debug("Count {}", count); - String str = String.format("DEBUG sentinelRecordLogger - Count %d" + System.lineSeparator(), count); - Assert.assertEquals(str, log.getLog()); - } - - // test error - while (count++ < 5000) { - log.clearLog(); - RecordLog.error("Count {}", count); - String str = String.format("ERROR sentinelRecordLogger - Count %d" + System.lineSeparator(), count); - Assert.assertEquals(str, log.getLog()); - } + @Override + protected String getLoggerName() { + return RecordLog.LOGGER_NAME; } + @Override + protected void debug(String msg, Object... args) { + RecordLog.debug(msg, args); + } - @Test - public void testLogException() { - RecordLog.info("init"); - log.clearLog(); - Exception e = new Exception("ex"); - - // info test - RecordLog.info("Error", e); - // split the log for test - String[] logSplit = log.getLog().split(System.lineSeparator()); - Assert.assertEquals("INFO sentinelRecordLogger - Error", logSplit[0]); - - // warn test - log.clearLog(); - RecordLog.warn("Error", e); - logSplit = log.getLog().split(System.lineSeparator()); - Assert.assertEquals("WARN sentinelRecordLogger - Error", logSplit[0]); + @Override + protected void trace(String msg, Object... args) { + RecordLog.trace(msg, args); + } - // trace test - log.clearLog(); - RecordLog.trace("Error", e); - logSplit = log.getLog().split(System.lineSeparator()); - Assert.assertEquals("TRACE sentinelRecordLogger - Error", logSplit[0]); + @Override + protected void info(String msg, Object... args) { + RecordLog.info(msg, args); + } - // debug test - log.clearLog(); - RecordLog.debug("Error", e); - logSplit = log.getLog().split(System.lineSeparator()); - Assert.assertEquals("DEBUG sentinelRecordLogger - Error", logSplit[0]); + @Override + protected void warn(String msg, Object... args) { + RecordLog.warn(msg, args); + } - // error test - log.clearLog(); - RecordLog.error("Error", e); - logSplit = log.getLog().split(System.lineSeparator()); - Assert.assertEquals("ERROR sentinelRecordLogger - Error", logSplit[0]); + @Override + protected void error(String msg, Object... args) { + RecordLog.error(msg, args); } } diff --git a/sentinel-logging/sentinel-logging-slf4j/src/test/resources/log4j2.xml b/sentinel-logging/sentinel-logging-slf4j/src/test/resources/log4j2.xml deleted file mode 100644 index 1ce6b195..00000000 --- a/sentinel-logging/sentinel-logging-slf4j/src/test/resources/log4j2.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file