From 4a01fe53de6ca96393134466bcccbcc6d6f341c3 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Fri, 1 Dec 2023 01:00:24 -0500 Subject: [PATCH 1/6] Remove slf4j from unit tests (broken) --- pom.xml | 13 +++++++++++++ src/test/java/jssc/junit/rules/VirtualPortRule.java | 8 +++++--- src/test/java/resources/log4j.properties | 4 ++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 src/test/java/resources/log4j.properties diff --git a/pom.xml b/pom.xml index f5a20a102..c812f6de1 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,7 @@ 4.12 + 2.22.0 1.2.3 2.5.0 @@ -81,6 +82,18 @@ native-lib-loader ${dependency.nativelibloader.version} + + org.apache.logging.log4j + log4j-api + ${dependency.log4j.version} + test + + + org.apache.logging.log4j + log4j-core + ${dependency.log4j.version} + test + junit junit diff --git a/src/test/java/jssc/junit/rules/VirtualPortRule.java b/src/test/java/jssc/junit/rules/VirtualPortRule.java index 7512d9cd6..f6dc448a9 100644 --- a/src/test/java/jssc/junit/rules/VirtualPortRule.java +++ b/src/test/java/jssc/junit/rules/VirtualPortRule.java @@ -32,16 +32,18 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import jssc.SerialNativeInterface; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class VirtualPortRule implements TestRule { - private static final Logger LOG = LoggerFactory.getLogger(VirtualPortRule.class); + private static final Logger LOG = LogManager.getLogger(VirtualPortRule.class); private static final ExecutorService executor = Executors.newCachedThreadPool(); diff --git a/src/test/java/resources/log4j.properties b/src/test/java/resources/log4j.properties new file mode 100644 index 000000000..b6f16c71e --- /dev/null +++ b/src/test/java/resources/log4j.properties @@ -0,0 +1,4 @@ +# Logging settings for unit tests + +# TODO: Why isn't this being recognized/honored by log4j? +log4j.rootLogger=ALL \ No newline at end of file From f36e9b59ff242d57892ef270b87f3218fdda1635 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Fri, 1 Dec 2023 01:29:15 -0500 Subject: [PATCH 2/6] Fix log4j configuration --- src/test/java/resources/log4j.properties | 4 ---- src/test/resources/log4j2.properties | 10 ++++++++++ 2 files changed, 10 insertions(+), 4 deletions(-) delete mode 100644 src/test/java/resources/log4j.properties create mode 100644 src/test/resources/log4j2.properties diff --git a/src/test/java/resources/log4j.properties b/src/test/java/resources/log4j.properties deleted file mode 100644 index b6f16c71e..000000000 --- a/src/test/java/resources/log4j.properties +++ /dev/null @@ -1,4 +0,0 @@ -# Logging settings for unit tests - -# TODO: Why isn't this being recognized/honored by log4j? -log4j.rootLogger=ALL \ No newline at end of file diff --git a/src/test/resources/log4j2.properties b/src/test/resources/log4j2.properties new file mode 100644 index 000000000..76bef8003 --- /dev/null +++ b/src/test/resources/log4j2.properties @@ -0,0 +1,10 @@ +# Logging settings for unit tests + +# The root logger with appender name +rootLogger=DEBUG, STDOUT + +# Assign STDOUT a valid appender & define its layout +appender.console.name=STDOUT +appender.console.type=Console +appender.console.layout.type=PatternLayout +appender.console.layout.pattern=%msg%n \ No newline at end of file From 861523d2c4246a5ad2192fb07ef3bbc307d64220 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Fri, 8 Dec 2023 17:42:11 -0500 Subject: [PATCH 3/6] Colorize output --- .../java/jssc/SerialNativeInterfaceTest.java | 3 ++- src/test/java/jssc/VirtualPortTest.java | 4 +++- .../ManualBootLibraryPathFailedTest.java | 3 ++- .../bootpath/ManualBootLibraryPathTest.java | 3 ++- .../junit/rules/DisplayMethodNameRule.java | 21 +++++++++++++++++++ src/test/resources/log4j2.properties | 2 +- 6 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 src/test/java/jssc/junit/rules/DisplayMethodNameRule.java diff --git a/src/test/java/jssc/SerialNativeInterfaceTest.java b/src/test/java/jssc/SerialNativeInterfaceTest.java index 541e2f753..8c8a7600f 100644 --- a/src/test/java/jssc/SerialNativeInterfaceTest.java +++ b/src/test/java/jssc/SerialNativeInterfaceTest.java @@ -1,5 +1,6 @@ package jssc; +import jssc.junit.rules.DisplayMethodNameRule; import org.junit.Assume; import org.junit.Before; import org.junit.Test; @@ -13,7 +14,7 @@ import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; -public class SerialNativeInterfaceTest { +public class SerialNativeInterfaceTest extends DisplayMethodNameRule { @Test public void testInitNativeInterface() { diff --git a/src/test/java/jssc/VirtualPortTest.java b/src/test/java/jssc/VirtualPortTest.java index 623aeb976..13bb419ef 100644 --- a/src/test/java/jssc/VirtualPortTest.java +++ b/src/test/java/jssc/VirtualPortTest.java @@ -24,12 +24,14 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; + +import jssc.junit.rules.DisplayMethodNameRule; import jssc.junit.rules.VirtualPortRule; import org.junit.Assume; import org.junit.Rule; import org.junit.Test; -public class VirtualPortTest { +public class VirtualPortTest extends DisplayMethodNameRule { private static final String HELLO_WORLD = "Hello, world!"; diff --git a/src/test/java/jssc/bootpath/ManualBootLibraryPathFailedTest.java b/src/test/java/jssc/bootpath/ManualBootLibraryPathFailedTest.java index 2ae9ea678..f7756378e 100644 --- a/src/test/java/jssc/bootpath/ManualBootLibraryPathFailedTest.java +++ b/src/test/java/jssc/bootpath/ManualBootLibraryPathFailedTest.java @@ -1,6 +1,7 @@ package jssc.bootpath; import jssc.SerialNativeInterface; +import jssc.junit.rules.DisplayMethodNameRule; import org.junit.Test; import static org.junit.Assert.assertTrue; @@ -16,7 +17,7 @@ * - maven-surefire-plugin DOES offer JVM unloading between classes using reuseForks=false * - Unloading is needed due to NativeLoader.loadLibrary(...) calls System.loadLibrary(...) which is static */ -public class ManualBootLibraryPathFailedTest { +public class ManualBootLibraryPathFailedTest extends DisplayMethodNameRule { @Test public void testBootPathOverride() { String nativeLibDir = "/"; // This should be valid on all platforms diff --git a/src/test/java/jssc/bootpath/ManualBootLibraryPathTest.java b/src/test/java/jssc/bootpath/ManualBootLibraryPathTest.java index 77983d1b6..1abb7ea24 100644 --- a/src/test/java/jssc/bootpath/ManualBootLibraryPathTest.java +++ b/src/test/java/jssc/bootpath/ManualBootLibraryPathTest.java @@ -1,6 +1,7 @@ package jssc.bootpath; import jssc.SerialNativeInterface; +import jssc.junit.rules.DisplayMethodNameRule; import org.junit.Test; import org.scijava.nativelib.NativeLibraryUtil; @@ -18,7 +19,7 @@ * - maven-surefire-plugin DOES offer JVM unloading between classes using reuseForks=false * - Unloading is needed due to NativeLoader.loadLibrary(...) calls System.loadLibrary(...) which is static */ -public class ManualBootLibraryPathTest { +public class ManualBootLibraryPathTest extends DisplayMethodNameRule { @Test public void testBootPathOverride() { String nativeLibDir = NativeLibraryUtil.getPlatformLibraryPath(System.getProperty("user.dir") + "/target/cmake/natives/"); diff --git a/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java b/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java new file mode 100644 index 000000000..5132efe00 --- /dev/null +++ b/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java @@ -0,0 +1,21 @@ +package jssc.junit.rules; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Rule; +import org.junit.rules.*; +import org.junit.runner.*; + +/** + * Adds the method name to the JUnit logs, useful for debugging + */ +public class DisplayMethodNameRule { + @Rule + public TestWatcher methodWatcher = new TestWatcher() { + @Override + protected void starting(Description description) { + Logger log = LogManager.getLogger(description.getTestClass()); + log.debug(description.getMethodName()); + } + }; +} \ No newline at end of file diff --git a/src/test/resources/log4j2.properties b/src/test/resources/log4j2.properties index 76bef8003..f6674f067 100644 --- a/src/test/resources/log4j2.properties +++ b/src/test/resources/log4j2.properties @@ -7,4 +7,4 @@ rootLogger=DEBUG, STDOUT appender.console.name=STDOUT appender.console.type=Console appender.console.layout.type=PatternLayout -appender.console.layout.pattern=%msg%n \ No newline at end of file +appender.console.layout.pattern=[%highlight{%p}{INFO=blue}] [%c{1}] %m%n \ No newline at end of file From c1d91c9e8cdf88d4edf128386615dd50b57acda5 Mon Sep 17 00:00:00 2001 From: Mateusz Pietryga Date: Sat, 9 Dec 2023 23:54:24 +0100 Subject: [PATCH 4/6] Fully switch to log4j for test logging --- pom.xml | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index c812f6de1..395e72022 100644 --- a/pom.xml +++ b/pom.xml @@ -84,13 +84,13 @@ org.apache.logging.log4j - log4j-api + log4j-core ${dependency.log4j.version} test org.apache.logging.log4j - log4j-core + log4j-slf4j-impl ${dependency.log4j.version} test @@ -100,12 +100,6 @@ ${dependency.junit.version} test - - ch.qos.logback - logback-classic - ${dependency.logback.version} - test - From ffc296bdfb935a4589e47c69f06802d1d0987954 Mon Sep 17 00:00:00 2001 From: Mateusz Pietryga Date: Sat, 9 Dec 2023 23:57:04 +0100 Subject: [PATCH 5/6] Maven-like Log4j configuration for test execution --- .../jssc/junit/rules/DisplayMethodNameRule.java | 7 ++++--- src/test/resources/log4j2.properties | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java b/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java index 5132efe00..a4e1c112a 100644 --- a/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java +++ b/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java @@ -2,9 +2,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.MarkerManager; import org.junit.Rule; -import org.junit.rules.*; -import org.junit.runner.*; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; /** * Adds the method name to the JUnit logs, useful for debugging @@ -15,7 +16,7 @@ public class DisplayMethodNameRule { @Override protected void starting(Description description) { Logger log = LogManager.getLogger(description.getTestClass()); - log.debug(description.getMethodName()); + log.info(MarkerManager.getMarker("MethodName"), description.getMethodName()); } }; } \ No newline at end of file diff --git a/src/test/resources/log4j2.properties b/src/test/resources/log4j2.properties index f6674f067..ce58c24b1 100644 --- a/src/test/resources/log4j2.properties +++ b/src/test/resources/log4j2.properties @@ -1,10 +1,22 @@ # Logging settings for unit tests # The root logger with appender name -rootLogger=DEBUG, STDOUT +rootLogger=DEBUG,STDOUT,TESTNAME # Assign STDOUT a valid appender & define its layout appender.console.name=STDOUT appender.console.type=Console appender.console.layout.type=PatternLayout -appender.console.layout.pattern=[%highlight{%p}{INFO=blue}] [%c{1}] %m%n \ No newline at end of file +appender.console.layout.pattern=[%highlight{%p}{INFO=blue}] [%c{1}] %m%n + +# Make logs for Junit4 method names look like maven +appender.testName.name=TESTNAME +appender.testName.type=Console +appender.testName.layout.type=PatternLayout +appender.testName.layout.pattern=[%highlight{%p}{INFO=blue}] Running %c.%highlight{%m}{FATAL=bold,white, ERROR=bold,white, WARN=bold,white, INFO=bold,white, DEBUG=bold,white, TRACE=bold,white}%n +appender.testName.filter.1.type=MarkerFilter +appender.testName.filter.1.marker=MethodName +appender.console.filter.1.type=MarkerFilter +appender.console.filter.1.marker=MethodName +appender.console.filter.1.onMatch=DENY +appender.console.filter.1.onMismatch=ACCEPT \ No newline at end of file From 9b88da9466cf1dc4a4db229daac1f45c0d1158b8 Mon Sep 17 00:00:00 2001 From: Mateusz Pietryga Date: Sun, 10 Dec 2023 00:13:15 +0100 Subject: [PATCH 6/6] Fix Ubuntu pipeline --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ea85d122e..a2cd1f016 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -48,7 +48,7 @@ jobs: java-version: 11 distribution: temurin - - run: sudo apt-get install socat ${{ matrix.packages }} + - run: sudo apt-get update && sudo apt-get install socat ${{ matrix.packages }} - run: mvn -P "${{ matrix.profile }}" --batch-mode macos: