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 @@ -221,10 +221,12 @@ public void beforeEach(ExtensionContext context) throws Exception {

PlexusContainer plexusContainer = getContainer(context);

((DefaultPlexusContainer) plexusContainer).addPlexusInjector(Collections.emptyList(), binder -> {
binder.install(ProviderMethodsModule.forObject(context.getRequiredTestInstance()));
binder.install(new MavenProvidesModule(context.getRequiredTestInstance()));
});
context.getRequiredTestInstances().getAllInstances().forEach(testInstance -> ((DefaultPlexusContainer)
plexusContainer)
.addPlexusInjector(Collections.emptyList(), binder -> {
binder.install(ProviderMethodsModule.forObject(testInstance));
binder.install(new MavenProvidesModule(testInstance));
}));

addMock(plexusContainer, Log.class, () -> new MojoLogWrapper(LoggerFactory.getLogger("anonymous")));
MavenProject mavenProject = addMock(plexusContainer, MavenProject.class, this::mockMavenProject);
Expand All @@ -243,9 +245,11 @@ public void beforeEach(ExtensionContext context) throws Exception {
executionScope.seed(MavenProject.class, mavenProject);
executionScope.seed(MojoExecution.class, mojoExecution);

((DefaultPlexusContainer) plexusContainer).addPlexusInjector(Collections.emptyList(), binder -> {
binder.requestInjection(context.getRequiredTestInstance());
});
context.getRequiredTestInstances().getAllInstances().forEach(testInstance -> ((DefaultPlexusContainer)
plexusContainer)
.addPlexusInjector(Collections.emptyList(), binder -> {
binder.requestInjection(testInstance);
}));

Map<Object, Object> map = plexusContainer.getContext().getContextData();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ void setup() {
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = PROPERTY_POM_DIR + POM_DOT_XML_FILE)
@MojoParameter(name = "plain", value = "test-${property}")
void testPropertyPom(ParametersMojo mojo) {

assertNotNull(log, "log from parent class should be injected properly");

assertEquals("test-testPropertyValue", mojo.getPlain());
assertEquals("testPropertyValue", mojo.getWithProperty());
assertEquals("default", mojo.getWithDefault());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -74,7 +75,7 @@ MojoExecution mockMojoExecution() {

@Test
@InjectMojo(pom = POM, goal = "test:test-plugin:0.0.1-SNAPSHOT:provides")
public void bennShouldBeInjected(ProvidesInjectMojo mojo) {
void bennShouldBeInjected(ProvidesInjectMojo mojo) {
assertNotNull(mojo);
// session provided by the @Provides method should be used
assertSame(session, mojo.getSession());
Expand All @@ -86,4 +87,32 @@ public void bennShouldBeInjected(ProvidesInjectMojo mojo) {
assertSame(mojoExecution, mojo.getMojoExecution());
assertSame(mojoExecution, mojo.getMojoExecutionFromBean());
}

@Nested
class NestedTest {

@Mock
private MavenProject nestedProject;

@Provides
public MavenProject mockMavenProject() {
return nestedProject;
}

@Test
@InjectMojo(pom = POM, goal = "test:test-plugin:0.0.1-SNAPSHOT:provides")
void bennFromParentClassShouldBeInjected(ProvidesInjectMojo mojo) {

assertNotNull(mojo);
// session provided by the @Provides method should be used
assertSame(session, mojo.getSession());
assertSame(session, mojo.getSessionFromBean());

assertSame(nestedProject, mojo.getProject());
assertSame(nestedProject, mojo.getProjectFromBean());

assertSame(mojoExecution, mojo.getMojoExecution());
assertSame(mojoExecution, mojo.getMojoExecutionFromBean());
}
}
}