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 org.apache.maven.artifact.repository.ArtifactRepository;
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.model.Build;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
27  import org.apache.maven.plugin.assembly.model.Assembly;
28  import org.apache.maven.plugin.assembly.model.DependencySet;
29  import org.apache.maven.plugin.assembly.testutils.ConfigSourceStub;
30  import org.apache.maven.plugin.assembly.testutils.MockManager;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.logging.Logger;
33  import org.codehaus.plexus.logging.console.ConsoleLogger;
34  import org.easymock.MockControl;
35  import org.easymock.classextension.MockClassControl;
36  
37  import java.io.IOException;
38  import java.util.List;
39  import java.util.Properties;
40  
41  import junit.framework.TestCase;
42  
43  public class AssemblyInterpolatorTest
44      extends TestCase
45  {
46  
47      private AssemblyInterpolator interpolator;
48  
49      private final AssemblerConfigurationSource configSourceStub = new ConfigSourceStub();
50  
51      @Override
52      public void setUp()
53          throws IOException
54      {
55          interpolator = new AssemblyInterpolator();
56  
57          interpolator.enableLogging( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
58      }
59  
60      public void testDependencySetOutputFileNameMappingsAreNotInterpolated()
61          throws IOException, AssemblyInterpolationException
62      {
63          final Model model = new Model();
64          model.setArtifactId( "artifact-id" );
65          model.setGroupId( "group.id" );
66          model.setVersion( "1" );
67          model.setPackaging( "jar" );
68  
69          final MavenProject project = new MavenProject( model );
70  
71          final Assembly assembly = new Assembly();
72  
73          // artifactId is blacklisted, but packaging is not.
74          final String outputFileNameMapping = "${artifactId}.${packaging}";
75  
76          final DependencySet set = new DependencySet();
77          set.setOutputFileNameMapping( outputFileNameMapping );
78  
79          assembly.addDependencySet( set );
80  
81          final Assembly outputAssembly = interpolator.interpolate( assembly, project, configSourceStub );
82  
83          final List<DependencySet> outputDependencySets = outputAssembly.getDependencySets();
84          assertEquals( 1, outputDependencySets.size() );
85  
86          final DependencySet outputSet = outputDependencySets.get( 0 );
87  
88          assertEquals( "${artifactId}.${packaging}", outputSet.getOutputFileNameMapping() );
89      }
90  
91      public void testDependencySetOutputDirectoryIsNotInterpolated()
92          throws IOException, AssemblyInterpolationException
93      {
94          final Model model = new Model();
95          model.setArtifactId( "artifact-id" );
96          model.setGroupId( "group.id" );
97          model.setVersion( "1" );
98          model.setPackaging( "jar" );
99  
100         final Assembly assembly = new Assembly();
101 
102         final String outputDirectory = "${artifactId}.${packaging}";
103 
104         final DependencySet set = new DependencySet();
105         set.setOutputDirectory( outputDirectory );
106 
107         assembly.addDependencySet( set );
108 
109         final Assembly outputAssembly =
110             interpolator.interpolate( assembly, new MavenProject( model ), configSourceStub );
111 
112         final List<DependencySet> outputDependencySets = outputAssembly.getDependencySets();
113         assertEquals( 1, outputDependencySets.size() );
114 
115         final DependencySet outputSet = outputDependencySets.get( 0 );
116 
117         assertEquals( "${artifactId}.${packaging}", outputSet.getOutputDirectory() );
118     }
119 
120     public void testShouldResolveModelGroupIdInAssemblyId()
121         throws AssemblyInterpolationException
122     {
123         final Model model = new Model();
124         model.setArtifactId( "artifact-id" );
125         model.setGroupId( "group.id" );
126         model.setVersion( "1" );
127         model.setPackaging( "jar" );
128 
129         final Assembly assembly = new Assembly();
130 
131         assembly.setId( "assembly.${groupId}" );
132 
133         final Assembly result = interpolator.interpolate( assembly, new MavenProject( model ), configSourceStub );
134 
135         assertEquals( "assembly.group.id", result.getId() );
136     }
137 
138     public void testShouldResolveModelPropertyBeforeModelGroupIdInAssemblyId()
139         throws AssemblyInterpolationException
140     {
141         final Model model = new Model();
142         model.setArtifactId( "artifact-id" );
143         model.setGroupId( "group.id" );
144         model.setVersion( "1" );
145         model.setPackaging( "jar" );
146 
147         final Properties props = new Properties();
148         props.setProperty( "groupId", "other.id" );
149 
150         model.setProperties( props );
151 
152         final Assembly assembly = new Assembly();
153 
154         assembly.setId( "assembly.${groupId}" );
155 
156         final Assembly result = interpolator.interpolate( assembly, new MavenProject( model ), configSourceStub );
157 
158         assertEquals( "assembly.other.id", result.getId() );
159     }
160 
161     public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId()
162         throws AssemblyInterpolationException
163     {
164         final Model model = new Model();
165         model.setArtifactId( "artifact-id" );
166         model.setGroupId( "group.id" );
167         model.setVersion( "1" );
168         model.setPackaging( "jar" );
169 
170         final Properties props = new Properties();
171         props.setProperty( "groupId", "other.id" );
172 
173         model.setProperties( props );
174 
175         final Assembly assembly = new Assembly();
176 
177         assembly.setId( "assembly.${groupId}" );
178 
179         final MockManager mm = new MockManager();
180 
181         final MockControl sessionControl = MockClassControl.createControl( MavenSession.class );
182         final MavenSession session = (MavenSession) sessionControl.getMock();
183 
184         mm.add( sessionControl );
185 
186         final Properties execProps = new Properties();
187         execProps.setProperty( "groupId", "still.another.id" );
188 
189         session.getExecutionProperties();
190         sessionControl.setReturnValue( execProps, MockControl.ZERO_OR_MORE );
191 
192         session.getUserProperties();
193         sessionControl.setReturnValue( new Properties(), MockControl.ZERO_OR_MORE );
194 
195         final MockControl csControl = MockControl.createControl( AssemblerConfigurationSource.class );
196         final AssemblerConfigurationSource cs = (AssemblerConfigurationSource) csControl.getMock();
197 
198         mm.add( csControl );
199 
200         final MockControl lrCtl = MockControl.createControl( ArtifactRepository.class );
201         final ArtifactRepository lr = (ArtifactRepository) lrCtl.getMock();
202         mm.add( lrCtl );
203 
204         lr.getBasedir();
205         lrCtl.setReturnValue( "/path/to/local/repo", MockControl.ZERO_OR_MORE );
206 
207         cs.getLocalRepository();
208         csControl.setReturnValue( lr, MockControl.ZERO_OR_MORE );
209 
210         cs.getMavenSession();
211         csControl.setReturnValue( session, MockControl.ZERO_OR_MORE );
212 
213         mm.replayAll();
214 
215         final Assembly result = interpolator.interpolate( assembly, new MavenProject( model ), cs );
216 
217         assertEquals( "assembly.still.another.id", result.getId() );
218 
219         mm.verifyAll();
220         mm.clear();
221     }
222 
223     public void testShouldNotTouchUnresolvedExpression()
224         throws AssemblyInterpolationException
225     {
226         final Model model = new Model();
227         model.setArtifactId( "artifact-id" );
228         model.setGroupId( "group.id" );
229         model.setVersion( "1" );
230         model.setPackaging( "jar" );
231 
232         final Assembly assembly = new Assembly();
233 
234         assembly.setId( "assembly.${unresolved}" );
235 
236         final Assembly result = interpolator.interpolate( assembly, new MavenProject( model ), configSourceStub );
237 
238         assertEquals( "assembly.${unresolved}", result.getId() );
239     }
240 
241     public void testShouldInterpolateMultiDotProjectExpression()
242         throws AssemblyInterpolationException
243     {
244         final Build build = new Build();
245         build.setFinalName( "final-name" );
246 
247         final Model model = new Model();
248         model.setBuild( build );
249 
250         final Assembly assembly = new Assembly();
251 
252         assembly.setId( "assembly.${project.build.finalName}" );
253 
254         final Assembly result = interpolator.interpolate( assembly, new MavenProject( model ), configSourceStub );
255 
256         assertEquals( "assembly.final-name", result.getId() );
257     }
258 
259 }