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