1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.testing;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24
25 import org.apache.maven.api.plugin.testing.Basedir;
26 import org.apache.maven.api.plugin.testing.InjectMojo;
27 import org.apache.maven.api.plugin.testing.MojoExtension;
28 import org.apache.maven.api.plugin.testing.MojoParameter;
29 import org.apache.maven.api.plugin.testing.MojoParameters;
30 import org.apache.maven.api.plugin.testing.MojoTest;
31 import org.apache.maven.execution.MavenSession;
32 import org.apache.maven.plugin.logging.Log;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Nested;
35 import org.junit.jupiter.api.Test;
36
37 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
38 import static org.junit.jupiter.api.Assertions.assertEquals;
39 import static org.junit.jupiter.api.Assertions.assertTrue;
40
41 @MojoTest
42 public class ParametersMojoTest {
43
44 private static final String POM_DOT_XML_FILE = "pom.xml";
45
46 private static final String DEFAULT_POM_DIR = "src/test/projects/default/";
47
48 private static final String EXPLICIT_POM_DIR = "src/test/projects/explicit/";
49
50 private static final String PROPERTY_POM_DIR = "src/test/projects/property/";
51
52 @Inject
53 private Log log;
54
55 @Test
56 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
57 void testDefaultPom(ParametersMojo mojo) {
58 assertEquals("default", mojo.getWithDefault());
59 assertEquals("default", mojo.getWithPropertyAndDefault());
60
61 assertDoesNotThrow(mojo::execute);
62 }
63
64 @Test
65 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM_DIR + POM_DOT_XML_FILE)
66 void testExplicitPom(ParametersMojo mojo) {
67 assertEquals("explicitValue", mojo.getPlain());
68 assertEquals("explicitWithPropertyValue", mojo.getWithProperty());
69 assertEquals("explicitWithDefaultValue", mojo.getWithDefault());
70 assertEquals("explicitWithPropertyAndDefaultValue", mojo.getWithPropertyAndDefault());
71
72 assertDoesNotThrow(mojo::execute);
73 }
74
75 @Test
76 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = PROPERTY_POM_DIR + POM_DOT_XML_FILE)
77 void testPropertyPom(ParametersMojo mojo) {
78 assertDoesNotThrow(mojo::execute);
79 }
80
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
105 @Test
106 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
107 void simpleMojo(ParametersMojo mojo) {
108 assertEquals(log, mojo.getLog());
109 assertDoesNotThrow(mojo::execute);
110 }
111
112 @Test
113 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
114 @MojoParameter(name = "plain", value = "plainValue")
115 @MojoParameter(name = "withDefault", value = "withDefaultValue")
116 void simpleMojoWithParameters(ParametersMojo mojo) {
117 assertEquals("plainValue", mojo.getPlain());
118 assertEquals("withDefaultValue", mojo.getWithDefault());
119 assertDoesNotThrow(mojo::execute);
120 }
121
122 @Test
123 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
124 @MojoParameters({
125 @MojoParameter(name = "plain", value = "plainValue"),
126 @MojoParameter(name = "withDefault", value = "withDefaultValue")
127 })
128 void simpleMojoWithParametersGroupingAnnotation(ParametersMojo mojo) {
129 assertEquals("plainValue", mojo.getPlain());
130 assertEquals("withDefaultValue", mojo.getWithDefault());
131 assertDoesNotThrow(mojo::execute);
132 }
133
134 @Test
135 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = DEFAULT_POM_DIR + POM_DOT_XML_FILE)
136 @MojoParameter(name = "plain", value = "plainValue")
137 void simpleMojoWithParameter(ParametersMojo mojo) {
138 assertEquals("plainValue", mojo.getPlain());
139 assertDoesNotThrow(mojo::execute);
140 }
141
142 @Test
143 @MojoParameter(name = "plain", value = "plainValue")
144 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = EXPLICIT_POM_DIR + POM_DOT_XML_FILE)
145 void simpleMojoWithParameterInjectionWinsOverConfig(ParametersMojo mojo) {
146 assertEquals("plainValue", mojo.getPlain());
147 assertDoesNotThrow(mojo::execute);
148 }
149
150 @Test
151 @Basedir("src/test/projects/basedir-set-by-annotation")
152 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters", pom = POM_DOT_XML_FILE)
153 void basedirInjectedWithBasedirAnnotation(ParametersMojo mojo) {
154 assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain());
155 assertDoesNotThrow(mojo::execute);
156 }
157
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
170 @Test
171 @Basedir("/projects/basedir-set-by-annotation-classpath")
172 @InjectMojo(goal = "parameters", pom = POM_DOT_XML_FILE)
173 void basedirInjectedWithBasedirFromClasspathAnnotation(ParametersMojo mojo) {
174 assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
175 assertDoesNotThrow(mojo::execute);
176 }
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
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 }
221
222 @Test
223 @Basedir("src/test/projects/basedir-set-by-annotation")
224 @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters")
225 void basedirInjectedWithBasedirAnnotationDefaultPom(ParametersMojo mojo) {
226 assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain());
227 assertDoesNotThrow(mojo::execute);
228 }
229
230 @Test
231 @Basedir("/projects/basedir-set-by-annotation-classpath")
232 @InjectMojo(goal = "parameters")
233 void basedirInjectedWithBasedirFromClasspathAnnotationDefaultPom(ParametersMojo mojo) {
234 assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
235 assertDoesNotThrow(mojo::execute);
236 }
237 }