From a8e5dd1d1d50a4fba83ce53044008a5f4803fa5f Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Thu, 19 Sep 2024 18:09:42 +0200 Subject: [PATCH] Migrate to JUnit 5 --- pom.xml | 14 ++- .../dependencies/ArtifactBuilder.java | 22 +++-- .../dependencies/ClassFileHelper.java | 22 ++--- .../dependencies/ClassFileTest.java | 22 +++-- .../dependencies/ClassesWithSameNameTest.java | 71 +++++++-------- .../EnforceBytecodeVersionTest.java | 8 +- .../dependencies/JarUtilsTest.java | 14 +-- .../encoding/RequireEncodingTest.java | 14 +-- .../it/BanDuplicateClassesLogParser.java | 2 +- .../model/AbstractRequireRolesTest.java | 10 +-- .../model/RequirePropertyDivergesTest.java | 88 ++++++++++--------- .../extraenforcer/model/RequireRolesTest.java | 55 +++++++----- .../extraenforcer/model/RuleXpp3DomTest.java | 33 +++---- .../org/freebsd/file/FileEncodingTest.java | 28 +++--- 14 files changed, 200 insertions(+), 203 deletions(-) diff --git a/pom.xml b/pom.xml index 65954fd9..59a9e2d8 100644 --- a/pom.xml +++ b/pom.xml @@ -58,7 +58,7 @@ MagicNumber - + 4.11.0 3.5.0 @@ -99,14 +99,20 @@ - junit - junit + org.junit.jupiter + junit-jupiter-api test org.mockito mockito-core - 4.11.0 + ${mockito.version} + test + + + org.mockito + mockito-junit-jupiter + ${mockito.version} test diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ArtifactBuilder.java b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ArtifactBuilder.java index e113d538..f7a10366 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ArtifactBuilder.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ArtifactBuilder.java @@ -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; @@ -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(); @@ -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(); } } diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassFileHelper.java b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassFileHelper.java index 18326dc9..9c935215 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassFileHelper.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassFileHelper.java @@ -23,9 +23,9 @@ 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. @@ -33,9 +33,9 @@ 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; } @@ -43,8 +43,8 @@ public ClassFile createWithContent(String pathToClassFile, String fileContents) 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) @@ -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 { diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassFileTest.java b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassFileTest.java index ba0e09dc..fceac26f 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassFileTest.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassFileTest.java @@ -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(); diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassesWithSameNameTest.java b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassesWithSameNameTest.java index 862db405..a6f55975 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassesWithSameNameTest.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassesWithSameNameTest.java @@ -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 { @@ -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); } @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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"); @@ -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); @@ -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); @@ -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); diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/EnforceBytecodeVersionTest.java b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/EnforceBytecodeVersionTest.java index 2019f70e..964c2e75 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/EnforceBytecodeVersionTest.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/EnforceBytecodeVersionTest.java @@ -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)); diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/JarUtilsTest.java b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/JarUtilsTest.java index 719eb62a..097728ca 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/JarUtilsTest.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/dependencies/JarUtilsTest.java @@ -1,18 +1,18 @@ package org.codehaus.mojo.extraenforcer.dependencies; import org.apache.maven.artifact.Artifact; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.codehaus.mojo.extraenforcer.dependencies.ArtifactBuilder.newBuilder; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -public class JarUtilsTest { +class JarUtilsTest { /** * "Sunny day" test: the method should return true for a jar artifact. */ @Test - public void isJarFileShouldReturnTrueForJarFile() { + void isJarFileShouldReturnTrueForJarFile() { Artifact artifact = newBuilder().withType("jar").build(); assertTrue(JarUtils.isJarFile(artifact)); } @@ -22,7 +22,7 @@ public void isJarFileShouldReturnTrueForJarFile() { * a folder with a bunch of packages/class files in it). */ @Test - public void isJarFileShouldReturnFalseForDirectory() { + void isJarFileShouldReturnFalseForDirectory() { Artifact artifact = newBuilder().withType("jar").withAnyDirectory().build(); assertFalse(JarUtils.isJarFile(artifact)); } @@ -32,7 +32,7 @@ public void isJarFileShouldReturnFalseForDirectory() { * not "jar". For example: a war or a zip file. */ @Test - public void isJarFileShouldReturnFalseWhenArtifactTypeIsNotJar() { + void isJarFileShouldReturnFalseWhenArtifactTypeIsNotJar() { Artifact artifact = newBuilder().withType("war").build(); assertFalse(JarUtils.isJarFile(artifact)); } diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/encoding/RequireEncodingTest.java b/src/test/java/org/codehaus/mojo/extraenforcer/encoding/RequireEncodingTest.java index 356be29d..8d49b31d 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/encoding/RequireEncodingTest.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/encoding/RequireEncodingTest.java @@ -6,28 +6,28 @@ import org.apache.maven.enforcer.rule.api.EnforcerLogger; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.project.MavenProject; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class RequireEncodingTest { +class RequireEncodingTest { private RequireEncoding rule; private MavenProject project; - @Before - public void initFields() { + @BeforeEach + void initFields() { project = mock(MavenProject.class); rule = new RequireEncoding(project); rule.setLog(mock(EnforcerLogger.class)); } @Test - public void failUTF8() throws Exception { + void failUTF8() throws Exception { when(project.getBasedir()).thenReturn(new File("src/test/resources").getAbsoluteFile()); diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/it/BanDuplicateClassesLogParser.java b/src/test/java/org/codehaus/mojo/extraenforcer/it/BanDuplicateClassesLogParser.java index 01d9d07b..8bcd0949 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/it/BanDuplicateClassesLogParser.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/it/BanDuplicateClassesLogParser.java @@ -84,7 +84,7 @@ private static Set readFoundInJars(BufferedReader reader) throws IOExcep private static Set readDuplicateClasses(BufferedReader reader) throws IOException { Set classes = new HashSet<>(); - for (String line = reader.readLine(); line != null && line.length() > 0; line = reader.readLine()) { + for (String line = reader.readLine(); line != null && !line.isEmpty(); line = reader.readLine()) { classes.add(line.trim()); } return classes; diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/model/AbstractRequireRolesTest.java b/src/test/java/org/codehaus/mojo/extraenforcer/model/AbstractRequireRolesTest.java index 40de1f42..57fe12fc 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/model/AbstractRequireRolesTest.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/model/AbstractRequireRolesTest.java @@ -20,21 +20,21 @@ */ import java.util.Set; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public class AbstractRequireRolesTest { +class AbstractRequireRolesTest { private static final String CSV_TO_SPLIT = "a,b,c"; private static final String CSV_WITH_SPACES_TO_SPLIT = " a, b ,c "; @Test - public void testCsvSplitSize() { + void csvSplitSize() { Set values = AbstractRequireRoles.splitCsvToSet(CSV_TO_SPLIT); assert values.size() == 3; } @Test - public void testCsvSplitExpectedElements() { + void csvSplitExpectedElements() { Set values = AbstractRequireRoles.splitCsvToSet(CSV_TO_SPLIT); assert values.contains("a"); @@ -43,7 +43,7 @@ public void testCsvSplitExpectedElements() { } @Test - public void testCsvSplitTrimsValues() { + void csvSplitTrimsValues() { Set values = AbstractRequireRoles.splitCsvToSet(CSV_WITH_SPACES_TO_SPLIT); assert values.contains("a"); diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/model/RequirePropertyDivergesTest.java b/src/test/java/org/codehaus/mojo/extraenforcer/model/RequirePropertyDivergesTest.java index 7d068395..b9864772 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/model/RequirePropertyDivergesTest.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/model/RequirePropertyDivergesTest.java @@ -32,20 +32,21 @@ import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; import org.codehaus.plexus.util.xml.Xpp3Dom; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author mirko */ -@RunWith(MockitoJUnitRunner.class) -public class RequirePropertyDivergesTest { +@ExtendWith(MockitoExtension.class) +class RequirePropertyDivergesTest { @Mock private ExpressionEvaluator evaluator; @@ -54,7 +55,7 @@ public class RequirePropertyDivergesTest { * Test of execute method, of class RequirePropertyDiverges. */ @Test - public void testExecuteInChild() throws EnforcerRuleException { + void executeInChild() throws EnforcerRuleException { final MavenProject project = createMavenProject("company", "child"); final MavenProject parent = createParentProject(); project.setParent(parent); @@ -68,7 +69,7 @@ public void testExecuteInChild() throws EnforcerRuleException { * Test of execute method, of class RequirePropertyDiverges. */ @Test - public void testExecuteInParent() throws EnforcerRuleException { + void executeInParent() throws EnforcerRuleException { final MavenProject project = createParentProject(); RequirePropertyDiverges mockInstance = createMockRule(project); setUpHelper(project, "parentValue"); @@ -91,7 +92,7 @@ private MavenProject createParentProject() { * Test of execute method, of class RequirePropertyDiverges. */ @Test - public void testExecuteInParentWithConfigurationInPluginManagement() throws EnforcerRuleException { + void executeInParentWithConfigurationInPluginManagement() throws EnforcerRuleException { final MavenProject project = createMavenProject("company", "company-parent-pom"); RequirePropertyDiverges mockInstance = createMockRule(project); final Build build = new Build(); @@ -116,7 +117,7 @@ public void testExecuteInParentWithConfigurationInPluginManagement() throws Enfo * Test of execute method, of class RequirePropertyDiverges. */ @Test - public void testExecuteInParentWithConfigurationInExecution() throws EnforcerRuleException { + void executeInParentWithConfigurationInExecution() throws EnforcerRuleException { final MavenProject project = createMavenProject("company", "company-parent-pom"); RequirePropertyDiverges mockInstance = createMockRule(project); final Build build = new Build(); @@ -133,7 +134,7 @@ public void testExecuteInParentWithConfigurationInExecution() throws EnforcerRul } @Test - public void testProjectWithoutEnforcer() { + void projectWithoutEnforcer() { final Build build = new Build(); RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); @@ -144,21 +145,21 @@ public void testProjectWithoutEnforcer() { /** * Test of execute method, of class RequirePropertyDiverges. */ - @Test(expected = EnforcerRuleException.class) - public void testExecuteInChildShouldFail() throws EnforcerRuleException { + @Test + void executeInChildShouldFail() { final MavenProject project = createMavenProject("company", "child"); RequirePropertyDiverges mockInstance = createMockRule(project); final MavenProject parent = createParentProject(); project.setParent(parent); setUpHelper(project, "parentValue"); - mockInstance.execute(); + assertThrows(EnforcerRuleException.class, () -> mockInstance.execute()); } /** * Test of checkPropValueNotBlank method, of class RequirePropertyDiverges. */ @Test - public void testCheckPropValueNotBlank() throws Exception { + void checkPropValueNotBlank() throws Exception { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); instance.setProperty("checkedProperty"); instance.checkPropValueNotBlank("propertyValue"); @@ -167,15 +168,15 @@ public void testCheckPropValueNotBlank() throws Exception { /** * Test of checkPropValueNotBlank method, of class RequirePropertyDiverges. */ - @Test(expected = EnforcerRuleException.class) - public void testCheckPropValueNotBlankNull() throws EnforcerRuleException { + @Test + void checkPropValueNotBlankNull() { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); instance.setProperty("checkedProperty"); - instance.checkPropValueNotBlank(null); + assertThrows(EnforcerRuleException.class, () -> instance.checkPropValueNotBlank(null)); } @Test - public void testCreateResultingErrorMessageReturningCustomMessage() { + void createResultingErrorMessageReturningCustomMessage() { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); instance.setProperty("checkedProperty"); instance.setMessage("This is needed for foo."); @@ -185,7 +186,7 @@ public void testCreateResultingErrorMessageReturningCustomMessage() { } @Test - public void testCreateResultingErrorMessageReturningDefaultMessage() { + void createResultingErrorMessageReturningDefaultMessage() { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); instance.setProperty("checkedProperty"); @@ -201,63 +202,64 @@ public void testCreateResultingErrorMessageReturningDefaultMessage() { } @Test - public void testGetRuleName() { + void getRuleName() { assertEquals("requirePropertyDiverges", RequirePropertyDiverges.getRuleName()); } - @Test(expected = EnforcerRuleException.class) - public void testGetPropertyValueFail() throws ExpressionEvaluationException, EnforcerRuleException { + @Test + void getPropertyValueFail() throws ExpressionEvaluationException { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); - when(evaluator.evaluate("${checkedProperty}")).thenThrow(ExpressionEvaluationException.class); instance.setProperty("checkedProperty"); - instance.getPropertyValue(); + assertThrows(EnforcerRuleException.class, instance::getPropertyValue); } - @Test(expected = EnforcerRuleException.class) - public void testCheckAgainstParentValueFailing() throws EnforcerRuleException, ExpressionEvaluationException { - testCheckAgainstParentValue("company.parent-pom", "company.parent-pom"); + @Test + void checkAgainstParentValueFailing() { + assertThrows( + EnforcerRuleException.class, + () -> testCheckAgainstParentValue("company.parent-pom", "company.parent-pom")); } @Test - public void testCheckAgainstParentValue() throws EnforcerRuleException, ExpressionEvaluationException { + void checkAgainstParentValue() throws EnforcerRuleException, ExpressionEvaluationException { testCheckAgainstParentValue("company.parent-pom", "company.project1"); } - @Test(expected = NullPointerException.class) - public void testGetParentReferenceNull() throws EnforcerRuleException { + @Test + void getParentReferenceNull() { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); - instance.getParentReference(null); + assertThrows(NullPointerException.class, () -> instance.getParentReference(null)); } - @Test(expected = EnforcerRuleException.class) - public void testGetParentReferenceEmpty() throws EnforcerRuleException { + @Test + void getParentReferenceEmpty() { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); - instance.getParentReference(""); + assertThrows(EnforcerRuleException.class, () -> instance.getParentReference("")); } @Test - public void testGetParentReferenceKnownValues() throws EnforcerRuleException { + void getParentReferenceKnownValues() throws EnforcerRuleException { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); assertEquals(RequirePropertyDiverges.ParentReference.BASE, instance.getParentReference("BaSE")); assertEquals(RequirePropertyDiverges.ParentReference.DEFINING, instance.getParentReference("Defining")); assertEquals(RequirePropertyDiverges.ParentReference.PARENT, instance.getParentReference("PaRenT")); } - @Test(expected = EnforcerRuleException.class) - public void testGetParentReferenceUnknownValue() throws EnforcerRuleException { + @Test + void getParentReferenceUnknownValue() { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); - instance.getParentReference("BOGUS"); + assertThrows(EnforcerRuleException.class, () -> instance.getParentReference("BOGUS")); } @Test - public void testGetParentReferenceDefault() throws EnforcerRuleException { + void getParentReferenceDefault() throws EnforcerRuleException { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); assertEquals(RequirePropertyDiverges.ParentReference.DEFINING, instance.getParentReference()); } @Test - public void testFindParentDefining() throws EnforcerRuleException { + void findParentDefining() { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); MavenProject root = createParentProject(); MavenProject base = createMavenProject("company", "main-pom"); @@ -271,7 +273,7 @@ public void testFindParentDefining() throws EnforcerRuleException { } @Test - public void testFindParentParent() throws EnforcerRuleException { + void findParentParent() { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); MavenProject root = createParentProject(); MavenProject base = createMavenProject("company", "main-pom"); @@ -287,7 +289,7 @@ public void testFindParentParent() throws EnforcerRuleException { } @Test - public void testFindParentBase() throws EnforcerRuleException { + void findParentBase() { RequirePropertyDiverges instance = createMockRule(mock(MavenProject.class)); MavenProject root = createParentProject(); MavenProject base = createMavenProject("company", "main-pom"); diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/model/RequireRolesTest.java b/src/test/java/org/codehaus/mojo/extraenforcer/model/RequireRolesTest.java index 0267b8b5..cc9109c6 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/model/RequireRolesTest.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/model/RequireRolesTest.java @@ -29,20 +29,22 @@ import org.apache.maven.model.Contributor; import org.apache.maven.model.Developer; import org.apache.maven.project.MavenProject; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * * @author Mirko Friedenhagen */ -public class RequireRolesTest { +class RequireRolesTest { private MavenProject mavenProject; @Test - public void shouldSucceedBecauseArchitectAsDeveloperAndBusinessEngineerAsContributorArePresent() throws Exception { + void shouldSucceedBecauseArchitectAsDeveloperAndBusinessEngineerAsContributorArePresent() + throws EnforcerRuleException { addProjectHavingAnArchitectAsDeveloperAndABusinessEngineerAsContributorToHelper(); newRequireDeveloperRoles("architect" /*required role*/, null /* valid roles not needed */) .execute(); @@ -50,35 +52,42 @@ public void shouldSucceedBecauseArchitectAsDeveloperAndBusinessEngineerAsContrib .execute(); } - @Test(expected = EnforcerRuleException.class) - public void shouldFailBecauseContributorWithRoleQualityManagerIsMissing() throws Exception { + @Test + void shouldFailBecauseContributorWithRoleQualityManagerIsMissing() { addProjectHavingAnArchitectAsDeveloperAndABusinessEngineerAsContributorToHelper(); - newRequireContributorRoles("business engineer, quality manager", null).execute(); + assertThrows(EnforcerRuleException.class, () -> newRequireContributorRoles( + "business engineer, quality manager", null) + .execute()); } - @Test(expected = EnforcerRuleException.class) - public void shouldFailBecauseDeveloperWithRoleCodeMonkeyIsMissing() throws Exception { + @Test + void shouldFailBecauseDeveloperWithRoleCodeMonkeyIsMissing() { addProjectHavingAnArchitectAsDeveloperAndABusinessEngineerAsContributorToHelper(); - newRequireDeveloperRoles("codemonkey" /* required but not in project */, null) - .execute(); + assertThrows( + EnforcerRuleException.class, + () -> newRequireDeveloperRoles("codemonkey" /* required but not in project */, null) + .execute()); } - @Test(expected = EnforcerRuleException.class) - public void shouldFailBecauseContributorRoleBusinessEngineerIsInvalid() throws Exception { + @Test + void shouldFailBecauseContributorRoleBusinessEngineerIsInvalid() { addProjectHavingAnArchitectAsDeveloperAndABusinessEngineerAsContributorToHelper(); - newRequireContributorRoles(null /* no required roles needed */, "hacker" /* only valid role */) - .execute(); + assertThrows( + EnforcerRuleException.class, + () -> newRequireContributorRoles(null /* no required roles needed */, "hacker" /* only valid role */) + .execute()); } - @Test(expected = EnforcerRuleException.class) - public void shouldFailBecauseNoContributorRolesAtAllAreValid() throws Exception { + @Test + void shouldFailBecauseNoContributorRolesAtAllAreValid() { addProjectHavingAnArchitectAsDeveloperAndABusinessEngineerAsContributorToHelper(); - newRequireContributorRoles(null /* no required roles needed */, "" /*but no role is valid at all */) - .execute(); + assertThrows(EnforcerRuleException.class, () -> newRequireContributorRoles( + null /* no required roles needed */, "" /*but no role is valid at all */) + .execute()); } @Test - public void shouldSucceedAsNoRolesAreRequiredAndAllAreAccepted() throws Exception { + void shouldSucceedAsNoRolesAreRequiredAndAllAreAccepted() throws Exception { addProjectHavingAnArchitectAsDeveloperAndABusinessEngineerAsContributorToHelper(); newRequireContributorRoles(null /* no required roles */, "*" /* any role is valid */) .execute(); @@ -90,7 +99,7 @@ public void shouldSucceedAsNoRolesAreRequiredAndAllAreAccepted() throws Exceptio * Test of getRolesFromString method, of class AbstractRequireRoles. */ @Test - public void testGetRolesFromString() { + void getRolesFromString() { HashSet expResult = new HashSet<>(Arrays.asList("architect", "codemonkey", "business engineer")); final RequireContributorRoles sut = new RequireContributorRoles(mavenProject); Set result = sut.getRolesFromString(" architect, business engineer , codemonkey "); @@ -101,7 +110,7 @@ public void testGetRolesFromString() { * Test of getRolesFromMaven method, of class AbstractRequireRoles. */ @Test - public void testGetRolesFromMaven() { + void getRolesFromMaven() { HashSet expResult = new HashSet<>(Arrays.asList("quality manager", "product owner", "business engineer")); final Contributor singleHero = new Contributor(); @@ -118,7 +127,7 @@ public void testGetRolesFromMaven() { assertEquals(expResult, result); } - private void addProjectHavingAnArchitectAsDeveloperAndABusinessEngineerAsContributorToHelper() throws Exception { + private void addProjectHavingAnArchitectAsDeveloperAndABusinessEngineerAsContributorToHelper() { mavenProject = new MavenProject(); final Developer developer = new Developer(); developer.addRole("architect"); diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/model/RuleXpp3DomTest.java b/src/test/java/org/codehaus/mojo/extraenforcer/model/RuleXpp3DomTest.java index 137edcfb..584cd341 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/model/RuleXpp3DomTest.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/model/RuleXpp3DomTest.java @@ -19,30 +19,21 @@ * under the License. */ -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; import org.codehaus.plexus.util.xml.Xpp3Dom; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; /** * * @author mfriedenhagen */ -@RunWith(MockitoJUnitRunner.class) -public class RuleXpp3DomTest { - - @Mock - private MavenProject project; - - @Mock - private ExpressionEvaluator evaluator; +@ExtendWith(MockitoExtension.class) +class RuleXpp3DomTest { @InjectMocks private RequirePropertyDiverges sut1; @@ -51,14 +42,14 @@ public class RuleXpp3DomTest { private RequirePropertyDiverges sut2; @Test - public void checkRuleWithoutRegex() { + void checkRuleWithoutRegex() { sut1.setProperty("foo"); sut2.setProperty("foo"); checkEquals(); } @Test - public void checkRuleWithoutRegexButMessage() { + void checkRuleWithoutRegexButMessage() { sut1.setProperty("foo"); sut1.setMessage("Oops"); sut2.setProperty("foo"); @@ -67,14 +58,14 @@ public void checkRuleWithoutRegexButMessage() { } @Test - public void checkRuleWithoutRegexDiverges() { + void checkRuleWithoutRegexDiverges() { sut1.setProperty("foo"); sut2.setProperty("foo2"); checkDiverges(); } @Test - public void checkRuleWithRegex() { + void checkRuleWithRegex() { sut1.setProperty("foo"); sut1.setRegex("http://company/wiki/company-parent-pom.*"); sut2.setProperty("foo"); @@ -83,7 +74,7 @@ public void checkRuleWithRegex() { } @Test - public void checkRuleWithRegexDiverges() { + void checkRuleWithRegexDiverges() { sut1.setProperty("foo"); sut1.setRegex("http://company/wiki/company-parent-pom.*"); sut2.setProperty("foo"); diff --git a/src/test/java/org/freebsd/file/FileEncodingTest.java b/src/test/java/org/freebsd/file/FileEncodingTest.java index 4b8c1c79..7552ddc0 100644 --- a/src/test/java/org/freebsd/file/FileEncodingTest.java +++ b/src/test/java/org/freebsd/file/FileEncodingTest.java @@ -2,28 +2,28 @@ import java.nio.charset.StandardCharsets; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class FileEncodingTest { +class FileEncodingTest { private FileEncoding rule; - @Before - public void initFields() { + @BeforeEach + void initFields() { rule = new FileEncoding(); } @Test - public void detectAscii() { + void detectAscii() { assertEncoding( new byte[] {'a', 'b', 'c', 'A', 'B', 'C', '1', '2', '3'}, StandardCharsets.US_ASCII.name(), "ASCII"); } @Test - public void detectISO() { + void detectISO() { assertEncoding( new byte[] {'a', 'b', 'c', 'A', 'B', 'C', '1', '2', '3', (byte) 0xF7}, StandardCharsets.ISO_8859_1.name(), @@ -31,7 +31,7 @@ public void detectISO() { } @Test - public void detectUTF7() { + void detectUTF7() { assertEncoding(new byte[] {'+', '/', 'v', '8', 'B', 'C', '1', '2', '3'}, "UTF-7", "UTF-7 UNICODE"); assertEncoding(new byte[] {'+', '/', 'v', '9', 'B', 'C', '1', '2', '3'}, "UTF-7", "UTF-7 UNICODE"); assertEncoding(new byte[] {'+', '/', 'v', '+', 'B', 'C', '1', '2', '3'}, "UTF-7", "UTF-7 UNICODE"); @@ -39,7 +39,7 @@ public void detectUTF7() { } @Test - public void detectUTF8() { + void detectUTF8() { assertEncoding( new byte[] {'a', 'b', 'c', 'A', 'B', 'C', '1', '2', '3', (byte) 0xC3, (byte) 0xB6}, StandardCharsets.UTF_8.name(), @@ -47,7 +47,7 @@ public void detectUTF8() { } @Test - public void detectUTF8WithBoom() { + void detectUTF8WithBoom() { assertEncoding( new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF, 'a', 'b', 'c', 'A', 'B', 'C', '1', '2', '3'}, StandardCharsets.UTF_8.name(), @@ -74,7 +74,7 @@ public void detectUTF8WithBoom() { } @Test - public void detectUTF16LE() { + void detectUTF16LE() { assertEncoding( new byte[] {(byte) 0xFF, (byte) 0xFE, (byte) 0xD6, (byte) 0x00, (byte) 0x41, (byte) 0x00}, StandardCharsets.UTF_16LE.name(), @@ -82,7 +82,7 @@ public void detectUTF16LE() { } @Test - public void detectUTF16BE() { + void detectUTF16BE() { assertEncoding( new byte[] {(byte) 0xFE, (byte) 0xFF, (byte) 0x00, (byte) 0xD6, (byte) 0x00, (byte) 0x41}, StandardCharsets.UTF_16BE.name(), @@ -90,7 +90,7 @@ public void detectUTF16BE() { } @Test - public void detectExtendedAscii() { + void detectExtendedAscii() { assertEncoding( new byte[] {'a', 'b', 'c', 'A', 'B', 'C', '1', '2', '3', (byte) 0x96}, "UNKNOWN-8BIT",