View Javadoc

1   package org.apache.maven.plugin;
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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import org.apache.maven.AbstractCoreMavenComponentTestCase;
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.artifact.ArtifactUtils;
32  import org.apache.maven.artifact.factory.ArtifactFactory;
33  import org.apache.maven.artifact.repository.ArtifactRepository;
34  import org.apache.maven.execution.DefaultMavenExecutionRequest;
35  import org.apache.maven.execution.DefaultMavenExecutionResult;
36  import org.apache.maven.execution.MavenExecutionRequest;
37  import org.apache.maven.execution.MavenSession;
38  import org.apache.maven.model.Build;
39  import org.apache.maven.model.Dependency;
40  import org.apache.maven.model.Model;
41  import org.apache.maven.plugin.descriptor.MojoDescriptor;
42  import org.apache.maven.plugin.descriptor.PluginDescriptor;
43  import org.apache.maven.project.DuplicateProjectException;
44  import org.apache.maven.project.MavenProject;
45  import org.apache.maven.repository.RepositorySystem;
46  import org.codehaus.plexus.MutablePlexusContainer;
47  import org.codehaus.plexus.PlexusContainer;
48  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
49  import org.codehaus.plexus.util.dag.CycleDetectedException;
50  
51  
52  /**
53   * @author Jason van Zyl
54   */
55  public class PluginParameterExpressionEvaluatorTest
56      extends AbstractCoreMavenComponentTestCase
57  {
58      private static final String FS = System.getProperty( "file.separator" );
59  
60      private RepositorySystem factory;
61  
62      public void setUp()
63          throws Exception
64      {
65          super.setUp();
66          factory = lookup( RepositorySystem.class );
67      }
68  
69      @Override
70      protected void tearDown()
71          throws Exception
72      {
73          factory = null;
74          super.tearDown();
75      }
76  
77      public void testPluginDescriptorExpressionReference()
78          throws Exception
79      {
80          MojoExecution exec = newMojoExecution();
81  
82          MavenSession session = newMavenSession();
83  
84          Object result = new PluginParameterExpressionEvaluator( session, exec ).evaluate( "${plugin}" );
85  
86          System.out.println( "Result: " + result );
87  
88          assertSame( "${plugin} expression does not return plugin descriptor.",
89                      exec.getMojoDescriptor().getPluginDescriptor(),
90                      result );
91      }
92  
93      public void testPluginArtifactsExpressionReference()
94          throws Exception
95      {
96          MojoExecution exec = newMojoExecution();
97  
98          Artifact depArtifact = createArtifact( "group", "artifact", "1" );
99  
100         List<Artifact> deps = new ArrayList<Artifact>();
101         deps.add( depArtifact );
102 
103         exec.getMojoDescriptor().getPluginDescriptor().setArtifacts( deps );
104 
105         MavenSession session = newMavenSession();
106 
107         List depResults = (List) new PluginParameterExpressionEvaluator( session, exec ).evaluate( "${plugin.artifacts}" );
108 
109         System.out.println( "Result: " + depResults );
110 
111         assertNotNull( depResults );
112         assertEquals( 1, depResults.size() );
113         assertSame( "dependency artifact is wrong.", depArtifact, depResults.get( 0 ) );
114     }
115 
116     public void testPluginArtifactMapExpressionReference()
117         throws Exception
118     {
119         MojoExecution exec = newMojoExecution();
120 
121         Artifact depArtifact = createArtifact( "group", "artifact", "1" );
122 
123         List<Artifact> deps = new ArrayList<Artifact>();
124         deps.add( depArtifact );
125 
126         exec.getMojoDescriptor().getPluginDescriptor().setArtifacts( deps );
127 
128         MavenSession session = newMavenSession();
129 
130         Map depResults = (Map) new PluginParameterExpressionEvaluator( session, exec ).evaluate( "${plugin.artifactMap}" );
131 
132         System.out.println( "Result: " + depResults );
133 
134         assertNotNull( depResults );
135         assertEquals( 1, depResults.size() );
136         assertSame( "dependency artifact is wrong.",
137                     depArtifact,
138                     depResults.get( ArtifactUtils.versionlessKey( depArtifact ) ) );
139     }
140 
141     public void testPluginArtifactIdExpressionReference()
142         throws Exception
143     {
144         MojoExecution exec = newMojoExecution();
145 
146         MavenSession session = newMavenSession();
147 
148         Object result = new PluginParameterExpressionEvaluator( session, exec ).evaluate( "${plugin.artifactId}" );
149 
150         System.out.println( "Result: " + result );
151 
152         assertSame( "${plugin.artifactId} expression does not return plugin descriptor's artifactId.",
153                     exec.getMojoDescriptor().getPluginDescriptor().getArtifactId(),
154                     result );
155     }
156 
157     public void testValueExtractionWithAPomValueContainingAPath()
158         throws Exception
159     {
160         String expected = getTestFile( "target/test-classes/target/classes" ).getCanonicalPath();
161 
162         Build build = new Build();
163         build.setDirectory( expected.substring( 0, expected.length() - "/classes".length() ) );
164 
165         Model model = new Model();
166         model.setBuild( build );
167 
168         MavenProject project = new MavenProject( model );
169         project.setFile( new File( "pom.xml" ).getCanonicalFile() );
170 
171         ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( project, null, new Properties() );
172 
173         Object value = expressionEvaluator.evaluate( "${project.build.directory}/classes" );
174         String actual = new File( value.toString() ).getCanonicalPath();
175 
176         assertEquals( expected, actual );
177     }
178 
179     public void testEscapedVariablePassthrough()
180         throws Exception
181     {
182         String var = "${var}";
183 
184         Model model = new Model();
185         model.setVersion( "1" );
186 
187         MavenProject project = new MavenProject( model );
188 
189         ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() );
190 
191         Object value = ee.evaluate( "$" + var );
192 
193         assertEquals( var, value );
194     }
195 
196     public void testEscapedVariablePassthroughInLargerExpression()
197         throws Exception
198     {
199         String var = "${var}";
200         String key = var + " with version: ${project.version}";
201 
202         Model model = new Model();
203         model.setVersion( "1" );
204 
205         MavenProject project = new MavenProject( model );
206 
207         ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() );
208 
209         Object value = ee.evaluate( "$" + key );
210 
211         assertEquals( "${var} with version: 1", value );
212     }
213 
214     public void testMultipleSubExpressionsInLargerExpression()
215         throws Exception
216     {
217         String key = "${project.artifactId} with version: ${project.version}";
218 
219         Model model = new Model();
220         model.setArtifactId( "test" );
221         model.setVersion( "1" );
222 
223         MavenProject project = new MavenProject( model );
224 
225         ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() );
226 
227         Object value = ee.evaluate( key );
228 
229         assertEquals( "test with version: 1", value );
230     }
231 
232     public void testMissingPOMPropertyRefInLargerExpression()
233         throws Exception
234     {
235         String expr = "/path/to/someproject-${baseVersion}";
236 
237         MavenProject project = new MavenProject( new Model() );
238 
239         ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() );
240 
241         Object value = ee.evaluate( expr );
242 
243         assertEquals( expr, value );
244     }
245 
246     public void testPOMPropertyExtractionWithMissingProject_WithDotNotation()
247         throws Exception
248     {
249         String key = "m2.name";
250         String checkValue = "value";
251 
252         Properties properties = new Properties();
253         properties.setProperty( key, checkValue );
254 
255         Model model = new Model();
256         model.setProperties( properties );
257 
258         MavenProject project = new MavenProject( model );
259 
260         ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() );
261 
262         Object value = ee.evaluate( "${" + key + "}" );
263 
264         assertEquals( checkValue, value );
265     }
266 
267     public void testBasedirExtractionWithMissingProject()
268         throws Exception
269     {
270         ExpressionEvaluator ee = createExpressionEvaluator( null, null, new Properties() );
271 
272         Object value = ee.evaluate( "${basedir}" );
273 
274         assertEquals( System.getProperty( "user.dir" ), value );
275     }
276 
277     public void testValueExtractionFromSystemPropertiesWithMissingProject()
278         throws Exception
279     {
280         String sysprop = "PPEET_sysprop1";
281 
282         Properties executionProperties = new Properties();
283 
284         if ( executionProperties.getProperty( sysprop ) == null )
285         {
286             executionProperties.setProperty( sysprop, "value" );
287         }
288 
289         ExpressionEvaluator ee = createExpressionEvaluator( null, null, executionProperties );
290 
291         Object value = ee.evaluate( "${" + sysprop + "}" );
292 
293         assertEquals( "value", value );
294     }
295 
296     public void testValueExtractionFromSystemPropertiesWithMissingProject_WithDotNotation()
297         throws Exception
298     {
299         String sysprop = "PPEET.sysprop2";
300 
301         Properties executionProperties = new Properties();
302 
303         if ( executionProperties.getProperty( sysprop ) == null )
304         {
305             executionProperties.setProperty( sysprop, "value" );
306         }
307 
308         ExpressionEvaluator ee = createExpressionEvaluator( null, null, executionProperties );
309 
310         Object value = ee.evaluate( "${" + sysprop + "}" );
311 
312         assertEquals( "value", value );
313     }
314 
315     private static MavenSession createSession( PlexusContainer container, ArtifactRepository repo, Properties properties )
316         throws CycleDetectedException, DuplicateProjectException
317     {
318         MavenExecutionRequest request = new DefaultMavenExecutionRequest()
319             .setSystemProperties( properties )
320             .setGoals( Collections.EMPTY_LIST )
321             .setBaseDirectory( new File( "" ) )
322             .setLocalRepository( repo );
323 
324         return new MavenSession( container, request, new DefaultMavenExecutionResult(), Collections.EMPTY_LIST  );
325     }
326 
327     public void testLocalRepositoryExtraction()
328         throws Exception
329     {
330         ExpressionEvaluator expressionEvaluator =
331             createExpressionEvaluator( createDefaultProject(), null, new Properties() );
332         Object value = expressionEvaluator.evaluate( "${localRepository}" );
333 
334         assertEquals( "local", ( (ArtifactRepository) value ).getId() );
335     }
336 
337     public void testTwoExpressions()
338         throws Exception
339     {
340         Build build = new Build();
341         build.setDirectory( "expected-directory" );
342         build.setFinalName( "expected-finalName" );
343 
344         Model model = new Model();
345         model.setBuild( build );
346 
347         ExpressionEvaluator expressionEvaluator =
348             createExpressionEvaluator( new MavenProject( model ), null, new Properties() );
349 
350         Object value = expressionEvaluator.evaluate( "${project.build.directory}" + FS + "${project.build.finalName}" );
351 
352         assertEquals( "expected-directory" + File.separatorChar + "expected-finalName", value );
353     }
354 
355     public void testShouldExtractPluginArtifacts()
356         throws Exception
357     {
358         PluginDescriptor pd = new PluginDescriptor();
359 
360         Artifact artifact = createArtifact( "testGroup", "testArtifact", "1.0" );
361 
362         pd.setArtifacts( Collections.singletonList( artifact ) );
363 
364         ExpressionEvaluator ee = createExpressionEvaluator( createDefaultProject(), pd, new Properties() );
365 
366         Object value = ee.evaluate( "${plugin.artifacts}" );
367 
368         assertTrue( value instanceof List );
369 
370         List artifacts = (List) value;
371 
372         assertEquals( 1, artifacts.size() );
373 
374         Artifact result = (Artifact) artifacts.get( 0 );
375 
376         assertEquals( "testGroup", result.getGroupId() );
377     }
378 
379     private MavenProject createDefaultProject()
380     {
381         return new MavenProject( new Model() );
382     }
383 
384     private ExpressionEvaluator createExpressionEvaluator( MavenProject project, PluginDescriptor pluginDescriptor, Properties executionProperties )
385         throws Exception
386     {
387         ArtifactRepository repo = factory.createDefaultLocalRepository();
388 
389         MutablePlexusContainer container = (MutablePlexusContainer) getContainer();
390         MavenSession session = createSession( container, repo, executionProperties );
391         session.setCurrentProject( project );
392 
393         MojoDescriptor mojo = new MojoDescriptor();
394         mojo.setPluginDescriptor( pluginDescriptor );
395         mojo.setGoal( "goal" );
396 
397         MojoExecution mojoExecution = new MojoExecution( mojo );
398 
399         return new PluginParameterExpressionEvaluator( session, mojoExecution );
400     }
401 
402     protected Artifact createArtifact( String groupId, String artifactId, String version )
403         throws Exception
404     {
405         Dependency dependency = new Dependency();
406         dependency.setGroupId( groupId );
407         dependency.setArtifactId( artifactId );
408         dependency.setVersion( version );
409         dependency.setType( "jar" );
410         dependency.setScope( "compile" );
411 
412         return factory.createDependencyArtifact( dependency );
413     }
414 
415     private MojoExecution newMojoExecution()
416     {
417         PluginDescriptor pd = new PluginDescriptor();
418         pd.setArtifactId( "my-plugin" );
419         pd.setGroupId( "org.myco.plugins" );
420         pd.setVersion( "1" );
421 
422         MojoDescriptor md = new MojoDescriptor();
423         md.setPluginDescriptor( pd );
424 
425         pd.addComponentDescriptor( md );
426 
427         return new MojoExecution( md );
428     }
429 
430     private MavenSession newMavenSession()
431         throws Exception
432     {
433         return createMavenSession( null );
434     }
435 
436     @Override
437     protected String getProjectsDirectory()
438     {
439         // TODO Auto-generated method stub
440         return null;
441     }
442 
443 }