Skip to content

Commit

Permalink
Add stylized unit test method names to maven output
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Nov 30, 2023
1 parent 68cce35 commit 7c28efe
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/test/java/jssc/SerialNativeInterfaceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jssc;

import jssc.junit.rules.DisplayMethodNameRule;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/jssc/VirtualPortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!";

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +17,7 @@
* - maven-surefire-plugin DOES offer JVM unloading between classes using <code>reuseForks=false</code>
* - 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
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/jssc/bootpath/ManualBootLibraryPathTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jssc.bootpath;

import jssc.SerialNativeInterface;
import jssc.junit.rules.DisplayMethodNameRule;
import org.junit.Test;
import org.scijava.nativelib.NativeLibraryUtil;

Expand All @@ -18,7 +19,7 @@
* - maven-surefire-plugin DOES offer JVM unloading between classes using <code>reuseForks=false</code>
* - 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/");
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/jssc/common/ConsoleColor.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
51 changes: 51 additions & 0 deletions src/test/java/jssc/common/ConsoleStyle.java
Original file line number Diff line number Diff line change
@@ -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() + "]";
}

}
26 changes: 26 additions & 0 deletions src/test/java/jssc/junit/rules/DisplayMethodNameRule.java
Original file line number Diff line number Diff line change
@@ -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));
}
};
}

0 comments on commit 7c28efe

Please sign in to comment.