Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shows the name of the current test in the maven logs #158

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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"));
tresf marked this conversation as resolved.
Show resolved Hide resolved
}

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() + "]";
}

}
20 changes: 20 additions & 0 deletions src/test/java/jssc/junit/rules/DisplayMethodNameRule.java
Original file line number Diff line number Diff line change
@@ -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) {
pietrygamat marked this conversation as resolved.
Show resolved Hide resolved
System.out.println(INFO.colorize(description));
}
};
}