Skip to content

Commit ad6036d

Browse files
dependabot[bot]slawekjaranowski
authored andcommitted
Add more tests for MojoParameter and Basedir
1 parent 302aee7 commit ad6036d

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ public void beforeEach(ExtensionContext context) throws Exception {
185185
basedir = Paths.get(resource.toURI()).toString();
186186
}
187187

188+
// as PluginParameterExpressionEvaluator changes the basedir to absolute path, we need to normalize it here too
189+
basedir = new File(basedir).getAbsolutePath();
190+
188191
setTestBasedir(basedir, context);
189192

190193
PlexusContainer plexusContainer = getContainer(context);

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,23 @@
2020

2121
import javax.inject.Inject;
2222

23+
import java.io.File;
24+
2325
import org.apache.maven.api.plugin.testing.Basedir;
2426
import org.apache.maven.api.plugin.testing.InjectMojo;
27+
import org.apache.maven.api.plugin.testing.MojoExtension;
2528
import org.apache.maven.api.plugin.testing.MojoParameter;
2629
import org.apache.maven.api.plugin.testing.MojoParameters;
2730
import org.apache.maven.api.plugin.testing.MojoTest;
31+
import org.apache.maven.execution.MavenSession;
2832
import org.apache.maven.plugin.logging.Log;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Nested;
2935
import org.junit.jupiter.api.Test;
3036

3137
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3238
import static org.junit.jupiter.api.Assertions.assertEquals;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
3340

3441
@MojoTest
3542
public class ParametersMojoTest {
@@ -48,13 +55,20 @@ public class ParametersMojoTest {
4855
@Test
4956
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
5057
void testDefaultPom(ParametersMojo mojo) {
58+
assertEquals("default", mojo.getWithDefault());
59+
assertEquals("default", mojo.getWithPropertyAndDefault());
60+
5161
assertDoesNotThrow(mojo::execute);
5262
}
5363

5464
@Test
5565
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM_DIR + POM_DOT_XML_FILE)
5666
void testExplicitPom(ParametersMojo mojo) {
5767
assertEquals("explicitValue", mojo.getPlain());
68+
assertEquals("explicitWithPropertyValue", mojo.getWithProperty());
69+
assertEquals("explicitWithDefaultValue", mojo.getWithDefault());
70+
assertEquals("explicitWithPropertyAndDefaultValue", mojo.getWithPropertyAndDefault());
71+
5872
assertDoesNotThrow(mojo::execute);
5973
}
6074

@@ -64,6 +78,30 @@ void testPropertyPom(ParametersMojo mojo) {
6478
assertDoesNotThrow(mojo::execute);
6579
}
6680

81+
@Nested
82+
class TestPropertyPom {
83+
84+
@Inject
85+
private MavenSession mavenSession;
86+
87+
@BeforeEach
88+
void setup() {
89+
mavenSession.getUserProperties().setProperty("property", "testPropertyValue");
90+
}
91+
92+
@Test
93+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = PROPERTY_POM_DIR + POM_DOT_XML_FILE)
94+
@MojoParameter(name = "plain", value = "test-${property}")
95+
void testPropertyPom(ParametersMojo mojo) {
96+
assertEquals("test-testPropertyValue", mojo.getPlain());
97+
assertEquals("testPropertyValue", mojo.getWithProperty());
98+
assertEquals("default", mojo.getWithDefault());
99+
assertEquals("testPropertyValue", mojo.getWithPropertyAndDefault());
100+
101+
assertDoesNotThrow(mojo::execute);
102+
}
103+
}
104+
67105
@Test
68106
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
69107
void simpleMojo(ParametersMojo mojo) {
@@ -117,11 +155,67 @@ void basedirInjectedWithBasedirAnnotation(ParametersMojo mojo) {
117155
assertDoesNotThrow(mojo::execute);
118156
}
119157

158+
@Test
159+
@Basedir("src/test/projects/basedir-set-by-annotation")
160+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM_DOT_XML_FILE)
161+
@MojoParameter(name = "withDefault", value = "${basedir}/test-default-value.txt")
162+
@MojoParameter(name = "withProperty", value = "${project.basedir}/test-default-value.txt")
163+
void basedirInjectedWithBasedirAnnotationAndParams(ParametersMojo mojo) {
164+
assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain());
165+
assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithDefault());
166+
assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithProperty());
167+
assertDoesNotThrow(mojo::execute);
168+
}
169+
120170
@Test
121171
@Basedir("/projects/basedir-set-by-annotation-classpath")
122172
@InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
123173
void basedirInjectedWithBasedirFromClasspathAnnotation(ParametersMojo mojo) {
124174
assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
125175
assertDoesNotThrow(mojo::execute);
126176
}
177+
178+
@Test
179+
@Basedir("/projects/basedir-set-by-annotation-classpath")
180+
@InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
181+
@MojoParameter(name = "withDefault", value = "${basedir}/test-default-value.txt")
182+
@MojoParameter(name = "withProperty", value = "${project.basedir}/test-default-value.txt")
183+
void basedirInjectedWithBasedirFromClasspathAnnotationAndParams(ParametersMojo mojo) {
184+
assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
185+
assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithDefault());
186+
assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithProperty());
187+
assertDoesNotThrow(mojo::execute);
188+
}
189+
190+
@Nested
191+
class BaseDirInBeforeEach {
192+
193+
@BeforeEach
194+
void setup() {
195+
// basedir defined for test should be already visible here
196+
String fs = File.separator;
197+
String endWith1 = fs + "src" + fs + "test" + fs + "projects" + fs + "basedir-set-by-annotation";
198+
String endWith2 = fs + "projects" + fs + "basedir-set-by-annotation-classpath";
199+
200+
assertTrue(
201+
MojoExtension.getBasedir().endsWith(endWith1)
202+
|| MojoExtension.getBasedir().endsWith(endWith2),
203+
"Basedir: " + MojoExtension.getBasedir() + " is not ends with expected value '" + endWith1
204+
+ "' or '" + endWith2 + "'");
205+
}
206+
207+
@Test
208+
@Basedir("src/test/projects/basedir-set-by-annotation")
209+
@InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
210+
void basedirInjectedWithBasedirAnnotation(ParametersMojo mojo) {
211+
assertDoesNotThrow(mojo::execute);
212+
}
213+
214+
@Test
215+
@Basedir("/projects/basedir-set-by-annotation-classpath")
216+
@InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
217+
void basedirInjectedWithBasedirFromClasspathAnnotation(ParametersMojo mojo) {
218+
assertDoesNotThrow(mojo::execute);
219+
}
220+
}
127221
}

0 commit comments

Comments
 (0)