diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java new file mode 100644 index 0000000..69eaba2 --- /dev/null +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java @@ -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 ""; +} diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/InjectMojo.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java similarity index 88% rename from maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/InjectMojo.java rename to maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java index 7b2fbdc..272eb9b 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/InjectMojo.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java @@ -16,8 +16,9 @@ * 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.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -25,11 +26,10 @@ * */ @Retention(RetentionPolicy.RUNTIME) +@Inherited public @interface InjectMojo { String goal(); - String pom(); - - boolean empty() default false; + String pom() default ""; } diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoExtension.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java similarity index 96% rename from maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoExtension.java rename to maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java index 3290ab3..3ea09b8 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoExtension.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java @@ -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); + + 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(""))); return pluginConfigurationElement; } diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoParameter.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java similarity index 92% rename from maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoParameter.java rename to maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java index a3a8294..8c37804 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoParameter.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java @@ -16,8 +16,9 @@ * 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.lang.annotation.Inherited; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -27,6 +28,7 @@ */ @Retention(RetentionPolicy.RUNTIME) @Repeatable(MojoParameters.class) +@Inherited public @interface MojoParameter { String name(); diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoParameters.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java similarity index 91% rename from maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoParameters.java rename to maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java index 33c0a23..373c926 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoParameters.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java @@ -16,8 +16,9 @@ * 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.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -25,6 +26,7 @@ * Mojo parameters container */ @Retention(RetentionPolicy.RUNTIME) +@Inherited public @interface MojoParameters { MojoParameter[] value(); } diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoTest.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoTest.java similarity index 96% rename from maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoTest.java rename to maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoTest.java index 09461bd..eb94c09 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/junit5/MojoTest.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoTest.java @@ -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.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/ResolverExpressionEvaluatorStub.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/ResolverExpressionEvaluatorStub.java new file mode 100644 index 0000000..e95d6f4 --- /dev/null +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/ResolverExpressionEvaluatorStub.java @@ -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()); + } + } +} diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java index a03feb8..2708210 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java @@ -36,6 +36,7 @@ import java.util.Properties; import com.google.inject.Module; +import org.apache.maven.api.plugin.testing.MojoTest; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.handler.DefaultArtifactHandler; @@ -79,8 +80,13 @@ import org.codehaus.plexus.util.xml.Xpp3DomBuilder; /** + * @deprecated As of version 3.4.0, it is advised to work with JUnit5 tests which do not + * use this class but {@link MojoTest} + * instead. + * * @author jesse */ +@Deprecated public abstract class AbstractMojoTestCase extends PlexusTestCase { private static final DefaultArtifactVersion MAVEN_VERSION; diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoLogWrapper.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoLogWrapper.java index 869525c..382bad8 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoLogWrapper.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoLogWrapper.java @@ -25,7 +25,13 @@ /** * @author jdcasey + * + * @deprecated As of version 3.4.0, it is advised to work with JUnit5 tests which do not + * use this class but {@link javax.inject.Inject} to inject a Log instance + * instead. + * */ +@Deprecated public class MojoLogWrapper implements Log { private final Logger logger; diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoParameters.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoParameters.java index 842fba6..aa8f940 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoParameters.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoParameters.java @@ -23,8 +23,13 @@ /** * Static helpers to create and manipulate mojo execution configuration parameters * + * @deprecated As of version 3.4.0, it is advised to work with JUnit5 tests which do not + * use this class but {@link org.apache.maven.api.plugin.testing.MojoParameters} + * instead. + * * @since 3.2.0 */ +@Deprecated public class MojoParameters { public static Xpp3Dom newParameter(String name, String value) { Xpp3Dom child = new Xpp3Dom(name); diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java index bbc882e..da5642a 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java @@ -22,6 +22,7 @@ import java.io.InputStream; import java.util.Map; +import org.apache.maven.api.plugin.testing.MojoExtension; import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenSession; @@ -52,9 +53,14 @@ * exhibited as {@code public} in the rule. You may annotate single tests methods with * {@link WithoutMojo} to prevent the rule from firing. * + * @deprecated As of version 3.4.0, it is advised to work with JUnit5 tests which do not + * use rules but extensions {@link MojoExtension} + * instead. + * * @author Mirko Friedenhagen * @since 2.2 */ +@Deprecated public class MojoRule implements TestRule { private final AbstractMojoTestCase testCase; diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/ResolverExpressionEvaluatorStub.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/ResolverExpressionEvaluatorStub.java index f4af2e7..5001b75 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/ResolverExpressionEvaluatorStub.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/ResolverExpressionEvaluatorStub.java @@ -30,7 +30,10 @@ * Stub for {@link ExpressionEvaluator} * * @author jesse + * @deprecated This stub is for deprecated JUnit 4 style tests. + * For JUnit Jupiter tests you have to use {@link org.apache.maven.api.plugin.testing.ResolverExpressionEvaluatorStub}. */ +@Deprecated public class ResolverExpressionEvaluatorStub implements ExpressionEvaluator { /** {@inheritDoc} */ @Override diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/WithoutMojo.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/WithoutMojo.java index 4fa6bbf..222d92d 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/WithoutMojo.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/WithoutMojo.java @@ -22,6 +22,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; +import org.apache.maven.api.plugin.testing.MojoExtension; + import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; @@ -29,8 +31,13 @@ * * An annotation for test methods that do not require the {@link MojoRule} to create and tear down the instance. * + * @deprecated As of version 3.4.0, it is advised to work with JUnit5 tests which do not + * use rules but extensions {@link MojoExtension} + * instead. + * * @author Mirko Friedenhagen */ +@Deprecated @Retention(RUNTIME) @Documented @Target(METHOD) diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/resources/TestResources.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/resources/TestResources.java index 1541506..d6b8673 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/resources/TestResources.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/resources/TestResources.java @@ -24,6 +24,7 @@ import java.util.Set; import java.util.TreeSet; +import org.apache.maven.api.plugin.testing.MojoExtension; import org.codehaus.plexus.util.DirectoryScanner; import org.codehaus.plexus.util.FileUtils; import org.junit.Assert; @@ -34,8 +35,13 @@ /** * Junit4 test {@link Rule} to extract and assert test resources. * + * @deprecated As of version 3.4.0, it is advised to work with JUnit5 tests which do not + * use rules but extensions {@link MojoExtension} + * instead. + * * @since 3.1.0 */ +@Deprecated public class TestResources extends TestWatcher { private final String projectsDir; diff --git a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/MojoTestCaseTest.java b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/MojoTestCaseTest.java index e1ab92d..1fa8947 100644 --- a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/MojoTestCaseTest.java +++ b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/MojoTestCaseTest.java @@ -21,7 +21,7 @@ import java.io.StringReader; import java.util.Map; -import org.apache.maven.plugin.testing.junit5.MojoTest; +import org.apache.maven.api.plugin.testing.MojoTest; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3DomBuilder; diff --git a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoJUnit4Test.java b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoJUnit4Test.java new file mode 100644 index 0000000..7235468 --- /dev/null +++ b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoJUnit4Test.java @@ -0,0 +1,95 @@ +/* + * 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.io.File; + +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuilder; +import org.apache.maven.project.ProjectBuildingException; +import org.apache.maven.project.ProjectBuildingRequest; +import org.eclipse.aether.DefaultRepositorySystemSession; + +public class ParametersMojoJUnit4Test extends AbstractMojoTestCase { + public void testDefault() throws Exception { + MavenProject project = readMavenProject(new File("src/test/projects/default")); + + ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(project, "parameters"); + + assertNull(mojo.getPlain()); + assertNull(mojo.getWithProperty()); + assertEquals("default", mojo.getWithDefault()); + assertEquals("default", mojo.getWithPropertyAndDefault()); + } + + public void testExplicit() throws Exception { + MavenProject project = readMavenProject(new File("src/test/projects/explicit")); + + ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(project, "parameters"); + + assertEquals("explicitValue", mojo.getPlain()); + assertEquals("explicitWithPropertyValue", mojo.getWithProperty()); + assertEquals("explicitWithDefaultValue", mojo.getWithDefault()); + assertEquals("explicitWithPropertyAndDefaultValue", mojo.getWithPropertyAndDefault()); + } + + public void testDefaultWithProperty() throws Exception { + MavenProject project = readMavenProject(new File("src/test/projects/default")); + MavenSession session = newMavenSession(project); + MojoExecution execution = newMojoExecution("parameters"); + + session.getUserProperties().put("property", "propertyValue"); + ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(session, execution); + + assertNull(mojo.getPlain()); + assertEquals("propertyValue", mojo.getWithProperty()); + assertEquals("default", mojo.getWithDefault()); + assertEquals("propertyValue", mojo.getWithPropertyAndDefault()); + } + + public void testExplicitWithProperty() throws Exception { + MavenProject project = readMavenProject(new File("src/test/projects/explicit")); + MavenSession session = newMavenSession(project); + MojoExecution execution = newMojoExecution("parameters"); + + session.getUserProperties().put("property", "propertyValue"); + ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(session, execution); + + assertEquals("explicitValue", mojo.getPlain()); + assertEquals("explicitWithPropertyValue", mojo.getWithProperty()); + assertEquals("explicitWithDefaultValue", mojo.getWithDefault()); + assertEquals("explicitWithPropertyAndDefaultValue", mojo.getWithPropertyAndDefault()); + } + + protected MavenProject readMavenProject(File basedir) throws ProjectBuildingException, Exception { + File pom = new File(basedir, "pom.xml"); + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + request.setBaseDirectory(basedir); + ProjectBuildingRequest configuration = request.getProjectBuildingRequest(); + configuration.setRepositorySession(new DefaultRepositorySystemSession()); + MavenProject project = + lookup(ProjectBuilder.class).build(pom, configuration).getProject(); + assertNotNull(project); + return project; + } +} diff --git a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java index 90cb4df..4b6baed 100644 --- a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java +++ b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java @@ -18,78 +18,102 @@ */ package org.apache.maven.plugin.testing; -import java.io.File; - -import org.apache.maven.execution.DefaultMavenExecutionRequest; -import org.apache.maven.execution.MavenExecutionRequest; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.MojoExecution; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuilder; -import org.apache.maven.project.ProjectBuildingException; -import org.apache.maven.project.ProjectBuildingRequest; -import org.eclipse.aether.DefaultRepositorySystemSession; - -public class ParametersMojoTest extends AbstractMojoTestCase { - public void testDefault() throws Exception { - MavenProject project = readMavenProject(new File("src/test/projects/default")); - - ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(project, "parameters"); - - assertNull(mojo.getPlain()); - assertNull(mojo.getWithProperty()); - assertEquals("default", mojo.getWithDefault()); - assertEquals("default", mojo.getWithPropertyAndDefault()); - } +import javax.inject.Inject; + +import org.apache.maven.api.plugin.testing.Basedir; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoParameter; +import org.apache.maven.api.plugin.testing.MojoParameters; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.plugin.logging.Log; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@MojoTest +public class ParametersMojoTest { + + private static final String POM_DOT_XML_FILE = "pom.xml"; + + private static final String DEFAULT_POM_DIR = "src/test/projects/default/"; + + private static final String EXPLICIT_POM_DIR = "src/test/projects/explicit/"; - public void testExplicit() throws Exception { - MavenProject project = readMavenProject(new File("src/test/projects/explicit")); + private static final String PROPERTY_POM_DIR = "src/test/projects/property/"; - ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(project, "parameters"); + @Inject + private Log log; + @Test + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE) + void testDefaultPom(ParametersMojo mojo) { + assertDoesNotThrow(mojo::execute); + } + + @Test + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM_DIR + POM_DOT_XML_FILE) + void testExplicitPom(ParametersMojo mojo) { assertEquals("explicitValue", mojo.getPlain()); - assertEquals("explicitWithPropertyValue", mojo.getWithProperty()); - assertEquals("explicitWithDefaultValue", mojo.getWithDefault()); - assertEquals("explicitWithPropertyAndDefaultValue", mojo.getWithPropertyAndDefault()); + assertDoesNotThrow(mojo::execute); } - public void testDefaultWithProperty() throws Exception { - MavenProject project = readMavenProject(new File("src/test/projects/default")); - MavenSession session = newMavenSession(project); - MojoExecution execution = newMojoExecution("parameters"); + @Test + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = PROPERTY_POM_DIR + POM_DOT_XML_FILE) + void testPropertyPom(ParametersMojo mojo) { + assertDoesNotThrow(mojo::execute); + } - session.getUserProperties().put("property", "propertyValue"); - ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(session, execution); + @Test + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE) + void simpleMojo(ParametersMojo mojo) { + assertEquals(log, mojo.getLog()); + assertDoesNotThrow(mojo::execute); + } - assertNull(mojo.getPlain()); - assertEquals("propertyValue", mojo.getWithProperty()); - assertEquals("default", mojo.getWithDefault()); - assertEquals("propertyValue", mojo.getWithPropertyAndDefault()); + @Test + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE) + @MojoParameter(name = "plain", value = "plainValue") + @MojoParameter(name = "withDefault", value = "withDefaultValue") + void simpleMojoWithParameters(ParametersMojo mojo) { + assertEquals("plainValue", mojo.getPlain()); + assertEquals("withDefaultValue", mojo.getWithDefault()); + assertDoesNotThrow(mojo::execute); } - public void testExplicitWithProperty() throws Exception { - MavenProject project = readMavenProject(new File("src/test/projects/explicit")); - MavenSession session = newMavenSession(project); - MojoExecution execution = newMojoExecution("parameters"); + @Test + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE) + @MojoParameters({ + @MojoParameter(name = "plain", value = "plainValue"), + @MojoParameter(name = "withDefault", value = "withDefaultValue") + }) + void simpleMojoWithParametersGroupingAnnotation(ParametersMojo mojo) { + assertEquals("plainValue", mojo.getPlain()); + assertEquals("withDefaultValue", mojo.getWithDefault()); + assertDoesNotThrow(mojo::execute); + } - session.getUserProperties().put("property", "propertyValue"); - ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(session, execution); + @Test + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE) + @MojoParameter(name = "plain", value = "plainValue") + void simpleMojoWithParameter(ParametersMojo mojo) { + assertEquals("plainValue", mojo.getPlain()); + assertDoesNotThrow(mojo::execute); + } - assertEquals("explicitValue", mojo.getPlain()); - assertEquals("explicitWithPropertyValue", mojo.getWithProperty()); - assertEquals("explicitWithDefaultValue", mojo.getWithDefault()); - assertEquals("explicitWithPropertyAndDefaultValue", mojo.getWithPropertyAndDefault()); + @Test + @MojoParameter(name = "plain", value = "plainValue") + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM_DIR + POM_DOT_XML_FILE) + void simpleMojoWithParameterInjectionWinsOverConfig(ParametersMojo mojo) { + assertEquals("plainValue", mojo.getPlain()); + assertDoesNotThrow(mojo::execute); } - protected MavenProject readMavenProject(File basedir) throws ProjectBuildingException, Exception { - File pom = new File(basedir, "pom.xml"); - MavenExecutionRequest request = new DefaultMavenExecutionRequest(); - request.setBaseDirectory(basedir); - ProjectBuildingRequest configuration = request.getProjectBuildingRequest(); - configuration.setRepositorySession(new DefaultRepositorySystemSession()); - MavenProject project = - lookup(ProjectBuilder.class).build(pom, configuration).getProject(); - assertNotNull(project); - return project; + @Test + @Basedir("src/test/projects/basedir-set-by-annotation") + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM_DOT_XML_FILE) + void basedirInjectedWithBasedirAnnotation(ParametersMojo mojo) { + assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain()); + assertDoesNotThrow(mojo::execute); } } diff --git a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/junit5/Junit5Test.java b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/junit5/Junit5Test.java deleted file mode 100644 index caab8f4..0000000 --- a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/junit5/Junit5Test.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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.junit5; - -import javax.inject.Inject; - -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugin.testing.ParametersMojo; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; - -@MojoTest -class Junit5Test { - - private static final String POM = "" - + "" - + " " - + " " - + " test-plugin" - + " " - + " " - + " " - + " " - + "" + ""; - - @Inject - private Log log; - - @Test - @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM) - void simpleMojo(ParametersMojo mojo) { - assertEquals(log, mojo.getLog()); - assertDoesNotThrow(mojo::execute); - } - - @Test - @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM) - @MojoParameter(name = "plain", value = "plainValue") - @MojoParameter(name = "withDefault", value = "withDefaultValue") - void simpleMojoWithParameters(ParametersMojo mojo) { - assertEquals("plainValue", mojo.getPlain()); - assertEquals("withDefaultValue", mojo.getWithDefault()); - assertDoesNotThrow(mojo::execute); - } - - @Test - @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM) - @MojoParameter(name = "plain", value = "plainValue") - void simpleMojoWithParameter(ParametersMojo mojo) { - assertEquals("plainValue", mojo.getPlain()); - assertDoesNotThrow(mojo::execute); - } -} diff --git a/maven-plugin-testing-harness/src/test/projects/basedir-set-by-annotation/pom.xml b/maven-plugin-testing-harness/src/test/projects/basedir-set-by-annotation/pom.xml new file mode 100644 index 0000000..82cb188 --- /dev/null +++ b/maven-plugin-testing-harness/src/test/projects/basedir-set-by-annotation/pom.xml @@ -0,0 +1,51 @@ + + + + + + 4.0.0 + + test + test-test + 1.0-SNAPSHOT + jar + + + + + test + test-plugin + 0.0.1-SNAPSHOT + + + test + + test + + compile + + + + i-have-a-basedir-set-by-annotation + + + + +