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         final File result = formatter.formatFileSetForAssembly( dir, fs );
143 
144         assertFalse( dir.equals( result ) );
145 
146         try
147         {
148             fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n" );
149             fileManager.assertFileContents( result, filename2, "Hello\r\nThis is also a test.\r\n" );
150         }
151         finally
152         {
153             FileUtils.deleteDirectory( result );
154         }
155     }
156 
157     public void testShouldConvertLineEndingsOnOneFileWithAnotherExplicitlyExcluded()
158         throws AssemblyFormattingException, IOException
159     {
160         final File dir = fileManager.createTempDir();
161 
162         final String filename1 = "one.txt";
163         final String filename2 = "two.txt";
164 
165         fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
166         fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
167 
168         final FileSet fs = new FileSet();
169 
170         fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
171         fs.setDirectory( dir.getCanonicalPath() );
172         fs.addExclude( "**/two.txt" );
173 
174         final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
175 
176         final File result = formatter.formatFileSetForAssembly( dir, fs );
177 
178         assertFalse( dir.equals( result ) );
179 
180         try
181         {
182             fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n" );
183             fileManager.assertFileExistence( result, filename2, false );
184         }
185         finally
186         {
187             FileUtils.deleteDirectory( result );
188         }
189     }
190 
191     public void testShouldConvertLineEndingsOnOneExplicitlyIncludedFile()
192         throws AssemblyFormattingException, IOException
193     {
194         final File dir = fileManager.createTempDir();
195 
196         final String filename1 = "one.txt";
197         final String filename2 = "two.txt";
198 
199         fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
200         fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
201 
202         final FileSet fs = new FileSet();
203 
204         fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
205         fs.setDirectory( dir.getCanonicalPath() );
206         fs.addInclude( "**/one.txt" );
207 
208         final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
209 
210         final File result = formatter.formatFileSetForAssembly( dir, fs );
211 
212         assertFalse( dir.equals( result ) );
213         try
214         {
215             fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n" );
216             fileManager.assertFileExistence( result, filename2, false );
217         }
218         finally
219         {
220             FileUtils.deleteDirectory( result );
221         }
222     }
223 
224     public void testShouldConvertLineEndingsOnOneFileAndIgnoreFileWithinDefaultExcludedDir()
225         throws AssemblyFormattingException, IOException
226     {
227         final File dir = fileManager.createTempDir();
228 
229         final String filename1 = "one.txt";
230         final String filename2 = "CVS/two.txt";
231 
232         fileManager.createFile( dir, filename1, "Hello\nThis is a test." );
233         fileManager.createFile( dir, filename2, "Hello\nThis is also a test." );
234 
235         final FileSet fs = new FileSet();
236 
237         fs.setLineEnding( AssemblyFileUtils.LINE_ENDING_CRLF );
238         fs.setDirectory( dir.getCanonicalPath() );
239 
240         final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
241 
242         final File result = formatter.formatFileSetForAssembly( dir, fs );
243 
244         assertFalse( dir.equals( result ) );
245 
246         try
247         {
248             fileManager.assertFileContents( result, filename1, "Hello\r\nThis is a test.\r\n" );
249             fileManager.assertFileExistence( result, filename2, false );
250         }
251         finally
252         {
253             FileUtils.deleteDirectory( result );
254         }
255     }
256 
257     public void testShouldFilterSeveralFiles() throws Exception
258     {
259         final File basedir = fileManager.createTempDir();
260 
261         final String filename1 = "one.txt";
262         final String filename2 = "two.txt";
263 
264         // this file will be filtered with a project expression
265         fileManager.createFile( basedir, filename1, "This is the filtered artifactId: ${project.artifactId}." );
266         // this one fill be filtered with a filter file
267         fileManager.createFile( basedir, filename2, "This is the filtered 'foo' property: ${foo}." );
268         final File filterProps = fileManager.createFile( basedir, "filter.properties", "foo=bar" );
269 
270         final FileSet fs = new FileSet();
271         fs.setFiltered( true );
272         fs.setDirectory( basedir.getCanonicalPath() );
273         fs.addInclude( "**/*.txt" );
274 
275         enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath() ) );
276 
277         mockManager.replayAll();
278 
279         final FileSetFormatter formatter = new FileSetFormatter( configSource, logger );
280         final File result = formatter.formatFileSetForAssembly( basedir, fs );
281 
282         assertFalse( result.equals( basedir ) );
283 
284         try
285         {
286             fileManager.assertFileContents( result, filename1, "This is the filtered artifactId: artifact." );
287             fileManager.assertFileContents( result, filename2, "This is the filtered 'foo' property: bar." );
288 
289             mockManager.verifyAll();
290         }
291         finally
292         {
293             FileUtils.deleteDirectory( result );
294         }
295     }
296 
297     private void enableBasicFilteringConfiguration( final File basedir, final List<String> filterFilenames )
298         throws Exception
299     {
300         configSource.getTemporaryRootDirectory();
301         configSourceControl.setReturnValue( basedir );
302 
303         final Model model = new Model();
304         model.setArtifactId( "artifact" );
305         model.setGroupId( "group" );
306         model.setVersion( "version" );
307 
308         final MavenProject project = new MavenProject( model );
309         project.getBuild()
310                .setFilters( filterFilenames );
311 
312         configSource.getProject();
313         configSourceControl.setReturnValue( project, MockControl.ONE_OR_MORE );
314 
315         configSource.getMavenFileFilter();
316         configSourceControl.setReturnValue( lookup( "org.apache.maven.shared.filtering.MavenFileFilter" ),
317                                             MockControl.ONE_OR_MORE );
318 
319         configSource.getMavenSession();
320         configSourceControl.setReturnValue( null, MockControl.ONE_OR_MORE );
321 
322         configSource.getFilters();
323         configSourceControl.setReturnValue( Collections.EMPTY_LIST, MockControl.ONE_OR_MORE );
324     }
325 
326 }