KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > plugin > PluginParameterExpressionEvaluatorTest


1 package org.apache.maven.plugin;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 import org.apache.maven.artifact.Artifact;
21 import org.apache.maven.artifact.factory.ArtifactFactory;
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.artifact.repository.DefaultArtifactRepository;
24 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
25 import org.apache.maven.execution.MavenSession;
26 import org.apache.maven.execution.ReactorManager;
27 import org.apache.maven.model.Build;
28 import org.apache.maven.model.Model;
29 import org.apache.maven.monitor.event.DefaultEventDispatcher;
30 import org.apache.maven.plugin.descriptor.MojoDescriptor;
31 import org.apache.maven.plugin.descriptor.PluginDescriptor;
32 import org.apache.maven.project.DuplicateProjectException;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.maven.settings.Settings;
35 import org.codehaus.plexus.PlexusContainer;
36 import org.codehaus.plexus.PlexusTestCase;
37 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
38 import org.codehaus.plexus.util.dag.CycleDetectedException;
39
40 import java.io.File JavaDoc;
41 import java.util.Collections JavaDoc;
42 import java.util.Date JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Properties JavaDoc;
45
46 /**
47  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl </a>
48  * @version $Id: PluginParameterExpressionEvaluatorTest.java,v 1.5 2005/03/08
49  * 06:06:21 jdcasey Exp $
50  */

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