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.task;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  
25  import org.apache.maven.model.Model;
26  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
27  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
28  import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest;
29  import org.apache.maven.plugins.assembly.model.FileSet;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.archiver.Archiver;
32  import org.junit.jupiter.api.Test;
33  import org.junit.jupiter.api.extension.ExtendWith;
34  import org.junit.jupiter.api.io.TempDir;
35  import org.mockito.junit.jupiter.MockitoExtension;
36  import org.mockito.junit.jupiter.MockitoSettings;
37  import org.mockito.quality.Strictness;
38  
39  import static org.junit.jupiter.api.Assertions.assertEquals;
40  import static org.junit.jupiter.api.Assertions.fail;
41  import static org.mockito.Mockito.any;
42  import static org.mockito.Mockito.atLeastOnce;
43  import static org.mockito.Mockito.mock;
44  import static org.mockito.Mockito.times;
45  import static org.mockito.Mockito.verify;
46  import static org.mockito.Mockito.when;
47  
48  @MockitoSettings(strictness = Strictness.WARN)
49  @ExtendWith(MockitoExtension.class)
50  public class AddFileSetsTaskTest {
51      @TempDir
52      private File temporaryFolder;
53  
54      @Test
55      public void testGetFileSetDirectoryShouldReturnAbsoluteSourceDir() throws Exception {
56          final File dir = newFolder(temporaryFolder, "junit");
57  
58          final FileSet fs = new FileSet();
59  
60          fs.setDirectory(dir.getAbsolutePath());
61  
62          final File result = new AddFileSetsTask(new ArrayList<>()).getFileSetDirectory(fs, null, null);
63  
64          assertEquals(dir.getAbsolutePath(), result.getAbsolutePath());
65      }
66  
67      @Test
68      public void testGetFileSetDirectoryShouldReturnBasedir() throws Exception {
69          final File dir = newFolder(temporaryFolder, "junit");
70  
71          final FileSet fs = new FileSet();
72  
73          final File result = new AddFileSetsTask(new ArrayList<>()).getFileSetDirectory(fs, dir, null);
74  
75          assertEquals(dir.getAbsolutePath(), result.getAbsolutePath());
76      }
77  
78      @Test
79      public void testGetFileSetDirectoryShouldReturnDirFromBasedirAndSourceDir() throws Exception {
80          final File dir = newFolder(temporaryFolder, "junit");
81  
82          final String srcPath = "source";
83  
84          final File srcDir = new File(dir, srcPath);
85  
86          final FileSet fs = new FileSet();
87  
88          fs.setDirectory(srcPath);
89  
90          final File result = new AddFileSetsTask(new ArrayList<>()).getFileSetDirectory(fs, dir, null);
91  
92          assertEquals(srcDir.getAbsolutePath(), result.getAbsolutePath());
93      }
94  
95      @Test
96      public void testGetFileSetDirectoryShouldReturnDirFromArchiveBasedirAndSourceDir() throws Exception {
97          final File dir = newFolder(temporaryFolder, "junit");
98  
99          final String srcPath = "source";
100 
101         final File srcDir = new File(dir, srcPath);
102 
103         final FileSet fs = new FileSet();
104 
105         fs.setDirectory(srcPath);
106 
107         final File result = new AddFileSetsTask(new ArrayList<>()).getFileSetDirectory(fs, null, dir);
108 
109         assertEquals(srcDir.getAbsolutePath(), result.getAbsolutePath());
110     }
111 
112     @Test
113     public void testAddFileSetShouldAddDirectory() throws Exception {
114         File basedir = temporaryFolder;
115 
116         final FileSet fs = new FileSet();
117         fs.setDirectory(newFolder(temporaryFolder, "dir").getName());
118         fs.setOutputDirectory("dir2");
119 
120         // the logger sends a debug message with this info inside the addFileSet(..) method..
121         final Archiver archiver = mock(Archiver.class);
122         when(archiver.getOverrideDirectoryMode()).thenReturn(-1);
123         when(archiver.getOverrideFileMode()).thenReturn(-1);
124 
125         final AssemblerConfigurationSource configSource = mock(AssemblerConfigurationSource.class);
126 
127         final MavenProject project = new MavenProject(new Model());
128         project.setGroupId("GROUPID");
129         project.setFile(new File(basedir, "pom.xml"));
130 
131         DefaultAssemblyArchiverTest.setupInterpolators(configSource, project);
132 
133         final AddFileSetsTask task = new AddFileSetsTask(new ArrayList<>());
134 
135         task.setProject(project);
136 
137         task.addFileSet(fs, archiver, configSource, null);
138 
139         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
140         verify(configSource, atLeastOnce()).getFinalName();
141         verify(configSource, atLeastOnce()).getMavenSession();
142 
143         verify(archiver, times(2)).getOverrideDirectoryMode();
144         verify(archiver, times(2)).getOverrideFileMode();
145         verify(archiver, atLeastOnce()).addFileSet(any(org.codehaus.plexus.archiver.FileSet.class));
146     }
147 
148     @Test
149     public void testAddFileSetShouldAddDirectoryUsingSourceDirNameForDestDir() throws Exception {
150         final FileSet fs = new FileSet();
151         final String dirname = "dir";
152         fs.setDirectory(dirname);
153 
154         final File archiveBaseDir = newFolder(temporaryFolder, "junit");
155 
156         // ensure this exists, so the directory addition will proceed.
157         final File srcDir = new File(archiveBaseDir, dirname);
158         srcDir.mkdirs();
159 
160         // the logger sends a debug message with this info inside the addFileSet(..) method..
161         final Archiver archiver = mock(Archiver.class);
162         when(archiver.getOverrideDirectoryMode()).thenReturn(-1);
163         when(archiver.getOverrideFileMode()).thenReturn(-1);
164 
165         final AssemblerConfigurationSource configSource = mock(AssemblerConfigurationSource.class);
166 
167         final MavenProject project = new MavenProject(new Model());
168         project.setGroupId("GROUPID");
169         DefaultAssemblyArchiverTest.setupInterpolators(configSource, project);
170 
171         final AddFileSetsTask task = new AddFileSetsTask(new ArrayList<>());
172         task.setProject(project);
173 
174         task.addFileSet(fs, archiver, configSource, archiveBaseDir);
175 
176         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
177         verify(configSource, atLeastOnce()).getFinalName();
178         verify(configSource, atLeastOnce()).getMavenSession();
179 
180         verify(archiver, times(2)).getOverrideDirectoryMode();
181         verify(archiver, times(2)).getOverrideFileMode();
182         verify(archiver).addFileSet(any(org.codehaus.plexus.archiver.FileSet.class));
183     }
184 
185     @Test
186     public void testAddFileSetShouldNotAddDirectoryWhenSourceDirNonExistent() throws Exception {
187         final FileSet fs = new FileSet();
188 
189         fs.setDirectory("dir");
190         final File archiveBaseDir = newFolder(temporaryFolder, "junit");
191 
192         final AssemblerConfigurationSource configSource = mock(AssemblerConfigurationSource.class);
193         when(configSource.getFinalName()).thenReturn("finalName");
194 
195         final Archiver archiver = mock(Archiver.class);
196         when(archiver.getOverrideDirectoryMode()).thenReturn(-1);
197         when(archiver.getOverrideFileMode()).thenReturn(-1);
198 
199         final MavenProject project = new MavenProject(new Model());
200         project.setGroupId("GROUPID");
201 
202         DefaultAssemblyArchiverTest.setupInterpolators(configSource, project);
203 
204         final AddFileSetsTask task = new AddFileSetsTask(new ArrayList<>());
205         task.setProject(project);
206 
207         task.addFileSet(fs, archiver, configSource, archiveBaseDir);
208 
209         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
210         verify(configSource, atLeastOnce()).getFinalName();
211         verify(configSource, atLeastOnce()).getMavenSession();
212 
213         verify(archiver).getOverrideDirectoryMode();
214         verify(archiver).getOverrideFileMode();
215     }
216 
217     @Test
218     public void testExecuteShouldThrowExceptionIfArchiveBasedirProvidedIsNonExistent() throws Exception {
219         File archiveBaseDir = new File(temporaryFolder, "archive");
220         final AssemblerConfigurationSource configSource = mock(AssemblerConfigurationSource.class);
221         when(configSource.getArchiveBaseDirectory()).thenReturn(archiveBaseDir);
222 
223         final AddFileSetsTask task = new AddFileSetsTask(new ArrayList<>());
224 
225         try {
226             task.execute(null, configSource);
227 
228             fail("Should throw exception due to non-existent archiveBasedir location that was provided.");
229         } catch (final ArchiveCreationException e) {
230             // should do this, because it cannot use the provide archiveBasedir.
231         }
232 
233         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
234         verify(configSource).getArchiveBaseDirectory();
235     }
236 
237     @Test
238     public void testExecuteShouldThrowExceptionIfArchiveBasedirProvidedIsNotADirectory() throws Exception {
239         File archiveBaseDir = File.createTempFile("junit", null, temporaryFolder);
240         final AssemblerConfigurationSource configSource = mock(AssemblerConfigurationSource.class);
241         when(configSource.getArchiveBaseDirectory()).thenReturn(archiveBaseDir);
242 
243         final AddFileSetsTask task = new AddFileSetsTask(new ArrayList<>());
244 
245         try {
246             task.execute(null, configSource);
247 
248             fail("Should throw exception due to non-directory archiveBasedir location that was provided.");
249         } catch (final ArchiveCreationException e) {
250             // should do this, because it cannot use the provide archiveBasedir.
251         }
252 
253         verify(configSource).getArchiveBaseDirectory();
254     }
255 
256     private static File newFolder(File root, String... subDirs) throws IOException {
257         String subFolder = String.join("/", subDirs);
258         File result = new File(root, subFolder);
259         if (!result.mkdirs()) {
260             throw new IOException("Couldn't create folders " + root);
261         }
262         return result;
263     }
264 }