From 7c28efef4c524c45afd320e4a9a04ed71630411c Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Thu, 30 Nov 2023 14:30:01 -0500 Subject: [PATCH 1/3] Add stylized unit test method names to maven output --- .../java/jssc/SerialNativeInterfaceTest.java | 3 +- src/test/java/jssc/VirtualPortTest.java | 4 +- .../ManualBootLibraryPathFailedTest.java | 3 +- .../bootpath/ManualBootLibraryPathTest.java | 3 +- src/test/java/jssc/common/ConsoleColor.java | 23 +++++++++ src/test/java/jssc/common/ConsoleStyle.java | 51 +++++++++++++++++++ .../junit/rules/DisplayMethodNameRule.java | 26 ++++++++++ 7 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 src/test/java/jssc/common/ConsoleColor.java create mode 100644 src/test/java/jssc/common/ConsoleStyle.java 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 541e2f7539..8c8a7600f1 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 623aeb9763..13bb419efe 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 2ae9ea6781..f7756378e5 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 77983d1b6f..1abb7ea248 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/common/ConsoleColor.java b/src/test/java/jssc/common/ConsoleColor.java new file mode 100644 index 0000000000..1cd3849700 --- /dev/null +++ b/src/test/java/jssc/common/ConsoleColor.java @@ -0,0 +1,23 @@ +package jssc.common; + +public enum ConsoleColor { + ANSI_RESET(0), + ANSI_BLACK(30), + ANSI_RED(31), + ANSI_GREEN(32), + ANSI_YELLOW(33), + ANSI_BLUE(34), + ANSI_PURPLE(35), + ANSI_CYAN(36), + ANSI_WHITE(37); + + String colorCode; + ConsoleColor(int colorCode) { + this.colorCode = "\u001B[" + colorCode + "m"; + } + + @Override + public String toString() { + return colorCode; + } +} diff --git a/src/test/java/jssc/common/ConsoleStyle.java b/src/test/java/jssc/common/ConsoleStyle.java new file mode 100644 index 0000000000..6eb3c10840 --- /dev/null +++ b/src/test/java/jssc/common/ConsoleStyle.java @@ -0,0 +1,51 @@ +package jssc.common; + +import org.junit.runner.Description; + +import static jssc.common.ConsoleColor.*; + +/** + * Utility class for coloring a console message similar to JUnit 4 + */ +public enum ConsoleStyle { + INFO, + WARNING, + ERROR; + + public String colorize(String message) { + // e.g. [INFO] --- surefire:3.0.0-M4:test (default-test) @ jssc --- + return String.format("%s --- %s @ %s", + getPrefix(), + styleMessage(ANSI_GREEN, "surefire:" + message), + styleMessage(ANSI_CYAN, "jssc")); + } + + public String colorize(Description description) { + return colorize(description.getMethodName()); + } + + private ConsoleColor getColor() { + switch(this) { + case ERROR: + return ANSI_RED; + case WARNING: + return ANSI_YELLOW; + case INFO: + default: + return ANSI_BLUE; + } + } + + private String styleSeverity() { + return styleMessage(getColor(), name()); + } + + private static String styleMessage(ConsoleColor color, String message) { + return color + message + ANSI_RESET; + } + + private String getPrefix() { + return ANSI_RESET + "[" + styleSeverity() + "]"; + } + +} 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 0000000000..2e9566a1ed --- /dev/null +++ b/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java @@ -0,0 +1,26 @@ +package jssc.junit.rules; + +import org.junit.Rule; +import org.junit.rules.*; +import org.junit.runner.*; + +import static jssc.common.ConsoleStyle.*; + +/** + * Adds the method name to the JUnit logs, useful for debugging + */ +public class DisplayMethodNameRule { + @Rule + public TestWatcher testWatcher = new TestWatcher() { + @Override + protected void starting(Description description) { + /*String nl = System.getProperty("line.separator"); + System.out.println(String.format(nl + + "-------------------------------------------------------" + nl + + "METHOD: %s" + nl + + "-------------------------------------------------------", + description.getMethodName()));*/ + System.out.println(INFO.colorize(description)); + } + }; +} From 762f1f23a28317e5cec72658cb48b2667ec735e8 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Thu, 30 Nov 2023 14:55:42 -0500 Subject: [PATCH 2/3] Cleanup --- src/test/java/jssc/junit/rules/DisplayMethodNameRule.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java b/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java index 2e9566a1ed..b2453dd229 100644 --- a/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java +++ b/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java @@ -14,12 +14,6 @@ public class DisplayMethodNameRule { public TestWatcher testWatcher = new TestWatcher() { @Override protected void starting(Description description) { - /*String nl = System.getProperty("line.separator"); - System.out.println(String.format(nl + - "-------------------------------------------------------" + nl + - "METHOD: %s" + nl + - "-------------------------------------------------------", - description.getMethodName()));*/ System.out.println(INFO.colorize(description)); } }; From 61e9bb407b96475730fb6c0d355a378659ae9470 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Thu, 30 Nov 2023 14:58:12 -0500 Subject: [PATCH 3/3] Reword --- src/test/java/jssc/common/ConsoleStyle.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/jssc/common/ConsoleStyle.java b/src/test/java/jssc/common/ConsoleStyle.java index 6eb3c10840..c6dbd498e8 100644 --- a/src/test/java/jssc/common/ConsoleStyle.java +++ b/src/test/java/jssc/common/ConsoleStyle.java @@ -5,7 +5,7 @@ import static jssc.common.ConsoleColor.*; /** - * Utility class for coloring a console message similar to JUnit 4 + * Utility class for coloring a console message similar to maven */ public enum ConsoleStyle { INFO,