From 493edc10d068f9bb0414799b86ff2a3c11cd8dbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Moreira?= Date: Wed, 19 Jun 2024 21:31:13 +0200 Subject: [PATCH] Upgrade to junit API 5 --- backend/pom.xml | 6 +-- .../app/config/DataStoreConfigTest.java | 33 ++++++------ .../switchboard/core/DataStoreTest.java | 20 ++++---- .../switchboard/core/LinkMetadataTest.java | 6 +-- .../switchboard/core/MediaLibraryTest.java | 51 +++++++++++-------- .../clarin/switchboard/core/QuirksTest.java | 4 +- .../switchboard/core/ToolRegistryTest.java | 8 +-- .../resources/DataResourceTest.java | 8 +-- 8 files changed, 72 insertions(+), 64 deletions(-) diff --git a/backend/pom.xml b/backend/pom.xml index 00272911..cba70ab2 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -73,9 +73,9 @@ - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter-api + 5.11.0-M2 test diff --git a/backend/src/test/java/eu/clarin/switchboard/app/config/DataStoreConfigTest.java b/backend/src/test/java/eu/clarin/switchboard/app/config/DataStoreConfigTest.java index 8072894e..57e90131 100644 --- a/backend/src/test/java/eu/clarin/switchboard/app/config/DataStoreConfigTest.java +++ b/backend/src/test/java/eu/clarin/switchboard/app/config/DataStoreConfigTest.java @@ -1,29 +1,30 @@ package eu.clarin.switchboard.app.config; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import static eu.clarin.switchboard.app.config.DataStoreConfig.convertStringWithMultiplicatorToLong; public class DataStoreConfigTest { @Test public void testConvertStringWithMultiplicatorToLong() { - Assert.assertEquals(0, convertStringWithMultiplicatorToLong("0")); - Assert.assertEquals(1, convertStringWithMultiplicatorToLong("1")); - Assert.assertEquals(1000, convertStringWithMultiplicatorToLong("1000")); - Assert.assertEquals(999999, convertStringWithMultiplicatorToLong("999999")); - Assert.assertEquals(999999999999L, convertStringWithMultiplicatorToLong("999999999999")); - Assert.assertEquals(1234 * 1024, convertStringWithMultiplicatorToLong("1234k")); - Assert.assertEquals(1234 * 1024, convertStringWithMultiplicatorToLong("1234K")); - Assert.assertEquals(1234 * 1024 * 1024, convertStringWithMultiplicatorToLong("1234m")); - Assert.assertEquals(1234 * 1024 * 1024, convertStringWithMultiplicatorToLong("1234M")); - Assert.assertEquals(1234 * 1024 * 1024 * 1024L, convertStringWithMultiplicatorToLong("1234g")); - Assert.assertEquals(1234 * 1024 * 1024 * 1024L, convertStringWithMultiplicatorToLong("1234G")); + Assertions.assertEquals(0, convertStringWithMultiplicatorToLong("0")); + Assertions.assertEquals(1, convertStringWithMultiplicatorToLong("1")); + Assertions.assertEquals(1000, convertStringWithMultiplicatorToLong("1000")); + Assertions.assertEquals(999999, convertStringWithMultiplicatorToLong("999999")); + Assertions.assertEquals(999999999999L, convertStringWithMultiplicatorToLong("999999999999")); + Assertions.assertEquals(1234 * 1024, convertStringWithMultiplicatorToLong("1234k")); + Assertions.assertEquals(1234 * 1024, convertStringWithMultiplicatorToLong("1234K")); + Assertions.assertEquals(1234 * 1024 * 1024, convertStringWithMultiplicatorToLong("1234m")); + Assertions.assertEquals(1234 * 1024 * 1024, convertStringWithMultiplicatorToLong("1234M")); + Assertions.assertEquals(1234 * 1024 * 1024 * 1024L, convertStringWithMultiplicatorToLong("1234g")); + Assertions.assertEquals(1234 * 1024 * 1024 * 1024L, convertStringWithMultiplicatorToLong("1234G")); } - @Test(expected = NumberFormatException.class) + @Test public void testBadNumber() { - convertStringWithMultiplicatorToLong("23asdf"); - Assert.fail(); // will not get here + Assertions.assertThrows(NumberFormatException.class, () -> { + convertStringWithMultiplicatorToLong("23asdf"); + }); } } diff --git a/backend/src/test/java/eu/clarin/switchboard/core/DataStoreTest.java b/backend/src/test/java/eu/clarin/switchboard/core/DataStoreTest.java index 414ab027..4da1b9f5 100644 --- a/backend/src/test/java/eu/clarin/switchboard/core/DataStoreTest.java +++ b/backend/src/test/java/eu/clarin/switchboard/core/DataStoreTest.java @@ -2,8 +2,8 @@ import eu.clarin.switchboard.app.config.DataStoreConfig; import eu.clarin.switchboard.core.xc.StoragePolicyException; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -14,13 +14,13 @@ import java.util.List; import java.util.UUID; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class DataStoreTest { DataStore dataStore; - @Before + @BeforeEach public void setUp() throws Exception { Path dataStoreRoot = Files.createTempDirectory("switchboard-test-"); String maxSize = "1k"; @@ -93,13 +93,13 @@ public void eraseAllStorage() throws IOException, StoragePolicyException { assertEquals(storePath.toFile().listFiles().length, 0); } - @Test(expected = StoragePolicyException.class) + @Test public void uploadTooLarge() throws IOException, StoragePolicyException { - UUID id = UUID.randomUUID(); - InputStream is = new ByteArrayInputStream(new byte[2 * 1024]); - Path path = dataStore.save(id, "test", is); - - assert (false); // will not get here + assertThrows(StoragePolicyException.class, () -> { + UUID id = UUID.randomUUID(); + InputStream is = new ByteArrayInputStream(new byte[2 * 1024]); + Path path = dataStore.save(id, "test", is); + }); } @Test diff --git a/backend/src/test/java/eu/clarin/switchboard/core/LinkMetadataTest.java b/backend/src/test/java/eu/clarin/switchboard/core/LinkMetadataTest.java index 9866bfd6..6c5b5d36 100644 --- a/backend/src/test/java/eu/clarin/switchboard/core/LinkMetadataTest.java +++ b/backend/src/test/java/eu/clarin/switchboard/core/LinkMetadataTest.java @@ -5,12 +5,12 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.cache.CacheConfig; import org.apache.http.impl.client.cache.CachingHttpClients; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class LinkMetadataTest { @Test diff --git a/backend/src/test/java/eu/clarin/switchboard/core/MediaLibraryTest.java b/backend/src/test/java/eu/clarin/switchboard/core/MediaLibraryTest.java index 5812fe84..73a642c9 100644 --- a/backend/src/test/java/eu/clarin/switchboard/core/MediaLibraryTest.java +++ b/backend/src/test/java/eu/clarin/switchboard/core/MediaLibraryTest.java @@ -8,8 +8,8 @@ import eu.clarin.switchboard.core.xc.StoragePolicyException; import eu.clarin.switchboard.profiler.DefaultProfiler; import eu.clarin.switchboard.profiler.api.ProfilingException; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -18,6 +18,8 @@ import java.nio.file.Path; import java.util.Collections; +import static org.junit.jupiter.api.Assertions.assertThrows; + public class MediaLibraryTest { DataStoreConfig dataStoreConfig; DefaultStoragePolicy storagePolicy; @@ -25,7 +27,7 @@ public class MediaLibraryTest { DefaultProfiler profiler; UrlResolverConfig urlResolver; - @Before + @BeforeEach public void setUp() throws Exception { Path dataStoreRoot = Files.createTempDirectory("switchboard-test-"); String maxSize = "1M"; @@ -57,31 +59,36 @@ public void allOk() throws IOException, StoragePolicyException, ProfilingExcepti assert (true); } - @Test(expected = StoragePolicyException.class) + @Test public void tooManyFiles() throws IOException, StoragePolicyException, ProfilingException, StorageException { - MediaLibrary mediaLibrary = new MediaLibrary( - dataStore, profiler, profiler.getTextExtractor(), - storagePolicy, urlResolver, dataStoreConfig); - mediaLibrary.addFile("test", new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)), null); - mediaLibrary.addFile("test", new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)), null); - mediaLibrary.addFile("test", new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)), null); - assert (false); // will not get here + assertThrows(StoragePolicyException.class, () -> { + MediaLibrary mediaLibrary = new MediaLibrary( + dataStore, profiler, profiler.getTextExtractor(), + storagePolicy, urlResolver, dataStoreConfig); + mediaLibrary.addFile("test", new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)), null); + mediaLibrary.addFile("test", new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)), null); + mediaLibrary.addFile("test", new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)), null); + }); } - @Test(expected = LinkException.class) + @Test public void addMedia1() throws CommonException, ProfilingException { - MediaLibrary mediaLibrary = new MediaLibrary( - dataStore, profiler, profiler.getTextExtractor(), - storagePolicy, urlResolver, dataStoreConfig); - mediaLibrary.addByUrl("http://this^is&a)bad@url", null); + assertThrows(LinkException.class, () -> { + MediaLibrary mediaLibrary = new MediaLibrary( + dataStore, profiler, profiler.getTextExtractor(), + storagePolicy, urlResolver, dataStoreConfig); + mediaLibrary.addByUrl("http://this^is&a)bad@url", null); + }); } - @Test(expected = StoragePolicyException.class) + @Test public void addMedia2() throws CommonException, ProfilingException { - MediaLibrary mediaLibrary = new MediaLibrary( - dataStore, profiler, profiler.getTextExtractor(), - storagePolicy, urlResolver, dataStoreConfig); - // a site that does a HTTP redirect - mediaLibrary.addByUrl("http://clarin.eu", null); + assertThrows(StoragePolicyException.class, () -> { + MediaLibrary mediaLibrary = new MediaLibrary( + dataStore, profiler, profiler.getTextExtractor(), + storagePolicy, urlResolver, dataStoreConfig); + // a site that does a HTTP redirect + mediaLibrary.addByUrl("http://clarin.eu", null); + }); } } diff --git a/backend/src/test/java/eu/clarin/switchboard/core/QuirksTest.java b/backend/src/test/java/eu/clarin/switchboard/core/QuirksTest.java index d78ebef8..cc74c55c 100644 --- a/backend/src/test/java/eu/clarin/switchboard/core/QuirksTest.java +++ b/backend/src/test/java/eu/clarin/switchboard/core/QuirksTest.java @@ -2,14 +2,14 @@ import eu.clarin.switchboard.core.xc.LinkException; import eu.clarin.switchboard.profiler.api.Profile; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; import java.util.UUID; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class QuirksTest { @Test diff --git a/backend/src/test/java/eu/clarin/switchboard/core/ToolRegistryTest.java b/backend/src/test/java/eu/clarin/switchboard/core/ToolRegistryTest.java index bf028b7d..aee22b32 100644 --- a/backend/src/test/java/eu/clarin/switchboard/core/ToolRegistryTest.java +++ b/backend/src/test/java/eu/clarin/switchboard/core/ToolRegistryTest.java @@ -2,21 +2,21 @@ import eu.clarin.switchboard.profiler.api.Profile; import eu.clarin.switchboard.tool.Tool; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Set; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static java.util.Arrays.asList; public class ToolRegistryTest { ToolRegistry toolRegistry; - @Before + @BeforeEach public void setUp() throws Exception { toolRegistry = new ToolRegistry("./src/test/resources/registry"); } diff --git a/backend/src/test/java/eu/clarin/switchboard/resources/DataResourceTest.java b/backend/src/test/java/eu/clarin/switchboard/resources/DataResourceTest.java index 9c9dd906..21b82a5b 100644 --- a/backend/src/test/java/eu/clarin/switchboard/resources/DataResourceTest.java +++ b/backend/src/test/java/eu/clarin/switchboard/resources/DataResourceTest.java @@ -7,8 +7,8 @@ import eu.clarin.switchboard.core.MediaLibrary; import eu.clarin.switchboard.profiler.DefaultProfiler; import eu.clarin.switchboard.profiler.api.Profiler; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.StreamingOutput; @@ -21,12 +21,12 @@ import java.util.Collections; import java.util.Map; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class DataResourceTest { DataResource dataResource; - @Before + @BeforeEach public void setUp() throws Exception { Path dataStoreRoot = Files.createTempDirectory("switchboard-test-"); String maxSize = "1M";