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.archive;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
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.archive.phase.AssemblyArchiverPhase;
31  import org.apache.maven.plugins.assembly.model.Assembly;
32  import org.apache.maven.plugins.assembly.mojos.AbstractAssemblyMojo;
33  import org.apache.maven.plugins.assembly.testutils.PojoConfigSource;
34  import org.apache.maven.project.MavenProject;
35  import org.codehaus.plexus.DefaultPlexusContainer;
36  import org.codehaus.plexus.PlexusContainer;
37  import org.codehaus.plexus.PlexusContainerException;
38  import org.codehaus.plexus.archiver.Archiver;
39  import org.codehaus.plexus.archiver.ArchiverException;
40  import org.codehaus.plexus.archiver.diags.NoOpArchiver;
41  import org.codehaus.plexus.archiver.manager.ArchiverManager;
42  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
43  import org.codehaus.plexus.archiver.tar.TarArchiver;
44  import org.codehaus.plexus.archiver.tar.TarLongFileMode;
45  import org.codehaus.plexus.archiver.war.WarArchiver;
46  import org.codehaus.plexus.archiver.zip.ZipArchiver;
47  import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
48  import org.junit.Before;
49  import org.junit.Rule;
50  import org.junit.Test;
51  import org.junit.rules.TemporaryFolder;
52  import org.junit.runner.RunWith;
53  import org.mockito.junit.MockitoJUnitRunner;
54  
55  import static org.junit.Assert.assertEquals;
56  import static org.junit.Assert.assertFalse;
57  import static org.junit.Assert.assertNotNull;
58  import static org.junit.Assert.assertNull;
59  import static org.junit.Assert.fail;
60  import static org.mockito.ArgumentMatchers.any;
61  import static org.mockito.ArgumentMatchers.eq;
62  import static org.mockito.Mockito.atLeastOnce;
63  import static org.mockito.Mockito.mock;
64  import static org.mockito.Mockito.times;
65  import static org.mockito.Mockito.verify;
66  import static org.mockito.Mockito.when;
67  
68  @RunWith(MockitoJUnitRunner.class)
69  public class DefaultAssemblyArchiverTest {
70      @Rule
71      public TemporaryFolder temporaryFolder = new TemporaryFolder();
72  
73      private ArchiverManager archiverManager;
74  
75      private PlexusContainer container;
76  
77      public static void setupInterpolators(AssemblerConfigurationSource configSource) {
78          when(configSource.getRepositoryInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
79          when(configSource.getCommandLinePropsInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
80          when(configSource.getEnvInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
81      }
82  
83      public static void setupInterpolators(AssemblerConfigurationSource configSource, MavenProject mavenProject) {
84          when(configSource.getCommandLinePropsInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
85          when(configSource.getEnvInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
86          when(configSource.getMainProjectInterpolator())
87                  .thenReturn(AbstractAssemblyMojo.mainProjectInterpolator(mavenProject));
88      }
89  
90      @Before
91      public void setup() throws PlexusContainerException {
92          this.archiverManager = mock(ArchiverManager.class);
93          this.container = new DefaultPlexusContainer();
94      }
95  
96      @Test(expected = InvalidAssemblerConfigurationException.class)
97      public void failWhenAssemblyIdIsNull() throws Exception {
98          final DefaultAssemblyArchiver archiver = createSubject(Collections.emptyList());
99          archiver.createArchive(new Assembly(), "full-name", "zip", null, null);
100     }
101 
102     @Test
103     public void testCreateArchive() throws Exception {
104         Archiver archiver = mock(Archiver.class);
105 
106         when(archiverManager.getArchiver("zip")).thenReturn(archiver);
107 
108         final AssemblyArchiverPhase phase = mock(AssemblyArchiverPhase.class);
109 
110         final File outDir = temporaryFolder.newFolder("out");
111 
112         final AssemblerConfigurationSource configSource = mock(AssemblerConfigurationSource.class);
113         when(configSource.getTemporaryRootDirectory()).thenReturn(new File(temporaryFolder.getRoot(), "temp"));
114         when(configSource.getOverrideUid()).thenReturn(0);
115         when(configSource.getOverrideUserName()).thenReturn("root");
116         when(configSource.getOverrideGid()).thenReturn(0);
117         when(configSource.getOverrideGroupName()).thenReturn("root");
118         when(configSource.getOutputDirectory()).thenReturn(outDir);
119         when(configSource.getFinalName()).thenReturn("finalName");
120         when(configSource.getWorkingDirectory()).thenReturn(new File("."));
121 
122         final Assembly assembly = new Assembly();
123         assembly.setId("id");
124 
125         final DefaultAssemblyArchiver subject = createSubject(Collections.singletonList(phase));
126 
127         subject.createArchive(assembly, "full-name", "zip", configSource, null);
128 
129         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
130         verify(configSource).getArchiverConfig();
131         verify(configSource).getFinalName();
132         verify(configSource).getOutputDirectory();
133         verify(configSource, atLeastOnce()).getOverrideUid();
134         verify(configSource, atLeastOnce()).getOverrideUserName();
135         verify(configSource, atLeastOnce()).getOverrideGid();
136         verify(configSource, atLeastOnce()).getOverrideGroupName();
137         verify(configSource).getTemporaryRootDirectory();
138         verify(configSource).getWorkingDirectory();
139         verify(configSource).isDryRun();
140         verify(configSource).isIgnoreDirFormatExtensions();
141         verify(configSource).isIgnorePermissions();
142         verify(configSource).isUpdateOnly();
143 
144         verify(phase).execute(eq(assembly), any(Archiver.class), eq(configSource));
145 
146         verify(archiver).createArchive();
147         verify(archiver).setDestFile(new File(outDir, "full-name.zip"));
148         verify(archiver, times(2)).setForced(true);
149         verify(archiver).setIgnorePermissions(false);
150         verify(archiver).setOverrideUid(0);
151         verify(archiver).setOverrideUserName("root");
152         verify(archiver).setOverrideGid(0);
153         verify(archiver).setOverrideGroupName("root");
154 
155         verify(archiverManager).getArchiver("zip");
156     }
157 
158     @Test
159     public void testCreateArchiver_ShouldConfigureArchiver() throws Exception {
160         final TestArchiverWithConfig archiver = new TestArchiverWithConfig();
161 
162         when(archiverManager.getArchiver("dummy")).thenReturn(archiver);
163 
164         final AssemblerConfigurationSource configSource = mock(AssemblerConfigurationSource.class);
165 
166         final String simpleConfig = "value";
167 
168         when(configSource.getArchiverConfig())
169                 .thenReturn("<configuration><simpleConfig>" + simpleConfig + "</simpleConfig></configuration>");
170 
171         final MavenProject project = new MavenProject(new Model());
172 
173         when(configSource.getProject()).thenReturn(project);
174         when(configSource.getWorkingDirectory()).thenReturn(new File("."));
175 
176         when(configSource.isIgnorePermissions()).thenReturn(true);
177         setupInterpolators(configSource);
178 
179         when(configSource.getOverrideUid()).thenReturn(0);
180         when(configSource.getOverrideUserName()).thenReturn("root");
181         when(configSource.getOverrideGid()).thenReturn(0);
182         when(configSource.getOverrideGroupName()).thenReturn("root");
183 
184         final DefaultAssemblyArchiver subject = createSubject(new ArrayList<>());
185 
186         subject.createArchiver("dummy", false, "finalName", configSource, null, null);
187 
188         assertEquals(simpleConfig, archiver.getSimpleConfig());
189 
190         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
191         verify(archiverManager).getArchiver("dummy");
192     }
193 
194     @Test
195     public void testCreateArchiver_ShouldCreateTarArchiverWithNoCompression() throws Exception {
196         final TestTarArchiver ttArchiver = new TestTarArchiver();
197 
198         when(archiverManager.getArchiver("tar")).thenReturn(ttArchiver);
199 
200         final AssemblerConfigurationSource configSource = mock(AssemblerConfigurationSource.class);
201         when(configSource.getTarLongFileMode()).thenReturn(TarLongFileMode.fail.toString());
202         when(configSource.getWorkingDirectory()).thenReturn(new File("."));
203         when(configSource.isIgnorePermissions()).thenReturn(true);
204         when(configSource.getOverrideUid()).thenReturn(0);
205         when(configSource.getOverrideUserName()).thenReturn("root");
206         when(configSource.getOverrideGid()).thenReturn(0);
207         when(configSource.getOverrideGroupName()).thenReturn("root");
208 
209         final DefaultAssemblyArchiver subject = createSubject(new ArrayList<>());
210 
211         subject.createArchiver("tar", false, "finalName", configSource, null, null);
212 
213         assertNull(ttArchiver.compressionMethod);
214         assertEquals(TarLongFileMode.fail, ttArchiver.longFileMode);
215 
216         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
217         verify(configSource).getArchiverConfig();
218         verify(configSource, times(2)).getOverrideGid();
219         verify(configSource, times(2)).getOverrideGroupName();
220         verify(configSource, times(2)).getOverrideUid();
221         verify(configSource, times(2)).getOverrideUserName();
222         verify(configSource).getTarLongFileMode();
223         verify(configSource).getWorkingDirectory();
224         verify(configSource).isDryRun();
225         verify(configSource).isIgnorePermissions();
226         verify(configSource).isUpdateOnly();
227 
228         verify(archiverManager).getArchiver("tar");
229     }
230 
231     @Test
232     public void testCreateArchiver_ShouldCreateWarArchiverWitEexpectWebXmlSetToFalse() throws Exception {
233         final TestWarArchiver twArchiver = new TestWarArchiver();
234 
235         when(archiverManager.getArchiver("war")).thenReturn(twArchiver);
236 
237         final AssemblerConfigurationSource configSource = mock(AssemblerConfigurationSource.class);
238         when(configSource.getOverrideGid()).thenReturn(0);
239         when(configSource.getOverrideGroupName()).thenReturn("root");
240         when(configSource.getOverrideUid()).thenReturn(0);
241         when(configSource.getOverrideUserName()).thenReturn("root");
242         when(configSource.getProject()).thenReturn(new MavenProject(new Model()));
243         when(configSource.getWorkingDirectory()).thenReturn(new File("."));
244         when(configSource.isIgnorePermissions()).thenReturn(true);
245         when(configSource.isRecompressZippedFiles()).thenReturn(false);
246 
247         final DefaultAssemblyArchiver subject = createSubject(new ArrayList<>());
248 
249         subject.createArchiver("war", false, null, configSource, null, null);
250 
251         assertNotNull(twArchiver.expectWebXml);
252         assertFalse(twArchiver.expectWebXml);
253 
254         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
255         verify(configSource).getArchiverConfig();
256         verify(configSource).getJarArchiveConfiguration();
257         verify(configSource).getMavenSession();
258         verify(configSource, times(2)).getOverrideGid();
259         verify(configSource, times(2)).getOverrideGroupName();
260         verify(configSource, times(2)).getOverrideUid();
261         verify(configSource, times(2)).getOverrideUserName();
262         verify(configSource).getProject();
263         verify(configSource).getWorkingDirectory();
264         verify(configSource).isDryRun();
265         verify(configSource).isIgnorePermissions();
266         verify(configSource).isUpdateOnly();
267 
268         verify(archiverManager).getArchiver("war");
269     }
270 
271     @Test
272     public void testCreateArchiver_ShouldCreateZipArchiver() throws Exception {
273         final ZipArchiver archiver = new ZipArchiver();
274 
275         when(archiverManager.getArchiver("zip")).thenReturn(archiver);
276 
277         final AssemblerConfigurationSource configSource = mock(AssemblerConfigurationSource.class);
278         when(configSource.getOverrideGid()).thenReturn(0);
279         when(configSource.getOverrideGroupName()).thenReturn("root");
280         when(configSource.getOverrideUid()).thenReturn(0);
281         when(configSource.getOverrideUserName()).thenReturn("root");
282         when(configSource.getWorkingDirectory()).thenReturn(new File("."));
283         when(configSource.isIgnorePermissions()).thenReturn(true);
284         when(configSource.isRecompressZippedFiles()).thenReturn(false);
285 
286         final DefaultAssemblyArchiver subject = createSubject(new ArrayList<>());
287 
288         subject.createArchiver("zip", false, null, configSource, null, null);
289 
290         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
291         verify(configSource).getArchiverConfig();
292         verify(configSource, times(2)).getOverrideGid();
293         verify(configSource, times(2)).getOverrideGroupName();
294         verify(configSource, times(2)).getOverrideUid();
295         verify(configSource, times(2)).getOverrideUserName();
296         verify(configSource).getWorkingDirectory();
297         verify(configSource).isDryRun();
298         verify(configSource).isIgnorePermissions();
299         verify(configSource).isUpdateOnly();
300 
301         verify(archiverManager).getArchiver("zip");
302     }
303 
304     @Test
305     public void testCreateTarArchiver_ShouldNotInitializeCompression() throws Exception {
306         final TestTarArchiver archiver = new TestTarArchiver();
307 
308         when(archiverManager.getArchiver("tar")).thenReturn(archiver);
309 
310         final DefaultAssemblyArchiver subject = createSubject(new ArrayList<>());
311 
312         PojoConfigSource configSource = new PojoConfigSource();
313         configSource.setTarLongFileMode(TarLongFileMode.fail.name());
314         configSource.setWorkingDirectory(new File(""));
315         configSource.setRecompressZippedFiles(false);
316 
317         subject.createArchiver("tar", true, "", configSource, null, null);
318 
319         assertNull(new TestTarArchiver().compressionMethod);
320         assertEquals(TarLongFileMode.fail, archiver.longFileMode);
321 
322         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
323         verify(archiverManager).getArchiver("tar");
324     }
325 
326     @Test
327     public void testCreateTarArchiver_InvalidFormat_ShouldFailWithInvalidCompression() throws Exception {
328 
329         when(archiverManager.getArchiver("tar.ZZZ")).thenThrow(new NoSuchArchiverException("no archiver"));
330 
331         final DefaultAssemblyArchiver subject = createSubject(new ArrayList<>());
332 
333         try {
334             subject.createArchiver("tar.ZZZ", true, "", null, null, null);
335 
336             fail("Invalid compression formats should throw an error.");
337         } catch (final NoSuchArchiverException e) {
338             // expected.
339         }
340 
341         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
342         verify(archiverManager).getArchiver("tar.ZZZ");
343     }
344 
345     private DefaultAssemblyArchiver createSubject(final List<AssemblyArchiverPhase> phases) {
346         return new DefaultAssemblyArchiver(archiverManager, phases, Collections.emptyMap(), container);
347     }
348 
349     private static final class TestTarArchiver extends TarArchiver {
350 
351         TarCompressionMethod compressionMethod;
352 
353         TarLongFileMode longFileMode;
354 
355         @Override
356         protected void execute() throws ArchiverException, IOException {
357             super.createArchive();
358         }
359 
360         @Override
361         public void setCompression(final TarCompressionMethod mode) {
362             compressionMethod = mode;
363             super.setCompression(mode);
364         }
365 
366         @Override
367         public void setLongfile(final TarLongFileMode mode) {
368             longFileMode = mode;
369             super.setLongfile(mode);
370         }
371     }
372 
373     private static final class TestWarArchiver extends WarArchiver {
374 
375         Boolean expectWebXml;
376 
377         @Override
378         public void setExpectWebXml(boolean expectWebXml) {
379             this.expectWebXml = expectWebXml;
380             super.setExpectWebXml(expectWebXml);
381         }
382     }
383 
384     public static final class TestArchiverWithConfig extends NoOpArchiver {
385 
386         private String simpleConfig;
387 
388         public String getSimpleConfig() {
389             return simpleConfig;
390         }
391 
392         public String getDuplicateBehavior() {
393             return Archiver.DUPLICATES_ADD;
394         }
395     }
396 }