Skip to content

Commit

Permalink
chore: Migrate to Junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-fra committed Aug 28, 2024
1 parent ec95a71 commit 484bf5b
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 271 deletions.
9 changes: 5 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<commons-io.version>2.16.1</commons-io.version>
<commons-lang.version>3.16.0</commons-lang.version>
<testcontainers.version>1.20.1</testcontainers.version>
<junit.version>4.13.2</junit.version>
<junit.version>5.11.0</junit.version>
<hamcrest.version>1.3</hamcrest.version>
</properties>

Expand Down Expand Up @@ -118,8 +118,8 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
Expand Down Expand Up @@ -210,8 +210,9 @@
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<release>${java.version}</release>
<encoding>${project.build.sourceEncoding}</encoding>
<optimize>true</optimize>
<parameters>true</parameters>
</configuration>
</plugin>
<plugin>
Expand Down
4 changes: 2 additions & 2 deletions signals/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,19 @@
import org.awaitility.Awaitility;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import sun.misc.Signal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

@RunWith(Parameterized.class)
@SuppressWarnings("squid:S1191")
public class SignalsHelperTest {

@Parameterized.Parameters(name = "SIG{0}")
public static Object[] data() {
return SignalsHelper.SIG.values();
}

private final SignalsHelper.SIG signalToTest;

public SignalsHelperTest(SignalsHelper.SIG signalToTest) {
this.signalToTest = signalToTest;
}

@Test
public void testSigUsr2() {
final String s = "USR2";
Expand All @@ -55,21 +45,25 @@ public void testSigUsr2() {
Awaitility.await()
.atMost(1, TimeUnit.SECONDS)
.until(counter::get, Matchers.is(1));
assertEquals("count signal events", 1, counter.get());
assertEquals(1, counter.get(), "count signal events");
}

@Test
public void testRegisterClearUsr2() {
testRegisterClear("USR2");
}

@Test
public void testWithEnum() {
@ParameterizedTest
@EnumSource(SignalsHelper.SIG.class)
public void testWithEnum(SignalsHelper.SIG signalToTest) {
assertOsSupport(signalToTest);
testRegisterClear(signalToTest);
}

@Test
public void testWithSignalName() {
@ParameterizedTest
@EnumSource(SignalsHelper.SIG.class)
public void testWithSignalName(SignalsHelper.SIG signalToTest) {
assertOsSupport(signalToTest);
testRegisterClear(signalToTest.getSigName());
}

Expand All @@ -84,7 +78,7 @@ private void testRegisterClear(final String signal) {
Awaitility.await()
.atMost(1, TimeUnit.SECONDS)
.until(counter::get, Matchers.is(1));
assertEquals("count signal events", 1, counter.get());
assertEquals(1, counter.get(), "count signal events");

SignalsHelper.clearHandler(signal);

Expand All @@ -107,7 +101,7 @@ private void testRegisterClear(SignalsHelper.SIG signal) {
Awaitility.await()
.atMost(1, TimeUnit.SECONDS)
.until(counter::get, Matchers.is(1));
assertEquals("count signal events", 1, counter.get());
assertEquals(1, counter.get(), "count signal events");

SignalsHelper.clearHandler(signal);

Expand All @@ -118,4 +112,23 @@ private void testRegisterClear(SignalsHelper.SIG signal) {
MatcherAssert.assertThat("check error message", e.getMessage(), Matchers.is("Unhandled signal: SIG" + signal));
}
}

private void assertOsSupport(SignalsHelper.SIG signal) {
// Some Signals can't be used on Mac
switch (signal) {
case STKFLT:
case PWR:
Assumptions.assumeFalse(
OS.current() == OS.MAC,
String.format("Signal %s unknown on MacOS", signal)
);
break;
case BUS:
Assumptions.assumeFalse(
OS.current() == OS.MAC,
String.format("Signal %s already used by VM or OS", signal)
);
break;
}
}
}
9 changes: 7 additions & 2 deletions slf4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;

import static org.hamcrest.MatcherAssert.assertThat;


public class LoggingContextBuilderTest {

@Before
@BeforeEach
public void setUp() {
MDC.clear();
}

@Test
@org.junit.jupiter.api.Test
public void testWithMDC() {
final String value = UUID.randomUUID().toString();
Map<String, String> ctx = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
import java.util.function.Function;
import java.util.function.Supplier;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;

import static org.hamcrest.MatcherAssert.assertThat;

public class LoggingContextTest {

@Before
@BeforeEach
public void setUp() {
MDC.clear();
}
Expand Down Expand Up @@ -167,6 +167,7 @@ public void testWrapInCopyRunable() throws Throwable {
throw e.getCause();
} finally {
executor.shutdownNow();
executor.close();
}
}

Expand All @@ -193,6 +194,7 @@ public void testWrapInCopyCallable() throws Throwable {
throw e.getCause();
} finally {
executor.shutdownNow();
executor.close();
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/testcontainers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>compile</scope>
</dependency>

Expand Down
4 changes: 2 additions & 2 deletions utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
62 changes: 29 additions & 33 deletions utils/src/test/java/io/redlink/utils/ChecksumUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,99 +17,95 @@
package io.redlink.utils;

import java.nio.charset.StandardCharsets;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.junit.Assert.*;

public class ChecksumUtilsTest {

@ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
@TempDir
private static Path tempDir;

private static Path path;

private static File file;

@BeforeClass
@BeforeAll
public static void setUp() throws IOException {
file = temporaryFolder.newFile("ASL.txt");
path = file.toPath();
path = tempDir.resolve("ASL.txt");

Files.copy(ChecksumUtilsTest.class.getResourceAsStream("/ASL-2.0.txt"), path, StandardCopyOption.REPLACE_EXISTING);
}

@Test
public void testCrc32String() {
assertEquals("CRC32 mismatch", "358ad45d",
ChecksumUtils.crc32("Lorem Ipsum"));
assertEquals("358ad45d",
ChecksumUtils.crc32("Lorem Ipsum"), "CRC32 mismatch");
}

@Test
public void testCrc32ByteArray() {
assertEquals("CRC32 mismatch", "358ad45d",
ChecksumUtils.crc32("Lorem Ipsum".getBytes(StandardCharsets.UTF_8)));
assertEquals("358ad45d",
ChecksumUtils.crc32("Lorem Ipsum".getBytes(StandardCharsets.UTF_8)), "CRC32 mismatch");
}

@Test
public void testCrc32File() throws IOException {
assertEquals("CRC32 mismatch", "86e2b4b4",
ChecksumUtils.crc32(file));
assertEquals("86e2b4b4",
ChecksumUtils.crc32(path.toFile()), "CRC32 mismatch");
}

@Test
public void testCrc32Path() throws IOException {
assertEquals("CRC32 mismatch", "86e2b4b4",
ChecksumUtils.crc32(path));
assertEquals("86e2b4b4",
ChecksumUtils.crc32(path), "CRC32 mismatch");
}

@Test
public void testCrc32InputStream() throws Exception {
final InputStream stream = getClass().getResourceAsStream("/ASL-2.0.txt");

assertEquals("CRC32 mismatch", "86e2b4b4",
ChecksumUtils.crc32(stream));
assertEquals("86e2b4b4",
ChecksumUtils.crc32(stream), "CRC32 mismatch");
}

@Test
public void testAdler32String() {
assertEquals("ADLER32 mismatch", "1867042e",
ChecksumUtils.adler32("Lorem Ipsum"));
assertEquals("1867042e",
ChecksumUtils.adler32("Lorem Ipsum"), "ADLER32 mismatch");
}

@Test
public void testAdler32ByteArray() {
assertEquals("ADLER32 mismatch", "1867042e",
ChecksumUtils.adler32("Lorem Ipsum".getBytes(StandardCharsets.UTF_8)));
assertEquals("1867042e",
ChecksumUtils.adler32("Lorem Ipsum".getBytes(StandardCharsets.UTF_8)), "ADLER32 mismatch");
}

@Test
public void testAdler32File() throws IOException {
assertEquals("ADLER32 mismatch", "3a27ec70",
ChecksumUtils.adler32(file));
assertEquals("3a27ec70",
ChecksumUtils.adler32(path.toFile()), "ADLER32 mismatch");
}

@Test
public void testAdler32Path() throws IOException {
assertEquals("ADLER32 mismatch", "3a27ec70",
ChecksumUtils.adler32(path));
assertEquals("3a27ec70",
ChecksumUtils.adler32(path), "ADLER32 mismatch");
}

@Test
public void testAdler32InputStream() throws Exception {
final InputStream stream = getClass().getResourceAsStream("/ASL-2.0.txt");

assertEquals("ADLER32 mismatch", "3a27ec70",
ChecksumUtils.adler32(stream));
assertEquals("3a27ec70",
ChecksumUtils.adler32(stream), "ADLER32 mismatch");
}

}
Loading

0 comments on commit 484bf5b

Please sign in to comment.