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 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.codehaus.plexus.logging.Logger;
42  import org.codehaus.plexus.logging.console.ConsoleLogger;
43  import org.junit.Rule;
44  import org.junit.Test;
45  import org.junit.rules.TemporaryFolder;
46  import org.junit.runner.RunWith;
47  import org.mockito.junit.MockitoJUnitRunner;
48  
49  @RunWith( MockitoJUnitRunner.class )
50  public class AddFileSetsTaskTest
51  {
52      @Rule
53      public TemporaryFolder temporaryFolder = new TemporaryFolder();
54  
55      @Test
56      public void testGetFileSetDirectory_ShouldReturnAbsoluteSourceDir()
57          throws Exception
58      {
59          final File dir = temporaryFolder.newFolder();
60  
61          final FileSet fs = new FileSet();
62  
63          fs.setDirectory( dir.getAbsolutePath() );
64  
65          final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, null, null );
66  
67          assertEquals( dir.getAbsolutePath(), result.getAbsolutePath() );
68      }
69  
70      @Test
71      public void testGetFileSetDirectory_ShouldReturnBasedir()
72          throws Exception
73      {
74          final File dir = temporaryFolder.newFolder();
75  
76          final FileSet fs = new FileSet();
77  
78          final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, dir, null );
79  
80          assertEquals( dir.getAbsolutePath(), result.getAbsolutePath() );
81      }
82  
83      @Test
84      public void testGetFileSetDirectory_ShouldReturnDirFromBasedirAndSourceDir()
85          throws Exception
86      {
87          final File dir = temporaryFolder.newFolder();
88  
89          final String srcPath = "source";
90  
91          final File srcDir = new File( dir, srcPath );
92  
93          final FileSet fs = new FileSet();
94  
95          fs.setDirectory( srcPath );
96  
97          final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, dir, null );
98  
99          assertEquals( srcDir.getAbsolutePath(), result.getAbsolutePath() );
100     }
101 
102     @Test
103     public void testGetFileSetDirectory_ShouldReturnDirFromArchiveBasedirAndSourceDir()
104         throws Exception
105     {
106         final File dir = temporaryFolder.newFolder();
107 
108         final String srcPath = "source";
109 
110         final File srcDir = new File( dir, srcPath );
111 
112         final FileSet fs = new FileSet();
113 
114         fs.setDirectory( srcPath );
115 
116         final File result = new AddFileSetsTask( new ArrayList<FileSet>() ).getFileSetDirectory( fs, null, dir );
117 
118         assertEquals( srcDir.getAbsolutePath(), result.getAbsolutePath() );
119     }
120 
121     @Test
122     public void testAddFileSet_ShouldAddDirectory()
123         throws Exception
124     {
125         File basedir = temporaryFolder.getRoot();
126         
127         final FileSet fs = new FileSet();
128         fs.setDirectory( temporaryFolder.newFolder( "dir" ).getName() );
129         fs.setOutputDirectory( "dir2" );
130 
131         // the logger sends a debug message with this info inside the addFileSet(..) method..
132         final Archiver archiver = mock( Archiver.class );
133         when( archiver.getOverrideDirectoryMode() ).thenReturn( -1 );
134         when( archiver.getOverrideFileMode() ).thenReturn( -1 );
135         
136         final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
137 
138         final MavenProject project = new MavenProject( new Model() );
139         project.setGroupId( "GROUPID" );
140         project.setFile( new File( basedir, "pom.xml" ) );
141 
142         DefaultAssemblyArchiverTest.setupInterpolators( configSource, project );
143 
144         final AddFileSetsTask task = new AddFileSetsTask( new ArrayList<FileSet>() );
145 
146         task.setLogger( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
147         task.setProject( project );
148 
149         task.addFileSet( fs, archiver, configSource, null );
150 
151         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
152         verify( configSource, atLeastOnce() ).getFinalName();
153         verify( configSource, atLeastOnce() ).getMavenSession();
154         
155         verify( archiver, times( 2 ) ).getOverrideDirectoryMode();
156         verify( archiver, times( 2 ) ).getOverrideFileMode();
157         verify( archiver, atLeastOnce() ) .addFileSet( any( org.codehaus.plexus.archiver.FileSet.class ) );
158     }
159 
160     @Test
161     public void testAddFileSet_ShouldAddDirectoryUsingSourceDirNameForDestDir()
162         throws Exception
163     {
164         final FileSet fs = new FileSet();
165         final String dirname = "dir";
166         fs.setDirectory( dirname );
167 
168         final File archiveBaseDir = temporaryFolder.newFolder();
169 
170         // ensure this exists, so the directory addition will proceed.
171         final File srcDir = new File( archiveBaseDir, dirname );
172         srcDir.mkdirs();
173 
174         // the logger sends a debug message with this info inside the addFileSet(..) method..
175         final Archiver archiver = mock( Archiver.class );
176         when( archiver.getOverrideDirectoryMode() ).thenReturn( -1 );
177         when( archiver.getOverrideFileMode() ).thenReturn( -1 );
178         
179         final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
180 
181         final MavenProject project = new MavenProject( new Model() );
182         project.setGroupId( "GROUPID" );
183         DefaultAssemblyArchiverTest.setupInterpolators( configSource, project );
184 
185         final AddFileSetsTask task = new AddFileSetsTask( new ArrayList<FileSet>() );
186         task.setLogger( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
187         task.setProject( project );
188 
189         task.addFileSet( fs, archiver, configSource, archiveBaseDir );
190 
191         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
192         verify( configSource, atLeastOnce() ).getFinalName();
193         verify( configSource, atLeastOnce() ).getMavenSession();
194         
195         verify( archiver, times( 2 ) ).getOverrideDirectoryMode();
196         verify( archiver, times( 2 ) ).getOverrideFileMode();
197         verify( archiver ).addFileSet( any( org.codehaus.plexus.archiver.FileSet.class ) );
198     }
199 
200     @Test
201     public void testAddFileSet_ShouldNotAddDirectoryWhenSourceDirNonExistent()
202         throws Exception
203     {
204         final FileSet fs = new FileSet();
205 
206         fs.setDirectory( "dir" );
207         final File archiveBaseDir = temporaryFolder.newFolder();
208 
209         final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
210         when( configSource.getFinalName() ).thenReturn( "finalName" );
211 
212         final Archiver archiver = mock( Archiver.class );
213         when( archiver.getOverrideDirectoryMode() ).thenReturn( -1 );
214         when( archiver.getOverrideFileMode() ).thenReturn( -1 );
215 
216         final MavenProject project = new MavenProject( new Model() );
217         project.setGroupId( "GROUPID" );
218 
219         DefaultAssemblyArchiverTest.setupInterpolators( configSource, project );
220 
221         final AddFileSetsTask task = new AddFileSetsTask( new ArrayList<FileSet>() );
222 
223         task.setLogger( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
224         task.setProject( project );
225 
226         task.addFileSet( fs, archiver, configSource, archiveBaseDir );
227 
228         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
229         verify( configSource, atLeastOnce() ).getFinalName();
230         verify( configSource, atLeastOnce() ).getMavenSession();
231 
232         verify( archiver ).getOverrideDirectoryMode();
233         verify( archiver ).getOverrideFileMode();
234     }
235 
236     @Test
237     public void testExecute_ShouldThrowExceptionIfArchiveBasedirProvidedIsNonExistent()
238         throws Exception
239     {
240         File archiveBaseDir = new File( temporaryFolder.getRoot(), "archive");
241         final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
242         when( configSource.getArchiveBaseDirectory() ).thenReturn( archiveBaseDir );
243 
244         final AddFileSetsTask task = new AddFileSetsTask( new ArrayList<FileSet>() );
245 
246         try
247         {
248             task.execute( null, configSource );
249 
250             fail( "Should throw exception due to non-existent archiveBasedir location that was provided." );
251         }
252         catch ( final ArchiveCreationException e )
253         {
254             // should do this, because it cannot use the provide archiveBasedir.
255         }
256 
257         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
258         verify( configSource ).getArchiveBaseDirectory();
259     }
260 
261     @Test
262     public void testExecute_ShouldThrowExceptionIfArchiveBasedirProvidedIsNotADirectory()
263         throws Exception
264     {
265         File archiveBaseDir = temporaryFolder.newFile();
266         final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
267         when( configSource.getArchiveBaseDirectory() ).thenReturn( archiveBaseDir );
268 
269         final AddFileSetsTask task = new AddFileSetsTask( new ArrayList<FileSet>() );
270 
271         try
272         {
273             task.execute( null, configSource );
274 
275             fail( "Should throw exception due to non-directory archiveBasedir location that was provided." );
276         }
277         catch ( final ArchiveCreationException e )
278         {
279             // should do this, because it cannot use the provide archiveBasedir.
280         }
281         
282         verify( configSource ).getArchiveBaseDirectory();
283     }
284 
285 }