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