View Javadoc
1   package org.apache.maven.plugin.assembly.interpolation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.model.Build;
26  import org.apache.maven.model.Model;
27  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
28  import org.apache.maven.plugin.assembly.testutils.PojoConfigSource;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
31  import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
32  import org.codehaus.plexus.interpolation.fixed.PropertiesBasedValueSource;
33  import org.easymock.classextension.EasyMockSupport;
34  import org.easymock.classextension.IMocksControl;
35  
36  import java.util.Properties;
37  
38  import static org.easymock.EasyMock.expect;
39  
40  public class AssemblyExpressionEvaluatorTest
41      extends TestCase
42  {
43  
44      private final PojoConfigSource configSourceStub = new PojoConfigSource();
45  
46      public void testShouldResolveModelGroupId()
47          throws ExpressionEvaluationException
48      {
49          final Model model = new Model();
50          model.setArtifactId( "artifact-id" );
51          model.setGroupId( "group.id" );
52          model.setVersion( "1" );
53          model.setPackaging( "jar" );
54  
55          configSourceStub.setMavenProject( new MavenProject( model ) );
56          setupInterpolation();
57  
58  
59  
60          final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${groupId}" );
61  
62          assertEquals( "assembly.group.id", result );
63      }
64  
65      private void setupInterpolation()
66      {
67          configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
68          configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
69          configSourceStub.setEnvInterpolator( FixedStringSearchInterpolator.create() );
70  
71      }
72  
73      public void testShouldResolveModelPropertyBeforeModelGroupId()
74          throws ExpressionEvaluationException
75      {
76          final Model model = new Model();
77          model.setArtifactId( "artifact-id" );
78          model.setGroupId( "group.id" );
79          model.setVersion( "1" );
80          model.setPackaging( "jar" );
81  
82          final Properties props = new Properties();
83          props.setProperty( "groupId", "other.id" );
84  
85          model.setProperties( props );
86  
87          configSourceStub.setMavenProject( new MavenProject( model ) );
88          setupInterpolation();
89  
90          final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${groupId}" );
91  
92          assertEquals( "assembly.other.id", result );
93      }
94  
95      public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId()
96          throws ExpressionEvaluationException
97      {
98          final Model model = new Model();
99          model.setArtifactId( "artifact-id" );
100         model.setGroupId( "group.id" );
101         model.setVersion( "1" );
102         model.setPackaging( "jar" );
103 
104         final Properties props = new Properties();
105         props.setProperty( "groupId", "other.id" );
106 
107         model.setProperties( props );
108 
109         final EasyMockSupport mm = new EasyMockSupport();
110 
111         MavenSession session = mm.createControl().createMock( MavenSession.class );
112 
113         final Properties execProps = new Properties();
114         execProps.setProperty( "groupId", "still.another.id" );
115 
116         PropertiesBasedValueSource cliProps = new PropertiesBasedValueSource( execProps );
117         expect( session.getExecutionProperties()).andReturn( execProps ).anyTimes();
118         expect( session.getUserProperties()).andReturn( new Properties() ).anyTimes();
119 
120 
121 
122 
123         AssemblerConfigurationSource cs = mm.createControl().createMock( AssemblerConfigurationSource.class );
124         expect( cs.getCommandLinePropsInterpolator() ).andReturn( FixedStringSearchInterpolator.create(cliProps) ).anyTimes();
125         expect( cs.getRepositoryInterpolator() ).andReturn( FixedStringSearchInterpolator.create() ).anyTimes();
126         expect( cs.getEnvInterpolator() ).andReturn( FixedStringSearchInterpolator.create() ).anyTimes();
127 
128         expect( cs.getMavenSession() ).andReturn( session ).anyTimes();
129         expect( cs.getProject() ).andReturn( new MavenProject( model ) );
130 
131         final IMocksControl lrCtl = mm.createControl();
132         final ArtifactRepository lr = lrCtl.createMock( ArtifactRepository.class );
133 
134         expect( lr.getBasedir()).andReturn( "/path/to/local/repo" ).anyTimes();
135         expect( cs.getLocalRepository()).andReturn( lr ).anyTimes();
136 
137         mm.replayAll();
138 
139         final Object result = new AssemblyExpressionEvaluator( cs ).evaluate( "assembly.${groupId}" );
140 
141         assertEquals( "assembly.still.another.id", result );
142 
143         mm.verifyAll();
144     }
145 
146     public void testShouldReturnUnchangedInputForUnresolvedExpression()
147         throws ExpressionEvaluationException
148     {
149         final Model model = new Model();
150         model.setArtifactId( "artifact-id" );
151         model.setGroupId( "group.id" );
152         model.setVersion( "1" );
153         model.setPackaging( "jar" );
154 
155         configSourceStub.setMavenProject( new MavenProject( model ) );
156         setupInterpolation();
157 
158         final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${unresolved}" );
159 
160         assertEquals( "assembly.${unresolved}", result );
161     }
162 
163     public void testShouldInterpolateMultiDotProjectExpression()
164         throws ExpressionEvaluationException
165     {
166         final Build build = new Build();
167         build.setFinalName( "final-name" );
168 
169         final Model model = new Model();
170         model.setBuild( build );
171 
172         configSourceStub.setMavenProject( new MavenProject( model ) );
173         setupInterpolation();
174 
175 
176         final Object result =
177             new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${project.build.finalName}" );
178 
179         assertEquals( "assembly.final-name", result );
180     }
181 
182 }