Skip to content

Commit

Permalink
Use log4j for unit tests (#159)
Browse files Browse the repository at this point in the history
* Shows method names for tests #156
* Removes slf4j from unit tests
* Fully switches to log4j for test logging
* Maven-like Log4j configuration for test execution
* Fixes Ubuntu pipeline

---------

Co-authored-by: Mateusz Pietryga <pietryga.m@gmail.com>
  • Loading branch information
tresf and pietrygamat authored Dec 9, 2023
1 parent b9b6b25 commit 56f58bb
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 13 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

<!-- dependency versions a-z -->
<dependency.junit.version>4.12</dependency.junit.version>
<dependency.log4j.version>2.22.0</dependency.log4j.version>
<dependency.logback.version>1.2.3</dependency.logback.version>
<dependency.nativelibloader.version>2.5.0</dependency.nativelibloader.version>

Expand Down Expand Up @@ -82,15 +83,21 @@
<version>${dependency.nativelibloader.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${dependency.junit.version}</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${dependency.log4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${dependency.logback.version}</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${dependency.log4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${dependency.junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
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
22 changes: 22 additions & 0 deletions src/test/java/jssc/junit/rules/DisplayMethodNameRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package jssc.junit.rules;

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.TestWatcher;
import org.junit.runner.Description;

/**
* 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.info(MarkerManager.getMarker("MethodName"), description.getMethodName());
}
};
}
8 changes: 5 additions & 3 deletions src/test/java/jssc/junit/rules/VirtualPortRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
22 changes: 22 additions & 0 deletions src/test/resources/log4j2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Logging settings for unit tests

# The root logger with appender name
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

# 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

0 comments on commit 56f58bb

Please sign in to comment.