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/common/ConsoleColor.java b/src/test/java/jssc/common/ConsoleColor.java new file mode 100644 index 000000000..1cd384970 --- /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 000000000..c6dbd498e --- /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 maven + */ +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 000000000..b2453dd22 --- /dev/null +++ b/src/test/java/jssc/junit/rules/DisplayMethodNameRule.java @@ -0,0 +1,20 @@ +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) { + System.out.println(INFO.colorize(description)); + } + }; +}