Skip to content

Commit f46bdbe

Browse files
Improve JUnit5 extension, make similar to Maven 4 implementation (#50)
* Allow a plugin defintion without configuration elements * Documented deprecation of the JUnit4 testing approach * Deleted jUnit5 folder and moved the junit5 sample tests over * relocated JUnit5 classes to have no package name change when migrating to Maven 4 * backporting from master branch * Use method from parent for set test context --------- Co-authored-by: Slawomir Jaranowski <s.jaranowski@gmail.com>
1 parent 5f52454 commit f46bdbe

File tree

19 files changed

+441
-147
lines changed

19 files changed

+441
-147
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+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.plugin.testing.junit5;
19+
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
}
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.plugin.testing.junit5;
19+
package org.apache.maven.api.plugin.testing;
2020

2121
import java.io.BufferedReader;
2222
import java.io.File;
@@ -54,7 +54,6 @@
5454
import org.apache.maven.plugin.descriptor.PluginDescriptor;
5555
import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
5656
import org.apache.maven.plugin.logging.Log;
57-
import org.apache.maven.plugin.testing.ConfigurationException;
5857
import org.apache.maven.plugin.testing.MojoLogWrapper;
5958
import org.apache.maven.project.MavenProject;
6059
import org.codehaus.plexus.DefaultPlexusContainer;
@@ -77,6 +76,7 @@
7776
import org.junit.jupiter.api.extension.ParameterContext;
7877
import org.junit.jupiter.api.extension.ParameterResolutionException;
7978
import org.junit.jupiter.api.extension.ParameterResolver;
79+
import org.junit.platform.commons.support.AnnotationSupport;
8080
import org.mockito.Mockito;
8181
import org.slf4j.LoggerFactory;
8282

@@ -131,10 +131,12 @@ public void beforeEach(ExtensionContext context) throws Exception {
131131
// TODO provide protected setters in PlexusExtension
132132
Field field = PlexusExtension.class.getDeclaredField("basedir");
133133
field.setAccessible(true);
134-
field.set(null, getBasedir());
135-
field = PlexusExtension.class.getDeclaredField("context");
136-
field.setAccessible(true);
137-
field.set(this, context);
134+
String basedir = AnnotationSupport.findAnnotation(context.getElement().get(), Basedir.class)
135+
.map(Basedir::value)
136+
.orElse(getBasedir());
137+
field.set(null, basedir);
138+
139+
setContext(context);
138140

139141
getContainer().addComponent(getContainer(), PlexusContainer.class.getName());
140142

@@ -165,6 +167,13 @@ public void beforeEach(ExtensionContext context) throws Exception {
165167
}
166168
}
167169

170+
@Override
171+
public void afterEach(ExtensionContext context) throws Exception {
172+
Field field = PlexusExtension.class.getDeclaredField("basedir");
173+
field.setAccessible(true);
174+
field.set(null, null);
175+
}
176+
168177
/**
169178
* Default MojoExecution mock
170179
*
@@ -333,9 +342,7 @@ public static Xpp3Dom extractPluginConfiguration(String artifactId, Xpp3Dom pomD
333342
.filter(e -> e.getChild("artifactId").getValue().equals(artifactId))
334343
.findFirst()
335344
.flatMap(buildElement -> child(buildElement, "configuration"))
336-
.orElseThrow(
337-
() -> new ConfigurationException("Cannot find a configuration element for a plugin with an "
338-
+ "artifactId of " + artifactId + "."));
345+
.orElse(Xpp3DomBuilder.build(new StringReader("<configuration/>")));
339346
return pluginConfigurationElement;
340347
}
341348

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.plugin.testing.junit5;
19+
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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.plugin.testing.junit5;
19+
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.plugin.testing.junit5;
19+
package org.apache.maven.api.plugin.testing;
2020

2121
import java.lang.annotation.ElementType;
2222
import java.lang.annotation.Retention;
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/AbstractMojoTestCase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Properties;
3737

3838
import com.google.inject.Module;
39+
import org.apache.maven.api.plugin.testing.MojoTest;
3940
import org.apache.maven.artifact.Artifact;
4041
import org.apache.maven.artifact.DefaultArtifact;
4142
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
@@ -79,8 +80,13 @@
7980
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
8081

8182
/**
83+
* @deprecated As of version 3.4.0, it is advised to work with JUnit5 tests which do not
84+
* use this class but {@link MojoTest}
85+
* instead.
86+
*
8287
* @author jesse
8388
*/
89+
@Deprecated
8490
public abstract class AbstractMojoTestCase extends PlexusTestCase {
8591
private static final DefaultArtifactVersion MAVEN_VERSION;
8692

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@
2525

2626
/**
2727
* @author jdcasey
28+
*
29+
* @deprecated As of version 3.4.0, it is advised to work with JUnit5 tests which do not
30+
* use this class but {@link javax.inject.Inject} to inject a Log instance
31+
* instead.
32+
*
2833
*/
34+
@Deprecated
2935
public class MojoLogWrapper implements Log {
3036
private final Logger logger;
3137

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
/**
2424
* Static helpers to create and manipulate mojo execution configuration parameters
2525
*
26+
* @deprecated 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.api.plugin.testing.MojoParameters}
28+
* instead.
29+
*
2630
* @since 3.2.0
2731
*/
32+
@Deprecated
2833
public class MojoParameters {
2934
public static Xpp3Dom newParameter(String name, String value) {
3035
Xpp3Dom child = new Xpp3Dom(name);

0 commit comments

Comments
 (0)