From 6b0d067b3dae9f2a37e9721bc1814701b479344e Mon Sep 17 00:00:00 2001 From: Harald Aamot Date: Fri, 13 Dec 2024 09:28:27 +0100 Subject: [PATCH 1/5] Allow a plugin defintion without configuration elements --- .../apache/maven/plugin/testing/junit5/MojoExtension.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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/plugin/testing/junit5/MojoExtension.java index 3290ab3..fc6746b 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/plugin/testing/junit5/MojoExtension.java @@ -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; @@ -333,9 +332,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; } From c564d7dd0c2fdb4229cdb38e66c79f9354fcb8e0 Mon Sep 17 00:00:00 2001 From: Harald Aamot Date: Fri, 13 Dec 2024 11:47:55 +0100 Subject: [PATCH 2/5] Documented deprecation of the JUnit4 testing approach --- .../apache/maven/plugin/testing/AbstractMojoTestCase.java | 5 +++++ .../org/apache/maven/plugin/testing/MojoLogWrapper.java | 6 ++++++ .../org/apache/maven/plugin/testing/MojoParameters.java | 5 +++++ .../main/java/org/apache/maven/plugin/testing/MojoRule.java | 5 +++++ .../java/org/apache/maven/plugin/testing/WithoutMojo.java | 5 +++++ .../maven/plugin/testing/resources/TestResources.java | 5 +++++ 6 files changed, 31 insertions(+) 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 8c11a8f..274f429 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 @@ -88,8 +88,13 @@ * TODO: create a standard directory structure for picking up POMs to make this even easier, we really just need a testing * descriptor and make this entirely declarative! * + * @deprected 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.plugin.testing.junit5.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..e543650 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 + * + * @deprected 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..911eb73 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 * + * @deprected 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.plugin.testing.junit5.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..97f1635 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 @@ -52,9 +52,14 @@ * exhibited as {@code public} in the rule. You may annotate single tests methods with * {@link WithoutMojo} to prevent the rule from firing. * + * @deprected As of version 3.4.0, it is advised to work with JUnit5 tests which do not + * use rules but extensions {@link org.apache.maven.plugin.testing.junit5.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/WithoutMojo.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/WithoutMojo.java index 4fa6bbf..7b6b8e7 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 @@ -29,8 +29,13 @@ * * An annotation for test methods that do not require the {@link MojoRule} to create and tear down the instance. * + * @deprected As of version 3.4.0, it is advised to work with JUnit5 tests which do not + * use rules but extensions {@link org.apache.maven.plugin.testing.junit5.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 54343f1..ac4e295 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 @@ -34,8 +34,13 @@ /** * Junit4 test {@link Rule} to extract and assert test resources. * + * @deprected As of version 3.4.0, it is advised to work with JUnit5 tests which do not + * use rules but extensions {@link org.apache.maven.plugin.testing.junit5.MojoExtension} + * instead. + * * @since 3.1.0 */ +@Deprecated public class TestResources extends TestWatcher { private final String projectsDir; From 60451e35f632bd7cc4eaef31fa81488d4aed9c56 Mon Sep 17 00:00:00 2001 From: Harald Aamot Date: Fri, 13 Dec 2024 11:49:23 +0100 Subject: [PATCH 3/5] Deleted jUnit5 folder and moved the junit5 sample tests over --- .../testing/ParametersMojoJUnit4Test.java | 95 ++++++++++++ .../plugin/testing/ParametersMojoTest.java | 136 ++++++++++-------- .../plugin/testing/junit5/Junit5Test.java | 71 --------- 3 files changed, 171 insertions(+), 131 deletions(-) create mode 100644 maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoJUnit4Test.java delete mode 100644 maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/junit5/Junit5Test.java 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..b00f8a7 --- /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.plain); + assertNull(mojo.withProperty); + assertEquals("default", mojo.withDefault); + assertEquals("default", mojo.withPropertyAndDefault); + } + + public void testExplicit() throws Exception { + MavenProject project = readMavenProject(new File("src/test/projects/explicit")); + + ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(project, "parameters"); + + assertEquals("explicitValue", mojo.plain); + assertEquals("explicitWithPropertyValue", mojo.withProperty); + assertEquals("explicitWithDefaultValue", mojo.withDefault); + assertEquals("explicitWithPropertyAndDefaultValue", mojo.withPropertyAndDefault); + } + + 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.plain); + assertEquals("propertyValue", mojo.withProperty); + assertEquals("default", mojo.withDefault); + assertEquals("propertyValue", mojo.withPropertyAndDefault); + } + + 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.plain); + assertEquals("explicitWithPropertyValue", mojo.withProperty); + assertEquals("explicitWithDefaultValue", mojo.withDefault); + assertEquals("explicitWithPropertyAndDefaultValue", mojo.withPropertyAndDefault); + } + + 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 ce7869c..aefb76b 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,94 @@ */ 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.plain); - assertNull(mojo.withProperty); - assertEquals("default", mojo.withDefault); - assertEquals("default", mojo.withPropertyAndDefault); - } +import javax.inject.Inject; - public void testExplicit() throws Exception { - MavenProject project = readMavenProject(new File("src/test/projects/explicit")); +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.plugin.testing.junit5.InjectMojo; +import org.apache.maven.plugin.testing.junit5.MojoParameter; +import org.apache.maven.plugin.testing.junit5.MojoParameters; +import org.apache.maven.plugin.testing.junit5.MojoTest; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; - ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(project, "parameters"); +import static org.junit.jupiter.api.Assertions.*; - assertEquals("explicitValue", mojo.plain); - assertEquals("explicitWithPropertyValue", mojo.withProperty); - assertEquals("explicitWithDefaultValue", mojo.withDefault); - assertEquals("explicitWithPropertyAndDefaultValue", mojo.withPropertyAndDefault); +@MojoTest +public class ParametersMojoTest { + + private static final Logger logger = LoggerFactory.getLogger(ParametersMojoTest.class); + + private static final String DEFAULT_POM = "src/test/projects/default/pom.xml"; + + private static final String EXPLICIT_POM = "src/test/projects/explicit/pom.xml"; + + private static final String PROPERTY_POM = "src/test/projects/property/pom.xml"; + + @Inject + private Log log; + + @Test + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) + void testDefaultPom(ParametersMojo mojo) { + 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 = EXPLICIT_POM) + void testExplicitPom(ParametersMojo mojo) { + assertEquals("explicitValue", mojo.plain); + 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 = PROPERTY_POM) + void testPropertyPom(ParametersMojo mojo) { + assertDoesNotThrow(mojo::execute); + } - assertNull(mojo.plain); - assertEquals("propertyValue", mojo.withProperty); - assertEquals("default", mojo.withDefault); - assertEquals("propertyValue", mojo.withPropertyAndDefault); + @Test + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) + void simpleMojo(ParametersMojo mojo) { + assertEquals(log, mojo.getLog()); + 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) + @MojoParameter(name = "plain", value = "plainValue") + @MojoParameter(name = "withDefault", value = "withDefaultValue") + void simpleMojoWithParameters(ParametersMojo mojo) { + assertEquals("plainValue", mojo.plain); + assertEquals("withDefaultValue", mojo.withDefault); + 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) + @MojoParameters({ + @MojoParameter(name = "plain", value = "plainValue"), + @MojoParameter(name = "withDefault", value = "withDefaultValue") + }) + void simpleMojoWithParametersGroupingAnnotation(ParametersMojo mojo) { + assertEquals("plainValue", mojo.plain); + assertEquals("withDefaultValue", mojo.withDefault); + assertDoesNotThrow(mojo::execute); + } - assertEquals("explicitValue", mojo.plain); - assertEquals("explicitWithPropertyValue", mojo.withProperty); - assertEquals("explicitWithDefaultValue", mojo.withDefault); - assertEquals("explicitWithPropertyAndDefaultValue", mojo.withPropertyAndDefault); + @Test + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) + @MojoParameter(name = "plain", value = "plainValue") + void simpleMojoWithParameter(ParametersMojo mojo) { + assertEquals("plainValue", mojo.plain); + 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 + @MojoParameter(name = "plain", value = "plainValue") + @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM) + void simpleMojoWithParameterInjectionWinsOverConfig(ParametersMojo mojo) { + assertEquals("plainValue", mojo.plain); + 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 9da0036..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.plain); - assertEquals("withDefaultValue", mojo.withDefault); - 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.plain); - assertDoesNotThrow(mojo::execute); - } -} From cb1a5e84c1e4d8b704b8a9e2893a6f3b44d6cd70 Mon Sep 17 00:00:00 2001 From: Harald Aamot Date: Thu, 19 Dec 2024 08:15:33 +0100 Subject: [PATCH 4/5] relocated JUnit5 classes to have no package name change when migrating to Maven 4 --- .../testing/junit5 => api/plugin/testing}/InjectMojo.java | 2 +- .../junit5 => api/plugin/testing}/MojoExtension.java | 2 +- .../junit5 => api/plugin/testing}/MojoParameter.java | 2 +- .../junit5 => api/plugin/testing}/MojoParameters.java | 2 +- .../testing/junit5 => api/plugin/testing}/MojoTest.java | 2 +- .../apache/maven/plugin/testing/AbstractMojoTestCase.java | 3 ++- .../java/org/apache/maven/plugin/testing/MojoRule.java | 3 ++- .../java/org/apache/maven/plugin/testing/WithoutMojo.java | 4 +++- .../maven/plugin/testing/resources/TestResources.java | 3 ++- .../org/apache/maven/plugin/testing/MojoTestCaseTest.java | 2 +- .../apache/maven/plugin/testing/ParametersMojoTest.java | 8 ++++---- 11 files changed, 19 insertions(+), 14 deletions(-) rename maven-plugin-testing-harness/src/main/java/org/apache/maven/{plugin/testing/junit5 => api/plugin/testing}/InjectMojo.java (95%) rename maven-plugin-testing-harness/src/main/java/org/apache/maven/{plugin/testing/junit5 => api/plugin/testing}/MojoExtension.java (99%) rename maven-plugin-testing-harness/src/main/java/org/apache/maven/{plugin/testing/junit5 => api/plugin/testing}/MojoParameter.java (95%) rename maven-plugin-testing-harness/src/main/java/org/apache/maven/{plugin/testing/junit5 => api/plugin/testing}/MojoParameters.java (95%) rename maven-plugin-testing-harness/src/main/java/org/apache/maven/{plugin/testing/junit5 => api/plugin/testing}/MojoTest.java (96%) 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 95% 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..e094d06 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,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.Retention; import java.lang.annotation.RetentionPolicy; 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 99% 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 fc6746b..ff21962 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; 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 95% 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..2a28f48 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,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.Repeatable; import java.lang.annotation.Retention; 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 95% 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..434abe1 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,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.Retention; import java.lang.annotation.RetentionPolicy; 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/plugin/testing/AbstractMojoTestCase.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java index 274f429..5a4b766 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; @@ -89,7 +90,7 @@ * descriptor and make this entirely declarative! * * @deprected 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.plugin.testing.junit5.MojoTest} + * use this class but {@link MojoTest} * instead. * * @author jesse 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 97f1635..bad9da7 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; @@ -53,7 +54,7 @@ * {@link WithoutMojo} to prevent the rule from firing. * * @deprected As of version 3.4.0, it is advised to work with JUnit5 tests which do not - * use rules but extensions {@link org.apache.maven.plugin.testing.junit5.MojoExtension} + * use rules but extensions {@link MojoExtension} * instead. * * @author Mirko Friedenhagen 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 7b6b8e7..a2e24ab 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; @@ -30,7 +32,7 @@ * An annotation for test methods that do not require the {@link MojoRule} to create and tear down the instance. * * @deprected As of version 3.4.0, it is advised to work with JUnit5 tests which do not - * use rules but extensions {@link org.apache.maven.plugin.testing.junit5.MojoExtension} + * use rules but extensions {@link MojoExtension} * instead. * * @author Mirko Friedenhagen 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 ac4e295..1aa0baf 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; @@ -35,7 +36,7 @@ * Junit4 test {@link Rule} to extract and assert test resources. * * @deprected As of version 3.4.0, it is advised to work with JUnit5 tests which do not - * use rules but extensions {@link org.apache.maven.plugin.testing.junit5.MojoExtension} + * use rules but extensions {@link MojoExtension} * instead. * * @since 3.1.0 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/ParametersMojoTest.java b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java index aefb76b..39c0424 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 @@ -20,11 +20,11 @@ import javax.inject.Inject; +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.apache.maven.plugin.testing.junit5.InjectMojo; -import org.apache.maven.plugin.testing.junit5.MojoParameter; -import org.apache.maven.plugin.testing.junit5.MojoParameters; -import org.apache.maven.plugin.testing.junit5.MojoTest; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; From c58431eeca561ef101971a7a1885acd79435f41f Mon Sep 17 00:00:00 2001 From: Harald Aamot Date: Thu, 9 Jan 2025 09:27:10 +0100 Subject: [PATCH 5/5] backporting from master branch --- .../maven/api/plugin/testing/Basedir.java | 32 +++++ .../maven/api/plugin/testing/InjectMojo.java | 6 +- .../api/plugin/testing/MojoExtension.java | 13 +- .../api/plugin/testing/MojoParameter.java | 2 + .../api/plugin/testing/MojoParameters.java | 2 + .../ResolverExpressionEvaluatorStub.java | 113 ++++++++++++++++++ .../plugin/testing/AbstractMojoTestCase.java | 1 + .../maven/plugin/testing/MojoParameters.java | 2 +- .../ResolverExpressionEvaluatorStub.java | 3 + .../plugin/testing/ParametersMojoTest.java | 36 +++--- .../basedir-set-by-annotation/pom.xml | 51 ++++++++ 11 files changed, 242 insertions(+), 19 deletions(-) create mode 100644 maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/Basedir.java create mode 100644 maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/ResolverExpressionEvaluatorStub.java create mode 100644 maven-plugin-testing-harness/src/test/projects/basedir-set-by-annotation/pom.xml 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/api/plugin/testing/InjectMojo.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java index e094d06..272eb9b 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/InjectMojo.java @@ -18,6 +18,7 @@ */ 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/api/plugin/testing/MojoExtension.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java index ff21962..62be824 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java @@ -76,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; @@ -130,7 +131,10 @@ 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()); + String basedir = AnnotationSupport.findAnnotation(context.getElement().get(), Basedir.class) + .map(Basedir::value) + .orElse(getBasedir()); + field.set(null, basedir); field = PlexusExtension.class.getDeclaredField("context"); field.setAccessible(true); field.set(this, context); @@ -164,6 +168,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 * diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java index 2a28f48..8c37804 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameter.java @@ -18,6 +18,7 @@ */ 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/api/plugin/testing/MojoParameters.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java index 434abe1..373c926 100644 --- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java +++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoParameters.java @@ -18,6 +18,7 @@ */ 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/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 809c7b3..5788755 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 @@ -87,6 +87,7 @@ * instead. * */ +@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/MojoParameters.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoParameters.java index 911eb73..8b84f10 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 @@ -24,7 +24,7 @@ * Static helpers to create and manipulate mojo execution configuration parameters * * @deprected 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.plugin.testing.junit5.MojoParameters} + * use this class but {@link org.apache.maven.api.plugin.testing.MojoParameters} * instead. * * @since 3.2.0 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..1c88b84 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 + * @deprected 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/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 39c0424..554d554 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 @@ -20,10 +20,8 @@ import javax.inject.Inject; -import org.apache.maven.api.plugin.testing.InjectMojo; -import org.apache.maven.api.plugin.testing.MojoParameter; +import org.apache.maven.api.plugin.testing.*; 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 org.slf4j.Logger; @@ -36,43 +34,45 @@ public class ParametersMojoTest { private static final Logger logger = LoggerFactory.getLogger(ParametersMojoTest.class); - private static final String DEFAULT_POM = "src/test/projects/default/pom.xml"; + private static final String POM_DOT_XML_FILE = "pom.xml"; - private static final String EXPLICIT_POM = "src/test/projects/explicit/pom.xml"; + private static final String DEFAULT_POM_DIR = "src/test/projects/default/"; - private static final String PROPERTY_POM = "src/test/projects/property/pom.xml"; + private static final String EXPLICIT_POM_DIR = "src/test/projects/explicit/"; + + private static final String PROPERTY_POM_DIR = "src/test/projects/property/"; @Inject private Log log; @Test - @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) + @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) + @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.plain); assertDoesNotThrow(mojo::execute); } @Test - @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = PROPERTY_POM) + @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); } @Test - @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) + @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); } @Test - @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) + @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) { @@ -82,7 +82,7 @@ void simpleMojoWithParameters(ParametersMojo mojo) { } @Test - @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) + @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") @@ -94,7 +94,7 @@ void simpleMojoWithParametersGroupingAnnotation(ParametersMojo mojo) { } @Test - @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM) + @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.plain); @@ -103,9 +103,17 @@ void simpleMojoWithParameter(ParametersMojo mojo) { @Test @MojoParameter(name = "plain", value = "plainValue") - @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM) + @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.plain); assertDoesNotThrow(mojo::execute); } + + @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.plain); + 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 + + + + +