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
6 changes: 6 additions & 0 deletions maven-plugin-testing-harness/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ under the License.
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.api.di;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Marks a method as a provider of beans for dependency injection.
*/
@Target(METHOD)
@Retention(RUNTIME)
@Documented
public @interface Provides {}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.io.StringReader;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -43,7 +45,10 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.internal.ProviderMethodsModule;
import org.apache.maven.api.di.Provides;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
import org.apache.maven.plugin.Mojo;
Expand Down Expand Up @@ -77,6 +82,7 @@
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.platform.commons.support.AnnotationSupport;
import org.junit.platform.commons.support.HierarchyTraversalMode;
import org.mockito.Mockito;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -142,6 +148,7 @@ public void beforeEach(ExtensionContext context) throws Exception {

((DefaultPlexusContainer) getContainer()).addPlexusInjector(Collections.emptyList(), binder -> {
binder.install(ProviderMethodsModule.forObject(context.getRequiredTestInstance()));
binder.install(new MavenProvidesModule(context.getRequiredTestInstance()));
binder.requestInjection(context.getRequiredTestInstance());
binder.bind(Log.class).toInstance(new MojoLogWrapper(LoggerFactory.getLogger("anonymous")));
binder.bind(MavenSession.class).toInstance(mockMavenSession());
Expand Down Expand Up @@ -190,8 +197,8 @@ private MojoExecution mockMojoExecution() {
*/
private MavenSession mockMavenSession() {
MavenSession session = Mockito.mock(MavenSession.class);
Mockito.when(session.getUserProperties()).thenReturn(new Properties());
Mockito.when(session.getSystemProperties()).thenReturn(new Properties());
Mockito.lenient().when(session.getUserProperties()).thenReturn(new Properties());
Mockito.lenient().when(session.getSystemProperties()).thenReturn(new Properties());
return session;
}

Expand Down Expand Up @@ -455,4 +462,32 @@ public File alignToBaseDirectory(File path) {
return evaluator.alignToBaseDirectory(path);
}
}

private static class MavenProvidesModule implements Module {
private final Object testInstance;

@Override
@SuppressWarnings("unchecked")
public void configure(Binder binder) {
List<Method> providesMethods = AnnotationSupport.findAnnotatedMethods(
testInstance.getClass(), Provides.class, HierarchyTraversalMode.BOTTOM_UP);

for (Method method : providesMethods) {
if (method.getParameterCount() > 0) {
throw new IllegalArgumentException("Parameterized method are not supported " + method);
}
try {
method.setAccessible(true);
Object value = method.invoke(testInstance);
binder.bind((Class<Object>) method.getReturnType()).toInstance(value);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
}
}

MavenProvidesModule(Object testInstance) {
this.testInstance = testInstance;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 javax.inject.Inject;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.project.MavenProject;

public class ProvidesInjectMojo extends AbstractMojo {

private final MavenSession session;
private final MavenProject project;

@Inject
public ProvidesInjectMojo(MavenSession session, MavenProject project) {
this.session = session;
this.project = project;
}

@Override
public void execute() {}

public MavenSession getSession() {
return session;
}

public MavenProject getProject() {
return project;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.di.Provides;
import org.apache.maven.api.plugin.testing.InjectMojo;
import org.apache.maven.api.plugin.testing.MojoTest;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;

@ExtendWith(MockitoExtension.class)
@MojoTest
public class ProvidesInjectMojoTest {

private static final String POM = "<project>" + "</project>";

@Mock
private MavenProject project;

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

@Test
@InjectMojo(pom = POM, goal = "test:test-plugin:0.0.1-SNAPSHOT:provides")
public void bennShouldBeInjected(ProvidesInjectMojo mojo) {
assertNotNull(mojo);
assertNotNull(mojo.getSession());
assertSame(project, mojo.getProject());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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 java.util.Properties;

import org.apache.maven.api.di.Provides;
import org.apache.maven.api.plugin.testing.InjectMojo;
import org.apache.maven.api.plugin.testing.MojoTest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
@MojoTest
class ProvidesInjectOverrideMojoTest {

private static final String POM = "<project></project>";

@Mock
private MavenProject project;

@Mock
private MavenSession session;

@BeforeEach
void setup() {
when(session.getUserProperties()).thenReturn(new Properties());
when(session.getSystemProperties()).thenReturn(new Properties());
}

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

@Provides
public MavenSession mockMavenSession() {
return session;
}

@Test
@InjectMojo(pom = POM, goal = "test:test-plugin:0.0.1-SNAPSHOT:provides")
public void bennShouldBeInjected(ProvidesInjectMojo mojo) {
assertNotNull(mojo);
// session provided by the @Provides method should be used
assertSame(session, mojo.getSession());
assertSame(project, mojo.getProject());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ under the License.
<withPropertyAndDefault implementation="java.lang.String" default-value="default">${property}</withPropertyAndDefault>
</configuration>
</mojo>
<mojo>
<goal>provides</goal>
<requiresDirectInvocation>false</requiresDirectInvocation>
<requiresProject>true</requiresProject>
<requiresReports>false</requiresReports>
<aggregator>false</aggregator>
<requiresOnline>false</requiresOnline>
<inheritedByDefault>true</inheritedByDefault>
<implementation>org.apache.maven.plugin.testing.ProvidesInjectMojo</implementation>
<language>java</language>
<instantiationStrategy>per-lookup</instantiationStrategy>
<executionStrategy>once-per-session</executionStrategy>
<threadSafe>false</threadSafe>
</mojo>
</mojos>
<dependencies/>
</plugin>