1 package org.apache.maven.plugin; 2 3 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 ; 41 import java.util.Collections ; 42 import java.util.Date ; 43 import java.util.List ; 44 import java.util.Properties ; 45 46 51 public class PluginParameterExpressionEvaluatorTest 52 extends PlexusTestCase 53 { 54 public void testValueExtractionWithAPomValueContainingAPath() 55 throws Exception 56 { 57 String 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 ( "pom.xml" ).getCanonicalFile() ); 67 68 ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( project, null, new Properties () ); 69 70 Object value = expressionEvaluator.evaluate( "${project.build.directory}/classes" ); 71 String actual = new File ( value.toString() ).getCanonicalPath(); 72 73 assertEquals( expected, actual ); 74 } 75 76 public void testEscapedVariablePassthrough() 77 throws Exception 78 { 79 String 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 () ); 87 88 Object value = ee.evaluate( "$" + var ); 89 90 assertEquals( var, value ); 91 } 92 93 public void testEscapedVariablePassthroughInLargerExpression() 94 throws Exception 95 { 96 String var = "${var}"; 97 String 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 () ); 105 106 Object value = ee.evaluate( "$" + key ); 107 108 assertEquals( "${var} with version: 1", value ); 109 } 110 111 public void testMultipleSubExpressionsInLargerExpression() 112 throws Exception 113 { 114 String 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 () ); 123 124 Object value = ee.evaluate( key ); 125 126 assertEquals( "test with version: 1", value ); 127 } 128 129 public void testPOMPropertyExtractionWithMissingProject_WithDotNotation() 130 throws Exception 131 { 132 String key = "m2.name"; 133 String checkValue = "value"; 134 135 Properties properties = new Properties (); 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 () ); 144 145 Object value = ee.evaluate( "${" + key + "}" ); 146 147 assertEquals( checkValue, value ); 148 } 149 150 public void testBasedirExtractionWithMissingProject() 151 throws Exception 152 { 153 ExpressionEvaluator ee = createExpressionEvaluator( null, null, new Properties () ); 154 155 Object value = ee.evaluate( "${basedir}" ); 156 157 assertEquals( System.getProperty( "user.dir" ), value ); 158 } 159 160 public void testValueExtractionFromSystemPropertiesWithMissingProject() 161 throws Exception 162 { 163 String sysprop = "PPEET_sysprop1"; 164 165 Properties executionProperties = new Properties (); 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 value = ee.evaluate( "${" + sysprop + "}" ); 175 176 assertEquals( "value", value ); 177 } 178 179 public void testValueExtractionFromSystemPropertiesWithMissingProject_WithDotNotation() 180 throws Exception 181 { 182 String sysprop = "PPEET.sysprop2"; 183 184 Properties executionProperties = new Properties (); 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 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 (), new Date () ); 204 } 205 206 public void testLocalRepositoryExtraction() 207 throws Exception 208 { 209 ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( createDefaultProject(), null, 210 new Properties () ); 211 Object value = expressionEvaluator.evaluate( "${localRepository}" ); 212 213 assertEquals( "local", ( (DefaultArtifactRepository) value ).getId() ); 214 } 215 216 public void testTwoExpressions() 217 throws Exception 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 () ); 228 229 Object 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 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 () ); 244 245 Object value = ee.evaluate( "${plugin.artifacts}" ); 246 247 assertTrue( value instanceof List ); 248 249 List artifacts = (List ) 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 executionProperties ) 265 throws Exception 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 groupId, String artifactId, String version ) 286 throws Exception 287 { 288 ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); 289 290 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); 292 } 293 294 } 295 | Popular Tags |