Skip to content

Commit 05aad2c

Browse files
aamotharaldslawekjaranowski
authored andcommitted
backporting from master branch
1 parent ee4b95e commit 05aad2c

File tree

10 files changed

+241
-19
lines changed

10 files changed

+241
-19
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.api.plugin.testing;
20+
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
25+
/**
26+
* Mojo parameters container
27+
*/
28+
@Retention(RetentionPolicy.RUNTIME)
29+
@Inherited
30+
public @interface Basedir {
31+
String value() default "";
32+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
*/
1919
package org.apache.maven.api.plugin.testing;
2020

21+
import java.lang.annotation.Inherited;
2122
import java.lang.annotation.Retention;
2223
import java.lang.annotation.RetentionPolicy;
2324

2425
/**
2526
*
2627
*/
2728
@Retention(RetentionPolicy.RUNTIME)
29+
@Inherited
2830
public @interface InjectMojo {
2931

3032
String goal();
3133

32-
String pom();
33-
34-
boolean empty() default false;
34+
String pom() default "";
3535
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.junit.jupiter.api.extension.ParameterContext;
7777
import org.junit.jupiter.api.extension.ParameterResolutionException;
7878
import org.junit.jupiter.api.extension.ParameterResolver;
79+
import org.junit.platform.commons.support.AnnotationSupport;
7980
import org.mockito.Mockito;
8081
import org.slf4j.LoggerFactory;
8182

@@ -130,7 +131,10 @@ public void beforeEach(ExtensionContext context) throws Exception {
130131
// TODO provide protected setters in PlexusExtension
131132
Field field = PlexusExtension.class.getDeclaredField("basedir");
132133
field.setAccessible(true);
133-
field.set(null, getBasedir());
134+
String basedir = AnnotationSupport.findAnnotation(context.getElement().get(), Basedir.class)
135+
.map(Basedir::value)
136+
.orElse(getBasedir());
137+
field.set(null, basedir);
134138
field = PlexusExtension.class.getDeclaredField("context");
135139
field.setAccessible(true);
136140
field.set(this, context);
@@ -164,6 +168,13 @@ public void beforeEach(ExtensionContext context) throws Exception {
164168
}
165169
}
166170

171+
@Override
172+
public void afterEach(ExtensionContext context) throws Exception {
173+
Field field = PlexusExtension.class.getDeclaredField("basedir");
174+
field.setAccessible(true);
175+
field.set(null, null);
176+
}
177+
167178
/**
168179
* Default MojoExecution mock
169180
*

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.maven.api.plugin.testing;
2020

21+
import java.lang.annotation.Inherited;
2122
import java.lang.annotation.Repeatable;
2223
import java.lang.annotation.Retention;
2324
import java.lang.annotation.RetentionPolicy;
@@ -27,6 +28,7 @@
2728
*/
2829
@Retention(RetentionPolicy.RUNTIME)
2930
@Repeatable(MojoParameters.class)
31+
@Inherited
3032
public @interface MojoParameter {
3133
String name();
3234

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
*/
1919
package org.apache.maven.api.plugin.testing;
2020

21+
import java.lang.annotation.Inherited;
2122
import java.lang.annotation.Retention;
2223
import java.lang.annotation.RetentionPolicy;
2324

2425
/**
2526
* Mojo parameters container
2627
*/
2728
@Retention(RetentionPolicy.RUNTIME)
29+
@Inherited
2830
public @interface MojoParameters {
2931
MojoParameter[] value();
3032
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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.api.plugin.testing;
20+
21+
import java.io.File;
22+
23+
import org.apache.maven.artifact.repository.MavenArtifactRepository;
24+
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
25+
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
26+
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
27+
28+
/**
29+
* Stub for {@link ExpressionEvaluator}
30+
*
31+
* @author jesse
32+
*/
33+
public class ResolverExpressionEvaluatorStub implements ExpressionEvaluator {
34+
/** {@inheritDoc} */
35+
@Override
36+
public Object evaluate(String expr) throws ExpressionEvaluationException {
37+
38+
Object value = null;
39+
40+
if (expr == null) {
41+
return null;
42+
}
43+
44+
String expression = stripTokens(expr);
45+
46+
if (expression.equals(expr)) {
47+
int index = expr.indexOf("${");
48+
if (index >= 0) {
49+
int lastIndex = expr.indexOf("}", index);
50+
if (lastIndex >= 0) {
51+
String retVal = expr.substring(0, index);
52+
53+
if (index > 0 && expr.charAt(index - 1) == '$') {
54+
retVal += expr.substring(index + 1, lastIndex + 1);
55+
} else {
56+
retVal += evaluate(expr.substring(index, lastIndex + 1));
57+
}
58+
59+
retVal += evaluate(expr.substring(lastIndex + 1));
60+
return retVal;
61+
}
62+
}
63+
64+
// Was not an expression
65+
if (expression.indexOf("$$") > -1) {
66+
return expression.replaceAll("\\$\\$", "\\$");
67+
}
68+
}
69+
70+
if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
71+
return MojoExtension.getBasedir();
72+
} else if (expression.startsWith("basedir") || expression.startsWith("project.basedir")) {
73+
int pathSeparator = expression.indexOf("/");
74+
75+
if (pathSeparator > 0) {
76+
value = MojoExtension.getBasedir() + expression.substring(pathSeparator);
77+
} else {
78+
System.out.println("Got expression '" + expression + "' that was not recognised");
79+
}
80+
return value;
81+
} else if ("localRepository".equals(expression)) {
82+
File localRepo = new File(MojoExtension.getBasedir(), "target/local-repo");
83+
return new MavenArtifactRepository(
84+
"localRepository",
85+
"file://" + localRepo.getAbsolutePath(),
86+
new DefaultRepositoryLayout(),
87+
null,
88+
null);
89+
} else {
90+
return expr;
91+
}
92+
}
93+
94+
private String stripTokens(String expr) {
95+
if (expr.startsWith("${") && expr.indexOf("}") == expr.length() - 1) {
96+
expr = expr.substring(2, expr.length() - 1);
97+
}
98+
99+
return expr;
100+
}
101+
102+
/** {@inheritDoc} */
103+
@Override
104+
public File alignToBaseDirectory(File file) {
105+
if (file.getAbsolutePath().startsWith(MojoExtension.getBasedir())) {
106+
return file;
107+
} else if (file.isAbsolute()) {
108+
return file;
109+
} else {
110+
return new File(MojoExtension.getBasedir(), file.getPath());
111+
}
112+
}
113+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Static helpers to create and manipulate mojo execution configuration parameters
2525
*
2626
* @deprected As of version 3.4.0, it is advised to work with JUnit5 tests which do not
27-
* use this class but {@link org.apache.maven.plugin.testing.junit5.MojoParameters}
27+
* use this class but {@link org.apache.maven.api.plugin.testing.MojoParameters}
2828
* instead.
2929
*
3030
* @since 3.2.0

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
* Stub for {@link ExpressionEvaluator}
3131
*
3232
* @author jesse
33+
* @deprected This stub is for deprecated JUnit 4 style tests.
34+
* For JUnit Jupiter tests you have to use {@link org.apache.maven.api.plugin.testing.ResolverExpressionEvaluatorStub}.
3335
*/
36+
@Deprecated
3437
public class ResolverExpressionEvaluatorStub implements ExpressionEvaluator {
3538
/** {@inheritDoc} */
3639
@Override

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020

2121
import javax.inject.Inject;
2222

23-
import org.apache.maven.api.plugin.testing.InjectMojo;
24-
import org.apache.maven.api.plugin.testing.MojoParameter;
23+
import org.apache.maven.api.plugin.testing.*;
2524
import org.apache.maven.api.plugin.testing.MojoParameters;
26-
import org.apache.maven.api.plugin.testing.MojoTest;
2725
import org.apache.maven.plugin.logging.Log;
2826
import org.junit.jupiter.api.Test;
2927
import org.slf4j.Logger;
@@ -36,43 +34,45 @@ public class ParametersMojoTest {
3634

3735
private static final Logger logger = LoggerFactory.getLogger(ParametersMojoTest.class);
3836

39-
private static final String DEFAULT_POM = "src/test/projects/default/pom.xml";
37+
private static final String POM_DOT_XML_FILE = "pom.xml";
4038

41-
private static final String EXPLICIT_POM = "src/test/projects/explicit/pom.xml";
39+
private static final String DEFAULT_POM_DIR = "src/test/projects/default/";
4240

43-
private static final String PROPERTY_POM = "src/test/projects/property/pom.xml";
41+
private static final String EXPLICIT_POM_DIR = "src/test/projects/explicit/";
42+
43+
private static final String PROPERTY_POM_DIR = "src/test/projects/property/";
4444

4545
@Inject
4646
private Log log;
4747

4848
@Test
49-
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM)
49+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
5050
void testDefaultPom(ParametersMojo mojo) {
5151
assertDoesNotThrow(mojo::execute);
5252
}
5353

5454
@Test
55-
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM)
55+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM_DIR + POM_DOT_XML_FILE)
5656
void testExplicitPom(ParametersMojo mojo) {
5757
assertEquals("explicitValue", mojo.plain);
5858
assertDoesNotThrow(mojo::execute);
5959
}
6060

6161
@Test
62-
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = PROPERTY_POM)
62+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = PROPERTY_POM_DIR + POM_DOT_XML_FILE)
6363
void testPropertyPom(ParametersMojo mojo) {
6464
assertDoesNotThrow(mojo::execute);
6565
}
6666

6767
@Test
68-
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM)
68+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
6969
void simpleMojo(ParametersMojo mojo) {
7070
assertEquals(log, mojo.getLog());
7171
assertDoesNotThrow(mojo::execute);
7272
}
7373

