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
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.factory.ArtifactFactory;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.repository.DefaultArtifactRepository;
27 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.execution.ReactorManager;
30 import org.apache.maven.model.Build;
31 import org.apache.maven.model.Model;
32 import org.apache.maven.monitor.event.DefaultEventDispatcher;
33 import org.apache.maven.plugin.descriptor.MojoDescriptor;
34 import org.apache.maven.plugin.descriptor.PluginDescriptor;
35 import org.apache.maven.project.DuplicateProjectException;
36 import org.apache.maven.project.MavenProject;
37 import org.apache.maven.settings.Settings;
38 import org.codehaus.plexus.PlexusContainer;
39 import org.codehaus.plexus.PlexusTestCase;
40 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
41 import org.codehaus.plexus.util.dag.CycleDetectedException;
42
43 import java.io.File;
44 import java.util.Collections;
45 import java.util.Date;
46 import java.util.List;
47 import java.util.Properties;
48
49
50
51
52
53
54 public class PluginParameterExpressionEvaluatorTest
55 extends PlexusTestCase
56 {
57 private static final String FS = System.getProperty( "file.separator" );
58
59 public void testValueExtractionWithAPomValueContainingAPath()
60 throws Exception
61 {
62 String expected = getTestFile( "target/test-classes/target/classes" ).getCanonicalPath();
63
64 Build build = new Build();
65 build.setDirectory( expected.substring( 0, expected.length() - "/classes".length() ) );
66
67 Model model = new Model();
68 model.setBuild( build );
69
70 MavenProject project = new MavenProject( model );
71 project.setFile( new File( "pom.xml" ).getCanonicalFile() );
72
73 ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( project, null, new Properties() );
74
75 Object value = expressionEvaluator.evaluate( "${project.build.directory}/classes" );
76 String actual = new File( value.toString() ).getCanonicalPath();
77
78 assertEquals( expected, actual );
79 }
80
81 public void testEscapedVariablePassthrough()
82 throws Exception
83 {
84 String var = "${var}";
85
86 Model model = new Model();
87 model.setVersion( "1" );
88
89 MavenProject project = new MavenProject( model );
90
91 ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() );
92
93 Object value = ee.evaluate( "$" + var );
94
95 assertEquals( var, value );
96 }
97
98 public void testEscapedVariablePassthroughInLargerExpression()
99 throws Exception
100 {
101 String var = "${var}";
102 String key = var + " with version: ${project.version}";
103
104 Model model = new Model();
105 model.setVersion( "1" );
106
107 MavenProject project = new MavenProject( model );
108
109 ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() );
110
111 Object value = ee.evaluate( "$" + key );
112
113 assertEquals( "${var} with version: 1", value );
114 }
115
116 public void testMultipleSubExpressionsInLargerExpression()
117 throws Exception
118 {
119 String key = "${project.artifactId} with version: ${project.version}";
120
121 Model model = new Model();
122 model.setArtifactId( "test" );
123 model.setVersion( "1" );
124
125 MavenProject project = new MavenProject( model );
126
127 ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() );
128
129 Object value = ee.evaluate( key );
130
131 assertEquals( "test with version: 1", value );
132 }
133
134 public void testPOMPropertyExtractionWithMissingProject_WithDotNotation()
135 throws Exception
136 {
137 String key = "m2.name";
138 String checkValue = "value";
139
140 Properties properties = new Properties();
141 properties.setProperty( key, checkValue );
142
143 Model model = new Model();
144 model.setProperties( properties );
145
146 MavenProject project = new MavenProject( model );
147
148 ExpressionEvaluator ee = createExpressionEvaluator( project, null, new Properties() );
149
150 Object value = ee.evaluate( "${" + key + "}" );
151
152 assertEquals( checkValue, value );
153 }
154
155 public void testBasedirExtractionWithMissingProject()
156 throws Exception
157 {
158 ExpressionEvaluator ee = createExpressionEvaluator( null, null, new Properties() );
159
160 Object value = ee.evaluate( "${basedir}" );
161
162 assertEquals( System.getProperty( "user.dir" ), value );
163 }
164
165 public void testValueExtractionFromSystemPropertiesWithMissingProject()
166 throws Exception
167 {
168 String sysprop = "PPEET_sysprop1";
169
170 Properties executionProperties = new Properties();
171
172 if ( executionProperties.getProperty( sysprop ) == null )
173 {
174 executionProperties.setProperty( sysprop, "value" );
175 }
176
177 ExpressionEvaluator ee = createExpressionEvaluator( null, null, executionProperties );
178
179 Object value = ee.evaluate( "${" + sysprop + "}" );
180
181 assertEquals( "value", value );
182 }
183
184 public void testValueExtractionFromSystemPropertiesWithMissingProject_WithDotNotation()
185 throws Exception
186 {
187 String sysprop = "PPEET.sysprop2";
188
189 Properties executionProperties = new Properties();
190
191 if ( executionProperties.getProperty( sysprop ) == null )
192 {
193 executionProperties.setProperty( sysprop, "value" );
194 }
195
196 ExpressionEvaluator ee = createExpressionEvaluator( null, null, executionProperties );
197
198 Object value = ee.evaluate( "${" + sysprop + "}" );
199
200 assertEquals( "value", value );
201 }
202
203 private static MavenSession createSession( PlexusContainer container, ArtifactRepository repo )
204 throws CycleDetectedException, DuplicateProjectException
205 {
206 return new MavenSession( container, new Settings(), repo, new DefaultEventDispatcher(),
207 new ReactorManager( Collections.EMPTY_LIST ), Collections.EMPTY_LIST, ".",
208 new Properties(), new Date() );
209 }
210
211 public void testLocalRepositoryExtraction()
212 throws Exception
213 {
214 ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( createDefaultProject(), null,
215 new Properties() );
216 Object value = expressionEvaluator.evaluate( "${localRepository}" );
217
218 assertEquals( "local", ( (DefaultArtifactRepository) value ).getId() );
219 }
220
221 public void testTwoExpressions()
222 throws Exception
223 {
224 Build build = new Build();
225 build.setDirectory( "expected-directory" );
226 build.setFinalName( "expected-finalName" );
227
228 Model model = new Model();
229 model.setBuild( build );
230
231 ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( new MavenProject( model ), null,
232 new Properties() );
233
234 Object value = expressionEvaluator.evaluate( "${project.build.directory}" + FS + "${project.build.finalName}" );
235
236 assertEquals( "expected-directory" + FS + "expected-finalName", value );
237 }
238
239 public void testShouldExtractPluginArtifacts()
240 throws Exception
241 {
242 PluginDescriptor pd = new PluginDescriptor();
243
244 Artifact artifact = createArtifact( "testGroup", "testArtifact", "1.0" );
245
246 pd.setArtifacts( Collections.singletonList( artifact ) );
247
248 ExpressionEvaluator ee = createExpressionEvaluator( createDefaultProject(), pd, new Properties() );
249
250 Object value = ee.evaluate( "${plugin.artifacts}" );
251
252 assertTrue( value instanceof List );
253
254 List artifacts = (List) value;
255
256 assertEquals( 1, artifacts.size() );
257
258 Artifact result = (Artifact) artifacts.get( 0 );
259
260 assertEquals( "testGroup", result.getGroupId() );
261 }
262
263 private MavenProject createDefaultProject()
264 {
265 return new MavenProject( new Model() );
266 }
267
268 private ExpressionEvaluator createExpressionEvaluator( MavenProject project, PluginDescriptor pluginDescriptor,
269 Properties executionProperties )
270 throws Exception
271 {
272 ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
273 "legacy" );
274
275 ArtifactRepository repo = new DefaultArtifactRepository( "local", "target/repo", repoLayout );
276
277 PlexusContainer container = getContainer();
278 MavenSession session = createSession( container, repo );
279
280 MojoDescriptor mojo = new MojoDescriptor();
281 mojo.setPluginDescriptor( pluginDescriptor );
282 mojo.setGoal( "goal" );
283
284 MojoExecution mojoExecution = new MojoExecution( mojo );
285
286 return new PluginParameterExpressionEvaluator( session, mojoExecution, null, container.getLogger(), project,
287 executionProperties );
288 }
289
290 protected Artifact createArtifact( String groupId, String artifactId, String version )
291 throws Exception
292 {
293 ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
294
295
296 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
297 }
298
299 }