1 package org.apache.maven.plugin.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 java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25
26 import junit.framework.TestCase;
27
28 import org.apache.maven.model.Model;
29 import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
30 import org.apache.maven.plugin.assembly.archive.DefaultAssemblyArchiverTest;
31 import org.apache.maven.plugin.assembly.archive.task.testutils.MockAndControlForAddFileSetsTask;
32 import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
33 import org.apache.maven.plugin.assembly.model.FileSet;
34 import org.apache.maven.plugin.assembly.testutils.TestFileManager;
35 import org.apache.maven.project.MavenProject;
36 import org.codehaus.plexus.logging.Logger;
37 import org.codehaus.plexus.logging.console.ConsoleLogger;
38 import org.easymock.classextension.EasyMockSupport;
39
40 import static org.easymock.EasyMock.expect;
41
42 @SuppressWarnings("ResultOfMethodCallIgnored")
43 public class AddFileSetsTaskTest
44 extends TestCase
45 {
46
47 private EasyMockSupport mockManager;
48
49 private TestFileManager fileManager;
50
51 private MockAndControlForAddFileSetsTask macTask;
52
53 @Override
54 public void setUp()
55 {
56 mockManager = new EasyMockSupport();
57
58 fileManager = new TestFileManager( "add-fileset.test.", "" );
59
60 macTask = new MockAndControlForAddFileSetsTask( mockManager, fileManager );
61 }
62
63 @Override
64 public void tearDown() throws IOException
65 {
66 fileManager.cleanUp();
67 }
68
69 public void testGetFileSetDirectory_ShouldReturnAbsoluteSourceDir()
70 throws ArchiveCreationException, AssemblyFormattingException
71 {
72 final File dir = fileManager.createTempDir();
73
74 final FileSet fs = new FileSet();
75
76 fs.setDirectory( dir.getAbsolutePath() );
77
78 final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, null, null );
79
80 assertEquals( dir.getAbsolutePath(), result.getAbsolutePath() );
81 }
82
83 public void testGetFileSetDirectory_ShouldReturnBasedir()
84 throws ArchiveCreationException, AssemblyFormattingException
85 {
86 final File dir = fileManager.createTempDir();
87
88 final FileSet fs = new FileSet();
89
90 final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, dir, null );
91
92 assertEquals( dir.getAbsolutePath(), result.getAbsolutePath() );
93 }
94
95 public void testGetFileSetDirectory_ShouldReturnDirFromBasedirAndSourceDir()
96 throws ArchiveCreationException, AssemblyFormattingException
97 {
98 final File dir = fileManager.createTempDir();
99
100 final String srcPath = "source";
101
102 final File srcDir = new File( dir, srcPath );
103
104 final FileSet fs = new FileSet();
105
106 fs.setDirectory( srcPath );
107
108 final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, dir, null );
109
110 assertEquals( srcDir.getAbsolutePath(), result.getAbsolutePath() );
111 }
112
113 public void testGetFileSetDirectory_ShouldReturnDirFromArchiveBasedirAndSourceDir()
114 throws ArchiveCreationException, AssemblyFormattingException
115 {
116 final File dir = fileManager.createTempDir();
117
118 final String srcPath = "source";
119
120 final File srcDir = new File( dir, srcPath );
121
122 final FileSet fs = new FileSet();
123
124 fs.setDirectory( srcPath );
125
126 final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, null, dir );
127
128 assertEquals( srcDir.getAbsolutePath(), result.getAbsolutePath() );
129 }
130
131 public void testAddFileSet_ShouldAddDirectory() throws ArchiveCreationException, AssemblyFormattingException
132 {
133 final FileSet fs = new FileSet();
134
135 final String dirname = "dir";
136
137 fs.setDirectory( dirname );
138 fs.setOutputDirectory( "dir2" );
139
140
141 final File srcDir = new File( macTask.archiveBaseDir, dirname );
142 srcDir.mkdirs();
143
144 final int[] modes = { -1, -1, -1, -1 };
145
146 macTask.expectAdditionOfSingleFileSet( null, null, true, modes, 1, true, false );
147
148
149
150 final MavenProject project = new MavenProject( new Model() );
151
152 DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource );
153 mockManager.replayAll();
154
155 final AddFileSetsTask task = new AddFileSetsTask( new ArrayList<FileSet>() );
156
157 task.setLogger( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
158 task.setProject( project );
159
160 task.addFileSet( fs, macTask.archiver, macTask.configSource, macTask.archiveBaseDir );
161
162 mockManager.verifyAll();
163 }
164
165 public void testAddFileSet_ShouldAddDirectoryUsingSourceDirNameForDestDir()
166 throws ArchiveCreationException, AssemblyFormattingException
167 {
168 final FileSet fs = new FileSet();
169
170 final String dirname = "dir";
171
172 fs.setDirectory( dirname );
173
174 final File archiveBaseDir = fileManager.createTempDir();
175
176
177 final File srcDir = new File( archiveBaseDir, dirname );
178 srcDir.mkdirs();
179
180 final int[] modes = { -1, -1, -1, -1 };
181
182 macTask.expectAdditionOfSingleFileSet( null, null, true, modes, 1, true, false );
183
184
185
186 final MavenProject project = new MavenProject( new Model() );
187 DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource );
188
189 mockManager.replayAll();
190
191 final AddFileSetsTask task = new AddFileSetsTask( new ArrayList<FileSet>() );
192
193 task.setLogger( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
194 task.setProject( project );
195
196 task.addFileSet( fs, macTask.archiver, macTask.configSource, archiveBaseDir );
197
198 mockManager.verifyAll();
199 }
200
201 public void testAddFileSet_ShouldNotAddDirectoryWhenSourceDirNonExistent()
202 throws ArchiveCreationException, AssemblyFormattingException
203 {
204 final FileSet fs = new FileSet();
205
206 final String dirname = "dir";
207
208 fs.setDirectory( dirname );
209
210 final File archiveBaseDir = fileManager.createTempDir();
211
212 macTask.expectGetFinalName( "finalName" );
213
214
215
216 expect(macTask.archiver.getOverrideDirectoryMode()).andReturn( -1 );
217 expect(macTask.archiver.getOverrideFileMode()).andReturn( -1 );
218
219 final MavenProject project = new MavenProject( new Model() );
220 DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource );
221
222 mockManager.replayAll();
223
224 final AddFileSetsTask task = new AddFileSetsTask( new ArrayList<FileSet>() );
225
226 task.setLogger( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
227 task.setProject( project );
228
229 task.addFileSet( fs, macTask.archiver, macTask.configSource, archiveBaseDir );
230
231 mockManager.verifyAll();
232 }
233
234 public void testExecute_ShouldThrowExceptionIfArchiveBasedirProvidedIsNonExistent()
235 throws AssemblyFormattingException
236 {
237 macTask.archiveBaseDir.delete();
238
239 macTask.expectGetArchiveBaseDirectory();
240
241 mockManager.replayAll();
242
243 final AddFileSetsTask task = new AddFileSetsTask( new ArrayList<FileSet>() );
244
245 try
246 {
247 task.execute( macTask.archiver, macTask.configSource );
248
249 fail( "Should throw exception due to non-existent archiveBasedir location that was provided." );
250 }
251 catch ( final ArchiveCreationException e )
252 {
253
254 }
255
256 mockManager.verifyAll();
257 }
258
259 public void testExecute_ShouldThrowExceptionIfArchiveBasedirProvidedIsNotADirectory()
260 throws AssemblyFormattingException, IOException
261 {
262
263 macTask.archiveBaseDir = fileManager.createTempFile();
264 macTask.expectGetArchiveBaseDirectory();
265
266 mockManager.replayAll();
267
268 final AddFileSetsTask task = new AddFileSetsTask( new ArrayList<FileSet>() );
269
270 try
271 {
272 task.execute( macTask.archiver, macTask.configSource );
273
274 fail( "Should throw exception due to non-directory archiveBasedir location that was provided." );
275 }
276 catch ( final ArchiveCreationException e )
277 {
278
279 }
280
281 mockManager.verifyAll();
282 }
283
284 }