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.Build;
23  import org.apache.maven.model.Model;
24  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
25  import org.apache.maven.plugin.assembly.testutils.MockManager;
26  import org.apache.maven.plugin.assembly.testutils.TestFileManager;
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.plexus.PlexusTestCase;
29  import org.codehaus.plexus.logging.Logger;
30  import org.codehaus.plexus.logging.console.ConsoleLogger;
31  import org.easymock.MockControl;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.util.ArrayList;
36  import java.util.Collections;
37  import java.util.List;
38  
39  public class FileFormatterTest
40      extends PlexusTestCase
41  {
42  
43      private final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
44  
45      private final TestFileManager fileManager = new TestFileManager( "fileFormatterTest.", "" );
46  
47      private final MockManager mockManager = new MockManager();
48  
49      private AssemblerConfigurationSource configSource;
50  
51      private MockControl configSourceControl;
52  
53      @Override
54      public void setUp() throws Exception
55      {
56          super.setUp();
57  
58          configSourceControl = MockControl.createControl( AssemblerConfigurationSource.class );
59          mockManager.add( configSourceControl );
60  
61          configSource = (AssemblerConfigurationSource) configSourceControl.getMock();
62      }
63  
64      @Override
65      public void tearDown() throws IOException
66      {
67          fileManager.cleanUp();
68      }
69  
70      public void testTemporaryRootDirectoryNotExist() throws IOException, AssemblyFormattingException
71      {
72          final File basedir = fileManager.createTempDir();
73          final File tempRoot = new File( basedir, "tempdir" );
74          configSource.getTemporaryRootDirectory();
75          configSourceControl.setReturnValue( tempRoot );
76  
77          final File file = fileManager.createFile( basedir, "one.txt", "This is a\ntest." );
78  
79          mockManager.replayAll();
80  
81          final File result = new FileFormatter( configSource, logger ).format( file, false, "dos", "UTF-8" );
82  
83          assertTrue( !file.equals( result ) );
84  
85          mockManager.verifyAll();
86      }
87  
88      public void testShouldNotTransformOneFile() throws IOException, AssemblyFormattingException
89      {
90          final File basedir = fileManager.createTempDir();
91  
92          configSource.getTemporaryRootDirectory();
93          configSourceControl.setReturnValue( basedir );
94  
95          // Model model = new Model();
96          // model.setArtifactId( "artifact" );
97          // model.setGroupId( "group" );
98          // model.setVersion( "version" );
99          //
100         // MavenProject project = new MavenProject( model );
101         //
102         // configSource.getProject();
103         // configSourceControl.setReturnValue( project );
104 
105         final File file = fileManager.createFile( basedir, "one.txt", "This is a test." );
106 
107         mockManager.replayAll();
108 
109         final File result = new FileFormatter( configSource, logger ).format( file, false, null, "UTF-8" );
110 
111         assertEquals( file, result );
112 
113         mockManager.verifyAll();
114     }
115 
116     // TODO: Should not be appending line-ending at the end if there is none in the source.
117     public void testShouldConvertCRLFLineEndingsInFile() throws IOException, AssemblyFormattingException
118     {
119         final File basedir = fileManager.createTempDir();
120 
121         configSource.getTemporaryRootDirectory();
122         configSourceControl.setReturnValue( basedir );
123 
124         final File file = fileManager.createFile( basedir, "one.txt", "This is a\ntest." );
125 
126         mockManager.replayAll();
127 
128         final File result = new FileFormatter( configSource, logger ).format( file, false, "dos", "UTF-8" );
129 
130         assertEquals( "This is a\r\ntest.\r\n", fileManager.getFileContents( result ) );
131 
132         mockManager.verifyAll();
133     }
134 
135     // TODO: Should not be appending line-ending at the end if there is none in the source.
136     public void testShouldConvertLFLineEndingsInFile() throws IOException, AssemblyFormattingException
137     {
138         final File basedir = fileManager.createTempDir();
139 
140         configSource.getTemporaryRootDirectory();
141         configSourceControl.setReturnValue( basedir );
142 
143         final File file = fileManager.createFile( basedir, "one.txt", "This is a\r\ntest." );
144 
145         mockManager.replayAll();
146 
147         final File result = new FileFormatter( configSource, logger ).format( file, false, "unix", "UTF-8" );
148 
149         assertEquals( "This is a\ntest.\n", fileManager.getFileContents( result ) );
150 
151         mockManager.verifyAll();
152     }
153 
154     public void testShouldFilterProjectExpressionInFile() throws Exception
155     {
156         final File basedir = fileManager.createTempDir();
157 
158         enableBasicFilteringConfiguration( basedir, null );
159 
160         final File file =
161             fileManager.createFile( basedir, "one.txt", "This is a test for project: ${artifactId} @artifactId@." );
162 
163         mockManager.replayAll();
164 
165         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
166 
167         assertEquals( "This is a test for project: artifact artifact.", fileManager.getFileContents( result ) );
168 
169         mockManager.verifyAll();
170     }
171 
172     public void testShouldFilterExpressionInPropertiesFileWithWindowsEscapes() throws Exception
173     {
174 
175         final File sourceDir = fileManager.createTempDir();
176         final MavenProject project = createBasicMavenProject();
177         final Build build = new Build();
178 
179         // project.build.outputDirectory = C:\out\deeper
180         build.setOutputDirectory( "C:\\out\\deeper" );
181         project.setBuild( build );
182 
183         enableBasicFilteringConfiguration( project, sourceDir );
184 
185         final File file = fileManager.createFile( sourceDir, "one.properties", "out=${project.build.outputDirectory}" );
186 
187         mockManager.replayAll();
188 
189         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
190 
191         // expect: C:\\out\\deeper
192         assertEquals( "out=C:\\\\out\\\\deeper", fileManager.getFileContents( result ) );
193 
194         mockManager.verifyAll();
195     }
196 
197     public void testShouldFilterExpressionInPropertiesFileWithoutWindowsEscapes() throws Exception
198     {
199 
200         final File sourceDir = fileManager.createTempDir();
201         final MavenProject project = createBasicMavenProject();
202         final Build build = new Build();
203         build.setOutputDirectory( "C:\\out\\deeper" );
204         project.setBuild( build );
205 
206         enableBasicFilteringConfiguration( project, sourceDir );
207 
208         final File file =
209             fileManager.createFile( sourceDir, "one.txt", "project.basedirA=${project.build.outputDirectory}" );
210 
211         mockManager.replayAll();
212 
213         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
214 
215         assertEquals( "project.basedirA=C:\\out\\deeper", fileManager.getFileContents( result ) );
216 
217         mockManager.verifyAll();
218     }
219 
220     public void testShouldFilterExpressionFromFiltersFileInFile() throws Exception
221     {
222         final File basedir = fileManager.createTempDir();
223 
224         final File filterProps = fileManager.createFile( basedir, "filter.properties", "property=Test" );
225 
226         enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath() ) );
227 
228         final File file =
229             fileManager.createFile( basedir, "one.txt", "This is a test for project: ${property} @property@." );
230 
231         mockManager.replayAll();
232 
233         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
234 
235         assertEquals( "This is a test for project: Test Test.", fileManager.getFileContents( result ) );
236 
237         mockManager.verifyAll();
238     }
239 
240     public void testShouldFilterExpressionFromFiltersFileInPropertiesFileWithoutWindowsEscapes() throws Exception
241     {
242         final File basedir = fileManager.createTempDir();
243 
244         final File filterProps = fileManager.createFile( basedir, "filter.properties", "property=C:\\\\Test" );
245 
246         enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath() ) );
247 
248         final File file = fileManager.createFile( basedir, "one.properties", "project: ${property} @property@." );
249 
250         mockManager.replayAll();
251 
252         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
253 
254         assertEquals( "project: C:\\\\Test C:\\\\Test.", fileManager.getFileContents( result ) );
255 
256         mockManager.verifyAll();
257     }
258 
259     public void testShouldFilterExpressionFromFiltersFileInNonPropertiesFileWithoutWindowsEscapes() throws Exception
260     {
261         final File basedir = fileManager.createTempDir();
262 
263         final File filterProps = fileManager.createFile( basedir, "filter.properties", "property=C:\\\\Test" );
264 
265         enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath() ) );
266 
267         final File file =
268             fileManager.createFile( basedir, "one.txt", "This is a test for project: ${property} @property@." );
269 
270         mockManager.replayAll();
271 
272         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
273 
274         assertEquals( "This is a test for project: C:\\Test C:\\Test.", fileManager.getFileContents( result ) );
275 
276         mockManager.verifyAll();
277     }
278 
279     public void testShouldNotIgnoreFirstWordInDotNotationExpressions() throws Exception
280     {
281         final File basedir = fileManager.createTempDir();
282 
283         enableBasicFilteringConfiguration( basedir, null );
284 
285         final File file =
286             fileManager.createFile( basedir, "one.txt", "testing ${bean.id} which used to resolve to project.id" );
287 
288         mockManager.replayAll();
289 
290         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
291 
292         assertEquals( "testing ${bean.id} which used to resolve to project.id", fileManager.getFileContents( result ) );
293 
294         mockManager.verifyAll();
295     }
296 
297     public void testShouldFilterExpressionsFromTwoFiltersFilesInFile() throws Exception
298     {
299         final File basedir = fileManager.createTempDir();
300 
301         final File filterProps = fileManager.createFile( basedir, "filter.properties", "property=Test" );
302         final File filterProps2 = fileManager.createFile( basedir, "filter2.properties", "otherProperty=OtherValue" );
303 
304         final List<String> filters = new ArrayList<String>();
305         filters.add( filterProps.getCanonicalPath() );
306         filters.add( filterProps2.getCanonicalPath() );
307 
308         enableBasicFilteringConfiguration( basedir, filters );
309 
310         final File file =
311             fileManager.createFile( basedir, "one.txt",
312                                     "property: ${property} @property@ otherProperty: ${otherProperty} @otherProperty@." );
313 
314         mockManager.replayAll();
315 
316         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
317 
318         assertEquals( "property: Test Test otherProperty: OtherValue OtherValue.", fileManager.getFileContents( result ) );
319 
320         mockManager.verifyAll();
321     }
322 
323     public void testShouldOverrideOneFilterValueWithAnotherAndFilterFile() throws Exception
324     {
325         final File basedir = fileManager.createTempDir();
326 
327         final File filterProps = fileManager.createFile( basedir, "filter.properties", "property=Test" );
328         final File filterProps2 = fileManager.createFile( basedir, "filter2.properties", "property=OtherValue" );
329 
330         final List<String> filters = new ArrayList<String>();
331         filters.add( filterProps.getCanonicalPath() );
332         filters.add( filterProps2.getCanonicalPath() );
333 
334         enableBasicFilteringConfiguration( basedir, filters );
335 
336         final File file = fileManager.createFile( basedir, "one.txt", "property: ${property} @property@." );
337 
338         mockManager.replayAll();
339 
340         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
341 
342         assertEquals( "property: OtherValue OtherValue.", fileManager.getFileContents( result ) );
343 
344         mockManager.verifyAll();
345     }
346 
347     public void testShouldOverrideProjectValueWithFilterValueAndFilterFile() throws Exception
348     {
349         final File basedir = fileManager.createTempDir();
350 
351         final File filterProps = fileManager.createFile( basedir, "filter.properties", "artifactId=Test" );
352 
353         final List<String> filters = new ArrayList<String>();
354         filters.add( filterProps.getCanonicalPath() );
355 
356         enableBasicFilteringConfiguration( basedir, filters );
357 
358         final File file =
359             fileManager.createFile( basedir, "one.txt", "project artifact-id: ${artifactId} @artifactId@." );
360 
361         mockManager.replayAll();
362 
363         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
364 
365         assertEquals( "project artifact-id: Test Test.", fileManager.getFileContents( result ) );
366 
367         mockManager.verifyAll();
368     }
369 
370     private MavenProject createBasicMavenProject()
371     {
372         final Model model = new Model();
373         model.setArtifactId( "artifact" );
374         model.setGroupId( "group" );
375         model.setVersion( "version" );
376 
377         return new MavenProject( model );
378     }
379 
380     private void enableBasicFilteringConfiguration( final MavenProject project, final File basedir ) throws Exception
381     {
382         configSource.getTemporaryRootDirectory();
383         configSourceControl.setReturnValue( basedir );
384 
385         configSource.getProject();
386         configSourceControl.setReturnValue( project, MockControl.ONE_OR_MORE );
387 
388         configSource.getMavenFileFilter();
389         configSourceControl.setReturnValue( lookup( "org.apache.maven.shared.filtering.MavenFileFilter" ) );
390 
391         configSource.getMavenSession();
392         configSourceControl.setReturnValue( null );
393 
394         configSource.getFilters();
395         configSourceControl.setReturnValue( Collections.EMPTY_LIST );
396     }
397 
398     private void enableBasicFilteringConfiguration( final File basedir, final List<String> filterFilenames )
399         throws Exception
400     {
401         final MavenProject project = createBasicMavenProject();
402         if ( filterFilenames != null )
403         {
404             project.getBuild()
405                    .setFilters( filterFilenames );
406         }
407 
408         enableBasicFilteringConfiguration( project, basedir );
409     }
410 
411 }