1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.interpolation;
20
21 import java.io.File;
22 import java.text.SimpleDateFormat;
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Properties;
28 import java.util.TimeZone;
29
30 import junit.framework.TestCase;
31 import org.apache.maven.model.Build;
32 import org.apache.maven.model.Dependency;
33 import org.apache.maven.model.Model;
34 import org.apache.maven.model.Organization;
35 import org.apache.maven.model.Repository;
36 import org.apache.maven.model.Resource;
37 import org.apache.maven.model.Scm;
38 import org.apache.maven.model.building.DefaultModelBuildingRequest;
39 import org.apache.maven.model.building.ModelBuildingRequest;
40 import org.apache.maven.model.building.SimpleProblemCollector;
41 import org.apache.maven.model.path.PathTranslator;
42
43
44
45
46 public abstract class AbstractModelInterpolatorTest extends TestCase {
47 private Properties context;
48
49 protected void setUp() throws Exception {
50 super.setUp();
51
52 context = new Properties();
53 context.put("basedir", "myBasedir");
54 context.put("project.baseUri", "myBaseUri");
55 }
56
57 protected void assertProblemFree(SimpleProblemCollector collector) {
58 assertEquals("Expected no errors", 0, collector.getErrors().size());
59 assertEquals("Expected no warnings", 0, collector.getWarnings().size());
60 assertEquals("Expected no fatals", 0, collector.getFatals().size());
61 }
62
63
64
65
66 @Deprecated
67 protected void assertColllectorState(
68 int numFatals, int numErrors, int numWarnings, SimpleProblemCollector collector) {
69 assertEquals("Errors", numErrors, collector.getErrors().size());
70 assertEquals("Warnings", numWarnings, collector.getWarnings().size());
71 assertEquals("Fatals", numFatals, collector.getFatals().size());
72 }
73
74 protected void assertCollectorState(
75 int numFatals, int numErrors, int numWarnings, SimpleProblemCollector collector) {
76 assertColllectorState(numFatals, numErrors, numWarnings, collector);
77 }
78
79 private ModelBuildingRequest createModelBuildingRequest(Properties p) {
80 ModelBuildingRequest config = new DefaultModelBuildingRequest();
81 if (p != null) {
82 config.setSystemProperties(p);
83 }
84 return config;
85 }
86
87 public void testDefaultBuildTimestampFormatShouldFormatTimeIn24HourFormat() {
88 Calendar cal = Calendar.getInstance();
89 cal.setTimeZone(MavenBuildTimestamp.DEFAULT_BUILD_TIME_ZONE);
90 cal.set(Calendar.HOUR, 12);
91 cal.set(Calendar.AM_PM, Calendar.AM);
92
93
94 cal.set(Calendar.HOUR_OF_DAY, 0);
95 cal.set(Calendar.MINUTE, 16);
96 cal.set(Calendar.SECOND, 0);
97 cal.set(Calendar.YEAR, 1976);
98 cal.set(Calendar.MONTH, Calendar.NOVEMBER);
99 cal.set(Calendar.DATE, 11);
100
101 Date firstTestDate = cal.getTime();
102
103 cal.set(Calendar.HOUR, 11);
104 cal.set(Calendar.AM_PM, Calendar.PM);
105
106
107 cal.set(Calendar.HOUR_OF_DAY, 23);
108
109 Date secondTestDate = cal.getTime();
110
111 SimpleDateFormat format = new SimpleDateFormat(MavenBuildTimestamp.DEFAULT_BUILD_TIMESTAMP_FORMAT);
112 format.setTimeZone(MavenBuildTimestamp.DEFAULT_BUILD_TIME_ZONE);
113 assertEquals("1976-11-11T00:16:00Z", format.format(firstTestDate));
114 assertEquals("1976-11-11T23:16:00Z", format.format(secondTestDate));
115 }
116
117 public void testDefaultBuildTimestampFormatWithLocalTimeZoneMidnightRollover() {
118 Calendar cal = Calendar.getInstance();
119 cal.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
120
121 cal.set(Calendar.HOUR_OF_DAY, 1);
122 cal.set(Calendar.MINUTE, 16);
123 cal.set(Calendar.SECOND, 0);
124 cal.set(Calendar.YEAR, 2014);
125 cal.set(Calendar.MONTH, Calendar.JUNE);
126 cal.set(Calendar.DATE, 16);
127
128 Date firstTestDate = cal.getTime();
129
130 cal.set(Calendar.MONTH, Calendar.NOVEMBER);
131
132 Date secondTestDate = cal.getTime();
133
134 SimpleDateFormat format = new SimpleDateFormat(MavenBuildTimestamp.DEFAULT_BUILD_TIMESTAMP_FORMAT);
135 format.setTimeZone(MavenBuildTimestamp.DEFAULT_BUILD_TIME_ZONE);
136 assertEquals("2014-06-15T23:16:00Z", format.format(firstTestDate));
137 assertEquals("2014-11-16T00:16:00Z", format.format(secondTestDate));
138 }
139
140 public void testShouldNotThrowExceptionOnReferenceToNonExistentValue() throws Exception {
141 Model model = new Model();
142
143 Scm scm = new Scm();
144 scm.setConnection("${test}/somepath");
145
146 model.setScm(scm);
147
148 ModelInterpolator interpolator = createInterpolator();
149
150 final SimpleProblemCollector collector = new SimpleProblemCollector();
151 Model out = interpolator.interpolateModel(model, new File("."), createModelBuildingRequest(context), collector);
152
153 assertProblemFree(collector);
154 assertEquals("${test}/somepath", out.getScm().getConnection());
155 }
156
157 public void testShouldThrowExceptionOnRecursiveScmConnectionReference() throws Exception {
158 Model model = new Model();
159
160 Scm scm = new Scm();
161 scm.setConnection("${project.scm.connection}/somepath");
162
163 model.setScm(scm);
164
165 try {
166 ModelInterpolator interpolator = createInterpolator();
167
168 final SimpleProblemCollector collector = new SimpleProblemCollector();
169 interpolator.interpolateModel(model, null, createModelBuildingRequest(context), collector);
170 assertCollectorState(0, 1, 0, collector);
171 } catch (Exception e) {
172
173 }
174 }
175
176 public void testShouldNotThrowExceptionOnReferenceToValueContainingNakedExpression() throws Exception {
177 Model model = new Model();
178
179 Scm scm = new Scm();
180 scm.setConnection("${test}/somepath");
181
182 model.setScm(scm);
183
184 model.addProperty("test", "test");
185
186 ModelInterpolator interpolator = createInterpolator();
187
188 final SimpleProblemCollector collector = new SimpleProblemCollector();
189 Model out = interpolator.interpolateModel(model, new File("."), createModelBuildingRequest(context), collector);
190
191 assertProblemFree(collector);
192
193 assertEquals("test/somepath", out.getScm().getConnection());
194 }
195
196 public void testShouldInterpolateOrganizationNameCorrectly() throws Exception {
197 String orgName = "MyCo";
198
199 Model model = new Model();
200 model.setName("${pom.organization.name} Tools");
201
202 Organization org = new Organization();
203 org.setName(orgName);
204
205 model.setOrganization(org);
206
207 ModelInterpolator interpolator = createInterpolator();
208
209 Model out = interpolator.interpolateModel(
210 model, new File("."), createModelBuildingRequest(context), new SimpleProblemCollector());
211
212 assertEquals(orgName + " Tools", out.getName());
213 }
214
215 public void testShouldInterpolateDependencyVersionToSetSameAsProjectVersion() throws Exception {
216 Model model = new Model();
217 model.setVersion("3.8.1");
218
219 Dependency dep = new Dependency();
220 dep.setVersion("${version}");
221
222 model.addDependency(dep);
223
224 ModelInterpolator interpolator = createInterpolator();
225
226 final SimpleProblemCollector collector = new SimpleProblemCollector();
227 Model out = interpolator.interpolateModel(model, new File("."), createModelBuildingRequest(context), collector);
228 assertCollectorState(0, 0, 1, collector);
229
230 assertEquals("3.8.1", (out.getDependencies().get(0)).getVersion());
231 }
232
233 public void testShouldNotInterpolateDependencyVersionWithInvalidReference() throws Exception {
234 Model model = new Model();
235 model.setVersion("3.8.1");
236
237 Dependency dep = new Dependency();
238 dep.setVersion("${something}");
239
240 model.addDependency(dep);
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 ModelInterpolator interpolator = createInterpolator();
258
259 final SimpleProblemCollector collector = new SimpleProblemCollector();
260 Model out = interpolator.interpolateModel(model, new File("."), createModelBuildingRequest(context), collector);
261 assertProblemFree(collector);
262
263 assertEquals("${something}", (out.getDependencies().get(0)).getVersion());
264 }
265
266 public void testTwoReferences() throws Exception {
267 Model model = new Model();
268 model.setVersion("3.8.1");
269 model.setArtifactId("foo");
270
271 Dependency dep = new Dependency();
272 dep.setVersion("${artifactId}-${version}");
273
274 model.addDependency(dep);
275
276 ModelInterpolator interpolator = createInterpolator();
277
278 final SimpleProblemCollector collector = new SimpleProblemCollector();
279 Model out = interpolator.interpolateModel(model, new File("."), createModelBuildingRequest(context), collector);
280 assertCollectorState(0, 0, 2, collector);
281
282 assertEquals("foo-3.8.1", (out.getDependencies().get(0)).getVersion());
283 }
284
285 public void testBasedir() throws Exception {
286 Model model = new Model();
287 model.setVersion("3.8.1");
288 model.setArtifactId("foo");
289
290 Repository repository = new Repository();
291
292 repository.setUrl("file://localhost/${basedir}/temp-repo");
293
294 model.addRepository(repository);
295
296 ModelInterpolator interpolator = createInterpolator();
297
298 final SimpleProblemCollector collector = new SimpleProblemCollector();
299 Model out = interpolator.interpolateModel(model, null, createModelBuildingRequest(context), collector);
300 assertProblemFree(collector);
301
302 assertEquals(
303 "file://localhost/myBasedir/temp-repo", (out.getRepositories().get(0)).getUrl());
304 }
305
306 public void testBaseUri() throws Exception {
307 Model model = new Model();
308 model.setVersion("3.8.1");
309 model.setArtifactId("foo");
310
311 Repository repository = new Repository();
312
313 repository.setUrl("${project.baseUri}/temp-repo");
314
315 model.addRepository(repository);
316
317 ModelInterpolator interpolator = createInterpolator();
318
319 final SimpleProblemCollector collector = new SimpleProblemCollector();
320 Model out = interpolator.interpolateModel(model, null, createModelBuildingRequest(context), collector);
321 assertProblemFree(collector);
322
323 assertEquals("myBaseUri/temp-repo", (out.getRepositories().get(0)).getUrl());
324 }
325
326 public void testEnvars() throws Exception {
327 Properties context = new Properties();
328
329 context.put("env.HOME", "/path/to/home");
330
331 Model model = new Model();
332
333 Properties modelProperties = new Properties();
334
335 modelProperties.setProperty("outputDirectory", "${env.HOME}");
336
337 model.setProperties(modelProperties);
338
339 ModelInterpolator interpolator = createInterpolator();
340
341 final SimpleProblemCollector collector = new SimpleProblemCollector();
342 Model out = interpolator.interpolateModel(model, new File("."), createModelBuildingRequest(context), collector);
343 assertProblemFree(collector);
344
345 assertEquals("/path/to/home", out.getProperties().getProperty("outputDirectory"));
346 }
347
348 public void testEnvarExpressionThatEvaluatesToNullReturnsTheLiteralString() throws Exception {
349 Model model = new Model();
350
351 Properties modelProperties = new Properties();
352
353 modelProperties.setProperty("outputDirectory", "${env.DOES_NOT_EXIST}");
354
355 model.setProperties(modelProperties);
356
357 ModelInterpolator interpolator = createInterpolator();
358
359 final SimpleProblemCollector collector = new SimpleProblemCollector();
360 Model out = interpolator.interpolateModel(model, new File("."), createModelBuildingRequest(context), collector);
361 assertProblemFree(collector);
362
363 assertEquals(out.getProperties().getProperty("outputDirectory"), "${env.DOES_NOT_EXIST}");
364 }
365
366 public void testExpressionThatEvaluatesToNullReturnsTheLiteralString() throws Exception {
367 Model model = new Model();
368
369 Properties modelProperties = new Properties();
370
371 modelProperties.setProperty("outputDirectory", "${DOES_NOT_EXIST}");
372
373 model.setProperties(modelProperties);
374
375 ModelInterpolator interpolator = createInterpolator();
376
377 final SimpleProblemCollector collector = new SimpleProblemCollector();
378 Model out = interpolator.interpolateModel(model, new File("."), createModelBuildingRequest(context), collector);
379 assertProblemFree(collector);
380
381 assertEquals(out.getProperties().getProperty("outputDirectory"), "${DOES_NOT_EXIST}");
382 }
383
384 public void testShouldInterpolateSourceDirectoryReferencedFromResourceDirectoryCorrectly() throws Exception {
385 Model model = new Model();
386
387 Build build = new Build();
388 build.setSourceDirectory("correct");
389
390 Resource res = new Resource();
391 res.setDirectory("${project.build.sourceDirectory}");
392
393 build.addResource(res);
394
395 Resource res2 = new Resource();
396 res2.setDirectory("${pom.build.sourceDirectory}");
397
398 build.addResource(res2);
399
400 Resource res3 = new Resource();
401 res3.setDirectory("${build.sourceDirectory}");
402
403 build.addResource(res3);
404
405 model.setBuild(build);
406
407 ModelInterpolator interpolator = createInterpolator();
408
409 final SimpleProblemCollector collector = new SimpleProblemCollector();
410 Model out = interpolator.interpolateModel(model, null, createModelBuildingRequest(context), collector);
411 assertCollectorState(0, 0, 2, collector);
412
413 List<Resource> outResources = out.getBuild().getResources();
414 Iterator<Resource> resIt = outResources.iterator();
415
416 assertEquals(build.getSourceDirectory(), resIt.next().getDirectory());
417 assertEquals(build.getSourceDirectory(), resIt.next().getDirectory());
418 assertEquals(build.getSourceDirectory(), resIt.next().getDirectory());
419 }
420
421 public void testShouldInterpolateUnprefixedBasedirExpression() throws Exception {
422 File basedir = new File("/test/path");
423 Model model = new Model();
424 Dependency dep = new Dependency();
425 dep.setSystemPath("${basedir}/artifact.jar");
426
427 model.addDependency(dep);
428
429 ModelInterpolator interpolator = createInterpolator();
430
431 final SimpleProblemCollector collector = new SimpleProblemCollector();
432 Model result = interpolator.interpolateModel(model, basedir, createModelBuildingRequest(context), collector);
433 assertProblemFree(collector);
434
435 List<Dependency> rDeps = result.getDependencies();
436 assertNotNull(rDeps);
437 assertEquals(1, rDeps.size());
438 assertEquals(
439 new File(basedir, "artifact.jar").getAbsolutePath(),
440 new File(rDeps.get(0).getSystemPath()).getAbsolutePath());
441 }
442
443 protected abstract ModelInterpolator createInterpolator(PathTranslator translator) throws Exception;
444
445 protected abstract ModelInterpolator createInterpolator() throws Exception;
446 }