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 @@ -171,6 +171,10 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte

Set<MojoParameter> mojoParameters = new LinkedHashSet<>();

extensionContext.getEnclosingTestClasses().forEach(testClass -> {
mojoParameters.addAll(Arrays.asList(testClass.getAnnotationsByType(MojoParameter.class)));
});

extensionContext
.getTestClass()
.map(c -> c.getAnnotationsByType(MojoParameter.class))
Expand Down Expand Up @@ -201,7 +205,8 @@ public void beforeEach(ExtensionContext context) throws Exception {
String basedir = AnnotationSupport.findAnnotation(context.getElement().get(), Basedir.class)
.map(Basedir::value)
.orElseGet(() -> {
return AnnotationSupport.findAnnotation(context.getTestClass(), Basedir.class)
return AnnotationSupport.findAnnotation(
context.getRequiredTestClass(), Basedir.class, context.getEnclosingTestClasses())
.map(Basedir::value)
.orElse(null);
});
Expand Down Expand Up @@ -518,7 +523,8 @@ protected Mojo lookupMojo(
}

private boolean isRealRepositorySessionNotRequired(ExtensionContext context) {
return !AnnotationSupport.findAnnotation(context.getTestClass(), MojoTest.class)
return !AnnotationSupport.findAnnotation(
context.getRequiredTestClass(), MojoTest.class, context.getEnclosingTestClasses())
.map(MojoTest::realRepositorySession)
.orElse(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
*/
package org.apache.maven.plugin.testing;

import java.io.File;

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.Nested;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -33,12 +36,14 @@
@MojoParameter(name = "plain", value = "class-value")
class AnnotationLevelMojoTest {

private static final String FS = File.separator;

@Test
@InjectMojo(goal = "parameters")
void classLevelValues(ParametersMojo mojo) {
assertEquals("class-value", mojo.getPlain());
assertTrue(
MojoExtension.getBasedir().endsWith("class-basedir"),
MojoExtension.getBasedir().endsWith(FS + "class-basedir"),
"Basedir value did not came from class annotation");
}

Expand All @@ -49,7 +54,7 @@ void classLevelValues(ParametersMojo mojo) {
void methodLevelValues(ParametersMojo mojo) {
assertEquals("method-value", mojo.getPlain());
assertTrue(
MojoExtension.getBasedir().endsWith("method-basedir"),
MojoExtension.getBasedir().endsWith(FS + "method-basedir"),
"Basedir value did not came from method annotation");
}

Expand All @@ -69,4 +74,61 @@ void mojoParameterOnMethod(
assertEquals("method-value", mojo.getPlain());
assertEquals("param-value", alternateMojo.getPlain());
}

@Nested
class NestedTest {
// all tests are duplicated from parent class

protected String nestedAnnotationValue() {
return "";
}

@Test
@InjectMojo(goal = "parameters")
void classLevelValues(ParametersMojo mojo) {
assertEquals(nestedAnnotationValue() + "class-value", mojo.getPlain());
assertTrue(
MojoExtension.getBasedir().endsWith(FS + nestedAnnotationValue() + "class-basedir"),
"Basedir value did not came from class annotation: " + MojoExtension.getBasedir());
}

@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(FS + "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());
}
}

@Nested
@Basedir("nested-class-basedir")
@MojoParameter(name = "plain", value = "nested-class-value")
class NestedAnnotationLevelTest extends NestedTest {

@Override
protected String nestedAnnotationValue() {
return "nested-";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.apache.maven.api.plugin.testing.MojoParameter;
import org.apache.maven.api.plugin.testing.MojoTest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand Down Expand Up @@ -62,8 +62,18 @@ void beforeEach() {
@Test
@InjectMojo(goal = "simple-resolve")
@MojoParameter(name = "artifact", value = "org.apache.commons:commons-lang3:3.20.0")
void artifactShouldBeResolved(SimpleResolveMojo mojo) throws MojoExecutionException {
void artifactShouldBeResolved(SimpleResolveMojo mojo) {
assertDoesNotThrow(mojo::execute);
}

@Nested
class NestedTest {
@Test
@InjectMojo(goal = "simple-resolve")
@MojoParameter(name = "artifact", value = "org.apache.commons:commons-lang3:3.20.0")
void artifactShouldBeResolved(SimpleResolveMojo mojo) {
assertDoesNotThrow(mojo::execute);
}
}
}
// END SNIPPET: resolve-mojo-test