Skip to content

Commit d3b2800

Browse files
[MCLEAN-106] Switch to junit 5 (#27)
Co-authored-by: Slawomir Jaranowski <s.jaranowski@gmail.com>
1 parent 7c1593f commit d3b2800

File tree

2 files changed

+116
-135
lines changed

2 files changed

+116
-135
lines changed

pom.xml

+36-12
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ under the License.
7474
<version>${mavenVersion}</version>
7575
<scope>provided</scope>
7676
</dependency>
77+
<dependency>
78+
<groupId>org.apache.maven</groupId>
79+
<artifactId>maven-core</artifactId>
80+
<version>${mavenVersion}</version>
81+
<scope>provided</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.apache.maven.resolver</groupId>
85+
<artifactId>maven-resolver-api</artifactId>
86+
<version>1.1.1</version>
87+
<scope>provided</scope>
88+
</dependency>
7789
<dependency>
7890
<groupId>org.codehaus.plexus</groupId>
7991
<artifactId>plexus-utils</artifactId>
@@ -90,30 +102,42 @@ under the License.
90102
<dependency>
91103
<groupId>org.apache.maven.plugin-testing</groupId>
92104
<artifactId>maven-plugin-testing-harness</artifactId>
93-
<version>3.3.0</version>
105+
<version>4.0.0-alpha-2</version>
106+
<scope>test</scope>
107+
</dependency>
108+
<dependency>
109+
<groupId>org.junit.jupiter</groupId>
110+
<artifactId>junit-jupiter-api</artifactId>
111+
<version>5.9.3</version>
94112
<scope>test</scope>
95113
</dependency>
96114
<dependency>
97115
<groupId>org.codehaus.plexus</groupId>
98-
<artifactId>plexus-xml</artifactId>
116+
<artifactId>plexus-testing</artifactId>
117+
<version>1.1.0</version>
99118
<scope>test</scope>
100119
</dependency>
101120
<dependency>
102-
<groupId>org.apache.maven</groupId>
103-
<artifactId>maven-compat</artifactId>
104-
<version>${mavenVersion}</version>
121+
<groupId>org.eclipse.sisu</groupId>
122+
<artifactId>org.eclipse.sisu.plexus</artifactId>
105123
<scope>test</scope>
106124
</dependency>
107125
<dependency>
108-
<groupId>org.apache.maven</groupId>
109-
<artifactId>maven-core</artifactId>
110-
<version>${mavenVersion}</version>
111-
<scope>provided</scope>
126+
<groupId>com.google.inject</groupId>
127+
<artifactId>guice</artifactId>
128+
<version>4.2.0</version>
129+
<classifier>no_aop</classifier>
130+
<scope>test</scope>
112131
</dependency>
113132
<dependency>
114-
<groupId>junit</groupId>
115-
<artifactId>junit</artifactId>
116-
<version>4.13.2</version>
133+
<groupId>org.codehaus.plexus</groupId>
134+
<artifactId>plexus-xml</artifactId>
135+
<scope>test</scope>
136+
</dependency>
137+
<dependency>
138+
<groupId>org.apache.maven</groupId>
139+
<artifactId>maven-resolver-provider</artifactId>
140+
<version>${mavenVersion}</version>
117141
<scope>test</scope>
118142
</dependency>
119143
</dependencies>

src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java

+80-123
Original file line numberDiff line numberDiff line change
@@ -29,64 +29,70 @@
2929
import java.nio.file.Paths;
3030
import java.util.Collections;
3131

32+
import com.google.inject.Provides;
33+
import com.google.inject.Singleton;
34+
import org.apache.maven.execution.DefaultMavenExecutionRequest;
35+
import org.apache.maven.execution.DefaultMavenExecutionResult;
36+
import org.apache.maven.execution.MavenExecutionRequest;
37+
import org.apache.maven.execution.MavenExecutionResult;
38+
import org.apache.maven.execution.MavenSession;
39+
import org.apache.maven.plugin.MojoExecution;
3240
import org.apache.maven.plugin.MojoExecutionException;
3341
import org.apache.maven.plugin.logging.SystemStreamLog;
34-
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
35-
36-
import static org.apache.commons.io.FileUtils.copyDirectory;
42+
import org.apache.maven.plugin.testing.junit5.InjectMojo;
43+
import org.apache.maven.plugin.testing.junit5.MojoTest;
44+
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
45+
import org.codehaus.plexus.PlexusContainer;
46+
import org.junit.jupiter.api.Test;
47+
import org.junit.jupiter.api.condition.DisabledOnOs;
48+
import org.junit.jupiter.api.condition.EnabledOnOs;
49+
import org.junit.jupiter.api.condition.OS;
50+
51+
import static org.apache.maven.plugin.testing.junit5.MojoExtension.setVariableValueToObject;
52+
import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
3753
import static org.codehaus.plexus.util.IOUtil.copy;
54+
import static org.junit.jupiter.api.Assertions.assertFalse;
55+
import static org.junit.jupiter.api.Assertions.assertNotNull;
56+
import static org.junit.jupiter.api.Assertions.assertThrows;
57+
import static org.junit.jupiter.api.Assertions.assertTrue;
58+
import static org.junit.jupiter.api.Assertions.fail;
3859

3960
/**
4061
* Test the clean mojo.
4162
*
4263
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
4364
*/
44-
public class CleanMojoTest extends AbstractMojoTestCase {
65+
@MojoTest
66+
public class CleanMojoTest {
4567
/**
4668
* Tests the simple removal of directories
4769
*
4870
* @throws Exception in case of an error.
4971
*/
50-
public void testBasicClean() throws Exception {
51-
String pluginPom = getBasedir() + "/src/test/resources/unit/basic-clean-test/plugin-pom.xml";
52-
53-
// safety
54-
copyDirectory(
55-
new File(getBasedir(), "src/test/resources/unit/basic-clean-test"),
56-
new File(getBasedir(), "target/test-classes/unit/basic-clean-test"));
57-
58-
CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom);
59-
assertNotNull(mojo);
60-
72+
@Test
73+
@InjectMojo(goal = "clean", pom = "classpath:/unit/basic-clean-test/plugin-pom.xml")
74+
public void testBasicClean(CleanMojo mojo) throws Exception {
6175
mojo.execute();
6276

6377
assertFalse(
64-
"Directory exists",
65-
checkExists(getBasedir() + "/target/test-classes/unit/" + "basic-clean-test/buildDirectory"));
78+
checkExists(getBasedir() + "/target/test-classes/unit/" + "basic-clean-test/buildDirectory"),
79+
"Directory exists");
6680
assertFalse(
67-
"Directory exists",
68-
checkExists(getBasedir() + "/target/test-classes/unit/basic-clean-test/" + "buildOutputDirectory"));
81+
checkExists(getBasedir() + "/target/test-classes/unit/basic-clean-test/" + "buildOutputDirectory"),
82+
"Directory exists");
6983
assertFalse(
70-
"Directory exists",
71-
checkExists(getBasedir() + "/target/test-classes/unit/basic-clean-test/" + "buildTestDirectory"));
84+
checkExists(getBasedir() + "/target/test-classes/unit/basic-clean-test/" + "buildTestDirectory"),
85+
"Directory exists");
7286
}
7387

