View Javadoc
1   package org.apache.maven.plugins.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 junit.framework.TestCase;
23  import org.apache.maven.model.Model;
24  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
25  import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest;
26  import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddFileSetsTask;
27  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
28  import org.apache.maven.plugins.assembly.model.FileSet;
29  import org.apache.maven.plugins.assembly.testutils.TestFileManager;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.logging.Logger;
32  import org.codehaus.plexus.logging.console.ConsoleLogger;
33  import org.easymock.classextension.EasyMockSupport;
34  
35  import java.io.File;
36  import java.io.IOException;
37  import java.util.ArrayList;
38  
39  import static org.easymock.EasyMock.expect;
40  
41  public class AddFileSetsTaskTest
42      extends TestCase
43  {
44  
45      private EasyMockSupport mockManager;
46  
47      private TestFileManager fileManager;
48  
49      private MockAndControlForAddFileSetsTask macTask;
50  
51      @Override
52      public void setUp()
53      {
54          mockManager = new EasyMockSupport();
55  
56          fileManager = new TestFileManager( "add-fileset.test.", "" );
57  
58          macTask = new MockAndControlForAddFileSetsTask( mockManager, fileManager );
59      }
60  
61      @Override
62      public void tearDown()
63          throws IOException
64      {
65          fileManager.cleanUp();
66      }
67  
68      public void testGetFileSetDirectory_ShouldReturnAbsoluteSourceDir()
69          throws ArchiveCreationException, AssemblyFormattingException
70      {
71          final File dir = fileManager.createTempDir();
72  
73          final FileSet fs = new FileSet();
74  
75          fs.setDirectory( dir.getAbsolutePath() );
76  
77          final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, null, null );
78  
79          assertEquals( dir.getAbsolutePath(), result.getAbsolutePath() );
80      }
81  
82      public void testGetFileSetDirectory_ShouldReturnBasedir()
83          throws ArchiveCreationException, AssemblyFormattingException
84      {
85          final File dir = fileManager.createTempDir();
86  
87          final FileSet fs = new FileSet();
88  
89          final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, dir, null );
90  
91          assertEquals( dir.getAbsolutePath(), result.getAbsolutePath() );
92      }
93  
94      public void testGetFileSetDirectory_ShouldReturnDirFromBasedirAndSourceDir()
95          throws ArchiveCreationException, AssemblyFormattingException
96      {
97          final File dir = fileManager.createTempDir();
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<FileSet>() ).getFileSetDirectory( fs, dir, null );
108 
109         assertEquals( srcDir.getAbsolutePath(), result.getAbsolutePath() );
110     }
111 
112     public void testGetFileSetDirectory_ShouldReturnDirFromArchiveBasedirAndSourceDir()
113         throws ArchiveCreationException, AssemblyFormattingException
114     {
115         final File dir = fileManager.createTempDir();
116 
117         final String srcPath = "source";
118 
119         final File srcDir = new File( dir, srcPath );
120 
121         final FileSet fs = new FileSet();
122 
123         fs.setDirectory( srcPath );
124 
125         final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, null, dir );
126 
127         assertEquals( srcDir.getAbsolutePath(), result.getAbsolutePath() );
128     }
129 
130     public void testAddFileSet_ShouldAddDirectory()
131         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 }