Skip to content

Commit

Permalink
Convert tests to JUnit 5 (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Aug 29, 2023
1 parent 336a090 commit 9d5683f
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 350 deletions.
26 changes: 12 additions & 14 deletions plexus-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,20 @@
<version>1</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
Expand All @@ -59,6 +51,12 @@
<version>6.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,35 @@

import java.nio.file.Paths;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public abstract class AbstractFilenameModuleNameExtractorTest {
protected abstract ModuleNameExtractor getExtractor();

@BeforeClass
public static void assume() {
assumeThat("Requires at least Java 9", System.getProperty("java.version"), not(startsWith("1.")));
}

@Test
public void testJarWithoutManifest() throws Exception {
void testJarWithoutManifest() throws Exception {
String name = getExtractor().extract(Paths.get("src/test/resources/jar.empty/plexus-java-1.0.0-SNAPSHOT.jar"));
assertEquals("plexus.java", name);
}

@Test
public void testJarWithManifest() throws Exception {
void testJarWithManifest() throws Exception {
String name = getExtractor()
.extract(Paths.get("src/test/resources/jar.manifest.with/plexus-java-1.0.0-SNAPSHOT.jar"));
assertEquals("org.codehaus.plexus.languages.java", name);
}

@Test
public void testJarUnsupported() throws Exception {
void testJarUnsupported() throws Exception {
String name = getExtractor().extract(Paths.get("src/test/resources/jar.unsupported/jdom-1.0.jar"));
assertEquals(null, name);
assertNull(name);
}

@Test
public void testJarWithSpacesInPath() throws Exception {
void testJarWithSpacesInPath() throws Exception {
String name = getExtractor()
.extract(Paths.get("src/test/resources/jar with spaces in path/plexus-java-1.0.0-SNAPSHOT.jar"));
assertEquals("org.codehaus.plexus.languages.java", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,29 @@
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaProvides;
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires;
import org.codehaus.plexus.languages.java.version.JavaVersion;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class BinaryModuleInfoParserTest {
private BinaryModuleInfoParser parser = new BinaryModuleInfoParser();
class BinaryModuleInfoParserTest {
private final BinaryModuleInfoParser parser = new BinaryModuleInfoParser();

@Test
public void testJarDescriptor() throws Exception {
void testJarDescriptor() throws Exception {
JavaModuleDescriptor descriptor =
parser.getModuleDescriptor(Paths.get("src/test/resources/jar.descriptor/asm-6.0_BETA.jar"));

assertNotNull(descriptor);
assertEquals("org.objectweb.asm", descriptor.name());
assertEquals(false, descriptor.isAutomatic());
assertThat(descriptor.name()).isEqualTo("org.objectweb.asm");
assertFalse(descriptor.isAutomatic());

assertEquals(1, descriptor.requires().size());
assertThat(descriptor.requires()).hasSize(1);
assertEquals("java.base", descriptor.requires().iterator().next().name());

Set<JavaExports> expectedExports = JavaModuleDescriptor.newAutomaticModule("_")
Expand All @@ -66,17 +67,17 @@ public void testJarDescriptor() throws Exception {
}

@Test
public void testMultiReleaseJarDescriptor() throws Exception {
void testMultiReleaseJarDescriptor() throws Exception {
JavaModuleDescriptor descriptor = parser.getModuleDescriptor(
Paths.get("src/test/resources/jar.mr.descriptor/jloadr-1.0-SNAPSHOT.jar"), JavaVersion.parse("17"));

assertNotNull(descriptor);
assertEquals("de.adito.jloadr", descriptor.name());
assertEquals(false, descriptor.isAutomatic());
assertFalse(descriptor.isAutomatic());
}

@Test
public void testIncompleteMultiReleaseJarDescriptor() throws Exception {
void testIncompleteMultiReleaseJarDescriptor() throws Exception {
// this jar is missing the Multi-Release: true entry in the Manifest
JavaModuleDescriptor descriptor = parser.getModuleDescriptor(
Paths.get("src/test/resources/jar.mr.incomplete.descriptor/jloadr-1.0-SNAPSHOT.jar"));
Expand All @@ -85,23 +86,23 @@ public void testIncompleteMultiReleaseJarDescriptor() throws Exception {
}

@Test
public void testClassicJar() throws Exception {
void testClassicJar() throws Exception {
JavaModuleDescriptor descriptor =
parser.getModuleDescriptor(Paths.get("src/test/resources/jar.empty/plexus-java-1.0.0-SNAPSHOT.jar"));

assertNull(descriptor);
}

@Test
public void testOutputDirectoryDescriptor() throws Exception {
void testOutputDirectoryDescriptor() throws Exception {
JavaModuleDescriptor descriptor =
parser.getModuleDescriptor(Paths.get("src/test/resources/dir.descriptor/out"));

assertNotNull(descriptor);
assertEquals("org.codehaus.plexus.languages.java.demo", descriptor.name());
assertEquals(false, descriptor.isAutomatic());
assertFalse(descriptor.isAutomatic());

assertEquals(3, descriptor.requires().size());
assertThat(descriptor.requires()).hasSize(3);

Set<JavaRequires> expectedRequires = JavaModuleDescriptor.newAutomaticModule("_")
.requires("java.base")
Expand All @@ -113,19 +114,21 @@ public void testOutputDirectoryDescriptor() throws Exception {
assertEquals(expectedRequires, descriptor.requires());
}

@Test(expected = NoSuchFileException.class)
public void testClassicOutputDirectory() throws Exception {
parser.getModuleDescriptor(Paths.get("src/test/resources/dir.empty/out"));
@Test
void testClassicOutputDirectory() {
assertThrows(
NoSuchFileException.class,
() -> parser.getModuleDescriptor(Paths.get("src/test/resources/dir.empty/out")));
}

@Test
public void testJModDescriptor() throws Exception {
void testJModDescriptor() throws Exception {
JavaModuleDescriptor descriptor = parser.getModuleDescriptor(
Paths.get("src/test/resources/jmod.descriptor/first-jmod-1.0-SNAPSHOT.jmod"));

assertNotNull(descriptor);
assertEquals("com.corporate.project", descriptor.name());
assertEquals(false, descriptor.isAutomatic());
assertFalse(descriptor.isAutomatic());

assertEquals(1, descriptor.requires().size());
assertEquals("java.base", descriptor.requires().iterator().next().name());
Expand All @@ -135,13 +138,14 @@ public void testJModDescriptor() throws Exception {
"com.corporate.project", descriptor.exports().iterator().next().source());
}

@Test(expected = IOException.class)
public void testInvalidFile() throws Exception {
parser.getModuleDescriptor(Paths.get("src/test/resources/nonjar/pom.xml"));
@Test
void testInvalidFile() {
assertThrows(
IOException.class, () -> parser.getModuleDescriptor(Paths.get("src/test/resources/nonjar/pom.xml")));
}

@Test
public void testUses() throws Exception {
void testUses() throws Exception {
try (InputStream is =
Files.newInputStream(Paths.get("src/test/resources/dir.descriptor.uses/out/module-info.class"))) {
JavaModuleDescriptor descriptor = parser.parse(is);
Expand All @@ -157,7 +161,7 @@ public void testUses() throws Exception {
}

@Test
public void testProvides() throws Exception {
void testProvides() throws Exception {
JavaModuleDescriptor descriptor =
parser.getModuleDescriptor(Paths.get("src/test/resources/jar.service/threeten-extra-1.4.jar"));

Expand All @@ -182,21 +186,21 @@ public void testProvides() throws Exception {
}

@Test
public void testRequires() throws Exception {
void testRequires() throws Exception {
try (InputStream is =
Files.newInputStream(Paths.get("src/test/resources/dir.descriptor.requires/out/module-info.class"))) {
JavaModuleDescriptor descriptor = parser.parse(is);

assertNotNull(descriptor);
assertThat(descriptor.requires().size(), is(5));
assertThat(descriptor.requires()).hasSize(5);

Set<JavaRequires> expectedRequires = JavaModuleDescriptor.newAutomaticModule("_")
.requires("java.base")
.requires("mod_r")
.requires(Collections.singleton(JavaRequires.JavaModifier.STATIC), "mod_r_s")
.requires(Collections.singleton(JavaRequires.JavaModifier.TRANSITIVE), "mod_r_t")
.requires(
new HashSet<JavaRequires.JavaModifier>(Arrays.asList(
new HashSet<>(Arrays.asList(
JavaRequires.JavaModifier.STATIC, JavaRequires.JavaModifier.TRANSITIVE)),
"mod_r_s_t")
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@

import java.nio.file.Path;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

public class CmdModuleNameExtractorTest {
class CmdModuleNameExtractorTest {
@Test
public void testMethodCount() throws Exception {
void testMethodCount() throws Exception {
// ensure that both implementations are in sync
assertEquals(2, CmdModuleNameExtractor.class.getDeclaredMethods().length);
assertThat(CmdModuleNameExtractor.class.getDeclaredMethods().length).isEqualTo(2);

// if these don't exist, a NoSuchMethodException is thrown
CmdModuleNameExtractor.class.getDeclaredMethod("main", String[].class);
Expand Down
Loading

0 comments on commit 9d5683f

Please sign in to comment.