View Javadoc
1   package org.apache.maven.plugins.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.plugins.assembly.AssemblerConfigurationSource;
28  import org.apache.maven.plugins.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          final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${groupId}" );
59  
60          assertEquals( "assembly.group.id", result );
61      }
62  
63      private void setupInterpolation()
64      {
65          configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
66          configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
67          configSourceStub.setEnvInterpolator( FixedStringSearchInterpolator.create() );
68  
69      }
70  
71      public void testShouldResolveModelPropertyBeforeModelGroupId()
72          throws ExpressionEvaluationException
73      {
74          final Model model = new Model();
75          model.setArtifactId( "artifact-id" );
76          model.setGroupId( "group.id" );
77          model.setVersion( "1" );
78          model.setPackaging( "jar" );
79  
80          final Properties props = new Properties();
81          props.setProperty( "groupId", "other.id" );
82  
83          model.setProperties( props );
84  
85          configSourceStub.setMavenProject( new MavenProject( model ) );
86          setupInterpolation();
87  
88          final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${groupId}" );
89  
90          assertEquals( "assembly.other.id", result );
91      }
92  
93      public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId()
94          throws ExpressionEvaluationException
95      {
96          final Model model = new Model();
97          model.setArtifactId( "artifact-id" );
98          model.setGroupId( "group.id" );
99          model.setVersion( "1" );
100         model.setPackaging( "jar" );
101 
102         final Properties props = new Properties();
103         props.setProperty( "groupId", "other.id" );
104 
105         model.setProperties( props );
106 
107         final EasyMockSupport mm = new EasyMockSupport();
108 
109         MavenSession session = mm.createControl().createMock( MavenSession.class );
110 
111         final Properties execProps = new Properties();
112         execProps.setProperty( "groupId", "still.another.id" );
113 
114         PropertiesBasedValueSource cliProps = new PropertiesBasedValueSource( execProps );
115         expect( session.getExecutionProperties() ).andReturn( execProps ).anyTimes();
116         expect( session.getUserProperties() ).andReturn( new Properties() ).anyTimes();
117 
118         AssemblerConfigurationSource cs = mm.createControl().createMock( AssemblerConfigurationSource.class );
119         expect( cs.getCommandLinePropsInterpolator() ).andReturn(
120             FixedStringSearchInterpolator.create( cliProps ) ).anyTimes();
121         expect( cs.getRepositoryInterpolator() ).andReturn( FixedStringSearchInterpolator.create() ).anyTimes();
122         expect( cs.getEnvInterpolator() ).andReturn( FixedStringSearchInterpolator.create() ).anyTimes();
123 
124         expect( cs.getMavenSession() ).andReturn( session ).anyTimes();
125         expect( cs.getProject() ).andReturn( new MavenProject( model ) );
126 
127         final IMocksControl lrCtl = mm.createControl();
128         final ArtifactRepository lr = lrCtl.createMock( ArtifactRepository.class );
129 
130         expect( lr.getBasedir() ).andReturn( "/path/to/local/repo" ).anyTimes();
131         expect( cs.getLocalRepository() ).andReturn( lr ).anyTimes();
132 
133         mm.replayAll();
134 
135         final Object result = new AssemblyExpressionEvaluator( cs ).evaluate( "assembly.${groupId}" );
136 
137         assertEquals( "assembly.still.another.id", result );
138 
139         mm.verifyAll();
140     }
141 
142     public void testShouldReturnUnchangedInputForUnresolvedExpression()
143         throws ExpressionEvaluationException
144     {
145         final Model model = new Model();
146         model.setArtifactId( "artifact-id" );
147         model.setGroupId( "group.id" );
148         model.setVersion( "1" );
149         model.setPackaging( "jar" );
150 
151         configSourceStub.setMavenProject( new MavenProject( model ) );
152         setupInterpolation();
153 
154         final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${unresolved}" );
155 
156         assertEquals( "assembly.${unresolved}", result );
157     }
158 
159     public void testShouldInterpolateMultiDotProjectExpression()
160         throws ExpressionEvaluationException
161     {
162         final Build build = new Build();
163         build.setFinalName( "final-name" );
164 
165         final Model model = new Model();
166         model.setBuild( build );
167 
168         configSourceStub.setMavenProject( new MavenProject( model ) );
169         setupInterpolation();
170 
171         final Object result =
172             new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${project.build.finalName}" );
173 
174         assertEquals( "assembly.final-name", result );
175     }
176 
177 }