View Javadoc
1   package org.apache.maven.plugins.assembly.archive.archiver;
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.hamcrest.MatcherAssert.assertThat;
23  import static org.hamcrest.Matchers.is;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertTrue;
27  import static org.mockito.Mockito.mock;
28  import static org.mockito.Mockito.verify;
29  import static org.mockito.Mockito.when;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.nio.charset.StandardCharsets;
34  import java.nio.file.Files;
35  import java.util.ArrayList;
36  import java.util.Arrays;
37  import java.util.List;
38  
39  import org.codehaus.plexus.archiver.Archiver;
40  import org.codehaus.plexus.archiver.ArchiverException;
41  import org.codehaus.plexus.archiver.FileSet;
42  import org.codehaus.plexus.archiver.diags.TrackingArchiver;
43  import org.codehaus.plexus.archiver.jar.JarArchiver;
44  import org.codehaus.plexus.archiver.util.DefaultFileSet;
45  import org.codehaus.plexus.components.io.fileselectors.FileInfo;
46  import org.codehaus.plexus.components.io.fileselectors.FileSelector;
47  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
48  import org.junit.Rule;
49  import org.junit.Test;
50  import org.junit.rules.TemporaryFolder;
51  import org.junit.runner.RunWith;
52  import org.mockito.ArgumentCaptor;
53  import org.mockito.junit.MockitoJUnitRunner;
54  
55  @RunWith( MockitoJUnitRunner.class )
56  public class AssemblyProxyArchiverTest
57  {
58      @Rule
59      public TemporaryFolder temporaryFolder = new TemporaryFolder();
60      
61      @Test( timeout = 5000 )
62      public void addFileSet_SkipWhenSourceIsAssemblyWorkDir()
63          throws IOException, ArchiverException
64      {
65          final File sources = temporaryFolder.getRoot();
66  
67          final File workdir = new File( sources, "workdir" );
68  
69          final TrackingArchiver tracker = new TrackingArchiver();
70          final AssemblyProxyArchiver archiver =
71              new AssemblyProxyArchiver( "", tracker, null, null, null, workdir );
72  
73          archiver.setForced( true );
74  
75          final DefaultFileSet fs = new DefaultFileSet();
76          fs.setDirectory( workdir );
77  
78          archiver.addFileSet( fs );
79  
80          assertTrue( tracker.added.isEmpty() );
81      }
82  
83      @Test( timeout = 5000 )
84      public void addFileSet_addExcludeWhenSourceContainsAssemblyWorkDir()
85          throws IOException, ArchiverException
86      {
87          final File sources = temporaryFolder.getRoot();
88  
89          final File workdir = new File( sources, "workdir" );
90          workdir.mkdir();
91  
92          Files.write( sources.toPath().resolve( "test-included.txt" ), Arrays.asList( "This is included" ),
93                       StandardCharsets.UTF_8 );
94          Files.write( workdir.toPath().resolve( "test-excluded.txt" ), Arrays.asList( "This is excluded" ),
95                       StandardCharsets.UTF_8 );
96  
97          final TrackingArchiver tracker = new TrackingArchiver();
98          final AssemblyProxyArchiver archiver =
99              new AssemblyProxyArchiver( "", tracker, null, null, null, workdir );
100 
101         archiver.setForced( true );
102 
103         final DefaultFileSet fs = new DefaultFileSet();
104         fs.setDirectory( sources );
105 
106         archiver.addFileSet( fs );
107 
108         assertEquals( 1, tracker.added.size() );
109 
110         final TrackingArchiver.Addition addition = tracker.added.get( 0 );
111         assertNotNull( addition.excludes );
112         assertEquals( 1, addition.excludes.length );
113         assertEquals( workdir.getName(), addition.excludes[0] );
114     }
115 
116     @Test
117     public void addFile_NoPerms_CallAcceptFilesOnlyOnce()
118         throws IOException, ArchiverException
119     {
120         final Archiver delegate = mock( Archiver.class );
121 
122         final CounterSelector counter = new CounterSelector( true );
123         final List<FileSelector> selectors = new ArrayList<>();
124         selectors.add( counter );
125 
126         final AssemblyProxyArchiver archiver =
127             new AssemblyProxyArchiver( "", delegate, null, selectors, null, new File( "." ) );
128         archiver.setForced( true );
129 
130         final File inputFile = temporaryFolder.newFile();
131         archiver.addFile( inputFile, "file.txt" );
132 
133         assertEquals( 1, counter.getCount() );
134         verify( delegate ).addFile( inputFile, "file.txt" );
135         verify( delegate ).setForced( true );
136     }
137 
138     @Test
139     public void addDirectory_NoPerms_CallAcceptFilesOnlyOnce()
140         throws IOException, ArchiverException
141     {
142         final Archiver delegate = new JarArchiver();
143 
144         final File output = temporaryFolder.newFile();
145 
146         delegate.setDestFile( output );
147 
148         final CounterSelector counter = new CounterSelector( true );
149         final List<FileSelector> selectors = new ArrayList<>();
150         selectors.add( counter );
151 
152         final AssemblyProxyArchiver archiver =
153             new AssemblyProxyArchiver( "", delegate, null, selectors, null, new File( "." ) );
154 
155         archiver.setForced( true );
156 
157         final File dir = temporaryFolder.newFolder();
158         Files.write( dir.toPath().resolve( "file.txt" ), Arrays.asList( "This is a test." ), StandardCharsets.UTF_8 );
159 
160         archiver.addDirectory( dir );
161 
162         archiver.createArchive();
163 
164         assertEquals( 1, counter.getCount() );
165     }
166     
167     @Test
168     public void assemblyWorkDir() 
169     {
170         final Archiver delegate = mock( Archiver.class );
171         final List<FileSelector> selectors = new ArrayList<>();
172 
173         final AssemblyProxyArchiver archiver =
174             new AssemblyProxyArchiver( "prefix", delegate, null, selectors, null,
175                                        new File( temporaryFolder.getRoot(), "module1" ) );
176         
177         FileSet fileSet = mock( FileSet.class );
178         when( fileSet.getDirectory() ).thenReturn( temporaryFolder.getRoot() ); 
179         when( fileSet.getStreamTransformer() ).thenReturn( mock( InputStreamTransformer.class ) );
180         
181         archiver.addFileSet( fileSet );
182 
183         ArgumentCaptor<FileSet> delFileSet = ArgumentCaptor.forClass( FileSet.class );
184         verify( delegate ).addFileSet( delFileSet.capture() );
185         
186         assertThat( delFileSet.getValue().getDirectory(), is( fileSet.getDirectory() ) );
187         assertThat( delFileSet.getValue().getExcludes(), is( new String[] { "module1" } ) );
188         assertThat( delFileSet.getValue().getFileMappers(), is( fileSet.getFileMappers() ) );
189         assertThat( delFileSet.getValue().getFileSelectors(), is( fileSet.getFileSelectors() ) );
190         assertThat( delFileSet.getValue().getIncludes(), is(  new String[0] ) );
191         assertThat( delFileSet.getValue().getPrefix(), is( "prefix/" ) );
192         assertThat( delFileSet.getValue().getStreamTransformer(), is( fileSet.getStreamTransformer() ) );
193     }
194     
195 
196     private static final class CounterSelector
197         implements FileSelector
198     {
199 
200         private int count = 0;
201 
202         private boolean answer = false;
203 
204         public CounterSelector( final boolean answer )
205         {
206             this.answer = answer;
207         }
208 
209         public int getCount()
210         {
211             return count;
212         }
213 
214         public boolean isSelected( final FileInfo fileInfo )
215             throws IOException
216         {
217             if ( fileInfo.isFile() )
218             {
219                 count++;
220                 System.out.println( "Counting file: " + fileInfo.getName() + ". Current count: " + count );
221             }
222 
223             return answer;
224         }
225 
226     }
227 
228 }