Skip to content

Commit 7153e9f

Browse files
[MPLUGINTESTING-91] Maven 3 compatible and improvements
- make some dependencies as optional - add test with JUnit 5 for Maven 3 - fix MojoParameters support
1 parent cb67665 commit 7153e9f

6 files changed

Lines changed: 212 additions & 10 deletions

File tree

maven-plugin-testing-harness/pom.xml

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ under the License.
3030
<name>Maven Plugin Testing Mechanism</name>
3131
<description>The Maven Plugin Testing Harness provides mechanisms to manage tests on Mojo.</description>
3232

33+
<dependencyManagement>
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.junit</groupId>
37+
<artifactId>junit-bom</artifactId>
38+
<version>5.10.0</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
</dependencies>
43+
</dependencyManagement>
44+
3345
<dependencies>
3446
<!-- maven -->
3547
<dependency>
@@ -70,14 +82,11 @@ under the License.
7082
<version>4.0.0</version>
7183
</dependency>
7284
<dependency>
85+
<!-- correct version must be provided by project depends on Maven version -->
7386
<groupId>org.codehaus.plexus</groupId>
7487
<artifactId>plexus-xml</artifactId>
7588
<version>4.0.0</version>
76-
</dependency>
77-
<dependency>
78-
<groupId>commons-io</groupId>
79-
<artifactId>commons-io</artifactId>
80-
<version>2.11.0</version>
89+
<optional>true</optional>
8190
</dependency>
8291
<dependency>
8392
<groupId>org.codehaus.plexus</groupId>
@@ -89,16 +98,41 @@ under the License.
8998
<groupId>org.codehaus.plexus</groupId>
9099
<artifactId>plexus-testing</artifactId>
91100
<version>1.1.0</version>
101+
<exclusions>
102+
<exclusion>
103+
<!-- we need as optional dependencies -->
104+
<groupId>org.junit.jupiter</groupId>
105+
<artifactId>junit-jupiter-api</artifactId>
106+
</exclusion>
107+
</exclusions>
108+
</dependency>
109+
<dependency>
110+
<!-- newer version is required by plexus-testing -->
111+
<groupId>com.google.guava</groupId>
112+
<artifactId>guava</artifactId>
113+
<version>32.0.1-jre</version>
92114
</dependency>
93115
<dependency>
94116
<groupId>junit</groupId>
95117
<artifactId>junit</artifactId>
96118
<version>4.13.2</version>
119+
<optional>true</optional>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.junit.jupiter</groupId>
123+
<artifactId>junit-jupiter-api</artifactId>
124+
<optional>true</optional>
97125
</dependency>
98126
<dependency>
99127
<groupId>org.mockito</groupId>
100128
<artifactId>mockito-core</artifactId>
101129
<version>4.6.1</version>
102130
</dependency>
131+
<dependency>
132+
<groupId>org.slf4j</groupId>
133+
<artifactId>slf4j-simple</artifactId>
134+
<version>1.7.36</version>
135+
<scope>test</scope>
136+
</dependency>
103137
</dependencies>
104138
</project>

maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@
2929
import java.nio.file.Path;
3030
import java.nio.file.Paths;
3131
import java.util.ArrayList;
32+
import java.util.Arrays;
33+
import java.util.Collection;
3234
import java.util.Collections;
3335
import java.util.HashMap;
36+
import java.util.HashSet;
3437
import java.util.List;
3538
import java.util.Map;
3639
import java.util.Objects;
3740
import java.util.Optional;
41+
import java.util.Set;
3842
import java.util.stream.Collectors;
3943
import java.util.stream.Stream;
4044

@@ -100,7 +104,18 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
100104
InjectMojo injectMojo = parameterContext
101105
.findAnnotation(InjectMojo.class)
102106
.orElseGet(() -> parameterContext.getDeclaringExecutable().getAnnotation(InjectMojo.class));
103-
List<MojoParameter> mojoParameters = parameterContext.findRepeatableAnnotations(MojoParameter.class);
107+
108+
Set<MojoParameter> mojoParameters =
109+
new HashSet<>(parameterContext.findRepeatableAnnotations(MojoParameter.class));
110+
111+
Optional.ofNullable(parameterContext.getDeclaringExecutable().getAnnotation(MojoParameter.class))
112+
.ifPresent(mojoParameters::add);
113+
114+
Optional.ofNullable(parameterContext.getDeclaringExecutable().getAnnotation(MojoParameters.class))
115+
.map(MojoParameters::value)
116+
.map(Arrays::asList)
117+
.ifPresent(mojoParameters::addAll);
118+
104119
Class<?> holder = parameterContext.getTarget().get().getClass();
105120
PluginDescriptor descriptor = extensionContext
106121
.getStore(ExtensionContext.Namespace.GLOBAL)
@@ -113,6 +128,7 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
113128

