From 6232ef90b56520b839ad44885eb72eea9cc9b024 Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Sun, 30 Sep 2018 19:08:40 +0200 Subject: [PATCH] adding generics to avoid casting the getRawLogs() --- README.md | 11 +++++------ src/main/java/dk/bitcraft/lc/CollectorImpl.java | 4 ++-- src/main/java/dk/bitcraft/lc/JUnit4LogCollector.java | 7 ++++--- src/main/java/dk/bitcraft/lc/JUnit5LogCollector.java | 7 ++++--- .../java/dk/bitcraft/lc/JavaUtilLoggingCollector.java | 4 ++-- src/main/java/dk/bitcraft/lc/Log4j2Collector.java | 4 ++-- src/main/java/dk/bitcraft/lc/LogBackCollector.java | 5 ++--- .../java/dk/bitcraft/lc/SLF4JSimpleCollector.java | 4 ++-- src/main/java/dk/bitcraft/lc/TestNGLogCollector.java | 2 +- src/test/java/dk/bitcraft/lc/JavaUtilLoggingTest.java | 4 ++-- src/test/java/dk/bitcraft/lc/Log4j2Test.java | 4 ++-- src/test/java/dk/bitcraft/lc/LogbackTest.java | 4 ++-- .../slf4j/src/test/java/test/RuleTest.java | 2 +- .../java/dk/bitcraft/lc/testng/PercentageTest.java | 2 +- 14 files changed, 32 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 0e6b330..6821962 100644 --- a/README.md +++ b/README.md @@ -39,14 +39,14 @@ The following examples show how to add the framework to a test, provide it with Logger logger = LogManager.getLogger("acme.Gizmo"); @Rule -public JUnit4LogCollector collector = new JUnit4LogCollector(logger); +public JUnit4LogCollector collector = new JUnit4LogCollector<>(logger); @Test public void testGizmo() { Gizmo gizmo = new Gizmo(); gizmo.run(); - List rawLogs = (List) collector.getRawLogs(); + List rawLogs = collector.getRawLogs(); assertTrue(rawLogs.stream().noneMatch(l -> l.getLevel() == Level.ERROR)); } ``` @@ -57,14 +57,14 @@ public void testGizmo() { public class GizmoTest { Logger logger = LogManager.getLogger("acme.Gizmo"); - JUnit5LogCollector collector = new JUnit5LogCollector(logger); + JUnit5LogCollector collector = new JUnit5LogCollector<>(logger); @Test void testGizmo() { Gizmo gizmo = new Gizmo(); gizmo.run(); - List rawLogs = (List) collector.getRawLogs(); + List rawLogs = collector.getRawLogs(); assertTrue(rawLogs.stream().noneMatch(l -> l.getLevel() == Level.ERROR)); } } @@ -96,7 +96,7 @@ public class GizmoTest { Gizmo gizmo = new Gizmo(); gizmo.run(); - List rawLogs = (List) TestNGLogCollector.getRawLogs(); + List rawLogs = TestNGLogCollector.getRawLogs(LoggingEvent.class); assertTrue(rawLogs.stream().noneMatch(l -> l.getLevel() == Level.ERROR)); } } @@ -127,6 +127,5 @@ testCompile 'dk.bitcraft:LogCollector:0.9.0' - Come up with a better name for the framework. - Improve Javadoc and documentation. - Discover the intricacies of the various logging frameworks in real-world settings and adapt to them. - - Avoid the ugly cast in `getRawLogs`. - Detect SLF4j's `NOPLogger` and replace it with `SimpleLogger` - Any ideas? Get in touch! diff --git a/src/main/java/dk/bitcraft/lc/CollectorImpl.java b/src/main/java/dk/bitcraft/lc/CollectorImpl.java index d7f43d4..c7e715b 100644 --- a/src/main/java/dk/bitcraft/lc/CollectorImpl.java +++ b/src/main/java/dk/bitcraft/lc/CollectorImpl.java @@ -2,9 +2,9 @@ import java.util.List; -interface CollectorImpl { +interface CollectorImpl { void setup(); void remove(); List getResult(); - List getRawLogs(); + List getRawLogs(); } diff --git a/src/main/java/dk/bitcraft/lc/JUnit4LogCollector.java b/src/main/java/dk/bitcraft/lc/JUnit4LogCollector.java index 9a7f042..d6f2af9 100644 --- a/src/main/java/dk/bitcraft/lc/JUnit4LogCollector.java +++ b/src/main/java/dk/bitcraft/lc/JUnit4LogCollector.java @@ -7,9 +7,10 @@ import java.util.Optional; -public class JUnit4LogCollector extends ExternalResource { - private CollectorImpl collector; +public class JUnit4LogCollector extends ExternalResource { + private CollectorImpl collector; + @SuppressWarnings("unchecked") public JUnit4LogCollector(Object logger) { collector = Arrays.stream(Frameworks.values()) .map(v -> v.getCollector(logger)) @@ -33,7 +34,7 @@ public List getLogs() { return collector.getResult(); } - public List getRawLogs() { + public List getRawLogs() { return collector.getRawLogs(); } } \ No newline at end of file diff --git a/src/main/java/dk/bitcraft/lc/JUnit5LogCollector.java b/src/main/java/dk/bitcraft/lc/JUnit5LogCollector.java index b2162db..f2bb3af 100644 --- a/src/main/java/dk/bitcraft/lc/JUnit5LogCollector.java +++ b/src/main/java/dk/bitcraft/lc/JUnit5LogCollector.java @@ -4,9 +4,10 @@ import java.util.List; import java.util.Optional; -public class JUnit5LogCollector { - private CollectorImpl collector; +public class JUnit5LogCollector { + private CollectorImpl collector; + @SuppressWarnings("unchecked") public JUnit5LogCollector(Object logger) { collector = Arrays.stream(Frameworks.values()) .map(v -> v.getCollector(logger)) @@ -28,7 +29,7 @@ public List getLogs() { return collector.getResult(); } - public List getRawLogs() { + public List getRawLogs() { return collector.getRawLogs(); } } diff --git a/src/main/java/dk/bitcraft/lc/JavaUtilLoggingCollector.java b/src/main/java/dk/bitcraft/lc/JavaUtilLoggingCollector.java index d56b46e..85d8237 100644 --- a/src/main/java/dk/bitcraft/lc/JavaUtilLoggingCollector.java +++ b/src/main/java/dk/bitcraft/lc/JavaUtilLoggingCollector.java @@ -10,7 +10,7 @@ import static java.util.stream.Collectors.toList; -public class JavaUtilLoggingCollector implements CollectorImpl { +public class JavaUtilLoggingCollector implements CollectorImpl { private final Logger logger; private ListHandler handler; @@ -39,7 +39,7 @@ public List getResult() { } @Override - public List getRawLogs() { + public List getRawLogs() { return handler.records.stream().collect(toList()); } diff --git a/src/main/java/dk/bitcraft/lc/Log4j2Collector.java b/src/main/java/dk/bitcraft/lc/Log4j2Collector.java index 8280fd2..b5c2779 100644 --- a/src/main/java/dk/bitcraft/lc/Log4j2Collector.java +++ b/src/main/java/dk/bitcraft/lc/Log4j2Collector.java @@ -13,7 +13,7 @@ import static java.util.stream.Collectors.toList; -class Log4j2Collector implements CollectorImpl { +class Log4j2Collector implements CollectorImpl { private final Logger logger; ListAppender appender; @@ -48,7 +48,7 @@ public List getResult() { } @Override - public List getRawLogs() { + public List getRawLogs() { return appender.events.stream().collect(toList()); } diff --git a/src/main/java/dk/bitcraft/lc/LogBackCollector.java b/src/main/java/dk/bitcraft/lc/LogBackCollector.java index 23fa82b..0037143 100644 --- a/src/main/java/dk/bitcraft/lc/LogBackCollector.java +++ b/src/main/java/dk/bitcraft/lc/LogBackCollector.java @@ -2,7 +2,6 @@ import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; @@ -10,7 +9,7 @@ import static java.util.stream.Collectors.toList; -class LogBackCollector implements CollectorImpl { +class LogBackCollector implements CollectorImpl { private final Logger logger; private final static String appenderName = UUID.randomUUID().toString(); @@ -41,7 +40,7 @@ public List getResult() { } @Override - public List getRawLogs() { + public List getRawLogs() { return appender.list.stream().collect(toList()); } } diff --git a/src/main/java/dk/bitcraft/lc/SLF4JSimpleCollector.java b/src/main/java/dk/bitcraft/lc/SLF4JSimpleCollector.java index 812863f..1ad349e 100644 --- a/src/main/java/dk/bitcraft/lc/SLF4JSimpleCollector.java +++ b/src/main/java/dk/bitcraft/lc/SLF4JSimpleCollector.java @@ -12,7 +12,7 @@ *

SLF4J's SimpleLogger works by printing directly to System.err, so this collector assumes control of System.err and captures its output.

*

Any other usage of System.err will be included among the captured log statements.

*/ -public class SLF4JSimpleCollector implements CollectorImpl { +public class SLF4JSimpleCollector implements CollectorImpl { private PrintStream systemErr; private CapturingPrintStream capturingPrintStream; @@ -38,7 +38,7 @@ public List getResult() { } @Override - public List getRawLogs() { + public List getRawLogs() { return getResult(); } diff --git a/src/main/java/dk/bitcraft/lc/TestNGLogCollector.java b/src/main/java/dk/bitcraft/lc/TestNGLogCollector.java index f87114b..a48e19b 100644 --- a/src/main/java/dk/bitcraft/lc/TestNGLogCollector.java +++ b/src/main/java/dk/bitcraft/lc/TestNGLogCollector.java @@ -41,7 +41,7 @@ public static List getLogs() { return collector.getResult(); } - public static List getRawLogs() { + public static List getRawLogs(Class clazz) { return collector.getRawLogs(); } diff --git a/src/test/java/dk/bitcraft/lc/JavaUtilLoggingTest.java b/src/test/java/dk/bitcraft/lc/JavaUtilLoggingTest.java index eed0f68..70b4a8e 100644 --- a/src/test/java/dk/bitcraft/lc/JavaUtilLoggingTest.java +++ b/src/test/java/dk/bitcraft/lc/JavaUtilLoggingTest.java @@ -13,7 +13,7 @@ public class JavaUtilLoggingTest { @Rule - public JUnit4LogCollector collector = new JUnit4LogCollector(Logger.getLogger("test.logger")); + public JUnit4LogCollector collector = new JUnit4LogCollector<>(Logger.getLogger("test.logger")); @Test public void test() { @@ -29,7 +29,7 @@ public void test() { assertThat(logs.get(0)).contains("This is an warning!"); assertThat(logs.get(1)).contains("This is another warning!"); - List rawLogs = (List) collector.getRawLogs(); + List rawLogs = collector.getRawLogs(); assertTrue(rawLogs.stream().allMatch(e -> e.getLevel() == Level.WARNING)); } } diff --git a/src/test/java/dk/bitcraft/lc/Log4j2Test.java b/src/test/java/dk/bitcraft/lc/Log4j2Test.java index 7dacb28..0f0b8c6 100644 --- a/src/test/java/dk/bitcraft/lc/Log4j2Test.java +++ b/src/test/java/dk/bitcraft/lc/Log4j2Test.java @@ -14,7 +14,7 @@ public class Log4j2Test { @Rule - public JUnit4LogCollector collector = new JUnit4LogCollector(LogManager.getLogger("test.logger")); + public JUnit4LogCollector collector = new JUnit4LogCollector<>(LogManager.getLogger("test.logger")); @Test public void test() { @@ -28,7 +28,7 @@ public void test() { assertThat(collector.getLogs()).hasSize(3) .contains("This is an error!", "This is another error!", "This is a third error!"); - List rawLogs = (List) collector.getRawLogs(); + List rawLogs = collector.getRawLogs(); assertThat(rawLogs).hasSize(3); assertTrue(rawLogs.stream().allMatch(l -> l.getLevel() == Level.ERROR)); diff --git a/src/test/java/dk/bitcraft/lc/LogbackTest.java b/src/test/java/dk/bitcraft/lc/LogbackTest.java index 833c75b..9004ecc 100644 --- a/src/test/java/dk/bitcraft/lc/LogbackTest.java +++ b/src/test/java/dk/bitcraft/lc/LogbackTest.java @@ -15,7 +15,7 @@ public class LogbackTest { @Rule - public JUnit4LogCollector collector = new JUnit4LogCollector(LoggerFactory.getLogger("test.logger")); + public JUnit4LogCollector collector = new JUnit4LogCollector<>(LoggerFactory.getLogger("test.logger")); @Test public void test() { @@ -32,7 +32,7 @@ public void test() { assertThat(collector.getLogs()).hasSize(1); assertThat(collector.getLogs().get(0)).contains("This should be collected!"); - List rawLogs = (List) collector.getRawLogs(); + List rawLogs = collector.getRawLogs(); assertTrue(rawLogs.stream().allMatch(e -> e.getLevel() == Level.WARN)); } } diff --git a/test-environments/slf4j/src/test/java/test/RuleTest.java b/test-environments/slf4j/src/test/java/test/RuleTest.java index 6599eca..b693c17 100644 --- a/test-environments/slf4j/src/test/java/test/RuleTest.java +++ b/test-environments/slf4j/src/test/java/test/RuleTest.java @@ -13,7 +13,7 @@ public class RuleTest { final static Logger logger = LoggerFactory.getLogger(RuleTest.class); @Rule - public JUnit4LogCollector collector = new JUnit4LogCollector(logger); + public JUnit4LogCollector collector = new JUnit4LogCollector<>(logger); @Test diff --git a/test-environments/testng/src/test/java/dk/bitcraft/lc/testng/PercentageTest.java b/test-environments/testng/src/test/java/dk/bitcraft/lc/testng/PercentageTest.java index 4cd036a..bdb61b0 100644 --- a/test-environments/testng/src/test/java/dk/bitcraft/lc/testng/PercentageTest.java +++ b/test-environments/testng/src/test/java/dk/bitcraft/lc/testng/PercentageTest.java @@ -32,7 +32,7 @@ public void sometimesFails() { log.warn("This is a warning."); log.info("This is purely informational."); - List logs = (List) TestNGLogCollector.getRawLogs(); + List logs = TestNGLogCollector.getRawLogs(LoggingEvent.class); assertThat(logs) .hasSize(2)