19
19
package org .apache .maven .plugin .compiler ;
20
20
21
21
import java .io .File ;
22
+ import java .io .IOException ;
23
+ import java .io .UncheckedIOException ;
22
24
import java .net .URI ;
23
25
import java .nio .file .Files ;
24
26
import java .nio .file .Path ;
48
50
import org .apache .maven .api .plugin .testing .stubs .SessionMock ;
49
51
import org .apache .maven .api .services .ArtifactManager ;
50
52
import org .apache .maven .api .services .MessageBuilderFactory ;
51
- import org .apache .maven .api .services .ProjectManager ;
52
53
import org .apache .maven .api .services .ToolchainManager ;
53
54
import org .apache .maven .impl .DefaultMessageBuilderFactory ;
54
55
import org .apache .maven .impl .InternalSession ;
@@ -79,16 +80,33 @@ public class CompilerMojoTestCase {
79
80
@ Inject
80
81
private Session session ;
81
82
83
+ /**
84
+ * Verifies that the {@value CompilerStub#OUTPUT_FILE} file exists, then deletes it.
85
+ * The deletion is necessary for preventing an {@link IndexOutOfBoundsException} in
86
+ * {@code maven-dependency-plugin} version 3.8.1, because the output file is empty.
87
+ *
88
+ * @param mojo the tested mojo
89
+ */
90
+ private static void assertCompilerStubOutputFileExists (AbstractCompilerMojo mojo ) {
91
+ try {
92
+ Files .delete (assertOutputFileExists (mojo , CompilerStub .OUTPUT_FILE ));
93
+ } catch (IOException e ) {
94
+ throw new UncheckedIOException (e );
95
+ }
96
+ }
97
+
82
98
/**
83
99
* Verifies that the given output file exists.
84
100
*
85
101
* @param mojo the tested mojo
86
102
* @param first the first path element
87
103
* @param more the other path elements, if any
104
+ * @return the file
88
105
*/
89
- private static void assertOutputFileExists (AbstractCompilerMojo mojo , String first , String ... more ) {
106
+ private static Path assertOutputFileExists (AbstractCompilerMojo mojo , String first , String ... more ) {
90
107
Path file = mojo .getOutputDirectory ().resolve (Path .of (first , more ));
91
108
assertTrue (Files .isRegularFile (file ), () -> "File not found: " + file );
109
+ return file ;
92
110
}
93
111
94
112
/**
@@ -228,11 +246,11 @@ public void testOneOutputFileForAllInput(
228
246
229
247
assertEquals (CompilerStub .COMPILER_ID , compileMojo .compilerId );
230
248
compileMojo .execute ();
231
- assertOutputFileExists (compileMojo , CompilerStub . OUTPUT_FILE );
249
+ assertCompilerStubOutputFileExists (compileMojo );
232
250
233
251
assertEquals (CompilerStub .COMPILER_ID , testCompileMojo .compilerId );
234
252
testCompileMojo .execute ();
235
- assertOutputFileExists (testCompileMojo , CompilerStub . OUTPUT_FILE );
253
+ assertCompilerStubOutputFileExists (testCompileMojo );
236
254
}
237
255
238
256
/**
@@ -245,7 +263,7 @@ public void testCompilerArgs(@InjectMojo(goal = "compile", pom = "plugin-config.
245
263
assertEquals (CompilerStub .COMPILER_ID , compileMojo .compilerId );
246
264
compileMojo .execute ();
247
265
248
- assertOutputFileExists (compileMojo , CompilerStub . OUTPUT_FILE );
266
+ assertCompilerStubOutputFileExists (compileMojo );
249
267
assertArrayEquals (
250
268
new String [] {"key1=value1" , "-Xlint" , "-my&special:param-with+chars/not>allowed_in_XML_element_names" },
251
269
compileMojo .compilerArgs .toArray (String []::new ));
@@ -429,7 +447,6 @@ private static InternalSession createSession() {
429
447
throw new RuntimeException ("Unable to setup junit jar path" , e );
430
448
}
431
449
432
- ProjectManager projectManager = session .getService (ProjectManager .class );
433
450
doAnswer (iom -> List .of ()).when (session ).resolveDependencies (any (), eq (PathScope .MAIN_COMPILE ));
434
451
doAnswer (iom -> artifacts ).when (session ).resolveDependencies (any (), eq (PathScope .TEST_COMPILE ));
435
452
0 commit comments