View Javadoc
1   package org.apache.maven.plugin.assembly.archive.task;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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         // ensure this exists, so the directory addition will proceed.
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 //        macTask.expectGetProject( null );
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         // ensure this exists, so the directory addition will proceed.
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         //macTask.expectGetProject( null );
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         //macTask.expectGetProject( null );
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             // should do this, because it cannot use the provide archiveBasedir.
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             // should do this, because it cannot use the provide archiveBasedir.
279         }
280 
281         mockManager.verifyAll();
282     }
283 
284 }