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