View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.interpolation;
20  
21  import java.io.IOException;
22  import java.io.StringReader;
23  import java.util.List;
24  import java.util.Properties;
25  
26  import org.apache.maven.model.Build;
27  import org.apache.maven.model.Model;
28  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
29  import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
30  import org.apache.maven.plugins.assembly.io.AssemblyReadException;
31  import org.apache.maven.plugins.assembly.io.DefaultAssemblyReader;
32  import org.apache.maven.plugins.assembly.io.DefaultAssemblyReaderTest;
33  import org.apache.maven.plugins.assembly.model.Assembly;
34  import org.apache.maven.plugins.assembly.model.DependencySet;
35  import org.apache.maven.plugins.assembly.testutils.PojoConfigSource;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
38  import org.codehaus.plexus.interpolation.fixed.PropertiesBasedValueSource;
39  import org.junit.Test;
40  import org.junit.runner.RunWith;
41  import org.mockito.junit.MockitoJUnitRunner;
42  
43  import static org.junit.Assert.assertEquals;
44  import static org.mockito.Mockito.mock;
45  import static org.mockito.Mockito.when;
46  
47  @RunWith(MockitoJUnitRunner.class)
48  public class AssemblyInterpolatorTest {
49      @Test
50      public void testDependencySetOutputFileNameMappingsAreNotInterpolated()
51              throws IOException, AssemblyInterpolationException, AssemblyReadException,
52                      InvalidAssemblerConfigurationException {
53          final Model model = new Model();
54          model.setArtifactId("artifact-id");
55          model.setGroupId("group.id");
56          model.setVersion("1");
57          model.setPackaging("jar");
58  
59          final MavenProject project = new MavenProject(model);
60  
61          final Assembly assembly = new Assembly();
62  
63          // artifactId is blacklisted, but packaging is not.
64          final String outputFileNameMapping = "${artifactId}.${packaging}";
65  
66          final DependencySet set = new DependencySet();
67          set.setOutputFileNameMapping(outputFileNameMapping);
68  
69          assembly.addDependencySet(set);
70  
71          final PojoConfigSource configSourceStub = new PojoConfigSource();
72  
73          configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
74          configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
75  
76          configSourceStub.setMavenProject(project);
77          final Assembly outputAssembly = roundTripInterpolation(assembly, configSourceStub);
78  
79          final List<DependencySet> outputDependencySets = outputAssembly.getDependencySets();
80          assertEquals(1, outputDependencySets.size());
81  
82          final DependencySet outputSet = outputDependencySets.get(0);
83  
84          assertEquals("${artifactId}.${packaging}", outputSet.getOutputFileNameMapping());
85      }
86  
87      @Test
88      public void testDependencySetOutputDirectoryIsNotInterpolated()
89              throws IOException, AssemblyInterpolationException, AssemblyReadException,
90                      InvalidAssemblerConfigurationException {
91          final Model model = new Model();
92          model.setArtifactId("artifact-id");
93          model.setGroupId("group.id");
94          model.setVersion("1");
95          model.setPackaging("jar");
96  
97          final Assembly assembly = new Assembly();
98  
99          final String outputDirectory = "${artifactId}.${packaging}";
100 
101         final DependencySet set = new DependencySet();
102         set.setOutputDirectory(outputDirectory);
103 
104         assembly.addDependencySet(set);
105 
106         final PojoConfigSource configSourceStub = new PojoConfigSource();
107 
108         configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
109         configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
110 
111         final MavenProject project = new MavenProject(model);
112         configSourceStub.setMavenProject(project);
113         final Assembly outputAssembly = roundTripInterpolation(assembly, configSourceStub);
114 
115         final List<DependencySet> outputDependencySets = outputAssembly.getDependencySets();
116         assertEquals(1, outputDependencySets.size());
117 
118         final DependencySet outputSet = outputDependencySets.get(0);
119 
120         assertEquals("${artifactId}.${packaging}", outputSet.getOutputDirectory());
121     }
122 
123     private Assembly roundTripInterpolation(Assembly assembly, AssemblerConfigurationSource configSource)
124             throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException {
125         final StringReader stringReader = DefaultAssemblyReaderTest.writeToStringReader(assembly);
126         return new DefaultAssemblyReader().readAssembly(stringReader, "testLocation", null, configSource);
127     }
128 
129     @Test
130     public void testShouldResolveModelGroupIdInAssemblyId()
131             throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
132                     IOException {
133         final Model model = new Model();
134         model.setArtifactId("artifact-id");
135         model.setGroupId("group.id");
136         model.setVersion("1");
137         model.setPackaging("jar");
138 
139         final Assembly assembly = new Assembly();
140 
141         assembly.setId("assembly.${groupId}");
142 
143         final MavenProject project = new MavenProject(model);
144         final PojoConfigSource configSourceStub = new PojoConfigSource();
145 
146         configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
147         configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
148         configSourceStub.setMavenProject(project);
149         final Assembly outputAssembly = roundTripInterpolation(assembly, configSourceStub);
150         assertEquals("assembly.group.id", outputAssembly.getId());
151     }
152 
153     @Test
154     public void testShouldResolveModelPropertyBeforeModelGroupIdInAssemblyId()
155             throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
156                     IOException {
157         final Model model = new Model();
158         model.setArtifactId("artifact-id");
159         model.setGroupId("group.id");
160         model.setVersion("1");
161         model.setPackaging("jar");
162 
163         final Properties props = new Properties();
164         props.setProperty("groupId", "other.id");
165 
166         model.setProperties(props);
167 
168         final PojoConfigSource configSourceStub = new PojoConfigSource();
169 
170         configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
171         configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
172 
173         final Assembly assembly = new Assembly();
174 
175         assembly.setId("assembly.${groupId}");
176 
177         final MavenProject project = new MavenProject(model);
178         configSourceStub.setMavenProject(project);
179         final Assembly result = roundTripInterpolation(assembly, configSourceStub);
180 
181         assertEquals("assembly.other.id", result.getId());
182     }
183 
184     @Test
185     public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId() throws Exception {
186         final Model model = new Model();
187         model.setArtifactId("artifact-id");
188         model.setGroupId("group.id");
189         model.setVersion("1");
190         model.setPackaging("jar");
191 
192         final Properties props = new Properties();
193         props.setProperty("groupId", "other.id");
194 
195         model.setProperties(props);
196 
197         final Assembly assembly = new Assembly();
198 
199         assembly.setId("assembly.${groupId}");
200 
201         final Properties execProps = new Properties();
202         execProps.setProperty("groupId", "still.another.id");
203 
204         final AssemblerConfigurationSource cs = mock(AssemblerConfigurationSource.class);
205         when(cs.getRepositoryInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
206         when(cs.getCommandLinePropsInterpolator())
207                 .thenReturn(FixedStringSearchInterpolator.create(new PropertiesBasedValueSource(execProps)));
208         when(cs.getEnvInterpolator()).thenReturn(FixedStringSearchInterpolator.empty());
209 
210         final MavenProject project = new MavenProject(model);
211         when(cs.getProject()).thenReturn(project);
212 
213         final Assembly result = roundTripInterpolation(assembly, cs);
214 
215         assertEquals("assembly.still.another.id", result.getId());
216     }
217 
218     @Test
219     public void testShouldNotTouchUnresolvedExpression()
220             throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
221                     IOException {
222         final Model model = new Model();
223         model.setArtifactId("artifact-id");
224         model.setGroupId("group.id");
225         model.setVersion("1");
226         model.setPackaging("jar");
227 
228         final Assembly assembly = new Assembly();
229 
230         assembly.setId("assembly.${unresolved}");
231 
232         final PojoConfigSource configSourceStub = new PojoConfigSource();
233 
234         configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
235         configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
236 
237         final MavenProject project = new MavenProject(model);
238         configSourceStub.setMavenProject(project);
239         final Assembly result = roundTripInterpolation(assembly, configSourceStub);
240         assertEquals("assembly.${unresolved}", result.getId());
241     }
242 
243     @Test
244     public void testShouldInterpolateMultiDotProjectExpression()
245             throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
246                     IOException {
247         final Build build = new Build();
248         build.setFinalName("final-name");
249 
250         final Model model = new Model();
251         model.setBuild(build);
252 
253         final Assembly assembly = new Assembly();
254 
255         assembly.setId("assembly.${project.build.finalName}");
256 
257         final PojoConfigSource configSourceStub = new PojoConfigSource();
258 
259         configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
260         configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
261 
262         final MavenProject project = new MavenProject(model);
263         configSourceStub.setMavenProject(project);
264         final Assembly result = roundTripInterpolation(assembly, configSourceStub);
265         assertEquals("assembly.final-name", result.getId());
266     }
267 }