114129
@Override
115130
public void beforeEach(ExtensionContext context) throws Exception {
131+
// TODO provide protected setters in PlexusExtension
116132
Field field = PlexusExtension.class.getDeclaredField("basedir");
117133
field.setAccessible(true);
118134
field.set(null, getBasedir());
@@ -152,7 +168,10 @@ protected String getPluginDescriptorLocation() {
152168
}
153169

154170
private Mojo lookupMojo(
155-
Class<?> holder, InjectMojo injectMojo, List<MojoParameter> mojoParameters, PluginDescriptor descriptor)
171+
Class<?> holder,
172+
InjectMojo injectMojo,
173+
Collection<MojoParameter> mojoParameters,
174+
PluginDescriptor descriptor)
156175
throws Exception {
157176
String goal = injectMojo.goal();
158177
String pom = injectMojo.pom();

maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoExtension.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@
2929
import java.nio.file.Path;
3030
import java.nio.file.Paths;
3131
import java.util.ArrayList;
32+
import java.util.Arrays;
33+
import java.util.Collection;
3234
import java.util.Collections;
3335
import java.util.HashMap;
36+
import java.util.HashSet;
3437
import java.util.List;
3538
import java.util.Map;
3639
import java.util.Objects;
3740
import java.util.Optional;
41+
import java.util.Properties;
42+
import java.util.Set;
3843
import java.util.stream.Collectors;
3944
import java.util.stream.Stream;
4045

@@ -74,6 +79,7 @@
7479
import org.junit.jupiter.api.extension.ParameterContext;
7580
import org.junit.jupiter.api.extension.ParameterResolutionException;
7681
import org.junit.jupiter.api.extension.ParameterResolver;
82+
import org.mockito.Mockito;
7783
import org.slf4j.LoggerFactory;
7884

7985
/**
@@ -100,7 +106,18 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
100106
InjectMojo injectMojo = parameterContext
101107
.findAnnotation(InjectMojo.class)
102108
.orElseGet(() -> parameterContext.getDeclaringExecutable().getAnnotation(InjectMojo.class));
103-
List<MojoParameter> mojoParameters = parameterContext.findRepeatableAnnotations(MojoParameter.class);
109+
110+
Set<MojoParameter> mojoParameters =
111+
new HashSet<>(parameterContext.findRepeatableAnnotations(MojoParameter.class));
112+
113+
Optional.ofNullable(parameterContext.getDeclaringExecutable().getAnnotation(MojoParameter.class))
114+
.ifPresent(mojoParameters::add);
115+
116+
Optional.ofNullable(parameterContext.getDeclaringExecutable().getAnnotation(MojoParameters.class))
117+
.map(MojoParameters::value)
118+
.map(Arrays::asList)
119+
.ifPresent(mojoParameters::addAll);
120+
104121
Class<?> holder = parameterContext.getTarget().get().getClass();
105122
PluginDescriptor descriptor = extensionContext
106123
.getStore(ExtensionContext.Namespace.GLOBAL)
@@ -113,6 +130,7 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
113130

114131
@Override
115132
public void beforeEach(ExtensionContext context) throws Exception {
133+
// TODO provide protected setters in PlexusExtension
116134
Field field = PlexusExtension.class.getDeclaredField("basedir");
117135
field.setAccessible(true);
118136
field.set(null, getBasedir());
@@ -126,6 +144,8 @@ public void beforeEach(ExtensionContext context) throws Exception {
126144
binder.install(ProviderMethodsModule.forObject(context.getRequiredTestInstance()));
127145
binder.requestInjection(context.getRequiredTestInstance());
128146
binder.bind(Log.class).toInstance(new MojoLogWrapper(LoggerFactory.getLogger("anonymous")));
147+
binder.bind(MavenSession.class).toInstance(mockMavenSession());
148+
binder.bind(MojoExecution.class).toInstance(mockMojoExecution());
129149
});
130150

131151
Map<Object, Object> map = getContainer().getContext().getContextData();
@@ -147,12 +167,36 @@ public void beforeEach(ExtensionContext context) throws Exception {
147167
}
148168
}
149169

170+
/**
171+
* Default MojoExecution mock
172+
*
173+
* @return a MojoExecution mock
174+
*/
175+
private MojoExecution mockMojoExecution() {
176+
return Mockito.mock(MojoExecution.class);
177+
}
178+
179+
/**
180+
* Default MavenSession mock
181+
*
182+
* @return a MavenSession mock
183+
*/
184+
private MavenSession mockMavenSession() {
185+
MavenSession session = Mockito.mock(MavenSession.class);
186+
Mockito.when(session.getUserProperties()).thenReturn(new Properties());
187+
Mockito.when(session.getSystemProperties()).thenReturn(new Properties());
188+
return session;
189+
}
190+
150191
protected String getPluginDescriptorLocation() {
151192
return "META-INF/maven/plugin.xml";
152193
}
153194

154195
private Mojo lookupMojo(
155-
Class<?> holder, InjectMojo injectMojo, List<MojoParameter> mojoParameters, PluginDescriptor descriptor)
196+
Class<?> holder,
197+
InjectMojo injectMojo,
198+
Collection<MojoParameter> mojoParameters,
199+
PluginDescriptor descriptor)
156200
throws Exception {
157201
String goal = injectMojo.goal();
158202
String pom = injectMojo.pom();
@@ -242,6 +286,8 @@ protected Mojo lookupMojo(String[] coord, Xpp3Dom pluginConfiguration, PluginDes
242286
getContainer().getContainerRealm());
243287
}
244288

