diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java index bb0b987..803c507 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java @@ -26,7 +26,7 @@ /** * Specifies the base directory for test resources in Maven plugin tests. - * This annotation can be applied to test methods to define where test resources are located. + * This annotation can be applied to test methods, or test class, to define where test resources are located. * **
Example usage:
*
@@ -50,7 +50,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Inherited
-@Target(ElementType.METHOD)
+@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Basedir {
String value() default "";
}
diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java
index 24961bc..7fc4fdf 100644
--- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java
+++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java
@@ -82,7 +82,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Inherited
-@Target(ElementType.METHOD)
+@Target({ElementType.METHOD, ElementType.PARAMETER})
public @interface InjectMojo {
String goal();
diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
index 66699f8..3461e39 100644
--- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
+++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
@@ -36,7 +36,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
-import java.util.HashSet;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -145,8 +145,9 @@ public class MojoExtension extends PlexusExtension implements ParameterResolver
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
- return parameterContext.isAnnotated(InjectMojo.class)
- || parameterContext.getDeclaringExecutable().isAnnotationPresent(InjectMojo.class);
+ return Mojo.class.isAssignableFrom(parameterContext.getParameter().getType())
+ && (parameterContext.isAnnotated(InjectMojo.class)
+ || parameterContext.getDeclaringExecutable().isAnnotationPresent(InjectMojo.class));
}
@Override
@@ -157,8 +158,13 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
.findAnnotation(InjectMojo.class)
.orElseGet(() -> parameterContext.getDeclaringExecutable().getAnnotation(InjectMojo.class));
- Set mojoParameters =
- new HashSet<>(parameterContext.findRepeatableAnnotations(MojoParameter.class));
+ Set mojoParameters = new LinkedHashSet<>();
+
+ extensionContext
+ .getTestClass()
+ .map(c -> c.getAnnotationsByType(MojoParameter.class))
+ .map(Arrays::asList)
+ .ifPresent(mojoParameters::addAll);
Optional.ofNullable(parameterContext.getDeclaringExecutable().getAnnotation(MojoParameter.class))
.ifPresent(mojoParameters::add);
@@ -168,6 +174,8 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
.map(Arrays::asList)
.ifPresent(mojoParameters::addAll);
+ mojoParameters.addAll(parameterContext.findRepeatableAnnotations(MojoParameter.class));
+
Class> holder = parameterContext.getTarget().get().getClass();
PluginDescriptor descriptor =
extensionContext.getStore(MOJO_EXTENSION).get(PluginDescriptor.class, PluginDescriptor.class);
@@ -181,7 +189,11 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
public void beforeEach(ExtensionContext context) throws Exception {
String basedir = AnnotationSupport.findAnnotation(context.getElement().get(), Basedir.class)
.map(Basedir::value)
- .orElse(null);
+ .orElseGet(() -> {
+ return AnnotationSupport.findAnnotation(context.getTestClass(), Basedir.class)
+ .map(Basedir::value)
+ .orElse(null);
+ });
if (basedir == null) {
basedir = getBasedir();
diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java
index 276ef65..780e32f 100644
--- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java
+++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java
@@ -31,9 +31,9 @@
* without requiring a full POM file.
*
* The annotation is repeatable, allowing multiple parameters to be set
- * on a single test method or parameter. For multiple parameters, you can
- * either use multiple {@code @MojoParameter} annotations or a single
- * {@link MojoParameters} annotation.
+ * on a single test method, parameter, or on the whole test class. For
+ * multiple parameters, you can either use multiple {@code @MojoParameter}
+ * annotations or a single {@link MojoParameters} annotation.
*
* Example usage with a single parameter:
*
@@ -69,7 +69,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MojoParameters.class)
@Inherited
-@Target(ElementType.METHOD)
+@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
public @interface MojoParameter {
String name();
diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java
index ebe63bf..5565281 100644
--- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java
+++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java
@@ -70,7 +70,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Inherited
-@Target(ElementType.METHOD)
+@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
public @interface MojoParameters {
MojoParameter[] value();
}
diff --git a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/AnnotationLevelMojoTest.java b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/AnnotationLevelMojoTest.java
new file mode 100644
index 0000000..d897c0c
--- /dev/null
+++ b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/AnnotationLevelMojoTest.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.testing;
+
+import org.apache.maven.api.plugin.testing.Basedir;
+import org.apache.maven.api.plugin.testing.InjectMojo;
+import org.apache.maven.api.plugin.testing.MojoExtension;
+import org.apache.maven.api.plugin.testing.MojoParameter;
+import org.apache.maven.api.plugin.testing.MojoTest;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@MojoTest
+@Basedir("class-basedir")
+@MojoParameter(name = "plain", value = "class-value")
+class AnnotationLevelMojoTest {
+
+ @Test
+ @InjectMojo(goal = "parameters")
+ void classLevelValues(ParametersMojo mojo) {
+ assertEquals("class-value", mojo.getPlain());
+ assertTrue(
+ MojoExtension.getBasedir().endsWith("class-basedir"),
+ "Basedir value did not came from class annotation");
+ }
+
+ @Test
+ @InjectMojo(goal = "parameters")
+ @Basedir("method-basedir")
+ @MojoParameter(name = "plain", value = "method-value")
+ void methodLevelValues(ParametersMojo mojo) {
+ assertEquals("method-value", mojo.getPlain());
+ assertTrue(
+ MojoExtension.getBasedir().endsWith("method-basedir"),
+ "Basedir value did not came from method annotation");
+ }
+
+ @Test
+ void parameterLevelValues(
+ @InjectMojo(goal = "parameters") @MojoParameter(name = "plain", value = "param-level-param-value")
+ ParametersMojo mojo) {
+ assertEquals("param-level-param-value", mojo.getPlain());
+ }
+
+ @Test
+ @MojoParameter(name = "plain", value = "method-value")
+ void mojoParameterOnMethod(
+ @InjectMojo(goal = "parameters") ParametersMojo mojo,
+ @InjectMojo(goal = "parameters") @MojoParameter(name = "plain", value = "param-value")
+ ParametersMojo alternateMojo) {
+ assertEquals("method-value", mojo.getPlain());
+ assertEquals("param-value", alternateMojo.getPlain());
+ }
+}
diff --git a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java
index fdc8f97..90eb789 100644
--- a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java
+++ b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java
@@ -33,9 +33,11 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@MojoTest
@@ -234,4 +236,11 @@ void basedirInjectedWithBasedirFromClasspathAnnotationDefaultPom(ParametersMojo
assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
assertDoesNotThrow(mojo::execute);
}
+
+ @Test
+ @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters")
+ void testMultipleResolvedParameters(ParametersMojo mojo, TestInfo testInfo) {
+ assertNotNull(mojo);
+ assertNotNull(testInfo);
+ }
}