View Javadoc

1   package org.apache.maven.plugin.assembly.format;
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.AssemblerConfigurationSource;
24  import org.apache.maven.plugin.assembly.model.FileSet;
25  import org.apache.maven.plugin.assembly.testutils.MockManager;
26  import org.apache.maven.plugin.assembly.testutils.TestFileManager;
27  import org.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.PlexusTestCase;
30  import org.codehaus.plexus.logging.Logger;
31  import org.codehaus.plexus.logging.console.ConsoleLogger;
32  import org.codehaus.plexus.util.FileUtils;
33  import org.easymock.MockControl;
34  
35  import java.io.File;
36  import java.io.IOException;
37  import java.util.Collections;
38  import java.util.List;
39  
40  public class FileSetFormatterTest
41      extends PlexusTestCase
42  {
43  
44      private MockManager mockManager;
45  
46      private TestFileManager fileManager;
47  
48      private Logger logger;
49  
50      private AssemblerConfigurationSource configSource;
51  
52      private MockControl configSourceControl;
53  
54      @Override
55      public void setUp() throws Exception
56      {
57          super.setUp();
58  
59          mockManager = new MockManager();
60  
61          fileManager = new TestFileManager( "fileSetFormatter-test.", "" );
62  
63          logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
64  
65          configSourceControl = MockControl.createControl( AssemblerConfigurationSource.class );
66          mockManager.add( configSourceControl );
67  
68          configSource = (AssemblerConfigurationSource) configSourceControl.getMock();
69      }
70  
71      @Override
72      public void tearDown() throws IOException
73      {
74          fileManager.cleanUp();
75      }
76  
77      public void testShouldReturnOriginalUnalteredDirectoryWhenLineEndingIsNull()
78          throws AssemblyFormattingException, IOException
79      {
80          final FileSet fs = new FileSet();
81  
82          final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
83  
84          final File dir = fileManager.createTempDir();
85  
86          final File result = formatter.formatFileSetForAssembly( dir, fs );
87  
88          assertEquals( dir, result );
89      }
90  
91      public void testShouldReturnOriginalUnalteredDirectoryWhenLineEndingIsKeep()
92          throws AssemblyFormattingException, IOException
93      {
94          final FileSet fs = new FileSet();
95          fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_KEEP );
96  
97          final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
98  
99          final File dir = fileManager.createTempDir();
100 
101         final File result = formatter.formatFileSetForAssembly( dir, fs );
102 
103         assertEquals( dir, result );
104     }
105 
106     public void testShouldReturnOriginalUnalteredDirectoryWhenIncludedFileSetIsEmpty()
107         throws AssemblyFormattingException, IOException
108     {
109         final File dir = fileManager.createTempDir();
110 
111         final FileSet fs = new FileSet();
112 
113         fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_LF );
114         fs.setDirectory( dir.getCanonicalPath() );
115         fs.addExclude( "**/*" );
116 
117         final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
118 
119         final File result = formatter.formatFileSetForAssembly( dir, fs );
120 
121         assertEquals( dir, result );
122     }
123 
124     public void testShouldConvertLineEndingsOnTwoFiles() throws AssemblyFormattingException, IOException
125     {
126         final File dir = fileManager.createTempDir();
127 
128         final String filename1 = "one.txt";
129         final String filename2 = "two.txt";
130 
131         fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
132         fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
133 
134         final FileSet fs = new FileSet();
135 
136         fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
137         fs.setDirectory( dir.getCanonicalPath() );
138         fs.addInclude( "**/*.txt" );
139 
140         final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
141 
142         configSource.getTemporaryRootDirectory();
143         configSourceControl.setReturnValue( dir );
144 
145         configSource.getEncoding();
146         configSourceControl.setReturnValue( "UTF-8", MockControl.ONE_OR_MORE );
147 
148         mockManager.replayAll();
149         
150         final File result = formatter.formatFileSetForAssembly( dir, fs );
151 
152         assertFalse( dir.equals( result ) );
153 
154         try
155         {
156             fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n" );
157             fileManager.assertFileContents( result, filename2, "Hello\r\nThis is also a test.\r\n" );
158         }
159         finally
160         {
161             FileUtils.deleteDirectory( result );
162         }
163     }
164 
165     public void testShouldConvertLineEndingsOnOneFileWithAnotherExplicitlyExcluded()
166         throws AssemblyFormattingException, IOException
167     {
168         final File dir = fileManager.createTempDir();
169 
170         final String filename1 = "one.txt";
171         final String filename2 = "two.txt";
172 
173         fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
174         fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
175 
176         final FileSet fs = new FileSet();
177 
178         fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
179         fs.setDirectory( dir.getCanonicalPath() );
180         fs.addExclude( "**/two.txt" );
181 
182         final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
183 
184         configSource.getTemporaryRootDirectory();
185         configSourceControl.setReturnValue( dir );
186 
187         configSource.getEncoding();
188         configSourceControl.setReturnValue( "UTF-8", MockControl.ONE_OR_MORE );
189 
190         mockManager.replayAll();
191         
192         final File result = formatter.formatFileSetForAssembly( dir, fs );
193 
194         assertFalse( dir.equals( result ) );
195 
196         try
197         {
198             fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n" );
199             fileManager.assertFileExistence( result, filename2, false );
200         }
201         finally
202         {
203             FileUtils.deleteDirectory( result );
204         }
205     }
206 
207     public void testShouldConvertLineEndingsOnOneExplicitlyIncludedFile()
208         throws AssemblyFormattingException, IOException
209     {
210         final File dir = fileManager.createTempDir();
211 
212         final String filename1 = "one.txt";
213         final String filename2 = "two.txt";
214 
215         fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
216         fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
217 
218         final FileSet fs = new FileSet();
219 
220         fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
221         fs.setDirectory( dir.getCanonicalPath() );
222         fs.addInclude( "**/one.txt" );
223 
224         final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
225 
226         configSource.getTemporaryRootDirectory();
227         configSourceControl.setReturnValue( dir );
228 
229         configSource.getEncoding();
230         configSourceControl.setReturnValue( "UTF-8", MockControl.ONE_OR_MORE );
231 
232         mockManager.replayAll();
233         
234         final File result = formatter.formatFileSetForAssembly( dir, fs );
235 
236         assertFalse( dir.equals( result ) );
237         try
238         {
239             fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n" );
240             fileManager.assertFileExistence( result, filename2, false );
241         }
242         finally
243         {
244             FileUtils.deleteDirectory( result );
245         }
246     }
247 
248     public void testShouldConvertLineEndingsOnOneFileAndIgnoreFileWithinDefaultExcludedDir()
249         throws AssemblyFormattingException, IOException
250     {
251         final File dir = fileManager.createTempDir();
252 
253         final String filename1 = "one.txt";
254         final String filename2 = "CVS/two.txt";
255 
256         fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
257         fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
258 
259         final FileSet fs = new FileSet();
260 
261         fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
262         fs.setDirectory( dir.getCanonicalPath() );
263 
264         final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
265 
266         configSource.getTemporaryRootDirectory();
267         configSourceControl.setReturnValue( dir );
268 
269         configSource.getEncoding();
270         configSourceControl.setReturnValue( "UTF-8", MockControl.ONE_OR_MORE );
271 
272         mockManager.replayAll();
273         
274         final File result = formatter.formatFileSetForAssembly( dir, fs );
275 
276         assertFalse( dir.equals( result ) );
277 
278         try
279         {
280             fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n" );
281             fileManager.assertFileExistence( result, filename2, false );
282         }
283         finally
284         {
285             FileUtils.deleteDirectory( result );
286         }
287     }
288 
289     public void testShouldFilterSeveralFiles() throws Exception
290     {
291         final File basedir = fileManager.createTempDir();
292 
293         final String filename1 = "one.txt";
294         final String filename2 = "two.txt";
295 
296         // this file will be filtered with a project expression
297         fileManager.createFile( basedir, filename1, "This is the filtered artifactId: ${project.artifactId}." );
298         // this one fill be filtered with a filter file
299         fileManager.createFile( basedir, filename2, "This is the filtered 'foo' property: ${foo}." );
300         final File filterProps = fileManager.createFile( basedir, "filter.properties", "foo=bar" );
301 
302         final FileSet fs = new FileSet();
303         fs.setFiltered( true );
304         fs.setDirectory( basedir.getCanonicalPath() );
305         fs.addInclude( "**/*.txt" );
306 
307         enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath() ) );
308 
309         mockManager.replayAll();
310 
311         final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
312         final File result = formatter.formatFileSetForAssembly( basedir, fs );
313 
314         assertFalse( result.equals( basedir ) );
315 
316         try
317         {
318             fileManager.assertFileContents( result, filename1, "This is the filtered artifactId: artifact." );
319             fileManager.assertFileContents( result, filename2, "This is the filtered 'foo' property: bar." );
320 
321             mockManager.verifyAll();
322         }
323         finally
324         {
325             FileUtils.deleteDirectory( result );
326         }
327     }
328 
329     private void enableBasicFilteringConfiguration( final File basedir, final List<String> filterFilenames )
330         throws Exception
331     {
332         configSource.getTemporaryRootDirectory();
333         configSourceControl.setReturnValue( basedir );
334 
335         final Model model = new Model();
336         model.setArtifactId( "artifact" );
337         model.setGroupId( "group" );
338         model.setVersion( "version" );
339 
340         final MavenProject project = new MavenProject( model );
341         project.getBuild()
342                .setFilters( filterFilenames );
343 
344         configSource.getProject();
345         configSourceControl.setReturnValue( project, MockControl.ONE_OR_MORE );
346 
347         configSource.getMavenFileFilter();
348         configSourceControl.setReturnValue( lookup( "org.apache.maven.shared.filtering.MavenFileFilter" ),
349                                             MockControl.ONE_OR_MORE );
350 
351         configSource.getMavenSession();
352         configSourceControl.setReturnValue( null, MockControl.ONE_OR_MORE );
353 
354         configSource.getFilters();
355         configSourceControl.setReturnValue( Collections.EMPTY_LIST, MockControl.ONE_OR_MORE );
356 
357         configSource.getEncoding();
358         configSourceControl.setReturnValue( "UTF-8", MockControl.ONE_OR_MORE );
359     }
360 
361 }