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