1   package org.apache.maven.project.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  
30  import java.io.IOException;
31  import java.util.Collections;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Properties;
36  
37  import junit.framework.TestCase;
38  
39  /**
40   * @author jdcasey
41   * @version $Id: RegexBasedModelInterpolatorTest.java 688884 2008-08-25 21:11:19Z jdcasey $
42   */
43  public class RegexBasedModelInterpolatorTest
44      extends TestCase
45  {
46      private Map context;
47  
48      protected void setUp()
49          throws Exception
50      {
51          super.setUp();
52  
53          context = Collections.singletonMap( "basedir", "myBasedir" );
54      }
55  
56      public void testShouldNotThrowExceptionOnReferenceToNonExistentValue()
57          throws IOException, ModelInterpolationException
58      {
59          Model model = new Model();
60  
61          Scm scm = new Scm();
62          scm.setConnection( "${test}/somepath" );
63  
64          model.setScm( scm );
65  
66          Model out = new RegexBasedModelInterpolator().interpolate( model, context );
67  
68          assertEquals( "${test}/somepath", out.getScm().getConnection() );
69      }
70  
71      public void testShouldThrowExceptionOnRecursiveScmConnectionReference()
72          throws IOException
73      {
74          Model model = new Model();
75  
76          Scm scm = new Scm();
77          scm.setConnection( "${project.scm.connection}/somepath" );
78  
79          model.setScm( scm );
80  
81          try
82          {
83              Model out = new RegexBasedModelInterpolator().interpolate( model, context );
84  
85              fail( "The interpolator should not allow self-referencing expressions in POM." );
86          }
87          catch ( ModelInterpolationException e )
88          {
89  
90          }
91      }
92  
93      public void testShouldNotThrowExceptionOnReferenceToValueContainingNakedExpression()
94          throws IOException, ModelInterpolationException
95      {
96          Model model = new Model();
97  
98          Scm scm = new Scm();
99          scm.setConnection( "${test}/somepath" );
100 
101         model.setScm( scm );
102 
103         model.addProperty( "test", "test" );
104 
105         Model out = new RegexBasedModelInterpolator().interpolate( model, context );
106 
107         assertEquals( "test/somepath", out.getScm().getConnection() );
108     }
109 
110     public void testShouldInterpolateOrganizationNameCorrectly()
111         throws Exception
112     {
113         String orgName = "MyCo";
114 
115         Model model = new Model();
116         model.setName( "${pom.organization.name} Tools" );
117 
118         Organization org = new Organization();
119         org.setName( orgName );
120 
121         model.setOrganization( org );
122 
123         Model out = new RegexBasedModelInterpolator().interpolate( model, context );
124 
125         assertEquals( orgName + " Tools", out.getName() );
126     }
127 
128     public void testShouldInterpolateDependencyVersionToSetSameAsProjectVersion()
129         throws Exception
130     {
131         Model model = new Model();
132         model.setVersion( "3.8.1" );
133 
134         Dependency dep = new Dependency();
135         dep.setVersion( "${version}" );
136 
137         model.addDependency( dep );
138 
139         Model out = new RegexBasedModelInterpolator().interpolate( model, context );
140 
141         assertEquals( "3.8.1", ( (Dependency) out.getDependencies().get( 0 ) ).getVersion() );
142     }
143 
144     public void testShouldNotInterpolateDependencyVersionWithInvalidReference()
145         throws Exception
146     {
147         Model model = new Model();
148         model.setVersion( "3.8.1" );
149 
150         Dependency dep = new Dependency();
151         dep.setVersion( "${something}" );
152 
153         model.addDependency( dep );
154 
155         /*
156          // This is the desired behaviour, however there are too many crappy poms in the repo and an issue with the
157          // timing of executing the interpolation
158 
159          try
160          {
161          new RegexBasedModelInterpolator().interpolate( model, context );
162          fail( "Should have failed to interpolate with invalid reference" );
163          }
164          catch ( ModelInterpolationException expected )
165          {
166          assertTrue( true );
167          }
168          */
169 
170         Model out = new RegexBasedModelInterpolator().interpolate( model, context );
171 
172         assertEquals( "${something}", ( (Dependency) out.getDependencies().get( 0 ) ).getVersion() );
173     }
174 
175     public void testTwoReferences()
176         throws Exception
177     {
178         Model model = new Model();
179         model.setVersion( "3.8.1" );
180         model.setArtifactId( "foo" );
181 
182         Dependency dep = new Dependency();
183         dep.setVersion( "${artifactId}-${version}" );
184 
185         model.addDependency( dep );
186 
187         Model out = new RegexBasedModelInterpolator().interpolate( model, context );
188 
189         assertEquals( "foo-3.8.1", ( (Dependency) out.getDependencies().get( 0 ) ).getVersion() );
190     }
191 
192     public void testBasedir()
193         throws Exception
194     {
195         Model model = new Model();
196         model.setVersion( "3.8.1" );
197         model.setArtifactId( "foo" );
198 
199         Repository repository = new Repository();
200 
201         repository.setUrl( "file://localhost/${basedir}/temp-repo" );
202 
203         model.addRepository( repository );
204 
205         Model out = new RegexBasedModelInterpolator().interpolate( model, context );
206 
207         assertEquals( "file://localhost/myBasedir/temp-repo", ( (Repository) out.getRepositories().get( 0 ) ).getUrl() );
208     }
209 
210     public void testEnvars()
211         throws Exception
212     {
213         Properties envars = new Properties();
214 
215         envars.setProperty( "HOME", "/path/to/home" );
216 
217         Model model = new Model();
218 
219         Properties modelProperties = new Properties();
220 
221         modelProperties.setProperty( "outputDirectory", "${env.HOME}" );
222 
223         model.setProperties( modelProperties );
224 
225         Model out = new RegexBasedModelInterpolator( envars ).interpolate( model, context );
226 
227         assertEquals( out.getProperties().getProperty( "outputDirectory" ), "/path/to/home" );
228     }
229 
230     public void testEnvarExpressionThatEvaluatesToNullReturnsTheLiteralString()
231         throws Exception
232     {
233         Properties envars = new Properties();
234 
235         Model model = new Model();
236 
237         Properties modelProperties = new Properties();
238 
239         modelProperties.setProperty( "outputDirectory", "${env.DOES_NOT_EXIST}" );
240 
241         model.setProperties( modelProperties );
242 
243         Model out = new RegexBasedModelInterpolator( envars ).interpolate( model, context );
244 
245         assertEquals( out.getProperties().getProperty( "outputDirectory" ), "${env.DOES_NOT_EXIST}" );
246     }
247 
248     public void testExpressionThatEvaluatesToNullReturnsTheLiteralString()
249         throws Exception
250     {
251         Model model = new Model();
252 
253         Properties modelProperties = new Properties();
254 
255         modelProperties.setProperty( "outputDirectory", "${DOES_NOT_EXIST}" );
256 
257         model.setProperties( modelProperties );
258 
259         Model out = new RegexBasedModelInterpolator().interpolate( model, context );
260 
261         assertEquals( out.getProperties().getProperty( "outputDirectory" ), "${DOES_NOT_EXIST}" );
262     }
263 
264     public void testShouldInterpolateSourceDirectoryReferencedFromResourceDirectoryCorrectly()
265         throws Exception
266     {
267         Model model = new Model();
268 
269         Build build = new Build();
270         build.setSourceDirectory( "correct" );
271 
272         Resource res = new Resource();
273         res.setDirectory( "${project.build.sourceDirectory}" );
274 
275         build.addResource( res );
276 
277         Resource res2 = new Resource();
278         res2.setDirectory( "${pom.build.sourceDirectory}" );
279 
280         build.addResource( res2 );
281 
282         Resource res3 = new Resource();
283         res3.setDirectory( "${build.sourceDirectory}" );
284 
285         build.addResource( res3 );
286 
287         model.setBuild( build );
288 
289         Model out = new RegexBasedModelInterpolator().interpolate( model, context );
290 
291         List outResources = out.getBuild().getResources();
292         Iterator resIt = outResources.iterator();
293 
294         assertEquals( build.getSourceDirectory(), ( (Resource) resIt.next() ).getDirectory() );
295         assertEquals( build.getSourceDirectory(), ( (Resource) resIt.next() ).getDirectory() );
296         assertEquals( build.getSourceDirectory(), ( (Resource) resIt.next() ).getDirectory() );
297     }
298 
299 //    public void testPOMExpressionDoesNotUseSystemProperty()
300 //        throws Exception
301 //    {
302 //        Model model = new Model();
303 //        model.setVersion( "1.0" );
304 //
305 //        Properties modelProperties = new Properties();
306 //        modelProperties.setProperty( "version", "prop version" );
307 //        modelProperties.setProperty( "foo.version", "prop foo.version" );
308 //        modelProperties.setProperty( "pom.version", "prop pom.version" );
309 //        modelProperties.setProperty( "project.version", "prop project.version" );
310 //
311 //        model.setProperties( modelProperties );
312 //
313 //        Dependency dep = new Dependency();
314 //        model.addDependency( dep );
315 //
316 //        checkDep( "prop version", "${version}", model );
317 //        checkDep( "1.0", "${pom.version}", model );
318 //        checkDep( "1.0", "${project.version}", model );
319 //        checkDep( "prop foo.version", "${foo.version}", model );
320 //    }
321 //
322 //    private void checkDep( String expectedVersion,
323 //                           String depVersionExpr,
324 //                           Model model )
325 //        throws Exception
326 //    {
327 //        ( (Dependency) model.getDependencies().get( 0 ) ).setVersion( depVersionExpr );
328 //        Model out = new RegexBasedModelInterpolator().interpolate( model, context );
329 //        String result = ( (Dependency) out.getDependencies().get( 0 ) ).getVersion();
330 //        assertEquals( "Expected '" + expectedVersion + "' for version expression '"
331 //                      + depVersionExpr + "', but was '" + result + "'", expectedVersion, result );
332 //    }
333 
334 }