diff --git a/pom.xml b/pom.xml index 456257f5..11ee271c 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ under the License. org.apache.maven.plugins maven-plugins - 43 + 44 @@ -88,7 +88,6 @@ under the License. 6.0.0 5.17.0 4.0.0-beta-4 - 2.15.0 0.9.0.M2 3.13.1 4.0.0-beta-1 diff --git a/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java b/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java index 62e260c9..ffd3a39c 100644 --- a/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java +++ b/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java @@ -19,6 +19,8 @@ package org.apache.maven.plugin.compiler; import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; @@ -48,7 +50,6 @@ import org.apache.maven.api.plugin.testing.stubs.SessionMock; import org.apache.maven.api.services.ArtifactManager; import org.apache.maven.api.services.MessageBuilderFactory; -import org.apache.maven.api.services.ProjectManager; import org.apache.maven.api.services.ToolchainManager; import org.apache.maven.impl.DefaultMessageBuilderFactory; import org.apache.maven.impl.InternalSession; @@ -79,16 +80,33 @@ public class CompilerMojoTestCase { @Inject private Session session; + /** + * Verifies that the {@value CompilerStub#OUTPUT_FILE} file exists, then deletes it. + * The deletion is necessary for preventing an {@link IndexOutOfBoundsException} in + * {@code maven-dependency-plugin} version 3.8.1, because the output file is empty. + * + * @param mojo the tested mojo + */ + private static void assertCompilerStubOutputFileExists(AbstractCompilerMojo mojo) { + try { + Files.delete(assertOutputFileExists(mojo, CompilerStub.OUTPUT_FILE)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + /** * Verifies that the given output file exists. * * @param mojo the tested mojo * @param first the first path element * @param more the other path elements, if any + * @return the file */ - private static void assertOutputFileExists(AbstractCompilerMojo mojo, String first, String... more) { + private static Path assertOutputFileExists(AbstractCompilerMojo mojo, String first, String... more) { Path file = mojo.getOutputDirectory().resolve(Path.of(first, more)); assertTrue(Files.isRegularFile(file), () -> "File not found: " + file); + return file; } /** @@ -228,11 +246,11 @@ public void testOneOutputFileForAllInput( assertEquals(CompilerStub.COMPILER_ID, compileMojo.compilerId); compileMojo.execute(); - assertOutputFileExists(compileMojo, CompilerStub.OUTPUT_FILE); + assertCompilerStubOutputFileExists(compileMojo); assertEquals(CompilerStub.COMPILER_ID, testCompileMojo.compilerId); testCompileMojo.execute(); - assertOutputFileExists(testCompileMojo, CompilerStub.OUTPUT_FILE); + assertCompilerStubOutputFileExists(testCompileMojo); } /** @@ -245,7 +263,7 @@ public void testCompilerArgs(@InjectMojo(goal = "compile", pom = "plugin-config. assertEquals(CompilerStub.COMPILER_ID, compileMojo.compilerId); compileMojo.execute(); - assertOutputFileExists(compileMojo, CompilerStub.OUTPUT_FILE); + assertCompilerStubOutputFileExists(compileMojo); assertArrayEquals( new String[] {"key1=value1", "-Xlint", "-my&special:param-with+chars/not>allowed_in_XML_element_names"}, compileMojo.compilerArgs.toArray(String[]::new)); @@ -429,7 +447,6 @@ private static InternalSession createSession() { throw new RuntimeException("Unable to setup junit jar path", e); } - ProjectManager projectManager = session.getService(ProjectManager.class); doAnswer(iom -> List.of()).when(session).resolveDependencies(any(), eq(PathScope.MAIN_COMPILE)); doAnswer(iom -> artifacts).when(session).resolveDependencies(any(), eq(PathScope.TEST_COMPILE)); diff --git a/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerStub.java b/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerStub.java index b31c37f4..dd14a810 100644 --- a/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerStub.java +++ b/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerStub.java @@ -85,7 +85,7 @@ public class CompilerStub implements JavaCompiler, StandardJavaFileManager { * * @see #getOptions() */ - private static final ThreadLocal> arguments = new ThreadLocal<>(); + private static final ThreadLocal> ARGUMENTS = new ThreadLocal<>(); /** * Invoked by reflection by {@link java.util.ServiceLoader}. @@ -309,7 +309,7 @@ public CompilationTask getTask( Iterable classes, Iterable compilationUnits) { - arguments.set(options); + ARGUMENTS.set(options); return new CompilationTask() { @Override public void addModules(Iterable moduleNames) {} @@ -354,7 +354,7 @@ public int run(InputStream in, OutputStream out, OutputStream err, String... arg */ public static List getOptions() { var options = new ArrayList(); - Iterable args = arguments.get(); + Iterable args = ARGUMENTS.get(); if (args != null) { args.forEach(options::add); }