diff --git a/pom.xml b/pom.xml index d1389de..d8232b4 100644 --- a/pom.xml +++ b/pom.xml @@ -87,7 +87,7 @@ 2.16.1 3.17.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 @@ -225,6 +226,7 @@ 3.10.0 ${java.version} + ${java.version} @@ -315,8 +317,9 @@ coverage-report + verify - report + report-aggregate ${project.build.directory}/jacoco 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..5dcc0e3 100644 --- a/signals/src/test/java/io/redlink/utils/signal/SignalsHelperTest.java +++ b/signals/src/test/java/io/redlink/utils/signal/SignalsHelperTest.java @@ -20,31 +20,21 @@ 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; - } +class SignalsHelperTest { @Test - public void testSigUsr2() { + void testSigUsr2() { final String s = "USR2"; final AtomicInteger counter = new AtomicInteger(); @@ -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() { + void testRegisterClearUsr2() { testRegisterClear("USR2"); } - @Test - public void testWithEnum() { + @ParameterizedTest + @EnumSource(SignalsHelper.SIG.class) + void testWithEnum(SignalsHelper.SIG signalToTest) { + assertOsSupport(signalToTest); testRegisterClear(signalToTest); } - @Test - public void testWithSignalName() { + @ParameterizedTest + @EnumSource(SignalsHelper.SIG.class) + 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,24 @@ 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; + default: //nop + } + } } diff --git a/slf4j/pom.xml b/slf4j/pom.xml index 2361c6e..0d3f501 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..9228453 100644 --- a/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextBuilderTest.java +++ b/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextBuilderTest.java @@ -21,21 +21,22 @@ 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 - public void setUp() { +class LoggingContextBuilderTest { + + @BeforeEach + void setUp() { MDC.clear(); } @Test - public void testWithMDC() { + void testWithMDC() { final String value = UUID.randomUUID().toString(); Map ctx = new HashMap<>(); ctx.put("mdc", value); @@ -49,7 +50,7 @@ public void testWithMDC() { } @Test - public void testWrapRunnable() { + void testWrapRunnable() { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); MDC.put("mdc", value); @@ -61,7 +62,7 @@ public void testWrapRunnable() { } @Test - public void testCleanWrapRunnable() { + void testCleanWrapRunnable() { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); MDC.put("mdc", value); @@ -73,7 +74,7 @@ public void testCleanWrapRunnable() { } @Test - public void testWrapCallable() throws Exception { + void testWrapCallable() throws Exception { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); MDC.put("mdc", value); @@ -87,7 +88,7 @@ public void testWrapCallable() throws Exception { } @Test - public void testWrapFunction() { + void testWrapFunction() { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); MDC.put("mdc", value); @@ -102,7 +103,7 @@ public void testWrapFunction() { } @Test - public void testWrapSupplier() { + void testWrapSupplier() { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); MDC.put("mdc", value); @@ -116,7 +117,7 @@ public void testWrapSupplier() { } @Test - public void testWrapConsumer() { + void testWrapConsumer() { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); MDC.put("mdc", value); 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..c6d7c61 100644 --- a/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextTest.java +++ b/slf4j/src/test/java/io/redlink/utils/logging/LoggingContextTest.java @@ -25,21 +25,21 @@ 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 { +class LoggingContextTest { - @Before - public void setUp() { + @BeforeEach + void setUp() { MDC.clear(); } @Test - public void testLoggingContext() { + void testLoggingContext() { MDC.put("foo", "before"); try (LoggingContext ignored = LoggingContext.create()) { @@ -55,7 +55,7 @@ public void testLoggingContext() { } @Test - public void testCleanLoggingContext() { + void testCleanLoggingContext() { MDC.put("foo", "before"); try (LoggingContext ignored = new LoggingContext(true)) { @@ -72,7 +72,7 @@ public void testCleanLoggingContext() { } @Test - public void testWithEmpty() { + void testWithEmpty() { try (LoggingContext ignored = LoggingContext.empty()) { assertThat("Empty Context", MDC.get("foo"), CoreMatchers.nullValue()); @@ -83,7 +83,7 @@ public void testWithEmpty() { } @Test - public void testWrapRunnable() { + void testWrapRunnable() { final String value = UUID.randomUUID().toString(); MDC.put("mdc", value); LoggingContext.wrap(() -> { @@ -94,7 +94,7 @@ public void testWrapRunnable() { } @Test - public void testWrapCallable() throws Exception { + void testWrapCallable() throws Exception { final String value = UUID.randomUUID().toString(); MDC.put("mdc", value); LoggingContext.wrap((Callable) () -> { @@ -107,7 +107,7 @@ public void testWrapCallable() throws Exception { } @Test - public void testWrapFunction() { + void testWrapFunction() { final String value = UUID.randomUUID().toString(); MDC.put("mdc", value); LoggingContext.wrap((String s) -> { @@ -121,7 +121,7 @@ public void testWrapFunction() { } @Test - public void testWrapSupplier() { + void testWrapSupplier() { final String value = UUID.randomUUID().toString(); MDC.put("mdc", value); LoggingContext.wrap((Supplier) () -> { @@ -134,7 +134,7 @@ public void testWrapSupplier() { } @Test - public void testWrapConsumer() { + void testWrapConsumer() { final String value = UUID.randomUUID().toString(); MDC.put("mdc", value); LoggingContext.wrap((String s) -> { @@ -146,7 +146,7 @@ public void testWrapConsumer() { } @Test - public void testWrapInCopyRunable() throws Throwable { + void testWrapInCopyRunable() throws Throwable { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); @@ -171,7 +171,7 @@ public void testWrapInCopyRunable() throws Throwable { } @Test - public void testWrapInCopyCallable() throws Throwable { + void testWrapInCopyCallable() throws Throwable { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); @@ -197,7 +197,7 @@ public void testWrapInCopyCallable() throws Throwable { } @Test - public void testWrapInCopyFunction() { + void testWrapInCopyFunction() { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); final String value3 = UUID.randomUUID().toString(); @@ -205,7 +205,7 @@ public void testWrapInCopyFunction() { MDC.put("mdc", value); try (LoggingContext ctx = LoggingContext.create()) { MDC.put("mdc", value2); - final Function function = ctx.wrapInCopy((name) -> { + final Function function = ctx.wrapInCopy((String name) -> { assertThat(MDC.get("mdc"), CoreMatchers.is(value2)); MDC.put("mdc", UUID.randomUUID().toString()); return "hello " + name; @@ -220,7 +220,7 @@ public void testWrapInCopyFunction() { } @Test - public void testWrapInCopySupplier() { + void testWrapInCopySupplier() { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); final String value3 = UUID.randomUUID().toString(); @@ -243,7 +243,7 @@ public void testWrapInCopySupplier() { } @Test - public void testWrapInCopyConsumer() { + void testWrapInCopyConsumer() { final String value = UUID.randomUUID().toString(); final String value2 = UUID.randomUUID().toString(); final String value3 = UUID.randomUUID().toString(); diff --git a/test/testcontainers/pom.xml b/test/testcontainers/pom.xml index 95ef514..ceb5b4a 100644 --- a/test/testcontainers/pom.xml +++ b/test/testcontainers/pom.xml @@ -29,12 +29,6 @@ Redlink Testcontainers - - junit - junit - compile - - io.redlink.utils utils @@ -47,6 +41,12 @@ compile + + org.junit.vintage + junit-vintage-engine + ${junit.version} + test + org.mongodb mongodb-driver-sync diff --git a/test/testcontainers/src/test/java/io/redlink/utils/test/testcontainers/VindContainerTest.java b/test/testcontainers/src/test/java/io/redlink/utils/test/testcontainers/VindContainerTest.java index 2cc9dae..71ea88e 100644 --- a/test/testcontainers/src/test/java/io/redlink/utils/test/testcontainers/VindContainerTest.java +++ b/test/testcontainers/src/test/java/io/redlink/utils/test/testcontainers/VindContainerTest.java @@ -18,12 +18,11 @@ import java.io.IOException; import java.util.Arrays; -import java.util.Collection; import java.util.Date; -import java.util.List; +import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; -import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.impl.HttpJdkSolrClient; import org.apache.solr.client.solrj.request.CoreAdminRequest; import org.apache.solr.client.solrj.response.CoreAdminResponse; import org.apache.solr.client.solrj.response.QueryResponse; @@ -38,7 +37,6 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.lessThan; import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.*; public class VindContainerTest { @@ -85,7 +83,7 @@ public void testMultipleVindCollections() throws IOException { private void validate(VindContainer vindContainer, String coreName) throws IOException { - try (HttpSolrClient solrClient = new HttpSolrClient.Builder(vindContainer.getSolrUrl()).build()) { + try (SolrClient solrClient = new HttpJdkSolrClient.Builder(vindContainer.getSolrUrl()).build()) { final CoreAdminResponse adminResponse = CoreAdminRequest.getStatus(coreName, solrClient); @@ -96,7 +94,7 @@ private void validate(VindContainer vindContainer, String coreName) throws IOExc Assert.fail(e.getMessage()); } - try (HttpSolrClient solrClient = new HttpSolrClient.Builder(vindContainer.getCoreUrl(coreName)).build()) { + try (SolrClient solrClient = new HttpJdkSolrClient.Builder(vindContainer.getCoreUrl(coreName)).build()) { final SolrPingResponse ping = solrClient.ping(); MatcherAssert.assertThat("ping", ping.getStatus(), is(0)); diff --git a/test/testcontainers/src/test/java/io/redlink/utils/test/testcontainers/ZookeeperContainerTest.java b/test/testcontainers/src/test/java/io/redlink/utils/test/testcontainers/ZookeeperContainerTest.java index 9155dbf..c9fc634 100644 --- a/test/testcontainers/src/test/java/io/redlink/utils/test/testcontainers/ZookeeperContainerTest.java +++ b/test/testcontainers/src/test/java/io/redlink/utils/test/testcontainers/ZookeeperContainerTest.java @@ -50,7 +50,7 @@ public void testRUOK() throws IOException { try (ZookeeperContainer zkContainer = new ZookeeperContainer()) { zkContainer.start(); - try (Socket s = new Socket(zkContainer.getContainerIpAddress(), zkContainer.getMappedPort(ZookeeperContainer.CONNECT_PORT))) { + try (Socket s = new Socket(zkContainer.getHost(), zkContainer.getMappedPort(ZookeeperContainer.CONNECT_PORT))) { s.setKeepAlive(true); try (OutputStream os = s.getOutputStream()) { 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..d7b0414 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.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class ChecksumUtilsTest { - @ClassRule - public static TemporaryFolder temporaryFolder = new TemporaryFolder(); +class ChecksumUtilsTest { - private static Path path; + @TempDir + private static Path tempDir; - private static File file; + private static Path path; - @BeforeClass - public static void setUp() throws IOException { - file = temporaryFolder.newFile("ASL.txt"); - path = file.toPath(); + @BeforeAll + static void setUp() throws IOException { + 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")); + void testCrc32String() { + assertEquals("358ad45d", + ChecksumUtils.crc32("Lorem Ipsum"), "CRC32 mismatch"); } @Test - public void testCrc32ByteArray() { - assertEquals("CRC32 mismatch", "358ad45d", - ChecksumUtils.crc32("Lorem Ipsum".getBytes(StandardCharsets.UTF_8))); + void testCrc32ByteArray() { + 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)); + void testCrc32File() throws IOException { + assertEquals("86e2b4b4", + ChecksumUtils.crc32(path.toFile()), "CRC32 mismatch"); } @Test - public void testCrc32Path() throws IOException { - assertEquals("CRC32 mismatch", "86e2b4b4", - ChecksumUtils.crc32(path)); + void testCrc32Path() throws IOException { + assertEquals("86e2b4b4", + ChecksumUtils.crc32(path), "CRC32 mismatch"); } @Test - public void testCrc32InputStream() throws Exception { + 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")); + void testAdler32String() { + assertEquals("1867042e", + ChecksumUtils.adler32("Lorem Ipsum"), "ADLER32 mismatch"); } @Test - public void testAdler32ByteArray() { - assertEquals("ADLER32 mismatch", "1867042e", - ChecksumUtils.adler32("Lorem Ipsum".getBytes(StandardCharsets.UTF_8))); + void testAdler32ByteArray() { + 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)); + void testAdler32File() throws IOException { + assertEquals("3a27ec70", + ChecksumUtils.adler32(path.toFile()), "ADLER32 mismatch"); } @Test - public void testAdler32Path() throws IOException { - assertEquals("ADLER32 mismatch", "3a27ec70", - ChecksumUtils.adler32(path)); + void testAdler32Path() throws IOException { + assertEquals("3a27ec70", + ChecksumUtils.adler32(path), "ADLER32 mismatch"); } @Test - public void testAdler32InputStream() throws Exception { + 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..f179d43 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,207 +25,230 @@ 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 { +class HashUtilsTest { - @ClassRule - public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + @TempDir + static Path tempDir; private static Path path; private static File file; - @BeforeClass - public static void setUp() throws IOException { - file = temporaryFolder.newFile("ASL.txt"); - path = file.toPath(); + @BeforeAll + static void setUp() throws IOException { + 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")); + void testHashString() { + 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))); + void testHashByteArray() { + 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)); + void testHashFile() throws IOException { + 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)); + void testHashPath() throws IOException { + assertEquals(HashUtils.md5sum(path), + HashUtils.hash(HashUtils.HashAlg.MD5, path), + "Hash mismatch"); } @Test - public void testHashInputStream() throws Exception { + 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")); + void testMd5sumString() { + assertEquals( "6dbd01b4309de2c22b027eb35a3ce18b", + HashUtils.md5sum("Lorem Ipsum"), + "MD5 mismatch"); } @Test - public void testMd5sumByteArray() { - assertEquals("MD5 mismatch", "6dbd01b4309de2c22b027eb35a3ce18b", - HashUtils.md5sum("Lorem Ipsum".getBytes(StandardCharsets.UTF_8))); + void testMd5sumByteArray() { + 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)); + void testMd5sumFile() throws IOException { + assertEquals("3b83ef96387f14655fc854ddc3c6bd57", + HashUtils.md5sum(file), + "MD5 mismatch"); } @Test - public void testMd5sumPath() throws IOException { - assertEquals("MD5 mismatch", "3b83ef96387f14655fc854ddc3c6bd57", - HashUtils.md5sum(path)); + void testMd5sumPath() throws IOException { + assertEquals("3b83ef96387f14655fc854ddc3c6bd57", + HashUtils.md5sum(path), + "MD5 mismatch"); } @Test - public void testMd5sumInputStream() throws Exception { + 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")); + void testSha1String() { + 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))); + void testSha1ByteArray() { + 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)); + void testSha1File() throws IOException { + assertEquals("2b8b815229aa8a61e483fb4ba0588b8b6c491890", + HashUtils.sha1(file), + "SHA1 mismatch"); } @Test - public void testSha1Path() throws IOException { - assertEquals("SHA1 mismatch", "2b8b815229aa8a61e483fb4ba0588b8b6c491890", - HashUtils.sha1(path)); + void testSha1Path() throws IOException { + assertEquals("2b8b815229aa8a61e483fb4ba0588b8b6c491890", + HashUtils.sha1(path), + "SHA1 mismatch"); } @Test - public void testSha1InputStream() throws Exception { + 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")); + void testSha256String() { + 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))); + void testSha256ByteArray() { + 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)); + void testSha256File() throws Exception { + assertEquals("cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", + HashUtils.sha256(file), + "SHA256 mismatch"); } @Test - public void testSha256Path() throws Exception { - assertEquals("SHA256 mismatch", "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", - HashUtils.sha256(path)); + void testSha256Path() throws Exception { + assertEquals("cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30", + HashUtils.sha256(path), + "SHA256 mismatch"); } @Test - public void testSha256InputStream() throws Exception { + 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")); + void testSha512String() { + 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))); + void testSha512ByteArray() { + 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)); + void testSha512File() throws Exception { + assertEquals("98f6b79b778f7b0a15415bd750c3a8a097d650511cb4ec8115188e115c47053fe700f578895c097051c9bc3dfb6197c2b13a15de203273e1a3218884f86e90e8", + HashUtils.sha512(file), + "SHA512 mismatch"); } @Test - public void testSha512Path() throws Exception { - assertEquals("SHA512 mismatch", - "98f6b79b778f7b0a15415bd750c3a8a097d650511cb4ec8115188e115c47053fe700f578895c097051c9bc3dfb6197c2b13a15de203273e1a3218884f86e90e8", - HashUtils.sha512(path)); + void testSha512Path() throws Exception { + assertEquals("98f6b79b778f7b0a15415bd750c3a8a097d650511cb4ec8115188e115c47053fe700f578895c097051c9bc3dfb6197c2b13a15de203273e1a3218884f86e90e8", + HashUtils.sha512(path), + "SHA512 mismatch"); } @Test - public void testSha512InputStream() throws Exception { + 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..141e4c5 100644 --- a/utils/src/test/java/io/redlink/utils/PathUtilsTest.java +++ b/utils/src/test/java/io/redlink/utils/PathUtilsTest.java @@ -16,13 +16,6 @@ package io.redlink.utils; -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 java.io.IOException; import java.io.InputStream; import java.nio.file.Files; @@ -30,29 +23,40 @@ import java.nio.file.StandardCopyOption; import java.nio.file.attribute.FileTime; import java.util.UUID; +import java.util.function.Consumer; +import java.util.stream.Stream; +import org.apache.commons.io.IOUtils; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeAll; +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 { +class PathUtilsTest { - @ClassRule - public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + @TempDir + static Path tempDir; private static Path sourceFolder; private static Path sourceFile; - @BeforeClass - public static void prepareFiles() throws IOException { - sourceFile = temporaryFolder.newFile("ASL-2.0.txt").toPath(); + @BeforeAll + static void prepareFiles() throws IOException { + 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")); @@ -65,47 +69,48 @@ public static void prepareFiles() throws IOException { //MacOS is not so specific on modification times, so let's trick a little. final long yesterday = System.currentTimeMillis() - 24 * 60 * 60 * 1000; Files.setLastModifiedTime(sourceFile, FileTime.fromMillis(yesterday)); - Files.walk(sourceFolder) + walk(sourceFolder, s -> s .forEach(f -> { try { Files.setLastModifiedTime(f, FileTime.fromMillis(yesterday)); } catch (IOException e) { fail(e.getMessage()); } - }); + }) + ); } @Test - public void testCopySingleFile() throws Exception { - final Path dest = temporaryFolder.newFolder("copy-single-file").toPath().resolve(UUID.randomUUID().toString()); + 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()); + 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,16 +118,15 @@ public void testCopySingleFilePreservingAttrs() throws Exception { } @Test - public void testCopyTree() throws Exception { - final Path dest = temporaryFolder.newFolder("copy-tree").toPath(); - + void testCopyTree(@TempDir Path dest) throws Exception { PathUtils.copyRecursive(sourceFolder, dest); - Files.walk(sourceFolder) + walk(sourceFolder, s -> s .map(sourceFolder::relativize) .map(dest::resolve) - .forEach(p -> assertTrue("exists " + p, Files.exists(p))); - Files.walk(sourceFolder) + .forEach(p -> assertTrue(Files.exists(p), "exists " + p)) + ); + walk(sourceFolder, s -> s .filter(Files::isRegularFile) .map(sourceFolder::relativize) .map(dest::resolve) @@ -131,25 +135,25 @@ 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()); } - }); + }) + ); } @Test - public void testCopyTreePreservingAttrs() throws Exception { - final Path dest = temporaryFolder.newFolder("copy-tree-w-attrs").toPath(); - + void testCopyTreePreservingAttrs(@TempDir Path dest) throws Exception { PathUtils.copyRecursive(sourceFolder, dest, true); - Files.walk(sourceFolder) + walk(sourceFolder, s -> s .map(sourceFolder::relativize) .map(dest::resolve) - .forEach(p -> assertTrue("exists " + p, Files.exists(p))); - Files.walk(sourceFolder) + .forEach(p -> assertTrue(Files.exists(p), "exists " + p)) + ); + walk(sourceFolder, s -> s .filter(Files::isRegularFile) .map(sourceFolder::relativize) .map(dest::resolve) @@ -158,33 +162,39 @@ 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()); } - }); - Files.walk(sourceFolder) + }) + ); + walk(sourceFolder, s -> s .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()); } - }); + }) + ); } @Test - public void testDeleteRecursive() throws Exception { - final Path dest1 = temporaryFolder.newFolder().toPath(); - assertTrue("source not found", Files.exists(dest1)); + 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"); + } + + private static void walk(Path start, Consumer> consumer) throws IOException { + try (Stream stream = Files.walk(start)) { + consumer.accept(stream); + } } } \ 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..54bf5e3 100644 --- a/utils/src/test/java/io/redlink/utils/RandomUtilsTest.java +++ b/utils/src/test/java/io/redlink/utils/RandomUtilsTest.java @@ -16,31 +16,31 @@ 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 { +class RandomUtilsTest { @Test - public void nextString() { + 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() { + 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..8c1cfc7 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 { +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 + 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 + 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 + 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 + 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/apache/commons/lang3/StringUtils.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..085207d 100644 --- a/utils/src/test/java/io/redlink/utils/concurrent/GateTest.java +++ b/utils/src/test/java/io/redlink/utils/concurrent/GateTest.java @@ -24,43 +24,44 @@ 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 { +class GateTest { @Test - public void testStateCheck() { + 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) - public void testOpenGate() throws InterruptedException { + @Test + @Timeout(100) + 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 - public void testSimpleSync() throws InterruptedException { + void testSimpleSync() throws InterruptedException { final Gate gate = new Gate(true); final Future f15 = createWorker(gate, Duration.ofSeconds(15), 15L); @@ -86,7 +87,7 @@ public void testSimpleSync() throws InterruptedException { } @Test - public void testWithTimout() throws InterruptedException, TimeoutException { + void testWithTimout() throws InterruptedException, TimeoutException { final Gate gate = new Gate(); final Future trueFuture = createWorker(gate, Duration.ofMillis(15), Boolean.TRUE); @@ -100,19 +101,20 @@ public void testWithTimout() throws InterruptedException, TimeoutException { } @Test - public void testTryAwait() throws InterruptedException { + 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) - public void testPlainAwait() throws InterruptedException { + @Test + @Timeout(100) + void testPlainAwait() throws InterruptedException { final Gate gate = new Gate(); final AtomicBoolean success = new AtomicBoolean(false); @@ -143,7 +145,7 @@ public void testPlainAwait() throws InterruptedException { gate.await(); postBarrier.await(); gate.await(); - assertTrue("Awaited", success.get()); + assertTrue(success.get(), "Awaited"); }