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