-
Notifications
You must be signed in to change notification settings - Fork 40
Improve JUnit5 extension, make similar to Maven 4 implementation #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
slawekjaranowski
merged 10 commits into
apache:maven-plugin-testing-3.x
from
aamotharald:contribution/MPLUGINTESTING-93
Oct 14, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e9d9bf7
Allow a plugin defintion without configuration elements
aamotharald cdcde8d
Documented deprecation of the JUnit4 testing approach
aamotharald 6cf125a
Deleted jUnit5 folder and moved the junit5 sample tests over
aamotharald cb9c4e6
relocated JUnit5 classes to have no package name change when migratin…
aamotharald e202df7
backporting from master branch
aamotharald cf1e27e
Fix after rebase
slawekjaranowski b83f900
Fix javadocs
slawekjaranowski 742e0c0
Remove TODO from deprecated classes
slawekjaranowski a33ed8d
Use method from parent for set test context
slawekjaranowski e425607
restore change in .gitignore
slawekjaranowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * 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.plugin.testing; | ||
|
|
||
| import java.lang.annotation.Inherited; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
|
|
||
| /** | ||
| * Mojo parameters container | ||
| */ | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Inherited | ||
| public @interface Basedir { | ||
| String value() default ""; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.maven.plugin.testing.junit5; | ||
| package org.apache.maven.api.plugin.testing; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.File; | ||
|
|
@@ -54,7 +54,6 @@ | |
| import org.apache.maven.plugin.descriptor.PluginDescriptor; | ||
| import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder; | ||
| import org.apache.maven.plugin.logging.Log; | ||
| import org.apache.maven.plugin.testing.ConfigurationException; | ||
| import org.apache.maven.plugin.testing.MojoLogWrapper; | ||
| import org.apache.maven.project.MavenProject; | ||
| import org.codehaus.plexus.DefaultPlexusContainer; | ||
|
|
@@ -77,6 +76,7 @@ | |
| import org.junit.jupiter.api.extension.ParameterContext; | ||
| import org.junit.jupiter.api.extension.ParameterResolutionException; | ||
| import org.junit.jupiter.api.extension.ParameterResolver; | ||
| import org.junit.platform.commons.support.AnnotationSupport; | ||
| import org.mockito.Mockito; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
@@ -131,10 +131,12 @@ public void beforeEach(ExtensionContext context) throws Exception { | |
| // TODO provide protected setters in PlexusExtension | ||
| Field field = PlexusExtension.class.getDeclaredField("basedir"); | ||
| field.setAccessible(true); | ||
| field.set(null, getBasedir()); | ||
| field = PlexusExtension.class.getDeclaredField("context"); | ||
| field.setAccessible(true); | ||
| field.set(this, context); | ||
| String basedir = AnnotationSupport.findAnnotation(context.getElement().get(), Basedir.class) | ||
| .map(Basedir::value) | ||
| .orElse(getBasedir()); | ||
| field.set(null, basedir); | ||
|
Comment on lines
+134
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will be fixed with: we should not override a static field |
||
|
|
||
| setContext(context); | ||
|
|
||
| getContainer().addComponent(getContainer(), PlexusContainer.class.getName()); | ||
|
|
||
|
|
@@ -165,6 +167,13 @@ public void beforeEach(ExtensionContext context) throws Exception { | |
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void afterEach(ExtensionContext context) throws Exception { | ||
| Field field = PlexusExtension.class.getDeclaredField("basedir"); | ||
| field.setAccessible(true); | ||
| field.set(null, null); | ||
| } | ||
|
|
||
| /** | ||
| * Default MojoExecution mock | ||
| * | ||
|
|
@@ -333,9 +342,7 @@ public static Xpp3Dom extractPluginConfiguration(String artifactId, Xpp3Dom pomD | |
| .filter(e -> e.getChild("artifactId").getValue().equals(artifactId)) | ||
| .findFirst() | ||
| .flatMap(buildElement -> child(buildElement, "configuration")) | ||
| .orElseThrow( | ||
| () -> new ConfigurationException("Cannot find a configuration element for a plugin with an " | ||
| + "artifactId of " + artifactId + ".")); | ||
| .orElse(Xpp3DomBuilder.build(new StringReader("<configuration/>"))); | ||
| return pluginConfigurationElement; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...ss/src/main/java/org/apache/maven/api/plugin/testing/ResolverExpressionEvaluatorStub.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * 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.plugin.testing; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| import org.apache.maven.artifact.repository.MavenArtifactRepository; | ||
| import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; | ||
| import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; | ||
| import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; | ||
|
|
||
| /** | ||
| * Stub for {@link ExpressionEvaluator} | ||
| * | ||
| * @author jesse | ||
| */ | ||
| public class ResolverExpressionEvaluatorStub implements ExpressionEvaluator { | ||
| /** {@inheritDoc} */ | ||
| @Override | ||
| public Object evaluate(String expr) throws ExpressionEvaluationException { | ||
|
|
||
| Object value = null; | ||
|
|
||
| if (expr == null) { | ||
| return null; | ||
| } | ||
|
|
||
| String expression = stripTokens(expr); | ||
|
|
||
| if (expression.equals(expr)) { | ||
| int index = expr.indexOf("${"); | ||
| if (index >= 0) { | ||
| int lastIndex = expr.indexOf("}", index); | ||
| if (lastIndex >= 0) { | ||
| String retVal = expr.substring(0, index); | ||
|
|
||
| if (index > 0 && expr.charAt(index - 1) == '$') { | ||
| retVal += expr.substring(index + 1, lastIndex + 1); | ||
| } else { | ||
| retVal += evaluate(expr.substring(index, lastIndex + 1)); | ||
| } | ||
|
|
||
| retVal += evaluate(expr.substring(lastIndex + 1)); | ||
| return retVal; | ||
| } | ||
| } | ||
|
|
||
| // Was not an expression | ||
| if (expression.indexOf("$$") > -1) { | ||
| return expression.replaceAll("\\$\\$", "\\$"); | ||
| } | ||
| } | ||
|
|
||
| if ("basedir".equals(expression) || "project.basedir".equals(expression)) { | ||
| return MojoExtension.getBasedir(); | ||
| } else if (expression.startsWith("basedir") || expression.startsWith("project.basedir")) { | ||
| int pathSeparator = expression.indexOf("/"); | ||
|
|
||
| if (pathSeparator > 0) { | ||
| value = MojoExtension.getBasedir() + expression.substring(pathSeparator); | ||
| } else { | ||
| System.out.println("Got expression '" + expression + "' that was not recognised"); | ||
| } | ||
| return value; | ||
| } else if ("localRepository".equals(expression)) { | ||
| File localRepo = new File(MojoExtension.getBasedir(), "target/local-repo"); | ||
| return new MavenArtifactRepository( | ||
| "localRepository", | ||
| "file://" + localRepo.getAbsolutePath(), | ||
| new DefaultRepositoryLayout(), | ||
| null, | ||
| null); | ||
| } else { | ||
| return expr; | ||
| } | ||
| } | ||
|
|
||
| private String stripTokens(String expr) { | ||
| if (expr.startsWith("${") && expr.indexOf("}") == expr.length() - 1) { | ||
| expr = expr.substring(2, expr.length() - 1); | ||
| } | ||
|
|
||
| return expr; | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override | ||
| public File alignToBaseDirectory(File file) { | ||
| if (file.getAbsolutePath().startsWith(MojoExtension.getBasedir())) { | ||
| return file; | ||
| } else if (file.isAbsolute()) { | ||
| return file; | ||
| } else { | ||
| return new File(MojoExtension.getBasedir(), file.getPath()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.