Skip to content

Commit

Permalink
Enable project to use JUnit5 in mix with JUnit4
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsuter committed Jul 19, 2024
1 parent 1310971 commit 69115ad
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 87 deletions.
19 changes: 16 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<maven.version>3.6.3</maven.version>
<!-- version should match ivy Engine sfl4j version! And version in EngineClassLoaderFactory.SLF4J_VERSION -->
<slf4j.version>2.0.13</slf4j.version>
<junit-jupiter.version>5.10.2</junit-jupiter.version>
<site.path>snapshot</site.path>
<other.site.path>release</other.site.path>
<other.site.name>Stable</other.site.name>
Expand Down Expand Up @@ -164,9 +165,15 @@

<!-- TEST scoped -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -175,6 +182,12 @@
<version>3.26.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
48 changes: 26 additions & 22 deletions src/test/java/ch/ivyteam/ivy/maven/engine/TestClasspathJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,72 @@
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");

} finally {
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
Expand Down

0 comments on commit 69115ad

Please sign in to comment.