From 69115ad794ef7e2e7e3da0b2292207c375e7f517 Mon Sep 17 00:00:00 2001 From: Alexander Suter Date: Fri, 19 Jul 2024 16:40:49 +0200 Subject: [PATCH] Enable project to use JUnit5 in mix with JUnit4 --- pom.xml | 19 +++++- .../maven/TestSlf4jWarningConfiguration.java | 7 +-- .../ivy/maven/engine/TestClasspathJar.java | 48 ++++++++------- .../engine/TestEngineVersionEvaluator.java | 38 +++++------- .../engine/deploy/TestYamlOptionsFactory.java | 15 +++-- .../deploy/dir/TestFileLogForwarder.java | 61 ++++++++++++------- .../download/TestLatestMinorVersionRange.java | 6 +- 7 files changed, 107 insertions(+), 87 deletions(-) diff --git a/pom.xml b/pom.xml index e93b9e59..51c810ed 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,7 @@ 3.6.3 2.0.13 + 5.10.2 snapshot release Stable @@ -164,9 +165,15 @@ - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + org.junit.vintage + junit-vintage-engine + ${junit-jupiter.version} test @@ -175,6 +182,12 @@ 3.26.3 test + + org.awaitility + awaitility + 4.2.1 + test + org.apache.maven.plugin-testing maven-plugin-testing-harness diff --git a/src/test/java/ch/ivyteam/ivy/maven/TestSlf4jWarningConfiguration.java b/src/test/java/ch/ivyteam/ivy/maven/TestSlf4jWarningConfiguration.java index 4ec7d797..5ae30929 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/TestSlf4jWarningConfiguration.java +++ b/src/test/java/ch/ivyteam/ivy/maven/TestSlf4jWarningConfiguration.java @@ -2,21 +2,20 @@ import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.simple.SimpleLogger; import ch.ivyteam.ivy.maven.engine.Slf4jSimpleEngineProperties; -public class TestSlf4jWarningConfiguration { +class TestSlf4jWarningConfiguration { /* * XIVY-3123 Streamline the log output to be maven-like, instead of logging * [WARN] we want [WARNING]. This allows us to use the maven log parser on our * jenkins pipelines to avoid introducing new warnings. */ @Test - public void mavenLikeWarning() { + void mavenLikeWarning() { Slf4jSimpleEngineProperties.install(); - assertThat(System.getProperty(SimpleLogger.WARN_LEVEL_STRING_KEY)) .as("SLF4J warning string is not maven-like [WARNING]") .isEqualTo("WARNING"); diff --git a/src/test/java/ch/ivyteam/ivy/maven/engine/TestClasspathJar.java b/src/test/java/ch/ivyteam/ivy/maven/engine/TestClasspathJar.java index c54f39ad..87701bd3 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/engine/TestClasspathJar.java +++ b/src/test/java/ch/ivyteam/ivy/maven/engine/TestClasspathJar.java @@ -18,38 +18,42 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.util.Arrays; +import java.nio.file.Path; +import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import org.apache.commons.io.IOUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import ch.ivyteam.ivy.maven.util.ClasspathJar; -public class TestClasspathJar { +class TestClasspathJar { + + @TempDir + Path tempDir; @Test - public void readWriteClasspath() throws IOException { - File jarFile = Files.createTempFile("my", ".jar").toFile(); - ClasspathJar jar = new ClasspathJar(jarFile); - File content = Files.createTempFile("content", ".jar").toFile(); - jar.createFileEntries(Arrays.asList(content)); - - assertThat(jar.getClasspathFiles()).contains(content.getName()); - - ZipInputStream jarStream = new ZipInputStream(new FileInputStream(jarFile)); - ZipEntry first = jarStream.getNextEntry(); - assertThat(first.getName()).isEqualTo("META-INF/MANIFEST.MF"); - String manifest = IOUtils.toString(jarStream, StandardCharsets.UTF_8); - assertThat(manifest) - .as("Manifest should not start with a whitespace or it will not be interpreted by the JVM") - .startsWith("Manifest-Version:"); + void readWriteClasspath() throws IOException { + var jarFile = tempDir.resolve("my.jar"); + Files.createFile(jarFile); + ClasspathJar jar = new ClasspathJar(jarFile.toFile()); + var content = tempDir.resolve("content.jar"); + Files.createFile(content); + jar.createFileEntries(List.of(content.toFile())); + + assertThat(jar.getClasspathFiles()).contains(content.getFileName().toString()); + + try (var in = new ZipInputStream(Files.newInputStream(jarFile))) { + ZipEntry first = in.getNextEntry(); + assertThat(first.getName()).isEqualTo("META-INF/MANIFEST.MF"); + String manifest = new String(in.readAllBytes(), StandardCharsets.UTF_8); + assertThat(manifest) + .as("Manifest should not start with a whitespace or it will not be interpreted by the JVM") + .startsWith("Manifest-Version:"); + } } - } diff --git a/src/test/java/ch/ivyteam/ivy/maven/engine/TestEngineVersionEvaluator.java b/src/test/java/ch/ivyteam/ivy/maven/engine/TestEngineVersionEvaluator.java index 360f7a9e..ca73b89b 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/engine/TestEngineVersionEvaluator.java +++ b/src/test/java/ch/ivyteam/ivy/maven/engine/TestEngineVersionEvaluator.java @@ -2,39 +2,29 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.io.File; +import java.io.IOException; import java.nio.file.Files; -import java.util.UUID; +import java.nio.file.Path; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import ch.ivyteam.ivy.maven.engine.EngineClassLoaderFactory.OsgiDir; -public class TestEngineVersionEvaluator { +class TestEngineVersionEvaluator { - @Test - public void isOSGiEngine_invalid() { - File tempDir = createTempDir(); - assertThat(EngineVersionEvaluator.isOSGiEngine(tempDir)).isFalse(); - } + @TempDir + Path tempDir; @Test - public void isOSGiEngine_valid() { - File tempDir = createTempDir(); - File systemDir = new File(tempDir, OsgiDir.INSTALL_AREA); - systemDir.mkdir(); - systemDir.deleteOnExit(); - assertThat(EngineVersionEvaluator.isOSGiEngine(tempDir)).isTrue(); + void isOSGiEngine_invalid() { + assertThat(EngineVersionEvaluator.isOSGiEngine(tempDir.toFile())).isFalse(); } - private static File createTempDir() { - try { - File tmpDir = Files.createTempDirectory(UUID.randomUUID().toString()).toFile(); - tmpDir.deleteOnExit(); - return tmpDir; - } catch (Exception e) { - throw new RuntimeException(e); - } + @Test + void isOSGiEngine_valid() throws IOException { + var systemDir = tempDir.resolve(OsgiDir.INSTALL_AREA); + Files.createDirectories(systemDir); + assertThat(EngineVersionEvaluator.isOSGiEngine(tempDir.toFile())).isTrue(); } - } diff --git a/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/TestYamlOptionsFactory.java b/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/TestYamlOptionsFactory.java index a89f01a7..7eb4f8af 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/TestYamlOptionsFactory.java +++ b/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/TestYamlOptionsFactory.java @@ -3,18 +3,17 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; -import java.io.InputStream; import java.nio.charset.StandardCharsets; -import org.apache.commons.io.IOUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import ch.ivyteam.ivy.maven.deploy.DeployToEngineMojo; import ch.ivyteam.ivy.maven.deploy.DeployToEngineMojo.DefaultDeployOptions; -public class TestYamlOptionsFactory { +class TestYamlOptionsFactory { + @Test - public void yamlWithAllNonDefaultOptions() throws Exception { + void yamlWithAllNonDefaultOptions() throws Exception { DeployToEngineMojo config = new DeployToEngineMojo(); config.deployTestUsers = "true"; config.deployTargetVersion = "RELEASED"; @@ -26,13 +25,13 @@ public void yamlWithAllNonDefaultOptions() throws Exception { } private String getFileContent(String file) throws IOException { - try (InputStream is = getClass().getResourceAsStream(file)) { - return IOUtils.toString(is, StandardCharsets.UTF_8); + try (var in = getClass().getResourceAsStream(file)) { + return new String(in.readAllBytes(), StandardCharsets.UTF_8); } } @Test - public void yamlWithAllDefaultOptions() throws Exception { + void yamlWithAllDefaultOptions() throws Exception { DeployToEngineMojo config = new DeployToEngineMojo(); config.deployTestUsers = DefaultDeployOptions.DEPLOY_TEST_USERS; config.deployTargetVersion = DefaultDeployOptions.VERSION_AUTO; diff --git a/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/dir/TestFileLogForwarder.java b/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/dir/TestFileLogForwarder.java index b030adba..3d91dcc8 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/dir/TestFileLogForwarder.java +++ b/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/dir/TestFileLogForwarder.java @@ -17,42 +17,47 @@ package ch.ivyteam.ivy.maven.engine.deploy.dir; import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; -import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; -import org.apache.commons.io.FileUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import ch.ivyteam.ivy.maven.log.LogCollector; import ch.ivyteam.ivy.maven.log.LogCollector.LogEntry; -public class TestFileLogForwarder { +class TestFileLogForwarder { + + @TempDir + Path tempDir; @Test - public void fileToMavenLog() throws Exception { - File fakeEngineLog = Files.createTempFile("myProject.iar", ".deploymentLog").toFile(); - LogCollector mavenLog = new LogCollector(); - FileLogForwarder logForwarder = new FileLogForwarder(fakeEngineLog, mavenLog, - new EngineLogLineHandler(mavenLog)); + void fileToMavenLog() throws Exception { + var fakeEngineLog = tempDir.resolve("myProject.iar.deploymentLog"); + Files.createFile(fakeEngineLog); + var mavenLog = new LogCollector(); + var logForwarder = new FileLogForwarder(fakeEngineLog.toFile(), mavenLog, new EngineLogLineHandler(mavenLog)); + var log = new FakeLogger(fakeEngineLog); try { logForwarder.activate(); - logAndWait(fakeEngineLog, "WARNING: starting"); - assertThat(mavenLog.getWarnings()).hasSize(1); + log.write("WARNING: starting"); + await().untilAsserted(() -> assertThat(mavenLog.getWarnings()).hasSize(1)); LogEntry firstEntry = mavenLog.getWarnings().get(mavenLog.getWarnings().size() - 1); assertThat(firstEntry.toString()).isEqualTo(" ENGINE: starting"); - logAndWait(fakeEngineLog, "WARNING: finished"); - assertThat(mavenLog.getWarnings()).hasSize(2); + log.write("WARNING: finished"); + await().untilAsserted(() -> assertThat(mavenLog.getWarnings()).hasSize(2)); LogEntry lastEntry = mavenLog.getWarnings().get(mavenLog.getWarnings().size() - 1); assertThat(lastEntry.toString()).isEqualTo(" ENGINE: finished"); - logAndWait(fakeEngineLog, "INFO: hi"); - assertThat(mavenLog.getDebug()).hasSize(1); + log.write("INFO: hi"); + await().untilAsserted(() -> assertThat(mavenLog.getDebug()).hasSize(1)); LogEntry debugEntry = mavenLog.getDebug().get(mavenLog.getDebug().size() - 1); assertThat(debugEntry.toString()).isEqualTo(" ENGINE: hi"); @@ -60,14 +65,24 @@ public void fileToMavenLog() throws Exception { logForwarder.deactivate(); } - logAndWait(fakeEngineLog, "WARNING: illegal"); - assertThat(mavenLog.getWarnings()).hasSize(2); + log.write("WARNING: illegal"); + await().untilAsserted(() -> assertThat(mavenLog.getWarnings()).hasSize(2)); } - private static void logAndWait(File fakeEngineLog, String log) throws IOException, InterruptedException { - boolean append = true; - FileUtils.write(fakeEngineLog, log, StandardCharsets.UTF_8, append); - Thread.sleep(1000); - } + private static final class FakeLogger { + private final Path file; + + private FakeLogger(Path file) { + this.file = file; + } + + private void write(String log) { + try { + Files.writeString(file, log, StandardOpenOption.APPEND); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } } diff --git a/src/test/java/ch/ivyteam/ivy/maven/engine/download/TestLatestMinorVersionRange.java b/src/test/java/ch/ivyteam/ivy/maven/engine/download/TestLatestMinorVersionRange.java index 0993911c..c5e53e7b 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/engine/download/TestLatestMinorVersionRange.java +++ b/src/test/java/ch/ivyteam/ivy/maven/engine/download/TestLatestMinorVersionRange.java @@ -3,12 +3,12 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public class TestLatestMinorVersionRange { +class TestLatestMinorVersionRange { @Test - public void get() { + void get() { assertThat(new LatestMinorVersionRange("8.0.0").get().toString()).isEqualTo("[8.0.0,8.1.0)"); assertThat(new LatestMinorVersionRange("8.0.1").get().toString()).isEqualTo("[8.0.1,8.1.0)"); assertThat(new LatestMinorVersionRange("8.1.0").get().toString()).isEqualTo("[8.1.0,8.2.0)");