7488
/**
7589
* Tests the removal of files and nested directories
7690
*
7791
* @throws Exception in case of an error.
7892
*/
79-
public void testCleanNestedStructure() throws Exception {
80-
String pluginPom = getBasedir() + "/src/test/resources/unit/nested-clean-test/plugin-pom.xml";
81-
82-
// safety
83-
copyDirectory(
84-
new File(getBasedir(), "src/test/resources/unit/nested-clean-test"),
85-
new File(getBasedir(), "target/test-classes/unit/nested-clean-test"));
86-
87-
CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom);
88-
assertNotNull(mojo);
89-
93+
@Test
94+
@InjectMojo(goal = "clean", pom = "classpath:/unit/nested-clean-test/plugin-pom.xml")
95+
public void testCleanNestedStructure(CleanMojo mojo) throws Exception {
9096
mojo.execute();
9197

9298
assertFalse(checkExists(getBasedir() + "/target/test-classes/unit/nested-clean-test/target"));
@@ -100,17 +106,9 @@ public void testCleanNestedStructure() throws Exception {
100106
*
101107
* @throws Exception in case of an error.
102108
*/
103-
public void testCleanEmptyDirectories() throws Exception {
104-
String pluginPom = getBasedir() + "/src/test/resources/unit/empty-clean-test/plugin-pom.xml";
105-
106-
// safety
107-
copyDirectory(
108-
new File(getBasedir(), "src/test/resources/unit/empty-clean-test"),
109-
new File(getBasedir(), "target/test-classes/unit/empty-clean-test"));
110-
111-
CleanMojo mojo = (CleanMojo) lookupEmptyMojo("clean", pluginPom);
112-
assertNotNull(mojo);
113-
109+
@Test
110+
@InjectMojo(goal = "clean", pom = "classpath:/unit/empty-clean-test/plugin-pom.xml")
111+
public void testCleanEmptyDirectories(CleanMojo mojo) throws Exception {
114112
mojo.execute();
115113

116114
assertTrue(checkExists(getBasedir() + "/target/test-classes/unit/empty-clean-test/testDirectoryStructure"));
@@ -127,17 +125,9 @@ public void testCleanEmptyDirectories() throws Exception {
127125
*
128126
* @throws Exception in case of an error.
129127
*/
130-
public void testFilesetsClean() throws Exception {
131-
String pluginPom = getBasedir() + "/src/test/resources/unit/fileset-clean-test/plugin-pom.xml";
132-
133-
// safety
134-
copyDirectory(
135-
new File(getBasedir(), "src/test/resources/unit/fileset-clean-test"),
136-
new File(getBasedir(), "target/test-classes/unit/fileset-clean-test"));
137-
138-
CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom);
139-
assertNotNull(mojo);
140-
128+
@Test
129+
@InjectMojo(goal = "clean", pom = "classpath:/unit/fileset-clean-test/plugin-pom.xml")
130+
public void testFilesetsClean(CleanMojo mojo) throws Exception {
141131
mojo.execute();
142132

143133
// fileset 1
@@ -162,42 +152,20 @@ public void testFilesetsClean() throws Exception {
162152
*
163153
* @throws Exception in case of an error.
164154
*/
165-
public void testCleanInvalidDirectory() throws Exception {
166-
String pluginPom = getBasedir() + "/src/test/resources/unit/invalid-directory-test/plugin-pom.xml";
167-
168-
// safety
169-
copyDirectory(
170-
new File(getBasedir(), "src/test/resources/unit/invalid-directory-test"),
171-
new File(getBasedir(), "target/test-classes/unit/invalid-directory-test"));
172-
173-
CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom);
174-
assertNotNull(mojo);
175-
176-
try {
177-
mojo.execute();
178-
179-
fail("Should fail to delete a file treated as a directory");
180-
} catch (MojoExecutionException expected) {
181-
assertTrue(true);
182-
}
155+
@Test
156+
@InjectMojo(goal = "clean", pom = "classpath:/unit/invalid-directory-test/plugin-pom.xml")
157+
public void testCleanInvalidDirectory(CleanMojo mojo) throws Exception {
158+
assertThrows(MojoExecutionException.class, mojo::execute);
183159
}
184160

185161
/**
186162
* Tests the removal of a missing directory
187163
*
188164
* @throws Exception in case of an error.
189165
*/
190-
public void testMissingDirectory() throws Exception {
191-
String pluginPom = getBasedir() + "/src/test/resources/unit/missing-directory-test/plugin-pom.xml";
192-
193-
// safety
194-
copyDirectory(
195-
new File(getBasedir(), "src/test/resources/unit/missing-directory-test"),
196-
new File(getBasedir(), "target/test-classes/unit/missing-directory-test"));
197-
198-
CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom);
199-
assertNotNull(mojo);
200-
166+
@Test
167+
@InjectMojo(goal = "clean", pom = "classpath:/unit/missing-directory-test/plugin-pom.xml")
168+
public void testMissingDirectory(CleanMojo mojo) throws Exception {
201169
mojo.execute();
202170

203171
assertFalse(checkExists(getBasedir() + "/target/test-classes/unit/missing-directory-test/does-not-exist"));
@@ -211,22 +179,10 @@ public void testMissingDirectory() throws Exception {
211179
*
212180
* @throws Exception in case of an error.
213181
*/
214-
public void testCleanLockedFile() throws Exception {
215-
if (!System.getProperty("os.name").toLowerCase().contains("windows")) {
216-
assertTrue("Ignored this test on non Windows based systems", true);
217-
return;
218-
}
219-
220-
String pluginPom = getBasedir() + "/src/test/resources/unit/locked-file-test/plugin-pom.xml";
221-
222-
// safety
223-
copyDirectory(
224-
new File(getBasedir(), "src/test/resources/unit/locked-file-test"),
225-
new File(getBasedir(), "target/test-classes/unit/locked-file-test"));
226-
227-
CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom);
228-
assertNotNull(mojo);
229-
182+
@Test
183+
@EnabledOnOs(OS.WINDOWS)
184+
@InjectMojo(goal = "clean", pom = "classpath:/unit/locked-file-test/plugin-pom.xml")
185+
public void testCleanLockedFile(CleanMojo mojo) throws Exception {
230186
File f = new File(getBasedir(), "target/test-classes/unit/locked-file-test/buildDirectory/file.txt");
231187
try (FileChannel channel = new RandomAccessFile(f, "rw").getChannel();
232188
FileLock ignored = channel.lock()) {
@@ -245,20 +201,10 @@ public void testCleanLockedFile() throws Exception {
245201
*
246202
* @throws Exception in case of an error.
247203
*/
248-
public void testCleanLockedFileWithNoError() throws Exception {
249-
if (!System.getProperty("os.name").toLowerCase().contains("windows")) {
250-
assertTrue("Ignore this test on non Windows based systems", true);
251-
return;
252-
}
253-
254-
String pluginPom = getBasedir() + "/src/test/resources/unit/locked-file-test/plugin-pom.xml";
255-
256-
// safety
257-
copyDirectory(
258-
new File(getBasedir(), "src/test/resources/unit/locked-file-test"),
259-
new File(getBasedir(), "target/test-classes/unit/locked-file-test"));
260-
261-
CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom);
204+
@Test
205+
@EnabledOnOs(OS.WINDOWS)
206+
@InjectMojo(goal = "clean", pom = "classpath:/unit/locked-file-test/plugin-pom.xml")
207+
public void testCleanLockedFileWithNoError(CleanMojo mojo) throws Exception {
262208
setVariableValueToObject(mojo, "failOnError", Boolean.FALSE);
263209
assertNotNull(mojo);
264210

@@ -277,12 +223,9 @@ public void testCleanLockedFileWithNoError() throws Exception {
277223
*
278224
* @throws Exception
279225
*/
226+
@Test
227+
@EnabledOnOs(OS.WINDOWS)
280228
public void testFollowLinksWithWindowsJunction() throws Exception {
281-
if (!System.getProperty("os.name").toLowerCase().contains("windows")) {
282-
assertTrue("Ignore this test on non Windows based systems", true);
283-
return;
284-
}
285-
286229
testSymlink((link, target) -> {
287230
Process process = new ProcessBuilder()
288231
.directory(link.getParent().toFile())
@@ -303,12 +246,9 @@ public void testFollowLinksWithWindowsJunction() throws Exception {
303246
*
304247
* @throws Exception
305248
*/
249+
@Test
250+
@DisabledOnOs(OS.WINDOWS)
306251
public void testFollowLinksWithSymLinkOnPosix() throws Exception {
307-
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
308-
assertTrue("Ignore this test on Windows based systems", true);
309-
return;
310-
}
311-
312252
testSymlink((link, target) -> {
313253
try {
314254
Files.createSymbolicLink(link, target);
@@ -354,6 +294,23 @@ private void testSymlink(LinkCreator linkCreator) throws Exception {
354294
assertFalse(Files.exists(dirWithLnk));
355295
}
356296

297+
@Provides
298+
@Singleton
299+
@SuppressWarnings("unused")
300+
private MavenSession createSession(PlexusContainer container) {
301+
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
302+
MavenExecutionResult result = new DefaultMavenExecutionResult();
303+
MavenSession session = new MavenSession(container, MavenRepositorySystemUtils.newSession(), request, result);
304+
return session;
305+
}
306+
307+
@Provides
308+
@Singleton
309+
@SuppressWarnings("unused")
310+
private MojoExecution createMojoExecution() {
311+
return new MojoExecution(null);
312+
}
313+
357314
/**
358315
* @param dir a dir or a file
359316
* @return true if a file/dir exists, false otherwise

0 commit comments

Comments
 (0)