1919package org .apache .maven .plugin .compiler ;
2020
2121import java .io .File ;
22+ import java .io .IOException ;
23+ import java .io .UncheckedIOException ;
2224import java .net .URI ;
2325import java .nio .file .Files ;
2426import java .nio .file .Path ;
4850import org .apache .maven .api .plugin .testing .stubs .SessionMock ;
4951import org .apache .maven .api .services .ArtifactManager ;
5052import org .apache .maven .api .services .MessageBuilderFactory ;
51- import org .apache .maven .api .services .ProjectManager ;
5253import org .apache .maven .api .services .ToolchainManager ;
5354import org .apache .maven .impl .DefaultMessageBuilderFactory ;
5455import org .apache .maven .impl .InternalSession ;
@@ -79,16 +80,33 @@ public class CompilerMojoTestCase {
7980 @ Inject
8081 private Session session ;
8182
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+
8298 /**
8399 * Verifies that the given output file exists.
84100 *
85101 * @param mojo the tested mojo
86102 * @param first the first path element
87103 * @param more the other path elements, if any
104+ * @return the file
88105 */
89- private static void assertOutputFileExists (AbstractCompilerMojo mojo , String first , String ... more ) {
106+ private static Path assertOutputFileExists (AbstractCompilerMojo mojo , String first , String ... more ) {
90107 Path file = mojo .getOutputDirectory ().resolve (Path .of (first , more ));
91108 assertTrue (Files .isRegularFile (file ), () -> "File not found: " + file );
109+ return file ;
92110 }
93111
94112 /**
@@ -228,11 +246,11 @@ public void testOneOutputFileForAllInput(
228246
229247 assertEquals (CompilerStub .COMPILER_ID , compileMojo .compilerId );
230248 compileMojo .execute ();
231- assertOutputFileExists (compileMojo , CompilerStub . OUTPUT_FILE );
249+ assertCompilerStubOutputFileExists (compileMojo );
232250
233251 assertEquals (CompilerStub .COMPILER_ID , testCompileMojo .compilerId );
234252 testCompileMojo .execute ();
235- assertOutputFileExists (testCompileMojo , CompilerStub . OUTPUT_FILE );
253+ assertCompilerStubOutputFileExists (testCompileMojo );
236254 }
237255
238256 /**
@@ -245,7 +263,7 @@ public void testCompilerArgs(@InjectMojo(goal = "compile", pom = "plugin-config.
245263 assertEquals (CompilerStub .COMPILER_ID , compileMojo .compilerId );
246264 compileMojo .execute ();
247265
248- assertOutputFileExists (compileMojo , CompilerStub . OUTPUT_FILE );
266+ assertCompilerStubOutputFileExists (compileMojo );
249267 assertArrayEquals (
250268 new String [] {"key1=value1" , "-Xlint" , "-my&special:param-with+chars/not>allowed_in_XML_element_names" },
251269 compileMojo .compilerArgs .toArray (String []::new ));
@@ -429,7 +447,6 @@ private static InternalSession createSession() {
429447 throw new RuntimeException ("Unable to setup junit jar path" , e );
430448 }
431449
432- ProjectManager projectManager = session .getService (ProjectManager .class );
433450 doAnswer (iom -> List .of ()).when (session ).resolveDependencies (any (), eq (PathScope .MAIN_COMPILE ));
434451 doAnswer (iom -> artifacts ).when (session ).resolveDependencies (any (), eq (PathScope .TEST_COMPILE ));
435452
0 commit comments