From db2f0b5b4b5bbd63de3cb61ce3d6f08dfb2ceb43 Mon Sep 17 00:00:00 2001 From: Jakob Frank Date: Wed, 28 Aug 2024 11:34:49 +0200 Subject: [PATCH] chore: Migrate to Junit5 --- pom.xml | 9 +- signals/pom.xml | 4 +- .../utils/signal/SignalsHelperTest.java | 61 +++--- slf4j/pom.xml | 9 +- .../logging/LoggingContextBuilderTest.java | 9 +- .../utils/logging/LoggingContextTest.java | 6 +- test/testcontainers/pom.xml | 4 +- utils/pom.xml | 4 +- .../io/redlink/utils/ChecksumUtilsTest.java | 62 +++--- .../java/io/redlink/utils/HashUtilsTest.java | 177 ++++++++++-------- .../java/io/redlink/utils/PathUtilsTest.java | 69 ++++--- .../io/redlink/utils/RandomUtilsTest.java | 18 +- .../utils/ResourceLoaderUtilsTest.java | 103 +++++----- .../io/redlink/utils/concurrent/GateTest.java | 44 ++--- 14 files changed, 308 insertions(+), 271 deletions(-) diff --git a/pom.xml b/pom.xml index 70a7cee..a6fa0ec 100644 --- a/pom.xml +++ b/pom.xml @@ -87,7 +87,7 @@ 2.16.1 3.16.0 1.20.1 - 4.13.2 + 5.11.0 1.3 @@ -118,8 +118,8 @@ - junit - junit + org.junit.jupiter + junit-jupiter ${junit.version} test @@ -210,8 +210,9 @@ ${java.version} ${java.version} + ${java.version} ${project.build.sourceEncoding} - true + true diff --git a/signals/pom.xml b/signals/pom.xml index cbc718f..506ce81 100644 --- a/signals/pom.xml +++ b/signals/pom.xml @@ -36,8 +36,8 @@ - junit - junit + org.junit.jupiter + junit-jupiter test diff --git a/signals/src/test/java/io/redlink/utils/signal/SignalsHelperTest.java b/signals/src/test/java/io/redlink/utils/signal/SignalsHelperTest.java index 1ba7387..0192a58 100644 --- a/signals/src/test/java/io/redlink/utils/signal/SignalsHelperTest.java +++ b/signals/src/test/java/io/redlink/utils/signal/SignalsHelperTest.java @@ -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"; @@ -55,7 +45,7 @@ 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 @@ -63,13 +53,17 @@ 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()); } @@ -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); @@ -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); @@ -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; + } + } } diff --git a/slf4j/pom.xml b/slf4j/pom.xml index cb3b9fe..72b735f 100644 --- a/slf4j/pom.xml +++ b/slf4j/pom.xml @@ -35,8 +35,13 @@ - junit - junit + org.junit.jupiter + junit-jupiter + test + + + org.hamcrest + hamcrest-library test diff --git a/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextBuilderTest.java b/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextBuilderTest.java index 4ba25f8..110f6a9 100644 --- a/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextBuilderTest.java +++ b/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextBuilderTest.java @@ -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 ctx = new HashMap<>(); diff --git a/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextTest.java b/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextTest.java index f79baaa..533ec23 100644 --- a/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextTest.java +++ b/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextTest.java @@ -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(); } diff --git a/test/testcontainers/pom.xml b/test/testcontainers/pom.xml index 3af2c6b..256b364 100644 --- a/test/testcontainers/pom.xml +++ b/test/testcontainers/pom.xml @@ -30,8 +30,8 @@ - junit - junit + org.junit.jupiter + junit-jupiter compile diff --git a/utils/pom.xml b/utils/pom.xml index 6eb0012..ad7631e 100644 --- a/utils/pom.xml +++ b/utils/pom.xml @@ -40,8 +40,8 @@ test - junit - junit + org.junit.jupiter + junit-jupiter test diff --git a/utils/src/test/java/io/redlink/utils/ChecksumUtilsTest.java b/utils/src/test/java/io/redlink/utils/ChecksumUtilsTest.java index 47a679e..12033ee 100644 --- a/utils/src/test/java/io/redlink/utils/ChecksumUtilsTest.java +++ b/utils/src/test/java/io/redlink/utils/ChecksumUtilsTest.java @@ -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"); } } diff --git a/utils/src/test/java/io/redlink/utils/HashUtilsTest.java b/utils/src/test/java/io/redlink/utils/HashUtilsTest.java index dc7d94b..edf9840 100644 --- a/utils/src/test/java/io/redlink/utils/HashUtilsTest.java +++ b/utils/src/test/java/io/redlink/utils/HashUtilsTest.java @@ -16,7 +16,7 @@ package io.redlink.utils; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.File; import java.io.IOException; @@ -25,54 +25,57 @@ 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 org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; /** * */ public class HashUtilsTest { - @ClassRule - public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + @TempDir + public 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"); + file = path.toFile(); Files.copy(HashUtilsTest.class.getResourceAsStream("/ASL-2.0.txt"), path, StandardCopyOption.REPLACE_EXISTING); } @Test public void testHashString() { - assertEquals("Hash mismatch", HashUtils.md5sum("Lorem Ipsum"), - HashUtils.hash(HashUtils.HashAlg.MD5, "Lorem Ipsum")); + assertEquals(HashUtils.md5sum("Lorem Ipsum"), + HashUtils.hash(HashUtils.HashAlg.MD5, "Lorem Ipsum"), + "Hash mismatch"); } @Test public void testHashByteArray() { - assertEquals("Hash mismatch", HashUtils.md5sum("Lorem Ipsum".getBytes(StandardCharsets.UTF_8)), - HashUtils.hash(HashUtils.HashAlg.MD5, "Lorem Ipsum".getBytes(StandardCharsets.UTF_8))); + assertEquals(HashUtils.md5sum("Lorem Ipsum".getBytes(StandardCharsets.UTF_8)), + HashUtils.hash(HashUtils.HashAlg.MD5, "Lorem Ipsum".getBytes(StandardCharsets.UTF_8)), + "Hash mismatch"); } @Test public void testHashFile() throws IOException { - assertEquals("Hash mismatch", HashUtils.md5sum(file), - HashUtils.hash(HashUtils.HashAlg.MD5, file)); + assertEquals(HashUtils.md5sum(file), + HashUtils.hash(HashUtils.HashAlg.MD5, file), + "Hash mismatch"); } @Test public void testHashPath() throws IOException { - assertEquals("Hash mismatch", HashUtils.md5sum(path), - HashUtils.hash(HashUtils.HashAlg.MD5, path)); + assertEquals(HashUtils.md5sum(path), + HashUtils.hash(HashUtils.HashAlg.MD5, path), + "Hash mismatch"); } @Test @@ -80,152 +83,172 @@ public void testHashInputStream() throws Exception { final InputStream streamMd5 = getClass().getResourceAsStream("/ASL-2.0.txt"); final InputStream streamHash = getClass().getResourceAsStream("/ASL-2.0.txt"); - assertEquals("Hash mismatch", HashUtils.md5sum(streamMd5), - HashUtils.hash(HashUtils.HashAlg.MD5, streamHash)); + assertEquals(HashUtils.md5sum(streamMd5), + HashUtils.hash(HashUtils.HashAlg.MD5, streamHash), + "Hash mismatch"); } @Test public void testMd5sumString() { - assertEquals("MD5 mismatch", "6dbd01b4309de2c22b027eb35a3ce18b", - HashUtils.md5sum("Lorem Ipsum")); + assertEquals( "6dbd01b4309de2c22b027eb35a3ce18b", + HashUtils.md5sum("Lorem Ipsum"), + "MD5 mismatch"); } @Test public void testMd5sumByteArray() { - assertEquals("MD5 mismatch", "6dbd01b4309de2c22b027eb35a3ce18b", - HashUtils.md5sum("Lorem Ipsum".getBytes(StandardCharsets.UTF_8))); + assertEquals("6dbd01b4309de2c22b027eb35a3ce18b", + HashUtils.md5sum("Lorem Ipsum".getBytes(StandardCharsets.UTF_8)), + "MD5 mismatch"); } @Test public void testMd5sumFile() throws IOException { - assertEquals("MD5 mismatch", "3b83ef96387f14655fc854ddc3c6bd57", - HashUtils.md5sum(file)); + assertEquals("3b83ef96387f14655fc854ddc3c6bd57", + HashUtils.md5sum(file), + "MD5 mismatch"); } @Test public void testMd5sumPath() throws IOException { - assertEquals("MD5 mismatch", "3b83ef96387f14655fc854ddc3c6bd57", - HashUtils.md5sum(path)); + assertEquals("3b83ef96387f14655fc854ddc3c6bd57", + HashUtils.md5sum(path), + "MD5 mismatch"); } @Test public void testMd5sumInputStream() throws Exception { final InputStream stream = getClass().getResourceAsStream("/ASL-2.0.txt"); - assertEquals("MD5 mismatch", "3b83ef96387f14655fc854ddc3c6bd57", - HashUtils.md5sum(stream)); + assertEquals("3b83ef96387f14655fc854ddc3c6bd57", + HashUtils.md5sum(stream), + "MD5 mismatch"); } @Test public void testSha1String() { - assertEquals("SHA1 mismatch", "da39a3ee5e6b4b0d3255bfef95601890afd80709", HashUtils.sha1("")); - assertEquals("SHA1 mismatch", "0646164d30b3bd0023a1e6878712eb1b9b15a1da", - HashUtils.sha1("Lorem Ipsum")); + assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", + HashUtils.sha1(""), + "SHA1 mismatch"); + assertEquals("0646164d30b3bd0023a1e6878712eb1b9b15a1da", + HashUtils.sha1("Lorem Ipsum"), + "SHA1 mismatch"); } @Test public void testSha1ByteArray() { - assertEquals("SHA1 mismatch", "da39a3ee5e6b4b0d3255bfef95601890afd80709", HashUtils.sha1(new byte[0])); - assertEquals("SHA1 mismatch", "0646164d30b3bd0023a1e6878712eb1b9b15a1da", - HashUtils.sha1("Lorem Ipsum".getBytes(StandardCharsets.UTF_8))); + assertEquals( "da39a3ee5e6b4b0d3255bfef95601890afd80709", HashUtils.sha1(new byte[0]),"SHA1 mismatch"); + assertEquals( "0646164d30b3bd0023a1e6878712eb1b9b15a1da", + HashUtils.sha1("Lorem Ipsum".getBytes(StandardCharsets.UTF_8)), + "SHA1 mismatch"); } @Test public void testSha1File() throws IOException { - assertEquals("SHA1 mismatch", "2b8b815229aa8a61e483fb4ba0588b8b6c491890", - HashUtils.sha1(file)); + assertEquals("2b8b815229aa8a61e483fb4ba0588b8b6c491890", + HashUtils.sha1(file), + "SHA1 mismatch"); } @Test public void testSha1Path() throws IOException { - assertEquals("SHA1 mismatch", "2b8b815229aa8a61e483fb4ba0588b8b6c491890", - HashUtils.sha1(path)); + assertEquals("2b8b815229aa8a61e483fb4ba0588b8b6c491890", + HashUtils.sha1(path), + "SHA1 mismatch"); } @Test public void testSha1InputStream() throws Exception { final InputStream stream = getClass().getResourceAsStream("/ASL-2.0.txt"); - assertEquals("SHA1 mismatch", "2b8b815229aa8a61e483fb4ba0588b8b6c491890", - HashUtils.sha1(stream)); + assertEquals("2b8b815229aa8a61e483fb4ba0588b8b6c491890", + HashUtils.sha1(stream), + "SHA1 mismatch"); } @Test public void testSha256String() { - assertEquals("SHA256 mismatch", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - HashUtils.sha256("")); - assertEquals("SHA256 mismatch", "030dc1f936c3415aff3f3357163515190d347a28e758e1f717d17bae453541c9", - HashUtils.sha256("Lorem Ipsum")); + assertEquals("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + HashUtils.sha256(""), + "SHA256 mismatch"); + assertEquals("030dc1f936c3415aff3f3357163515190d347a28e758e1f717d17bae453541c9", + HashUtils.sha256("Lorem Ipsum"), + "SHA256 mismatch"); } @Test public void testSha256ByteArray() { - assertEquals("SHA256 mismatch", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - HashUtils.sha256(new byte[0])); - assertEquals("SHA256 mismatch", "030dc1f936c3415aff3f3357163515190d347a28e758e1f717d17bae453541c9", - HashUtils.sha256("Lorem Ipsum".getBytes(StandardCharsets.UTF_8))); + assertEquals("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + HashUtils.sha256(new byte[0]), + "SHA256 mismatch"); + assertEquals("030dc1f936c3415aff3f3357163515190d347a28e758e1f717d17bae453541c9", + HashUtils.sha256("Lorem Ipsum".getBytes(StandardCharsets.UTF_8)), + "SHA256 mismatch"); } @Test public void testSha256File() throws Exception { - assertEquals("SHA256 mismatch", "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", - HashUtils.sha256(file)); + assertEquals("cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", + HashUtils.sha256(file), + "SHA256 mismatch"); } @Test public void testSha256Path() throws Exception { - assertEquals("SHA256 mismatch", "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", - HashUtils.sha256(path)); + assertEquals("cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", + HashUtils.sha256(path), + "SHA256 mismatch"); } @Test public void testSha256InputStream() throws Exception { final InputStream stream = getClass().getResourceAsStream("/ASL-2.0.txt"); - assertEquals("SHA256 mismatch", "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", - HashUtils.sha256(stream)); + assertEquals( "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", + HashUtils.sha256(stream), + "SHA256 mismatch"); } @Test public void testSha512String() { - assertEquals("SHA512 mismatch", - "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", - HashUtils.sha512("")); - assertEquals("SHA512 mismatch", - "7ffb69027702d73e3376de17b1377c29eb61a5510bc6196b5a251dc83ef1b444e98138c0f60727ba0e945a62af0715ae5bb4a6d7435ef1bd8184c7c7c158f317", - HashUtils.sha512("Lorem Ipsum")); + assertEquals("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", + HashUtils.sha512(""), + "SHA256 mismatch"); + assertEquals("7ffb69027702d73e3376de17b1377c29eb61a5510bc6196b5a251dc83ef1b444e98138c0f60727ba0e945a62af0715ae5bb4a6d7435ef1bd8184c7c7c158f317", + HashUtils.sha512("Lorem Ipsum"), + "SHA256 mismatch"); } @Test public void testSha512ByteArray() { - assertEquals("SHA512 mismatch", - "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", - HashUtils.sha512(new byte[0])); - assertEquals("SHA512 mismatch", - "7ffb69027702d73e3376de17b1377c29eb61a5510bc6196b5a251dc83ef1b444e98138c0f60727ba0e945a62af0715ae5bb4a6d7435ef1bd8184c7c7c158f317", - HashUtils.sha512("Lorem Ipsum".getBytes(StandardCharsets.UTF_8))); + assertEquals("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", + HashUtils.sha512(new byte[0]), + "SHA512 mismatch"); + assertEquals("7ffb69027702d73e3376de17b1377c29eb61a5510bc6196b5a251dc83ef1b444e98138c0f60727ba0e945a62af0715ae5bb4a6d7435ef1bd8184c7c7c158f317", + HashUtils.sha512("Lorem Ipsum".getBytes(StandardCharsets.UTF_8)), + "SHA512 mismatch"); } @Test public void testSha512File() throws Exception { - assertEquals("SHA512 mismatch", - "98f6b79b778f7b0a15415bd750c3a8a097d650511cb4ec8115188e115c47053fe700f578895c097051c9bc3dfb6197c2b13a15de203273e1a3218884f86e90e8", - HashUtils.sha512(file)); + assertEquals("98f6b79b778f7b0a15415bd750c3a8a097d650511cb4ec8115188e115c47053fe700f578895c097051c9bc3dfb6197c2b13a15de203273e1a3218884f86e90e8", + HashUtils.sha512(file), + "SHA512 mismatch"); } @Test public void testSha512Path() throws Exception { - assertEquals("SHA512 mismatch", - "98f6b79b778f7b0a15415bd750c3a8a097d650511cb4ec8115188e115c47053fe700f578895c097051c9bc3dfb6197c2b13a15de203273e1a3218884f86e90e8", - HashUtils.sha512(path)); + assertEquals("98f6b79b778f7b0a15415bd750c3a8a097d650511cb4ec8115188e115c47053fe700f578895c097051c9bc3dfb6197c2b13a15de203273e1a3218884f86e90e8", + HashUtils.sha512(path), + "SHA512 mismatch"); } @Test public void testSha512InputStream() throws Exception { final InputStream stream = getClass().getResourceAsStream("/ASL-2.0.txt"); - assertEquals("SHA512 mismatch", - "98f6b79b778f7b0a15415bd750c3a8a097d650511cb4ec8115188e115c47053fe700f578895c097051c9bc3dfb6197c2b13a15de203273e1a3218884f86e90e8", - HashUtils.sha512(stream)); + assertEquals("98f6b79b778f7b0a15415bd750c3a8a097d650511cb4ec8115188e115c47053fe700f578895c097051c9bc3dfb6197c2b13a15de203273e1a3218884f86e90e8", + HashUtils.sha512(stream), + "SHA512 mismatch"); } } \ No newline at end of file diff --git a/utils/src/test/java/io/redlink/utils/PathUtilsTest.java b/utils/src/test/java/io/redlink/utils/PathUtilsTest.java index 4fce632..f078ba1 100644 --- a/utils/src/test/java/io/redlink/utils/PathUtilsTest.java +++ b/utils/src/test/java/io/redlink/utils/PathUtilsTest.java @@ -18,10 +18,7 @@ import org.apache.commons.io.IOUtils; import org.hamcrest.Matchers; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.BeforeAll; import java.io.IOException; import java.io.InputStream; @@ -30,29 +27,35 @@ import java.nio.file.StandardCopyOption; import java.nio.file.attribute.FileTime; import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; -import static org.junit.Assert.*; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * */ public class PathUtilsTest { - @ClassRule - public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + @TempDir + public static Path tempDir; private static Path sourceFolder; private static Path sourceFile; - @BeforeClass + @BeforeAll public static void prepareFiles() throws IOException { - sourceFile = temporaryFolder.newFile("ASL-2.0.txt").toPath(); + sourceFile = tempDir.resolve("ASL-2.0.txt"); Files.copy(PathUtilsTest.class.getResourceAsStream("/ASL-2.0.txt"), sourceFile, StandardCopyOption.REPLACE_EXISTING); - sourceFolder = temporaryFolder.newFolder("tree").toPath(); + sourceFolder = tempDir.resolve("tree"); + Files.createDirectories(sourceFolder); final Path foo = Files.createDirectories(sourceFolder.resolve("foo")); Files.copy(PathUtilsTest.class.getResourceAsStream("/ASL-2.0.txt"), foo.resolve("File1")); @@ -76,36 +79,36 @@ public static void prepareFiles() throws IOException { } @Test - public void testCopySingleFile() throws Exception { - final Path dest = temporaryFolder.newFolder("copy-single-file").toPath().resolve(UUID.randomUUID().toString()); + public void testCopySingleFile(@TempDir Path baseDir) throws Exception { + final Path dest = baseDir.resolve(UUID.randomUUID().toString()); Files.createDirectories(dest.getParent()); PathUtils.copy(sourceFile, dest); - assertTrue("destination not found", Files.exists(dest)); + assertTrue(Files.exists(dest), "destination not found"); try ( InputStream expected = Files.newInputStream(sourceFile); InputStream real = Files.newInputStream(dest) ) { - assertTrue("content mismatch", IOUtils.contentEquals(expected, real)); + assertTrue(IOUtils.contentEquals(expected, real), "content mismatch"); } assertThat("mtime", Files.getLastModifiedTime(dest), Matchers.greaterThan(Files.getLastModifiedTime(sourceFile))); } @Test - public void testCopySingleFilePreservingAttrs() throws Exception { - final Path dest = temporaryFolder.newFolder("copy-single-file-w-attrs").toPath().resolve(UUID.randomUUID().toString()); + public void testCopySingleFilePreservingAttrs(@TempDir Path baseDir) throws Exception { + final Path dest = baseDir.resolve(UUID.randomUUID().toString()); Files.createDirectories(dest.getParent()); PathUtils.copy(sourceFile, dest, true); - assertTrue("destination not found", Files.exists(dest)); + assertTrue(Files.exists(dest), "destination not found"); try ( InputStream expected = Files.newInputStream(sourceFile); InputStream real = Files.newInputStream(dest) ) { - assertTrue("content mismatch", IOUtils.contentEquals(expected, real)); + assertTrue(IOUtils.contentEquals(expected, real), "content mismatch"); } assertThat("mtime", Files.getLastModifiedTime(dest), Matchers.comparesEqualTo(Files.getLastModifiedTime(sourceFile))); @@ -113,15 +116,13 @@ public void testCopySingleFilePreservingAttrs() throws Exception { } @Test - public void testCopyTree() throws Exception { - final Path dest = temporaryFolder.newFolder("copy-tree").toPath(); - + public void testCopyTree(@TempDir Path dest) throws Exception { PathUtils.copyRecursive(sourceFolder, dest); Files.walk(sourceFolder) .map(sourceFolder::relativize) .map(dest::resolve) - .forEach(p -> assertTrue("exists " + p, Files.exists(p))); + .forEach(p -> assertTrue(Files.exists(p), "exists " + p)); Files.walk(sourceFolder) .filter(Files::isRegularFile) .map(sourceFolder::relativize) @@ -131,7 +132,7 @@ public void testCopyTree() throws Exception { InputStream expected = PathUtilsTest.class.getResourceAsStream("/ASL-2.0.txt"); InputStream real = Files.newInputStream(f) ) { - assertTrue("content " + f, IOUtils.contentEquals(expected, real)); + assertTrue(IOUtils.contentEquals(expected, real), "content " + f); } catch (IOException e) { fail("content of " + f + " " + e.getMessage()); } @@ -140,15 +141,13 @@ public void testCopyTree() throws Exception { } @Test - public void testCopyTreePreservingAttrs() throws Exception { - final Path dest = temporaryFolder.newFolder("copy-tree-w-attrs").toPath(); - + public void testCopyTreePreservingAttrs(@TempDir Path dest) throws Exception { PathUtils.copyRecursive(sourceFolder, dest, true); Files.walk(sourceFolder) .map(sourceFolder::relativize) .map(dest::resolve) - .forEach(p -> assertTrue("exists " + p, Files.exists(p))); + .forEach(p -> assertTrue(Files.exists(p), "exists " + p)); Files.walk(sourceFolder) .filter(Files::isRegularFile) .map(sourceFolder::relativize) @@ -158,7 +157,7 @@ public void testCopyTreePreservingAttrs() throws Exception { InputStream expected = PathUtilsTest.class.getResourceAsStream("/ASL-2.0.txt"); InputStream real = Files.newInputStream(f) ) { - assertTrue("content " + f, IOUtils.contentEquals(expected, real)); + assertTrue(IOUtils.contentEquals(expected, real), "content " + f); } catch (IOException e) { fail("content of " + f + " " + e.getMessage()); } @@ -167,7 +166,7 @@ public void testCopyTreePreservingAttrs() throws Exception { .map(sourceFolder::relativize) .forEach(p -> { try { - assertEquals("lastMod " + p, Files.getLastModifiedTime(sourceFolder.resolve(p)), Files.getLastModifiedTime(dest.resolve(p))); + assertEquals(Files.getLastModifiedTime(sourceFolder.resolve(p)), Files.getLastModifiedTime(dest.resolve(p)), "lastMod " + p); } catch (IOException e) { fail(e.getMessage()); } @@ -175,16 +174,14 @@ public void testCopyTreePreservingAttrs() throws Exception { } @Test - public void testDeleteRecursive() throws Exception { - final Path dest1 = temporaryFolder.newFolder().toPath(); - assertTrue("source not found", Files.exists(dest1)); + public void testDeleteRecursive(@TempDir Path dest1, @TempDir Path dest2) throws Exception { + assertTrue(Files.exists(dest1), "source not found"); PathUtils.deleteRecursive(dest1); - assertFalse("target not deleted", Files.exists(dest1)); + assertFalse(Files.exists(dest1), "target not deleted"); - final Path dest2 = temporaryFolder.newFolder().toPath(); PathUtils.copyRecursive(sourceFolder, dest2); - assertTrue("source not found", Files.exists(dest2)); + assertTrue(Files.exists(dest2), "source not found"); PathUtils.deleteRecursive(dest2); - assertFalse("target not deleted", Files.exists(dest2)); + assertFalse(Files.exists(dest2), "target not deleted"); } } \ No newline at end of file diff --git a/utils/src/test/java/io/redlink/utils/RandomUtilsTest.java b/utils/src/test/java/io/redlink/utils/RandomUtilsTest.java index fe2ee3d..2488332 100644 --- a/utils/src/test/java/io/redlink/utils/RandomUtilsTest.java +++ b/utils/src/test/java/io/redlink/utils/RandomUtilsTest.java @@ -16,9 +16,9 @@ package io.redlink.utils; import java.util.Random; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class RandomUtilsTest { @@ -26,21 +26,21 @@ public class RandomUtilsTest { @Test public void nextString() { for (int i = 0; i < 50; i++) { - assertEquals("random string length", i, RandomUtils.nextString(i).length()); + assertEquals(i, RandomUtils.nextString(i).length(), "random string length"); } } @Test public void nextStringRnd() { Random rnd = new Random(42); - assertEquals("pre-seed random string", "6FyS", RandomUtils.nextString(rnd, 4)); - assertEquals("pre-seed random string", "2X3wn3y0", RandomUtils.nextString(rnd, 8)); - assertEquals("pre-seed random string", "cWWOeQNjeDWN", RandomUtils.nextString(rnd, 12)); - assertEquals("pre-seed random string", "TN6iPQSqmhdR4Ppo", RandomUtils.nextString(rnd, 16)); - assertEquals("pre-seed random string", "RjpBpcptlzNu6wyGvRfJZR", RandomUtils.nextString(rnd, 22)); + assertEquals("6FyS", RandomUtils.nextString(rnd, 4), "pre-seed random string"); + assertEquals("2X3wn3y0", RandomUtils.nextString(rnd, 8), "pre-seed random string"); + assertEquals("cWWOeQNjeDWN", RandomUtils.nextString(rnd, 12), "pre-seed random string"); + assertEquals("TN6iPQSqmhdR4Ppo", RandomUtils.nextString(rnd, 16), "pre-seed random string"); + assertEquals("RjpBpcptlzNu6wyGvRfJZR", RandomUtils.nextString(rnd, 22), "pre-seed random string"); for (int i = 0; i < 50; i++) { - assertEquals("random string length", i, RandomUtils.nextString(rnd, i).length()); + assertEquals(i, RandomUtils.nextString(rnd, i).length(), "random string length"); } } diff --git a/utils/src/test/java/io/redlink/utils/ResourceLoaderUtilsTest.java b/utils/src/test/java/io/redlink/utils/ResourceLoaderUtilsTest.java index b53cfe1..b266966 100644 --- a/utils/src/test/java/io/redlink/utils/ResourceLoaderUtilsTest.java +++ b/utils/src/test/java/io/redlink/utils/ResourceLoaderUtilsTest.java @@ -17,105 +17,104 @@ package io.redlink.utils; import java.io.InputStream; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import static org.apache.commons.lang3.StringUtils.prependIfMissing; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeNotNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; /** */ -@RunWith(Parameterized.class) public class ResourceLoaderUtilsTest { - private final String resource; - - @Parameterized.Parameters(name = "{index}: \"{0}\"") - public static Iterable data() { - return Arrays.asList( - new Object[] {"/ASL-2.0.txt"}, - new Object[] {"HashUtilsTest.class"}, - new Object[] {"/org/junit/Test.class"} - ); - } - - public ResourceLoaderUtilsTest(String resource) { - this.resource = resource; - } - - @Test - public void testGetResourceAsPath_Class() throws Exception { - assumeNotNull("Could not read resource the classic way", - ResourceLoaderUtilsTest.class.getResource(resource)); + @ParamTest + public void testGetResourceAsPath_Class(String resource) throws Exception { + assumeTrue(ResourceLoaderUtilsTest.class.getResource(resource) != null, + "Could not read resource the classic way"); final Path resourceAsPath = ResourceLoaderUtils.getResourceAsPath(resource, ResourceLoaderUtilsTest.class); - assertNotNull("getResourceAsPath() returned null", resourceAsPath); + assertNotNull(resourceAsPath, "getResourceAsPath() returned null"); try ( InputStream expected = ResourceLoaderUtilsTest.class.getResourceAsStream(resource); InputStream real =Files.newInputStream(resourceAsPath) ) { - assertTrue("content differs!", IOUtils.contentEquals(expected, real)); + assertTrue(IOUtils.contentEquals(expected, real), "content differs!"); } } - @Test - public void testGetResourceAsPath_ClassLoader() throws Exception { + @ParamTest + public void testGetResourceAsPath_ClassLoader(String rsc) throws Exception { final String resource; - if (StringUtils.startsWith(this.resource, "/")) { - resource = this.resource.substring(1); + if (StringUtils.startsWith(rsc, "/")) { + resource = rsc.substring(1); } else { - resource = prependIfMissing(this.resource, this.getClass().getPackage().getName().replace('.', '/') + "/"); + resource = prependIfMissing(rsc, this.getClass().getPackage().getName().replace('.', '/') + "/"); } - assumeNotNull("Could not read resource the classic way", - ClassLoader.getSystemClassLoader().getResource(resource)); + assumeTrue(ClassLoader.getSystemClassLoader().getResource(resource) != null, + "Could not read resource the classic way"); final Path resourceAsPath = ResourceLoaderUtils.getResourceAsPath(resource, ClassLoader.getSystemClassLoader()); - assertNotNull("getResourceAsPath() returned null", resourceAsPath); + assertNotNull(resourceAsPath, "getResourceAsPath() returned null"); try ( InputStream expected = ClassLoader.getSystemClassLoader().getResourceAsStream(resource); InputStream real = Files.newInputStream(resourceAsPath) ) { - assertTrue("content differs!", IOUtils.contentEquals(expected, real)); + assertTrue(IOUtils.contentEquals(expected, real), "content differs!"); } } - @Test - public void testGetResourceAsPath() throws Exception { + @ParamTest + public void testGetResourceAsPath(String rsc) throws Exception { final String resource; - if (StringUtils.startsWith(this.resource, "/")) { - resource = this.resource.substring(1); + if (StringUtils.startsWith(rsc, "/")) { + resource = rsc.substring(1); } else { - resource = prependIfMissing(this.resource, this.getClass().getPackage().getName().replace('.', '/') + "/"); + resource = prependIfMissing(rsc, this.getClass().getPackage().getName().replace('.', '/') + "/"); } - assumeNotNull("Could not read resource the classic way", - Thread.currentThread().getContextClassLoader().getResource(resource)); + assumeTrue(Thread.currentThread().getContextClassLoader().getResource(resource) != null, + "Could not read resource the classic way"); final Path resourceAsPath = ResourceLoaderUtils.getResourceAsPath(resource); - assertNotNull("getResourceAsPath() returned null", resourceAsPath); + assertNotNull(resourceAsPath, "getResourceAsPath() returned null"); try ( InputStream expected = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); InputStream real = Files.newInputStream(resourceAsPath) ) { - assertTrue("content differs!", IOUtils.contentEquals(expected, real)); + assertTrue(IOUtils.contentEquals(expected, real), "content differs!"); } } - @Test - public void testNullResource() { - assertNull("non-existing resource", - ResourceLoaderUtils.getResourceAsPath(resource + ".does-not-exist")); + @ParamTest + public void testNullResource(String resource) { + assertNull( + ResourceLoaderUtils.getResourceAsPath(resource + ".does-not-exist"), + "non-existing resource" + ); } + + @ParameterizedTest(name = "{index}: \"{0}\"") + @ValueSource(strings = { + "/ASL-2.0.txt", + "HashUtilsTest.class", + "/org/junit/Test.class" + }) + @Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD}) + @Retention(RetentionPolicy.RUNTIME) + @interface ParamTest {} + } \ No newline at end of file diff --git a/utils/src/test/java/io/redlink/utils/concurrent/GateTest.java b/utils/src/test/java/io/redlink/utils/concurrent/GateTest.java index 1bc4428..345fc05 100644 --- a/utils/src/test/java/io/redlink/utils/concurrent/GateTest.java +++ b/utils/src/test/java/io/redlink/utils/concurrent/GateTest.java @@ -24,39 +24,40 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class GateTest { @Test public void testStateCheck() { final Gate gate = new Gate(); - assertTrue("Initial State is closed", gate.isClosed()); + assertTrue(gate.isClosed(), "Initial State is closed"); gate.open(); - assertTrue("Gate is opened", gate.isOpen()); - assertFalse("Gate is not closed", gate.isClosed()); + assertTrue(gate.isOpen(), "Gate is opened"); + assertFalse(gate.isClosed(), "Gate is not closed"); gate.close(); - assertFalse("Gate is not opened", gate.isOpen()); - assertTrue("Gate is closed", gate.isClosed()); + assertFalse(gate.isOpen(), "Gate is not opened"); + assertTrue(gate.isClosed(), "Gate is closed"); gate.setClosed(false); - assertTrue("Gate is open", gate.isOpen()); + assertTrue(gate.isOpen(), "Gate is open"); gate.setClosed(true); - assertTrue("Gate is closed", gate.isClosed()); + assertTrue(gate.isClosed(), "Gate is closed"); } - @Test(timeout = 100L) + @Test + @Timeout(100) public void testOpenGate() throws InterruptedException { final Gate gate = new Gate(false); - Assert.assertTrue("Gate is open", gate.tryAwait(-1, TimeUnit.MILLISECONDS)); - Assert.assertTrue("Gate is open", gate.tryAwait(Duration.ofMillis(1).negated())); + assertTrue(gate.tryAwait(-1, TimeUnit.MILLISECONDS), "Gate is open"); + assertTrue(gate.tryAwait(Duration.ofMillis(1).negated()), "Gate is open"); } @Test @@ -103,15 +104,16 @@ public void testWithTimout() throws InterruptedException, TimeoutException { public void testTryAwait() throws InterruptedException { final Gate gate = new Gate(); - assertFalse("Gate opened", gate.tryAwait(Duration.ofMillis(5))); - assertFalse("Gate opened", gate.tryAwait(5, TimeUnit.MILLISECONDS)); + assertFalse(gate.tryAwait(Duration.ofMillis(5)), "Gate opened"); + assertFalse(gate.tryAwait(5, TimeUnit.MILLISECONDS), "Gate opened"); gate.open(); - assertTrue("Gate closed", gate.tryAwait(Duration.ofMillis(5))); - assertTrue("Gate closed", gate.tryAwait(5, TimeUnit.MILLISECONDS)); + assertTrue(gate.tryAwait(Duration.ofMillis(5)), "Gate closed"); + assertTrue(gate.tryAwait(5, TimeUnit.MILLISECONDS), "Gate closed"); } - @Test(timeout = 100L) + @Test + @Timeout(100) public void testPlainAwait() throws InterruptedException { final Gate gate = new Gate(); @@ -143,7 +145,7 @@ public void testPlainAwait() throws InterruptedException { gate.await(); postBarrier.await(); gate.await(); - assertTrue("Awaited", success.get()); + assertTrue(success.get(), "Awaited"); }