289+
mojo.setLog(getContainer().lookup(Log.class));
290+
245291
return mojo;
246292
}
247293

maven-plugin-testing-harness/src/test/java/org/apache/maven/api/plugin/testing/ExpressionEvaluatorTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.junit.jupiter.api.Test;
3131

3232
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
33+
import static org.junit.jupiter.api.Assertions.assertEquals;
34+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3335
import static org.mockito.Mockito.doAnswer;
3436
import static org.mockito.Mockito.doReturn;
3537

@@ -59,6 +61,30 @@ public class ExpressionEvaluatorTest {
5961
@Test
6062
@InjectMojo(goal = COORDINATES, pom = CONFIG)
6163
public void testInjection(ExpressionEvaluatorMojo mojo) {
64+
assertNotNull(mojo.basedir);
65+
assertNotNull(mojo.workdir);
66+
assertDoesNotThrow(mojo::execute);
67+
}
68+
69+
@Test
70+
@InjectMojo(goal = COORDINATES, pom = CONFIG)
71+
@MojoParameter(name = "param", value = "paramValue")
72+
public void testParam(ExpressionEvaluatorMojo mojo) {
73+
assertNotNull(mojo.basedir);
74+
assertNotNull(mojo.workdir);
75+
assertEquals("paramValue", mojo.param);
76+
assertDoesNotThrow(mojo::execute);
77+
}
78+
79+
@Test
80+
@InjectMojo(goal = COORDINATES, pom = CONFIG)
81+
@MojoParameter(name = "param", value = "paramValue")
82+
@MojoParameter(name = "param2", value = "param2Value")
83+
public void testParams(ExpressionEvaluatorMojo mojo) {
84+
assertNotNull(mojo.basedir);
85+
assertNotNull(mojo.workdir);
86+
assertEquals("paramValue", mojo.param);
87+
assertEquals("param2Value", mojo.param2);
6288
assertDoesNotThrow(mojo::execute);
6389
}
6490

@@ -68,6 +94,10 @@ public static class ExpressionEvaluatorMojo implements org.apache.maven.api.plug
6894

6995
private String workdir;
7096

97+
private String param;
98+
99+
private String param2;
100+
71101
/** {@inheritDoc} */
72102
@Override
73103
public void execute() throws MojoException {

maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ public class ParametersMojo extends AbstractMojo {
3232
public String withPropertyAndDefault;
3333

3434
@Override
35-
public void execute() throws MojoExecutionException, MojoFailureException {}
35+
public void execute() throws MojoExecutionException, MojoFailureException {
36+
getLog().info("Plain value = " + plain);
37+
}
3638
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.plugin.testing.junit5;
20+
21+
import javax.inject.Inject;
22+
23+
import org.apache.maven.plugin.logging.Log;
24+
import org.apache.maven.plugin.testing.ParametersMojo;
25+
import org.junit.jupiter.api.Test;
26+
27+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
30+
@MojoTest
31+
class Junit5Test {
32+
33+
private static final String POM = "<project>"
34+
+ "<build>"
35+
+ " <plugins>"
36+
+ " <plugin>"
37+
+ " <artifactId>test-plugin</artifactId>"
38+
+ " <configuration>"
39+
+ " </configuration>"
40+
+ " </plugin>"
41+
+ " </plugins>"
42+
+ "</build>" + "</project>";
43+
44+
@Inject
45+
private Log log;
46+
47+
@Test
48+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM)
49+
void simpleMojo(ParametersMojo mojo) {
50+
assertEquals(log, mojo.getLog());
51+
assertDoesNotThrow(mojo::execute);
52+
}
53+
54+
@Test
55+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM)
56+
@MojoParameter(name = "plain", value = "plainValue")
57+
@MojoParameter(name = "withDefault", value = "withDefaultValue")
58+
void simpleMojoWithParameters(ParametersMojo mojo) {
59+
assertEquals("plainValue", mojo.plain);
60+
assertEquals("withDefaultValue", mojo.withDefault);
61+
assertDoesNotThrow(mojo::execute);
62+
}
63+
64+
@Test
65+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM)
66+
@MojoParameter(name = "plain", value = "plainValue")
67+
void simpleMojoWithParameter(ParametersMojo mojo) {
68+
assertEquals("plainValue", mojo.plain);
69+
assertDoesNotThrow(mojo::execute);
70+
}
71+
}

0 commit comments

Comments
 (0)