Skip to content

Commit 4b03b2c

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

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222

2323
import org.apache.maven.api.plugin.testing.Basedir;
2424
import org.apache.maven.api.plugin.testing.InjectMojo;
25+
import org.apache.maven.api.plugin.testing.MojoExtension;
2526
import org.apache.maven.api.plugin.testing.MojoParameter;
2627
import org.apache.maven.api.plugin.testing.MojoParameters;
2728
import org.apache.maven.api.plugin.testing.MojoTest;
29+
import org.apache.maven.execution.MavenSession;
2830
import org.apache.maven.plugin.logging.Log;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Nested;
2933
import org.junit.jupiter.api.Test;
3034

3135
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3236
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.assertTrue;
3338

3439
@MojoTest
3540
public class ParametersMojoTest {
@@ -48,13 +53,20 @@ public class ParametersMojoTest {
4853
@Test
4954
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
5055
void testDefaultPom(ParametersMojo mojo) {
56+
assertEquals("default", mojo.getWithDefault());
57+
assertEquals("default", mojo.getWithPropertyAndDefault());
58+
5159
assertDoesNotThrow(mojo::execute);
5260
}
5361

5462
@Test
5563
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM_DIR + POM_DOT_XML_FILE)
5664
void testExplicitPom(ParametersMojo mojo) {
5765
assertEquals("explicitValue", mojo.getPlain());
66+
assertEquals("explicitWithPropertyValue", mojo.getWithProperty());
67+
assertEquals("explicitWithDefaultValue", mojo.getWithDefault());
68+
assertEquals("explicitWithPropertyAndDefaultValue", mojo.getWithPropertyAndDefault());
69+
5870
assertDoesNotThrow(mojo::execute);
5971
}
6072

@@ -64,6 +76,30 @@ void testPropertyPom(ParametersMojo mojo) {
6476
assertDoesNotThrow(mojo::execute);
6577
}
6678

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

156+
@Test
157+
@Basedir("src/test/projects/basedir-set-by-annotation")
158+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM_DOT_XML_FILE)
159+
@MojoParameter(name = "withDefault", value = "${basedir}/test-default-value.txt")
160+
@MojoParameter(name = "withProperty", value = "${project.basedir}/test-default-value.txt")
161+
void basedirInjectedWithBasedirAnnotationAndParams(ParametersMojo mojo) {
162+
assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain());
163+
assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithDefault());
164+
assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithProperty());
165+
assertDoesNotThrow(mojo::execute);
166+
}
167+
120168
@Test
121169
@Basedir("/projects/basedir-set-by-annotation-classpath")
122170
@InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
123171
void basedirInjectedWithBasedirFromClasspathAnnotation(ParametersMojo mojo) {
124172
assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
125173
assertDoesNotThrow(mojo::execute);
126174
}
175+
176+
@Test
177+
@Basedir("/projects/basedir-set-by-annotation-classpath")
178+
@InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
179+
@MojoParameter(name = "withDefault", value = "${basedir}/test-default-value.txt")
180+
@MojoParameter(name = "withProperty", value = "${project.basedir}/test-default-value.txt")
181+
void basedirInjectedWithBasedirFromClasspathAnnotationAndParams(ParametersMojo mojo) {
182+
assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
183+
assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithDefault());
184+
assertEquals(MojoExtension.getBasedir() + "/test-default-value.txt", mojo.getWithProperty());
185+
assertDoesNotThrow(mojo::execute);
186+
}
187+
188+
@Nested
189+
class BaseDirInBeforeEach {
190+
191+
@BeforeEach
192+
void setup() {
193+
// basedir defined for test should be already visible here
194+
assertTrue(
195+
MojoExtension.getBasedir().endsWith("src/test/projects/basedir-set-by-annotation")
196+
|| MojoExtension.getBasedir().endsWith("/projects/basedir-set-by-annotation-classpath"),
197+
"Basedir: " + MojoExtension.getBasedir() + " is not ends with expected value");
198+
}
199+
200+
@Test
201+
@Basedir("src/test/projects/basedir-set-by-annotation")
202+
@InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
203+
void basedirInjectedWithBasedirAnnotation(ParametersMojo mojo) {
204+
assertDoesNotThrow(mojo::execute);
205+
}
206+
207+
@Test
208+
@Basedir("/projects/basedir-set-by-annotation-classpath")
209+
@InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
210+
void basedirInjectedWithBasedirFromClasspathAnnotation(ParametersMojo mojo) {
211+
assertDoesNotThrow(mojo::execute);
212+
}
213+
}
127214
}

0 commit comments

Comments
 (0)