From a1bb14fad9c3cda503e436605fc2b3f457a5f847 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Thu, 7 Nov 2024 13:45:36 -0500 Subject: [PATCH] Convert to JUnit 5 (#445) * Convert to JUnit 5 * Convert to JUnit 5 * Convert to JUnit 5 * Convert to JUnit 5 * Convert to JUnit 5 * Convert to JUnit 5 * rebase --- pom.xml | 6 --- .../AbstractDependencyMojoTestCase.java | 7 +++- .../utils/filters/TestDestFileFilter.java | 36 +++++++++--------- .../utils/filters/TestMarkerFileFilter.java | 38 +++++++------------ .../filters/TestResolveMarkerFileFilter.java | 36 +++++++----------- .../markers/TestDefaultMarkerFileHandler.java | 35 ++++++++--------- .../markers/TestUnpackMarkerFileHandler.java | 37 ++++++++---------- 7 files changed, 84 insertions(+), 111 deletions(-) diff --git a/pom.xml b/pom.xml index 48ef57070..b75c3dee6 100644 --- a/pom.xml +++ b/pom.xml @@ -161,12 +161,6 @@ under the License. maven-reporting-impl 4.0.0 - - commons-io - commons-io - 2.17.0 - test - diff --git a/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java b/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java index cde5ea883..fc8eacc05 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java +++ b/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; @@ -49,15 +50,17 @@ protected void setUp(String testDirStr, boolean createFiles) throws Exception { protected void setUp(String testDirStr, boolean createFiles, boolean flattenedPath) throws Exception { // required for mojo lookups to work super.setUp(); + testDir = new File( getBasedir(), "target" + File.separatorChar + "unit-tests" + File.separatorChar + testDirStr + File.separatorChar); - FileUtils.deleteDirectory(testDir); - assertFalse(testDir.exists()); + testDir = Files.createTempDirectory("testDirStr").toFile(); + testDir.deleteOnExit(); stubFactory = new DependencyArtifactStubFactory(this.testDir, createFiles, flattenedPath); } + @Override protected void tearDown() { if (testDir != null) { try { diff --git a/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestDestFileFilter.java b/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestDestFileFilter.java index 2c52f5f3e..b97f83fa0 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestDestFileFilter.java +++ b/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestDestFileFilter.java @@ -23,43 +23,37 @@ import java.util.HashSet; import java.util.Set; -import junit.framework.TestCase; -import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugin.testing.SilentLog; import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory; import org.apache.maven.plugins.dependency.utils.DependencyUtil; import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; +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.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author brianf */ -public class TestDestFileFilter extends TestCase { +public class TestDestFileFilter { Set artifacts = new HashSet<>(); - Log log = new SilentLog(); - + @TempDir File outputFolder; DependencyArtifactStubFactory fact; + @BeforeEach protected void setUp() throws Exception { - super.setUp(); - - outputFolder = new File("target/markers/"); - FileUtils.deleteDirectory(outputFolder); - assertFalse(outputFolder.exists()); - this.fact = new DependencyArtifactStubFactory(outputFolder, false); artifacts = fact.getReleaseAndSnapshotArtifacts(); } - protected void tearDown() throws IOException { - FileUtils.deleteDirectory(outputFolder); - } - public void createFile(Artifact artifact) throws IOException { createFile(artifact, false, false, false); } @@ -91,6 +85,7 @@ public File createFile( return destFile; } + @Test public void testDestFileRelease() throws IOException, ArtifactFilterException { DestFileFilter filter = new DestFileFilter(outputFolder); Artifact artifact = fact.getReleaseArtifact(); @@ -103,6 +98,7 @@ public void testDestFileRelease() throws IOException, ArtifactFilterException { assertTrue(filter.isArtifactIncluded(artifact)); } + @Test public void testDestFileSnapshot() throws IOException, ArtifactFilterException { DestFileFilter filter = new DestFileFilter(outputFolder); Artifact artifact = fact.getSnapshotArtifact(); @@ -115,6 +111,7 @@ public void testDestFileSnapshot() throws IOException, ArtifactFilterException { assertTrue(filter.isArtifactIncluded(artifact)); } + @Test public void testDestFileStripVersion() throws IOException, ArtifactFilterException { DestFileFilter filter = new DestFileFilter(outputFolder); Artifact artifact = fact.getSnapshotArtifact(); @@ -128,6 +125,7 @@ public void testDestFileStripVersion() throws IOException, ArtifactFilterExcepti assertTrue(filter.isArtifactIncluded(artifact)); } + @Test public void testDestFileStripClassifier() throws IOException, ArtifactFilterException { DestFileFilter filter = new DestFileFilter(outputFolder); Artifact artifact = fact.getSnapshotArtifact(); @@ -141,6 +139,7 @@ public void testDestFileStripClassifier() throws IOException, ArtifactFilterExce assertTrue(filter.isArtifactIncluded(artifact)); } + @Test public void testDestFileSubPerArtifact() throws IOException, ArtifactFilterException { DestFileFilter filter = new DestFileFilter(outputFolder); Artifact artifact = fact.getSnapshotArtifact(); @@ -154,6 +153,7 @@ public void testDestFileSubPerArtifact() throws IOException, ArtifactFilterExcep assertTrue(filter.isArtifactIncluded(artifact)); } + @Test public void testDestFileSubPerType() throws MojoExecutionException, IOException, ArtifactFilterException { DestFileFilter filter = new DestFileFilter(outputFolder); Artifact artifact = fact.getSnapshotArtifact(); @@ -167,6 +167,7 @@ public void testDestFileSubPerType() throws MojoExecutionException, IOException, assertTrue(filter.isArtifactIncluded(artifact)); } + @Test public void testDestFileOverwriteIfNewer() throws MojoExecutionException, IOException, ArtifactFilterException { DestFileFilter filter = new DestFileFilter(outputFolder); @@ -192,6 +193,7 @@ public void testDestFileOverwriteIfNewer() throws MojoExecutionException, IOExce assertFalse(filter.isArtifactIncluded(artifact)); } + @Test public void testGettersSetters() { DestFileFilter filter = new DestFileFilter(null); assertNull(filter.getOutputFileDirectory()); diff --git a/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestMarkerFileFilter.java b/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestMarkerFileFilter.java index 717b19421..beec00806 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestMarkerFileFilter.java +++ b/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestMarkerFileFilter.java @@ -23,45 +23,38 @@ import java.util.HashSet; import java.util.Set; -import junit.framework.TestCase; -import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugin.testing.SilentLog; import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory; import org.apache.maven.plugins.dependency.utils.markers.DefaultFileMarkerHandler; import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; +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.assertTrue; /** * @author brianf */ -public class TestMarkerFileFilter extends TestCase { +public class TestMarkerFileFilter { Set artifacts = new HashSet<>(); - Log log = new SilentLog(); - + @TempDir File outputFolder; DependencyArtifactStubFactory fact; + @BeforeEach protected void setUp() throws Exception { - super.setUp(); - - outputFolder = new File("target/markers/"); - FileUtils.deleteDirectory(outputFolder); - assertFalse(outputFolder.exists()); - this.fact = new DependencyArtifactStubFactory(outputFolder, false); artifacts = fact.getReleaseAndSnapshotArtifacts(); } - protected void tearDown() throws IOException { - FileUtils.deleteDirectory(outputFolder); - } - + @Test public void testMarkerFile() throws ArtifactFilterException { - MarkerFileFilter filter = new MarkerFileFilter(true, true, false, new DefaultFileMarkerHandler(outputFolder)); Set result = filter.filter(artifacts); assertEquals(2, result.size()); @@ -73,7 +66,6 @@ public void testMarkerFile() throws ArtifactFilterException { } public void testMarkerSnapshots() throws ArtifactFilterException, MojoExecutionException, IOException { - DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(fact.getSnapshotArtifact(), outputFolder); handler.setMarker(); @@ -85,8 +77,6 @@ public void testMarkerSnapshots() throws ArtifactFilterException, MojoExecutionE result = filter.filter(artifacts); assertEquals(2, result.size()); assertTrue(handler.clearMarker()); - FileUtils.deleteDirectory(outputFolder); - assertFalse(outputFolder.exists()); } public void testMarkerRelease() throws IOException, ArtifactFilterException, MojoExecutionException { @@ -102,10 +92,9 @@ public void testMarkerRelease() throws IOException, ArtifactFilterException, Moj assertEquals(2, result.size()); assertTrue(handler.clearMarker()); - FileUtils.deleteDirectory(outputFolder); - assertFalse(outputFolder.exists()); } + @Test public void testMarkerTimestamp() throws IOException, MojoExecutionException, ArtifactFilterException { // filter includes release artifact because no marker present // filter includes snapshot artifact because it is newer than marker @@ -137,10 +126,9 @@ public void testMarkerTimestamp() throws IOException, MojoExecutionException, Ar assertFalse(handler.isMarkerSet()); snap.getFile().delete(); release.getFile().delete(); - FileUtils.deleteDirectory(outputFolder); - assertFalse(outputFolder.exists()); } + @Test public void testGettersSetters() { MarkerFileFilter filter = new MarkerFileFilter(true, false, true, new DefaultFileMarkerHandler(outputFolder)); assertTrue(filter.isOverWriteReleases()); diff --git a/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestResolveMarkerFileFilter.java b/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestResolveMarkerFileFilter.java index 25e629355..10bce1a31 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestResolveMarkerFileFilter.java +++ b/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestResolveMarkerFileFilter.java @@ -20,46 +20,36 @@ import java.io.File; import java.io.IOException; -import java.util.HashSet; -import java.util.Set; -import junit.framework.TestCase; -import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugin.testing.SilentLog; import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory; import org.apache.maven.plugins.dependency.utils.markers.SourcesFileMarkerHandler; import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; +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.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author brianf */ -public class TestResolveMarkerFileFilter extends TestCase { - Set artifacts = new HashSet<>(); - - Log log = new SilentLog(); +public class TestResolveMarkerFileFilter { + @TempDir File outputFolder; DependencyArtifactStubFactory fact; - protected void setUp() throws Exception { - super.setUp(); - - outputFolder = new File("target/markers/"); - FileUtils.deleteDirectory(outputFolder); - assertFalse(outputFolder.exists()); - - this.fact = new DependencyArtifactStubFactory(outputFolder, false); - artifacts = fact.getReleaseAndSnapshotArtifacts(); - } - - protected void tearDown() throws IOException { - FileUtils.deleteDirectory(outputFolder); + @BeforeEach + protected void setUp() throws IOException { + fact = new DependencyArtifactStubFactory(outputFolder, false); + fact.getReleaseAndSnapshotArtifacts(); } + @Test public void testResolveFile() throws IOException, ArtifactFilterException, MojoExecutionException { SourcesFileMarkerHandler handler = new SourcesFileMarkerHandler(outputFolder); diff --git a/src/test/java/org/apache/maven/plugins/dependency/utils/markers/TestDefaultMarkerFileHandler.java b/src/test/java/org/apache/maven/plugins/dependency/utils/markers/TestDefaultMarkerFileHandler.java index c9eb2d4a3..7828759e5 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/utils/markers/TestDefaultMarkerFileHandler.java +++ b/src/test/java/org/apache/maven/plugins/dependency/utils/markers/TestDefaultMarkerFileHandler.java @@ -23,31 +23,34 @@ import java.util.ArrayList; import java.util.List; -import junit.framework.TestCase; -import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugin.testing.SilentLog; import org.apache.maven.plugins.dependency.testUtils.stubs.StubDefaultFileMarkerHandler; +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.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * @author brianf */ -public class TestDefaultMarkerFileHandler extends TestCase { +public class TestDefaultMarkerFileHandler { List artifacts = new ArrayList<>(); - Log log = new SilentLog(); - + @TempDir File outputFolder; + @BeforeEach protected void setUp() throws Exception { - super.setUp(); - ArtifactHandler ah = new DefaultArtifactHandler(); VersionRange vr = VersionRange.createFromVersion("1.1"); Artifact artifact = new DefaultArtifact("test", "1", vr, Artifact.SCOPE_COMPILE, "jar", "", ah, false); @@ -58,16 +61,9 @@ protected void setUp() throws Exception { artifacts.add(artifact); artifact = new DefaultArtifact("test", "4", vr, Artifact.SCOPE_RUNTIME, "zip", "", ah, false); artifacts.add(artifact); - - outputFolder = new File("target/markers/"); - FileUtils.deleteDirectory(this.outputFolder); - assertFalse(outputFolder.exists()); - } - - protected void tearDown() throws IOException { - FileUtils.deleteDirectory(this.outputFolder); } + @Test public void testSetMarker() throws MojoExecutionException { DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(artifacts.get(0), this.outputFolder); assertFalse(handler.isMarkerSet()); @@ -87,6 +83,7 @@ public void testSetMarker() throws MojoExecutionException { assertFalse(handler.isMarkerSet()); } + @Test public void testMarkerFile() throws MojoExecutionException, IOException { DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(artifacts.get(0), this.outputFolder); @@ -108,6 +105,7 @@ public void testMarkerFile() throws MojoExecutionException, IOException { assertFalse(handle.exists()); } + @Test public void testMarkerTimeStamp() throws MojoExecutionException, IOException, InterruptedException { File theFile = new File(outputFolder, "theFile.jar"); outputFolder.mkdirs(); @@ -130,6 +128,7 @@ public void testMarkerTimeStamp() throws MojoExecutionException, IOException, In assertFalse(handler.isMarkerSet()); } + @Test public void testMarkerFileException() { // this stub wraps the file with an object to throw exceptions StubDefaultFileMarkerHandler handler = new StubDefaultFileMarkerHandler(artifacts.get(0), this.outputFolder); @@ -141,6 +140,7 @@ public void testMarkerFileException() { } } + @Test public void testGetterSetter() { DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(null, null); assertNull(handler.getArtifact()); @@ -152,6 +152,7 @@ public void testGetterSetter() { assertSame(outputFolder, handler.getMarkerFilesDirectory()); } + @Test public void testNullParent() throws MojoExecutionException { // the parent isn't set so this will create the marker in the local // folder. We must clear the diff --git a/src/test/java/org/apache/maven/plugins/dependency/utils/markers/TestUnpackMarkerFileHandler.java b/src/test/java/org/apache/maven/plugins/dependency/utils/markers/TestUnpackMarkerFileHandler.java index 7be795fb5..af165728f 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/utils/markers/TestUnpackMarkerFileHandler.java +++ b/src/test/java/org/apache/maven/plugins/dependency/utils/markers/TestUnpackMarkerFileHandler.java @@ -23,37 +23,32 @@ import java.util.ArrayList; import java.util.List; -import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.apache.maven.plugin.testing.SilentLog; import org.apache.maven.plugins.dependency.fromConfiguration.ArtifactItem; import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory; import org.apache.maven.plugins.dependency.testUtils.stubs.StubUnpackFileMarkerHandler; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class TestUnpackMarkerFileHandler extends AbstractMojoTestCase { List artifactItems = new ArrayList<>(); - Log log = new SilentLog(); - + @TempDir File outputFolder; + @TempDir protected File testDir; protected DependencyArtifactStubFactory stubFactory; + @Override + @BeforeEach protected void setUp() throws Exception { super.setUp(); - testDir = new File( - getBasedir(), - "target" + File.separatorChar + "unit-tests" + File.separatorChar + "unpack-markers" - + File.separatorChar); - FileUtils.deleteDirectory(testDir); - assertFalse(testDir.exists()); - stubFactory = new DependencyArtifactStubFactory(this.testDir, false); Artifact artifact = stubFactory.createArtifact("test", "test", "1"); ArtifactItem artifactItem; @@ -72,14 +67,6 @@ protected void setUp() throws Exception { artifactItem.setIncludes("**/*.xml"); artifactItem.setExcludes("**/*.class"); artifactItems.add(artifactItem); - - outputFolder = new File("target/markers/"); - FileUtils.deleteDirectory(this.outputFolder); - assertFalse(outputFolder.exists()); - } - - protected void tearDown() throws IOException { - FileUtils.deleteDirectory(this.outputFolder); } /** @@ -87,6 +74,7 @@ protected void tearDown() throws IOException { * * @throws MojoExecutionException in case of an error. */ + @Test public void testSetMarker() throws MojoExecutionException { UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(artifactItems.get(0), this.outputFolder); assertFalse(handler.isMarkerSet()); @@ -106,6 +94,7 @@ public void testSetMarker() throws MojoExecutionException { assertFalse(handler.isMarkerSet()); } + @Test public void testMarkerFile() throws MojoExecutionException, IOException { UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(artifactItems.get(0), this.outputFolder); @@ -127,9 +116,9 @@ public void testMarkerFile() throws MojoExecutionException, IOException { assertFalse(handle.exists()); } + @Test public void testMarkerTimeStamp() throws MojoExecutionException, IOException, InterruptedException { File theFile = new File(outputFolder, "theFile.jar"); - outputFolder.mkdirs(); theFile.createNewFile(); ArtifactItem theArtifactItem = artifactItems.get(0); Artifact theArtifact = theArtifactItem.getArtifact(); @@ -150,6 +139,7 @@ public void testMarkerTimeStamp() throws MojoExecutionException, IOException, In assertFalse(handler.isMarkerSet()); } + @Test public void testMarkerFileException() { // this stub wraps the file with an object to throw exceptions StubUnpackFileMarkerHandler handler = new StubUnpackFileMarkerHandler(artifactItems.get(0), this.outputFolder); @@ -161,6 +151,7 @@ public void testMarkerFileException() { } } + @Test public void testGetterSetter() { UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(null, null); assertNull(handler.getArtifactItem()); @@ -174,6 +165,7 @@ public void testGetterSetter() { assertSame(outputFolder, handler.getMarkerFilesDirectory()); } + @Test public void testNullParent() throws MojoExecutionException { // the parent isn't set so this will create the marker in the local // folder. We must clear the @@ -186,6 +178,7 @@ public void testNullParent() throws MojoExecutionException { assertFalse(handler.isMarkerSet()); } + @Test public void testIncludesMarker() throws MojoExecutionException, IOException { UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(artifactItems.get(1), outputFolder); File handle = handler.getMarkerFile(); @@ -208,6 +201,7 @@ public void testIncludesMarker() throws MojoExecutionException, IOException { assertFalse(handle.exists()); } + @Test public void testExcludesMarker() throws MojoExecutionException, IOException { UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(artifactItems.get(2), outputFolder); File handle = handler.getMarkerFile(); @@ -230,6 +224,7 @@ public void testExcludesMarker() throws MojoExecutionException, IOException { assertFalse(handle.exists()); } + @Test public void testIncludesExcludesMarker() throws MojoExecutionException, IOException { UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(artifactItems.get(3), outputFolder); File handle = handler.getMarkerFile();