View Javadoc
1   package org.apache.maven.model.interpolation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.model.Build;
23  import org.apache.maven.model.Dependency;
24  import org.apache.maven.model.Model;
25  import org.apache.maven.model.Organization;
26  import org.apache.maven.model.Repository;
27  import org.apache.maven.model.Resource;
28  import org.apache.maven.model.Scm;
29  import org.apache.maven.model.building.DefaultModelBuildingRequest;
30  import org.apache.maven.model.building.ModelBuildingRequest;
31  import org.apache.maven.model.building.SimpleProblemCollector;
32  import org.apache.maven.model.path.PathTranslator;
33  
34  import junit.framework.TestCase;
35  
36  import java.io.File;
37  import java.text.SimpleDateFormat;
38  import java.util.Calendar;
39  import java.util.Date;
40  import java.util.Iterator;
41  import java.util.List;
42  import java.util.Properties;
43  import java.util.TimeZone;
44  
45  /**
46   * @author jdcasey
47   */
48  public abstract class AbstractModelInterpolatorTest
49      extends TestCase
50  {
51      private Properties context;
52  
53      protected void setUp()
54          throws Exception
55      {
56          super.setUp();
57  
58          context = new Properties();
59          context.put( "basedir", "myBasedir" );
60          context.put( "project.baseUri", "myBaseUri" );
61      }
62  
63  
64      protected void assertProblemFree( SimpleProblemCollector collector )
65      {
66          assertEquals( "Expected no errors", 0, collector.getErrors().size() );
67          assertEquals( "Expected no warnings", 0, collector.getWarnings().size() );
68          assertEquals( "Expected no fatals", 0, collector.getFatals().size() );
69      }
70  
71      /**
72       * @deprecated instead use {@link #assertCollectorState(int, int, int, SimpleProblemCollector)}
73       */
74      @Deprecated
75      protected void assertColllectorState( int numFatals, int numErrors, int numWarnings,
76                                            SimpleProblemCollector collector )
77      {
78          assertEquals( "Errors",  numErrors, collector.getErrors().size() );
79          assertEquals( "Warnings", numWarnings, collector.getWarnings().size() );
80          assertEquals( "Fatals", numFatals, collector.getFatals().size() );
81      }
82  
83      protected void assertCollectorState( int numFatals, int numErrors, int numWarnings,
84                                            SimpleProblemCollector collector )
85      {
86          assertColllectorState(numFatals, numErrors, numWarnings, collector);
87      }
88  
89      private ModelBuildingRequest createModelBuildingRequest( Properties p )
90      {
91          ModelBuildingRequest config = new DefaultModelBuildingRequest();
92          if ( p != null )
93          {
94              config.setSystemProperties( p );
95          }
96          return config;
97      }
98  
99      public void testDefaultBuildTimestampFormatShouldFormatTimeIn24HourFormat()
100     {
101         Calendar cal = Calendar.getInstance();
102         cal.setTimeZone( MavenBuildTimestamp.DEFAULT_BUILD_TIME_ZONE );
103         cal.set( Calendar.HOUR, 12 );
104         cal.set( Calendar.AM_PM, Calendar.AM );
105 
106         // just to make sure all the bases are covered...
107         cal.set( Calendar.HOUR_OF_DAY, 0 );
108         cal.set( Calendar.MINUTE, 16 );
109         cal.set( Calendar.SECOND, 0 );
110         cal.set( Calendar.YEAR, 1976 );
111         cal.set( Calendar.MONTH, Calendar.NOVEMBER );
112         cal.set( Calendar.DATE, 11 );
113 
114         Date firstTestDate = cal.getTime();
115 
116         cal.set( Calendar.HOUR, 11 );
117         cal.set( Calendar.AM_PM, Calendar.PM );
118 
119         // just to make sure all the bases are covered...
120         cal.set( Calendar.HOUR_OF_DAY, 23 );
121 
122         Date secondTestDate = cal.getTime();
123 
124         SimpleDateFormat format =
125             new SimpleDateFormat( MavenBuildTimestamp.DEFAULT_BUILD_TIMESTAMP_FORMAT );
126         format.setTimeZone( MavenBuildTimestamp.DEFAULT_BUILD_TIME_ZONE );
127         assertEquals( "1976-11-11T00:16:00Z", format.format( firstTestDate ) );
128         assertEquals( "1976-11-11T23:16:00Z", format.format( secondTestDate ) );
129     }
130 
131     public void testDefaultBuildTimestampFormatWithLocalTimeZoneMidnightRollover()
132     {
133         Calendar cal = Calendar.getInstance();
134         cal.setTimeZone( TimeZone.getTimeZone( "Europe/Berlin" ) );
135 
136         cal.set( Calendar.HOUR_OF_DAY, 1 );
137         cal.set( Calendar.MINUTE, 16 );
138         cal.set( Calendar.SECOND, 0 );
139         cal.set( Calendar.YEAR, 2014 );
140         cal.set( Calendar.MONTH, Calendar.JUNE );
141         cal.set( Calendar.DATE, 16 );
142 
143         Date firstTestDate = cal.getTime();
144 
145         cal.set( Calendar.MONTH, Calendar.NOVEMBER );
146 
147         Date secondTestDate = cal.getTime();
148 
149         SimpleDateFormat format =
150             new SimpleDateFormat( MavenBuildTimestamp.DEFAULT_BUILD_TIMESTAMP_FORMAT );
151         format.setTimeZone( MavenBuildTimestamp.DEFAULT_BUILD_TIME_ZONE );
152         assertEquals( "2014-06-15T23:16:00Z", format.format( firstTestDate ) );
153         assertEquals( "2014-11-16T00:16:00Z", format.format( secondTestDate ) );
154     }
155 
156     public void testShouldNotThrowExceptionOnReferenceToNonExistentValue()
157         throws Exception
158     {
159         Model model = new Model();
160 
161         Scm scm = new Scm();
162         scm.setConnection( "${test}/somepath" );
163 
164         model.setScm( scm );
165 
166         ModelInterpolator interpolator = createInterpolator();
167 
168         final SimpleProblemCollector collector = new SimpleProblemCollector();
169         Model out =
170             interpolator.interpolateModel( model, new File( "." ), createModelBuildingRequest( context ), collector );
171 
172         assertProblemFree(  collector );
173         assertEquals( "${test}/somepath", out.getScm().getConnection() );
174     }
175 
176     public void testShouldThrowExceptionOnRecursiveScmConnectionReference()
177         throws Exception
178     {
179         Model model = new Model();
180 
181         Scm scm = new Scm();
182         scm.setConnection( "${project.scm.connection}/somepath" );
183 
184         model.setScm( scm );
185 
186         try
187         {
188             ModelInterpolator interpolator = createInterpolator();
189 
190             final SimpleProblemCollector collector = new SimpleProblemCollector();
191             interpolator.interpolateModel( model, null, createModelBuildingRequest( context ), collector );
192             assertCollectorState(  0, 1, 0, collector );
193         }
194         catch ( Exception e )
195         {
196 
197         }
198     }
199 
200     public void testShouldNotThrowExceptionOnReferenceToValueContainingNakedExpression()
201         throws Exception
202     {
203         Model model = new Model();
204 
205         Scm scm = new Scm();
206         scm.setConnection( "${test}/somepath" );
207 
208         model.setScm( scm );
209 
210         model.addProperty( "test", "test" );
211 
212         ModelInterpolator interpolator = createInterpolator();
213 
214         final SimpleProblemCollector collector = new SimpleProblemCollector();
215         Model out =
216             interpolator.interpolateModel( model, new File( "." ), createModelBuildingRequest( context ), collector );
217 
218         assertProblemFree(  collector );
219 
220         assertEquals( "test/somepath", out.getScm().getConnection() );
221     }
222 
223     public void testShouldInterpolateOrganizationNameCorrectly()
224         throws Exception
225     {
226         String orgName = "MyCo";
227 
228         Model model = new Model();
229         model.setName( "${pom.organization.name} Tools" );
230 
231         Organization org = new Organization();
232         org.setName( orgName );
233 
234         model.setOrganization( org );
235 
236         ModelInterpolator interpolator = createInterpolator();
237 
238         Model out =
239             interpolator.interpolateModel( model, new File( "." ), createModelBuildingRequest( context ),
240                                            new SimpleProblemCollector() );
241 
242         assertEquals( orgName + " Tools", out.getName() );
243     }
244 
245     public void testShouldInterpolateDependencyVersionToSetSameAsProjectVersion()
246         throws Exception
247     {
248         Model model = new Model();
249         model.setVersion( "3.8.1" );
250 
251         Dependency dep = new Dependency();
252         dep.setVersion( "${version}" );
253 
254         model.addDependency( dep );
255 
256         ModelInterpolator interpolator = createInterpolator();
257 
258         final SimpleProblemCollector collector = new SimpleProblemCollector();
259         Model out =
260             interpolator.interpolateModel( model, new File( "." ), createModelBuildingRequest( context ), collector );
261         assertCollectorState(0, 0, 1, collector );
262 
263         assertEquals( "3.8.1", ( out.getDependencies().get( 0 ) ).getVersion() );
264     }
265 
266     public void testShouldNotInterpolateDependencyVersionWithInvalidReference()
267         throws Exception
268     {
269         Model model = new Model();
270         model.setVersion( "3.8.1" );
271 
272         Dependency dep = new Dependency();
273         dep.setVersion( "${something}" );
274 
275         model.addDependency( dep );
276 
277         /*
278          // This is the desired behaviour, however there are too many crappy poms in the repo and an issue with the
279          // timing of executing the interpolation
280 
281          try
282          {
283          new RegexBasedModelInterpolator().interpolate( model, context );
284          fail( "Should have failed to interpolate with invalid reference" );
285          }
286          catch ( ModelInterpolationException expected )
287          {
288          assertTrue( true );
289          }
290          */
291 
292         ModelInterpolator interpolator = createInterpolator();
293 
294         final SimpleProblemCollector collector = new SimpleProblemCollector();
295         Model out =
296             interpolator.interpolateModel( model, new File( "." ), createModelBuildingRequest( context ), collector );
297         assertProblemFree( collector );
298 
299         assertEquals( "${something}", ( out.getDependencies().get( 0 ) ).getVersion() );
300     }
301 
302     public void testTwoReferences()
303         throws Exception
304     {
305         Model model = new Model();
306         model.setVersion( "3.8.1" );
307         model.setArtifactId( "foo" );
308 
309         Dependency dep = new Dependency();
310         dep.setVersion( "${artifactId}-${version}" );
311 
312         model.addDependency( dep );
313 
314         ModelInterpolator interpolator = createInterpolator();
315 
316         final SimpleProblemCollector collector = new SimpleProblemCollector();
317         Model out =
318             interpolator.interpolateModel( model, new File( "." ), createModelBuildingRequest( context ), collector );
319         assertCollectorState( 0, 0, 2, collector );
320 
321         assertEquals( "foo-3.8.1", ( out.getDependencies().get( 0 ) ).getVersion() );
322     }
323 
324     public void testBasedir()
325         throws Exception
326     {
327         Model model = new Model();
328         model.setVersion( "3.8.1" );
329         model.setArtifactId( "foo" );
330 
331         Repository repository = new Repository();
332 
333         repository.setUrl( "file://localhost/${basedir}/temp-repo" );
334 
335         model.addRepository( repository );
336 
337         ModelInterpolator interpolator = createInterpolator();
338 
339         final SimpleProblemCollector collector = new SimpleProblemCollector();
340         Model out = interpolator.interpolateModel( model, null, createModelBuildingRequest( context ), collector );
341         assertProblemFree( collector );
342 
343         assertEquals( "file://localhost/myBasedir/temp-repo", ( out.getRepositories().get( 0 ) ).getUrl() );
344     }
345 
346     public void testBaseUri()
347         throws Exception
348     {
349         Model model = new Model();
350         model.setVersion( "3.8.1" );
351         model.setArtifactId( "foo" );
352 
353         Repository repository = new Repository();
354 
355         repository.setUrl( "${project.baseUri}/temp-repo" );
356 
357         model.addRepository( repository );
358 
359         ModelInterpolator interpolator = createInterpolator();
360 
361         final SimpleProblemCollector collector = new SimpleProblemCollector();
362         Model out = interpolator.interpolateModel( model, null, createModelBuildingRequest( context ), collector );
363         assertProblemFree( collector );
364 
365         assertEquals( "myBaseUri/temp-repo", ( out.getRepositories().get( 0 ) ).getUrl() );
366     }
367 
368     public void testEnvars()
369         throws Exception
370     {
371          Properties context = new Properties();
372 
373         context.put( "env.HOME", "/path/to/home" );
374 
375         Model model = new Model();
376 
377         Properties modelProperties = new Properties();
378 
379         modelProperties.setProperty( "outputDirectory", "${env.HOME}" );
380 
381         model.setProperties( modelProperties );
382 
383         ModelInterpolator interpolator = createInterpolator();
384 
385         final SimpleProblemCollector collector = new SimpleProblemCollector();
386         Model out =
387             interpolator.interpolateModel( model, new File( "." ), createModelBuildingRequest( context ), collector );
388         assertProblemFree( collector );
389 
390         assertEquals( "/path/to/home", out.getProperties().getProperty( "outputDirectory" ) );
391     }
392 
393     public void testEnvarExpressionThatEvaluatesToNullReturnsTheLiteralString()
394         throws Exception
395     {
396         Model model = new Model();
397 
398         Properties modelProperties = new Properties();
399 
400         modelProperties.setProperty( "outputDirectory", "${env.DOES_NOT_EXIST}" );
401 
402         model.setProperties( modelProperties );
403 
404         ModelInterpolator interpolator = createInterpolator();
405 
406         final SimpleProblemCollector collector = new SimpleProblemCollector();
407         Model out =
408             interpolator.interpolateModel( model, new File( "." ), createModelBuildingRequest( context ), collector );
409         assertProblemFree( collector );
410 
411         assertEquals( out.getProperties().getProperty( "outputDirectory" ), "${env.DOES_NOT_EXIST}" );
412     }
413 
414     public void testExpressionThatEvaluatesToNullReturnsTheLiteralString()
415         throws Exception
416     {
417         Model model = new Model();
418 
419         Properties modelProperties = new Properties();
420 
421         modelProperties.setProperty( "outputDirectory", "${DOES_NOT_EXIST}" );
422 
423         model.setProperties( modelProperties );
424 
425         ModelInterpolator interpolator = createInterpolator();
426 
427         final SimpleProblemCollector collector = new SimpleProblemCollector();
428         Model out =
429             interpolator.interpolateModel( model, new File( "." ), createModelBuildingRequest( context ), collector );
430         assertProblemFree( collector );
431 
432         assertEquals( out.getProperties().getProperty( "outputDirectory" ), "${DOES_NOT_EXIST}" );
433     }
434 
435     public void testShouldInterpolateSourceDirectoryReferencedFromResourceDirectoryCorrectly()
436         throws Exception
437     {
438         Model model = new Model();
439 
440         Build build = new Build();
441         build.setSourceDirectory( "correct" );
442 
443         Resource res = new Resource();
444         res.setDirectory( "${project.build.sourceDirectory}" );
445 
446         build.addResource( res );
447 
448         Resource res2 = new Resource();
449         res2.setDirectory( "${pom.build.sourceDirectory}" );
450 
451         build.addResource( res2 );
452 
453         Resource res3 = new Resource();
454         res3.setDirectory( "${build.sourceDirectory}" );
455 
456         build.addResource( res3 );
457 
458         model.setBuild( build );
459 
460         ModelInterpolator interpolator = createInterpolator();
461 
462         final SimpleProblemCollector collector = new SimpleProblemCollector();
463         Model out = interpolator.interpolateModel( model, null, createModelBuildingRequest( context ), collector );
464         assertCollectorState( 0, 0, 2, collector );
465 
466 
467         List<Resource> outResources = out.getBuild().getResources();
468         Iterator<Resource> resIt = outResources.iterator();
469 
470         assertEquals( build.getSourceDirectory(), resIt.next().getDirectory() );
471         assertEquals( build.getSourceDirectory(), resIt.next().getDirectory() );
472         assertEquals( build.getSourceDirectory(), resIt.next().getDirectory() );
473     }
474 
475     public void testShouldInterpolateUnprefixedBasedirExpression()
476         throws Exception
477     {
478         File basedir = new File( "/test/path" );
479         Model model = new Model();
480         Dependency dep = new Dependency();
481         dep.setSystemPath( "${basedir}/artifact.jar" );
482 
483         model.addDependency( dep );
484 
485         ModelInterpolator interpolator = createInterpolator();
486 
487         final SimpleProblemCollector collector = new SimpleProblemCollector();
488         Model result = interpolator.interpolateModel( model, basedir, createModelBuildingRequest( context ), collector );
489         assertProblemFree(  collector );
490 
491 
492         List<Dependency> rDeps = result.getDependencies();
493         assertNotNull( rDeps );
494         assertEquals( 1, rDeps.size() );
495         assertEquals( new File( basedir, "artifact.jar" ).getAbsolutePath(),
496                       new File( rDeps.get( 0 ).getSystemPath() ).getAbsolutePath() );
497     }
498 
499     protected abstract ModelInterpolator createInterpolator( PathTranslator translator )
500         throws Exception;
501 
502     protected abstract ModelInterpolator createInterpolator()
503         throws Exception;
504 
505 }