Skip to content

Commit

Permalink
Migrate to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Sep 19, 2024
1 parent a5b2a00 commit a8e5dd1
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 203 deletions.
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

<properties>
<checkstyle.violation.ignore>MagicNumber</checkstyle.violation.ignore>

<mockito.version>4.11.0</mockito.version>
<!-- override from parent pom, here we need more control -->
<maven-enforcer-plugin.version>3.5.0</maven-enforcer-plugin.version>
<!-- we need dot less properties for .vm -->
Expand Down Expand Up @@ -99,14 +99,20 @@

<!-- testing support -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
Expand All @@ -31,13 +32,9 @@
* Test helper for working with {@link Artifact}s.
*/
public class ArtifactBuilder {
private String groupId = "groupId";
private String artifactId = "artifactId";
private VersionRange versionRange = VersionRange.createFromVersion("1.0");
private String scope = "scope";
private String type = "type";
private String classifier = "classifier";
private File fileOrDirectory = getAnyFile();
private Path fileOrDirectory = getAnyFile();

public static ArtifactBuilder newBuilder() {
return new ArtifactBuilder();
Expand All @@ -58,29 +55,30 @@ public ArtifactBuilder withAnyDirectory() {
return this;
}

public ArtifactBuilder withFileOrDirectory(File directory) {
public ArtifactBuilder withFileOrDirectory(Path directory) {
fileOrDirectory = directory;
return this;
}

public Artifact build() {
Artifact artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, classifier, null);
artifact.setFile(fileOrDirectory);
Artifact artifact =
new DefaultArtifact("groupId", "artifactId", versionRange, "scope", type, "classifier", null);
artifact.setFile(fileOrDirectory.toFile());

return artifact;
}

private static File getAnyFile() {
private static Path getAnyFile() {
// the actual file isn't important, just so long as it exists
URL url = ArtifactBuilder.class.getResource("/utf8.txt");
try {
return new File(url.toURI());
return new File(url.toURI()).toPath();
} catch (URISyntaxException exception) {
throw new RuntimeException(exception);
}
}

private File getAnyDirectory() {
return getAnyFile().getParentFile();
private Path getAnyDirectory() {
return getAnyFile().getParent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.artifact.Artifact;
import org.junit.rules.TemporaryFolder;

/**
* Test utility to make writing tests with {@link ClassFile}s easier.
*/
public class ClassFileHelper {
private static int uniqueId = 0;

private final TemporaryFolder temporaryFolder;
private final Path temporaryFolder;

public ClassFileHelper(TemporaryFolder temporaryFolder) {
public ClassFileHelper(Path temporaryFolder) {
this.temporaryFolder = temporaryFolder;
}

public ClassFile createWithContent(String pathToClassFile, String fileContents) throws IOException {
uniqueId++;
String uniqueIdStr = Integer.toString(uniqueId);

File tempDirectory = createTempDirectory(uniqueIdStr);
createClassFile(tempDirectory, pathToClassFile, fileContents);
Path tempDirectory = Files.createTempDirectory(temporaryFolder, uniqueIdStr);
createClassFile(tempDirectory.toFile(), pathToClassFile, fileContents);

Artifact artifact = ArtifactBuilder.newBuilder()
.withFileOrDirectory(tempDirectory)
Expand All @@ -53,17 +53,7 @@ public ClassFile createWithContent(String pathToClassFile, String fileContents)
.build();

return new ClassFile(
pathToClassFile,
artifact,
() -> Files.newInputStream(tempDirectory.toPath().resolve(pathToClassFile)));
}

private File createTempDirectory(String uniqueIdStr) {
try {
return temporaryFolder.newFolder(uniqueIdStr);
} catch (IOException exception) {
throw new RuntimeException("unable to create temporary folder", exception);
}
pathToClassFile, artifact, () -> Files.newInputStream(tempDirectory.resolve(pathToClassFile)));
}

private void createClassFile(File directory, String pathToClassFile, String fileContents) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package org.codehaus.mojo.extraenforcer.dependencies;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.nio.file.Path;

import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ClassFileTest {
private static final String PATH_TO_CLASS_FILE =
ClassFileTest.class.getName().replace('.', '/') + ".class";

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
private ClassFileHelper classFileHelper;

private final ClassFileHelper classFileHelper = new ClassFileHelper(tempFolder);
@BeforeEach
void setUp(@TempDir Path tempFolder) {
classFileHelper = new ClassFileHelper(tempFolder);
}

@Test
public void getHashComputesHashOfFile() throws Exception {
void getHashComputesHashOfFile() throws Exception {
ClassFile classFile = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "the content of the file");

assertEquals("7b7f48e1c0e847133d8881d5743d253756bf44e490e2252556ad4816a0a77b67", classFile.getHash());
}

@Test
public void getHashReturnsConsistentHashWhenInvokedTwice() throws Exception {
void getHashReturnsConsistentHashWhenInvokedTwice() throws Exception {
ClassFile classFile = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content");

String hash1 = classFile.getHash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@
* under the License.
*/

import java.io.IOException;
import java.nio.file.Path;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerLogger;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

public class ClassesWithSameNameTest {
Expand All @@ -48,13 +50,10 @@ public class ClassesWithSameNameTest {

private static final EnforcerLogger LOG = mock(EnforcerLogger.class);

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private ClassFileHelper classFileHelper;

@Before
public void beforeEachTest() {
@BeforeEach
void beforeEachTest(@TempDir Path temporaryFolder) {
classFileHelper = new ClassFileHelper(temporaryFolder);
}

Expand All @@ -64,7 +63,7 @@ public void beforeEachTest() {
* files are exactly the same.
*/
@Test
public void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicate() throws Exception {
void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicate() throws Exception {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
Expand All @@ -75,7 +74,7 @@ public void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicate() throws Excep
}

@Test
public void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateButBytecodeIsIdentical() throws Exception {
void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateButBytecodeIsIdentical() throws Exception {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "content matches in both");
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "content matches in both");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
Expand All @@ -86,7 +85,7 @@ public void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateButBytecodeIsI
}

@Test
public void hasDuplicatesShouldReturnFalseWhenClassHasNoDuplicates() throws Exception {
void hasDuplicatesShouldReturnFalseWhenClassHasNoDuplicates() throws Exception {
ClassFile classFile = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile);

Expand All @@ -104,7 +103,7 @@ public void hasDuplicatesShouldReturnFalseWhenClassHasNoDuplicates() throws Exce
* bytecode).
*/
@Test
public void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicateButBytecodeDiffers() throws Exception {
void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicateButBytecodeDiffers() throws Exception {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "1");
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "2");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
Expand All @@ -122,7 +121,7 @@ public void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicateButBytecodeDiff
* We set the test up so it finds duplicates only if the bytecode differs.
*/
@Test
public void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateAndBytecodeDiffers() throws Exception {
void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateAndBytecodeDiffers() throws Exception {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "1");
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "2");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
Expand All @@ -136,7 +135,7 @@ public void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateAndBytecodeDif
* This tests the normal condition where we just output the class file path.
*/
@Test
public void toOutputStringOutputsPlainArtifactWhenJustNamesAreDuplicate() throws Exception {
void toOutputStringOutputsPlainArtifactWhenJustNamesAreDuplicate() throws Exception {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
Expand All @@ -152,7 +151,7 @@ public void toOutputStringOutputsPlainArtifactWhenJustNamesAreDuplicate() throws
* determine which artifacts they can ignore when fix the BanDuplicateClasses error.
*/
@Test
public void toOutputStringOutputsTwoArtifactsWhereBytecodeIsExactMatch() throws Exception {
void toOutputStringOutputsTwoArtifactsWhereBytecodeIsExactMatch() throws Exception {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "content matches in both");
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "content matches in both");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
Expand All @@ -171,8 +170,7 @@ public void toOutputStringOutputsTwoArtifactsWhereBytecodeIsExactMatch() throws
* 1 and 2 don't match 3 and 4.
*/
@Test
public void toOutputStringOutputsFourArtifactsWhereBytecodeIsExactMatchInTwoAndExactMatchInOtherTwo()
throws Exception {
void toOutputStringOutputsFourArtifactsWhereBytecodeIsExactMatchInTwoAndExactMatchInOtherTwo() throws Exception {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 1 and 2");
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 1 and 2");
ClassFile classFile3 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 3 and 4");
Expand All @@ -194,16 +192,15 @@ public void toOutputStringOutputsFourArtifactsWhereBytecodeIsExactMatchInTwoAndE
* The method should return the 2nd-to-last element in the last, but if there's only 1 element
* there's no 2nd-to-last element to return.
*/
@Test(expected = IllegalArgumentException.class)
public void previousShouldThrowIfOnlyOneArtifact() throws Exception {
@Test
void previousShouldThrowIfOnlyOneArtifact() throws IOException {
ClassFile classFile = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 1 and 2");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile);

classesWithSameName.previous();
assertThrows(IllegalArgumentException.class, classesWithSameName::previous);
}

@Test
public void previousShouldReturn2ndToLastElement() throws Exception {
void previousShouldReturn2ndToLastElement() throws Exception {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 1 and 2");
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 1 and 2");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
Expand All @@ -214,7 +211,7 @@ public void previousShouldReturn2ndToLastElement() throws Exception {
}

@Test
public void addShouldAddArtifact() throws Exception {
void addShouldAddArtifact() throws Exception {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1);
Expand All @@ -224,25 +221,25 @@ public void addShouldAddArtifact() throws Exception {
assertEquals(2, classesWithSameName.getAllArtifactsThisClassWasFoundIn().size());
}

@Test(expected = IllegalArgumentException.class)
public void addShouldThrowWhenClassNameDoesNotMatch() throws Exception {
@Test
void addShouldThrowWhenClassNameDoesNotMatch() throws IOException {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassFile classFile2 = classFileHelper.createWithContent("some/other/path.class", "");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1);

classesWithSameName.add(classFile2);
assertThrows(IllegalArgumentException.class, () -> classesWithSameName.add(classFile2));
}

@Test(expected = IllegalArgumentException.class)
public void constructorShouldThrowWhenClassNameDoesNotMatch() throws Exception {
@Test
void constructorShouldThrowWhenClassNameDoesNotMatch() throws IOException {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassFile classFile2 = classFileHelper.createWithContent("some/other/path.class", "");

new ClassesWithSameName(LOG, classFile1, classFile2);
assertThrows(IllegalArgumentException.class, () -> {
new ClassesWithSameName(LOG, classFile1, classFile2);
});
}

@Test
public void getAllArtifactsThisClassWasFoundInShouldReturnAllArtifacts() throws Exception {
void getAllArtifactsThisClassWasFoundInShouldReturnAllArtifacts() throws Exception {
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package org.codehaus.mojo.extraenforcer.dependencies;

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 EnforceBytecodeVersionTest {
class EnforceBytecodeVersionTest {

@Test
public void renderVersion() {
void renderVersion() {
assertEquals("JDK 1.5", EnforceBytecodeVersion.renderVersion(49, 0));
assertEquals("JDK 1.7", EnforceBytecodeVersion.renderVersion(51, 0));
assertEquals("JDK 11", EnforceBytecodeVersion.renderVersion(55, 0));
Expand Down
Loading

0 comments on commit a8e5dd1

Please sign in to comment.