29
29
import java .nio .file .Paths ;
30
30
import java .util .Collections ;
31
31
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 ;
32
40
import org .apache .maven .plugin .MojoExecutionException ;
33
41
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 ;
37
53
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 ;
38
59
39
60
/**
40
61
* Test the clean mojo.
41
62
*
42
63
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
43
64
*/
44
- public class CleanMojoTest extends AbstractMojoTestCase {
65
+ @ MojoTest
66
+ public class CleanMojoTest {
45
67
/**
46
68
* Tests the simple removal of directories
47
69
*
48
70
* @throws Exception in case of an error.
49
71
*/
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 {
61
75
mojo .execute ();
62
76
63
77
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" );
66
80
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" );
69
83
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" );
72
86
}
73
87
74
88
/**
75
89
* Tests the removal of files and nested directories
76
90
*
77
91
* @throws Exception in case of an error.
78
92
*/
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 {
90
96
mojo .execute ();
91
97
92
98
assertFalse (checkExists (getBasedir () + "/target/test-classes/unit/nested-clean-test/target" ));
@@ -100,17 +106,9 @@ public void testCleanNestedStructure() throws Exception {
100
106
*
101
107
* @throws Exception in case of an error.
102
108
*/
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 {
114
112
mojo .execute ();
115
113
116
114
assertTrue (checkExists (getBasedir () + "/target/test-classes/unit/empty-clean-test/testDirectoryStructure" ));
@@ -127,17 +125,9 @@ public void testCleanEmptyDirectories() throws Exception {
127
125
*
128
126
* @throws Exception in case of an error.
129
127
*/
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 {
141
131
mojo .execute ();
142
132
143
133
// fileset 1
@@ -162,42 +152,20 @@ public void testFilesetsClean() throws Exception {
162
152
*
163
153
* @throws Exception in case of an error.
164
154
*/
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 );
183
159
}
184
160
185
161
/**
186
162
* Tests the removal of a missing directory
187
163
*
188
164
* @throws Exception in case of an error.
189
165
*/
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 {
201
169
mojo .execute ();
202
170
203
171
assertFalse (checkExists (getBasedir () + "/target/test-classes/unit/missing-directory-test/does-not-exist" ));
@@ -211,22 +179,10 @@ public void testMissingDirectory() throws Exception {
211
179
*
212
180
* @throws Exception in case of an error.
213
181
*/
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 {
230
186
File f = new File (getBasedir (), "target/test-classes/unit/locked-file-test/buildDirectory/file.txt" );
231
187
try (FileChannel channel = new RandomAccessFile (f , "rw" ).getChannel ();
232
188
FileLock ignored = channel .lock ()) {
@@ -245,20 +201,10 @@ public void testCleanLockedFile() throws Exception {
245
201
*
246
202
* @throws Exception in case of an error.
247
203
*/
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 {
262
208
setVariableValueToObject (mojo , "failOnError" , Boolean .FALSE );
263
209
assertNotNull (mojo );
264
210
@@ -277,12 +223,9 @@ public void testCleanLockedFileWithNoError() throws Exception {
277
223
*
278
224
* @throws Exception
279
225
*/
226
+ @ Test
227
+ @ EnabledOnOs (OS .WINDOWS )
280
228
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
-
286
229
testSymlink ((link , target ) -> {
287
230
Process process = new ProcessBuilder ()
288
231
.directory (link .getParent ().toFile ())
@@ -303,12 +246,9 @@ public void testFollowLinksWithWindowsJunction() throws Exception {
303
246
*
304
247
* @throws Exception
305
248
*/
249
+ @ Test
250
+ @ DisabledOnOs (OS .WINDOWS )
306
251
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
-
312
252
testSymlink ((link , target ) -> {
313
253
try {
314
254
Files .createSymbolicLink (link , target );
@@ -354,6 +294,23 @@ private void testSymlink(LinkCreator linkCreator) throws Exception {
354
294
assertFalse (Files .exists (dirWithLnk ));
355
295
}
356
296
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
+
357
314
/**
358
315
* @param dir a dir or a file
359
316
* @return true if a file/dir exists, false otherwise
0 commit comments