From ca9abbbd8e5daee6da1e35db3c037a537d7e9a9c Mon Sep 17 00:00:00 2001
From: Michiel Hendriks Example usage: 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.
@@ -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 d6e0bcc..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;
@@ -158,8 +158,13 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
.findAnnotation(InjectMojo.class)
.orElseGet(() -> parameterContext.getDeclaringExecutable().getAnnotation(InjectMojo.class));
- Set
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());
+ }
+}