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.jupiter.api.Test;
40  import org.junit.jupiter.api.extension.ExtendWith;
41  import org.mockito.junit.jupiter.MockitoExtension;
42  import org.mockito.junit.jupiter.MockitoSettings;
43  import org.mockito.quality.Strictness;
44  
45  import static org.junit.jupiter.api.Assertions.assertEquals;
46  import static org.mockito.Mockito.mock;
47  import static org.mockito.Mockito.when;
48  
49  @MockitoSettings(strictness = Strictness.WARN)
50  @ExtendWith(MockitoExtension.class)
51  public class AssemblyInterpolatorTest {
52      @Test
53      public void testDependencySetOutputFileNameMappingsAreNotInterpolated()
54              throws IOException, AssemblyInterpolationException, AssemblyReadException,
55                      InvalidAssemblerConfigurationException {
56          final Model model = new Model();
57          model.setArtifactId("artifact-id");
58          model.setGroupId("group.id");
59          model.setVersion("1");
60          model.setPackaging("jar");
61  
62          final MavenProject project = new MavenProject(model);
63  
64          final Assembly assembly = new Assembly();
65  
66          // artifactId is blacklisted, but packaging is not.
67          final String outputFileNameMapping = "${artifactId}.${packaging}";
68  
69          final DependencySet set = new DependencySet();
70          set.setOutputFileNameMapping(outputFileNameMapping);
71  
72          assembly.addDependencySet(set);
73  
74          final PojoConfigSource configSourceStub = new PojoConfigSource();
75  
76          configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
77          configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
78  
79          configSourceStub.setMavenProject(project);
80          final Assembly outputAssembly = roundTripInterpolation(assembly, configSourceStub);
81  
82          final List<DependencySet> outputDependencySets = outputAssembly.getDependencySets();
83          assertEquals(1, outputDependencySets.size());
84  
85          final DependencySet outputSet = outputDependencySets.get(0);
86  
87          assertEquals("${artifactId}.${packaging}", outputSet.getOutputFileNameMapping());
88      }
89  
90      @Test
91      public void testDependencySetOutputDirectoryIsNotInterpolated()
92              throws IOException, AssemblyInterpolationException, AssemblyReadException,
93                      InvalidAssemblerConfigurationException {
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 PojoConfigSource configSourceStub = new PojoConfigSource();
110 
111         configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
112         configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
113 
114         final MavenProject project = new MavenProject(model);
115         configSourceStub.setMavenProject(project);
116         final Assembly outputAssembly = roundTripInterpolation(assembly, configSourceStub);
117 
118         final List<DependencySet> outputDependencySets = outputAssembly.getDependencySets();
119         assertEquals(1, outputDependencySets.size());
120 
121         final DependencySet outputSet = outputDependencySets.get(0);
122 
123         assertEquals("${artifactId}.${packaging}", outputSet.getOutputDirectory());
124     }
125 
126     private Assembly roundTripInterpolation(Assembly assembly, AssemblerConfigurationSource configSource)
127             throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException {
128         final StringReader stringReader = DefaultAssemblyReaderTest.writeToStringReader(assembly);
129         return new DefaultAssemblyReader().readAssembly(stringReader, "testLocation", null, configSource);
130     }
131 
132     @Test
133     public void testShouldResolveModelGroupIdInAssemblyId()
134             throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
135                     IOException {
136         final Model model = new Model();
137         model.setArtifactId("artifact-id");
138         model.setGroupId("group.id");
139         model.setVersion("1");
140         model.setPackaging("jar");
141 
142         final Assembly assembly = new Assembly();
143 
144         assembly.setId("assembly.${groupId}");
145 
146         final MavenProject project = new MavenProject(model);
147         final PojoConfigSource configSourceStub = new PojoConfigSource();
148 
149         configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
150         configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
151         configSourceStub.setMavenProject(project);
152         final Assembly outputAssembly = roundTripInterpolation(assembly, configSourceStub);
153         assertEquals("assembly.group.id", outputAssembly.getId());
154     }
155 
156     @Test
157     public void testShouldResolveModelPropertyBeforeModelGroupIdInAssemblyId()
158             throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
159                     IOException {
160         final Model model = new Model();
161         model.setArtifactId("artifact-id");
162         model.setGroupId("group.id");
163         model.setVersion("1");
164         model.setPackaging("jar");
165 
166         final Properties props = new Properties();
167         props.setProperty("groupId", "other.id");
168 
169         model.setProperties(props);
170 
171         final PojoConfigSource configSourceStub = new PojoConfigSource();
172 
173         configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
174         configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
175 
176         final Assembly assembly = new Assembly();
177 
178         assembly.setId("assembly.${groupId}");
179 
180         final MavenProject project = new MavenProject(model);
181         configSourceStub.setMavenProject(project);
182         final Assembly result = roundTripInterpolation(assembly, configSourceStub);
183 
184         assertEquals("assembly.other.id", result.getId());
185     }
186 
187     @Test
188     public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId() throws Exception {
189         final Model model = new Model();
190         model.setArtifactId("artifact-id");
191         model.setGroupId("group.id");
192         model.setVersion("1");
193         model.setPackaging("jar");
194 
195         final Properties props = new Properties();
196         props.setProperty("groupId", "other.id");
197 
198         model.setProperties(props);
199 
200         final Assembly assembly = new Assembly();
201 
202         assembly.setId("assembly.${groupId}");
203 
204         final Properties execProps = new Properties();
205         execProps.setProperty("groupId", "still.another.id");
206 
207         final AssemblerConfigurationSource cs = mock(AssemblerConfigurationSource.class);
208         when(cs.getRepositoryInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
209         when(cs.getCommandLinePropsInterpolator())
210                 .thenReturn(FixedStringSearchInterpolator.create(new PropertiesBasedValueSource(execProps)));
211         when(cs.getEnvInterpolator()).thenReturn(FixedStringSearchInterpolator.empty());
212 
213         final MavenProject project = new MavenProject(model);
214         when(cs.getProject()).thenReturn(project);
215 
216         final Assembly result = roundTripInterpolation(assembly, cs);
217 
218         assertEquals("assembly.still.another.id", result.getId());
219     }
220 
221     @Test
222     public void testShouldNotTouchUnresolvedExpression()
223             throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
224                     IOException {
225         final Model model = new Model();
226         model.setArtifactId("artifact-id");
227         model.setGroupId("group.id");
228         model.setVersion("1");
229         model.setPackaging("jar");
230 
231         final Assembly assembly = new Assembly();
232 
233         assembly.setId("assembly.${unresolved}");
234 
235         final PojoConfigSource configSourceStub = new PojoConfigSource();
236 
237         configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
238         configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
239 
240         final MavenProject project = new MavenProject(model);
241         configSourceStub.setMavenProject(project);
242         final Assembly result = roundTripInterpolation(assembly, configSourceStub);
243         assertEquals("assembly.${unresolved}", result.getId());
244     }
245 
246     @Test
247     public void testShouldInterpolateMultiDotProjectExpression()
248             throws AssemblyInterpolationException, InvalidAssemblerConfigurationException, AssemblyReadException,
249                     IOException {
250         final Build build = new Build();
251         build.setFinalName("final-name");
252 
253         final Model model = new Model();
254         model.setBuild(build);
255 
256         final Assembly assembly = new Assembly();
257 
258         assembly.setId("assembly.${project.build.finalName}");
259 
260         final PojoConfigSource configSourceStub = new PojoConfigSource();
261 
262         configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
263         configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
264 
265         final MavenProject project = new MavenProject(model);
266         configSourceStub.setMavenProject(project);
267         final Assembly result = roundTripInterpolation(assembly, configSourceStub);
268         assertEquals("assembly.final-name", result.getId());
269     }
270 }