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