Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
** <p>Example usage:</p>
* <pre>
Expand All @@ -50,7 +50,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Target(ElementType.METHOD)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Basedir {
String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Target(ElementType.METHOD)
@Target({ElementType.METHOD, ElementType.PARAMETER})
public @interface InjectMojo {

String goal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -157,8 +158,13 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
.findAnnotation(InjectMojo.class)
.orElseGet(() -> parameterContext.getDeclaringExecutable().getAnnotation(InjectMojo.class));

Set<MojoParameter> mojoParameters =
new HashSet<>(parameterContext.findRepeatableAnnotations(MojoParameter.class));
Set<MojoParameter> 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);
Expand All @@ -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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
* without requiring a full POM file.
*
* <p>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.</p>
* 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.</p>
*
* <p>Example usage with a single parameter:</p>
* <pre>
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Target(ElementType.METHOD)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
public @interface MojoParameters {
MojoParameter[] value();
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}