7474
@Test
75-
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM)
75+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
7676
@MojoParameter(name = "plain", value = "plainValue")
7777
@MojoParameter(name = "withDefault", value = "withDefaultValue")
7878
void simpleMojoWithParameters(ParametersMojo mojo) {
@@ -82,7 +82,7 @@ void simpleMojoWithParameters(ParametersMojo mojo) {
8282
}
8383

8484
@Test
85-
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM)
85+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
8686
@MojoParameters({
8787
@MojoParameter(name = "plain", value = "plainValue"),
8888
@MojoParameter(name = "withDefault", value = "withDefaultValue")
@@ -94,7 +94,7 @@ void simpleMojoWithParametersGroupingAnnotation(ParametersMojo mojo) {
9494
}
9595

9696
@Test
97-
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM)
97+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
9898
@MojoParameter(name = "plain", value = "plainValue")
9999
void simpleMojoWithParameter(ParametersMojo mojo) {
100100
assertEquals("plainValue", mojo.plain);
@@ -103,9 +103,17 @@ void simpleMojoWithParameter(ParametersMojo mojo) {
103103

104104
@Test
105105
@MojoParameter(name = "plain", value = "plainValue")
106-
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM)
106+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM_DIR + POM_DOT_XML_FILE)
107107
void simpleMojoWithParameterInjectionWinsOverConfig(ParametersMojo mojo) {
108108
assertEquals("plainValue", mojo.plain);
109109
assertDoesNotThrow(mojo::execute);
110110
}
111+
112+
@Test
113+
@Basedir("src/test/projects/basedir-set-by-annotation")
114+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM_DOT_XML_FILE)
115+
void basedirInjectedWithBasedirAnnotation(ParametersMojo mojo) {
116+
assertEquals("i-have-a-basedir-set-by-annotation", mojo.plain);
117+
assertDoesNotThrow(mojo::execute);
118+
}
111119
}

0 commit comments

Comments
 (0)