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     public void testShouldConvertCRLFLineEndingsInFile() throws IOException, AssemblyFormattingException
117     {
118         final File basedir = fileManager.createTempDir();
119 
120         configSource.getTemporaryRootDirectory();
121         configSourceControl.setReturnValue( basedir );
122 
123         final File file = fileManager.createFile( basedir, "one.txt", "This is a\ntest." );
124 
125         mockManager.replayAll();
126 
127         final File result = new FileFormatter( configSource, logger ).format( file, false, "dos", "UTF-8" );
128 
129         assertEquals( "This is a\r\ntest.", fileManager.getFileContents( result ) );
130 
131         mockManager.verifyAll();
132     }
133 
134     public void testShouldConvertLFLineEndingsInFile() throws IOException, AssemblyFormattingException
135     {
136         final File basedir = fileManager.createTempDir();
137 
138         configSource.getTemporaryRootDirectory();
139         configSourceControl.setReturnValue( basedir );
140 
141         final File file = fileManager.createFile( basedir, "one.txt", "This is a\r\ntest." );
142 
143         mockManager.replayAll();
144 
145         final File result = new FileFormatter( configSource, logger ).format( file, false, "unix", "UTF-8" );
146 
147         assertEquals( "This is a\ntest.", fileManager.getFileContents( result ) );
148 
149         mockManager.verifyAll();
150     }
151 
152     public void testShouldFilterProjectExpressionInFile() throws Exception
153     {
154         final File basedir = fileManager.createTempDir();
155 
156         enableBasicFilteringConfiguration( basedir, null );
157 
158         final File file =
159             fileManager.createFile( basedir, "one.txt", "This is a test for project: ${artifactId} @artifactId@." );
160 
161         mockManager.replayAll();
162 
163         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
164 
165         assertEquals( "This is a test for project: artifact artifact.", fileManager.getFileContents( result ) );
166 
167         mockManager.verifyAll();
168     }
169 
170     public void testShouldFilterExpressionInPropertiesFileWithWindowsEscapes() throws Exception
171     {
172 
173         final File sourceDir = fileManager.createTempDir();
174         final MavenProject project = createBasicMavenProject();
175         final Build build = new Build();
176 
177         // project.build.outputDirectory = C:\out\deeper
178         build.setOutputDirectory( "C:\\out\\deeper" );
179         project.setBuild( build );
180 
181         enableBasicFilteringConfiguration( project, sourceDir );
182 
183         final File file = fileManager.createFile( sourceDir, "one.properties", "out=${project.build.outputDirectory}" );
184 
185         mockManager.replayAll();
186 
187         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
188 
189         // expect: C:\\out\\deeper
190         assertEquals( "out=C:\\\\out\\\\deeper", fileManager.getFileContents( result ) );
191 
192         mockManager.verifyAll();
193     }
194 
195     public void testShouldFilterExpressionInPropertiesFileWithoutWindowsEscapes() throws Exception
196     {
197 
198         final File sourceDir = fileManager.createTempDir();
199         final MavenProject project = createBasicMavenProject();
200         final Build build = new Build();
201         build.setOutputDirectory( "C:\\out\\deeper" );
202         project.setBuild( build );
203 
204         enableBasicFilteringConfiguration( project, sourceDir );
205 
206         final File file =
207             fileManager.createFile( sourceDir, "one.txt", "project.basedirA=${project.build.outputDirectory}" );
208 
209         mockManager.replayAll();
210 
211         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
212 
213         assertEquals( "project.basedirA=C:\\out\\deeper", fileManager.getFileContents( result ) );
214 
215         mockManager.verifyAll();
216     }
217 
218     public void testShouldFilterExpressionFromFiltersFileInFile() throws Exception
219     {
220         final File basedir = fileManager.createTempDir();
221 
222         final File filterProps = fileManager.createFile( basedir, "filter.properties", "property=Test" );
223 
224         enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath() ) );
225 
226         final File file =
227             fileManager.createFile( basedir, "one.txt", "This is a test for project: ${property} @property@." );
228 
229         mockManager.replayAll();
230 
231         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
232 
233         assertEquals( "This is a test for project: Test Test.", fileManager.getFileContents( result ) );
234 
235         mockManager.verifyAll();
236     }
237 
238     public void testShouldFilterExpressionFromFiltersFileInPropertiesFileWithoutWindowsEscapes() throws Exception
239     {
240         final File basedir = fileManager.createTempDir();
241 
242         final File filterProps = fileManager.createFile( basedir, "filter.properties", "property=C:\\\\Test" );
243 
244         enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath() ) );
245 
246         final File file = fileManager.createFile( basedir, "one.properties", "project: ${property} @property@." );
247 
248         mockManager.replayAll();
249 
250         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
251 
252         assertEquals( "project: C:\\\\Test C:\\\\Test.", fileManager.getFileContents( result ) );
253 
254         mockManager.verifyAll();
255     }
256 
257     public void testShouldFilterExpressionFromFiltersFileInNonPropertiesFileWithoutWindowsEscapes() throws Exception
258     {
259         final File basedir = fileManager.createTempDir();
260 
261         final File filterProps = fileManager.createFile( basedir, "filter.properties", "property=C:\\\\Test" );
262 
263         enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath() ) );
264 
265         final File file =
266             fileManager.createFile( basedir, "one.txt", "This is a test for project: ${property} @property@." );
267 
268         mockManager.replayAll();
269 
270         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
271 
272         assertEquals( "This is a test for project: C:\\Test C:\\Test.", fileManager.getFileContents( result ) );
273 
274         mockManager.verifyAll();
275     }
276 
277     public void testShouldNotIgnoreFirstWordInDotNotationExpressions() throws Exception
278     {
279         final File basedir = fileManager.createTempDir();
280 
281         enableBasicFilteringConfiguration( basedir, null );
282 
283         final File file =
284             fileManager.createFile( basedir, "one.txt", "testing ${bean.id} which used to resolve to project.id" );
285 
286         mockManager.replayAll();
287 
288         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
289 
290         assertEquals( "testing ${bean.id} which used to resolve to project.id", fileManager.getFileContents( result ) );
291 
292         mockManager.verifyAll();
293     }
294 
295     public void testShouldFilterExpressionsFromTwoFiltersFilesInFile() throws Exception
296     {
297         final File basedir = fileManager.createTempDir();
298 
299         final File filterProps = fileManager.createFile( basedir, "filter.properties", "property=Test" );
300         final File filterProps2 = fileManager.createFile( basedir, "filter2.properties", "otherProperty=OtherValue" );
301 
302         final List<String> filters = new ArrayList<String>();
303         filters.add( filterProps.getCanonicalPath() );
304         filters.add( filterProps2.getCanonicalPath() );
305 
306         enableBasicFilteringConfiguration( basedir, filters );
307 
308         final File file =
309             fileManager.createFile( basedir, "one.txt",
310                                     "property: ${property} @property@ otherProperty: ${otherProperty} @otherProperty@." );
311 
312         mockManager.replayAll();
313 
314         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
315 
316         assertEquals( "property: Test Test otherProperty: OtherValue OtherValue.", fileManager.getFileContents( result ) );
317 
318         mockManager.verifyAll();
319     }
320 
321     public void testShouldOverrideOneFilterValueWithAnotherAndFilterFile() throws Exception
322     {
323         final File basedir = fileManager.createTempDir();
324 
325         final File filterProps = fileManager.createFile( basedir, "filter.properties", "property=Test" );
326         final File filterProps2 = fileManager.createFile( basedir, "filter2.properties", "property=OtherValue" );
327 
328         final List<String> filters = new ArrayList<String>();
329         filters.add( filterProps.getCanonicalPath() );
330         filters.add( filterProps2.getCanonicalPath() );
331 
332         enableBasicFilteringConfiguration( basedir, filters );
333 
334         final File file = fileManager.createFile( basedir, "one.txt", "property: ${property} @property@." );
335 
336         mockManager.replayAll();
337 
338         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
339 
340         assertEquals( "property: OtherValue OtherValue.", fileManager.getFileContents( result ) );
341 
342         mockManager.verifyAll();
343     }
344 
345     public void testShouldOverrideProjectValueWithFilterValueAndFilterFile() throws Exception
346     {
347         final File basedir = fileManager.createTempDir();
348 
349         final File filterProps = fileManager.createFile( basedir, "filter.properties", "artifactId=Test" );
350 
351         final List<String> filters = new ArrayList<String>();
352         filters.add( filterProps.getCanonicalPath() );
353 
354         enableBasicFilteringConfiguration( basedir, filters );
355 
356         final File file =
357             fileManager.createFile( basedir, "one.txt", "project artifact-id: ${artifactId} @artifactId@." );
358 
359         mockManager.replayAll();
360 
361         final File result = new FileFormatter( configSource, logger ).format( file, true, null, "UTF-8" );
362 
363         assertEquals( "project artifact-id: Test Test.", fileManager.getFileContents( result ) );
364 
365         mockManager.verifyAll();
366     }
367 
368     private MavenProject createBasicMavenProject()
369     {
370         final Model model = new Model();
371         model.setArtifactId( "artifact" );
372         model.setGroupId( "group" );
373         model.setVersion( "version" );
374 
375         return new MavenProject( model );
376     }
377 
378     private void enableBasicFilteringConfiguration( final MavenProject project, final File basedir ) throws Exception
379     {
380         configSource.getTemporaryRootDirectory();
381         configSourceControl.setReturnValue( basedir );
382 
383         configSource.getEscapeString();
384         configSourceControl.setReturnValue( null, MockControl.ONE_OR_MORE );
385 
386         configSource.getProject();
387         configSourceControl.setReturnValue( project, MockControl.ONE_OR_MORE );
388 
389         configSource.getMavenFileFilter();
390         configSourceControl.setReturnValue( lookup( "org.apache.maven.shared.filtering.MavenFileFilter" ) );
391 
392         configSource.getMavenSession();
393         configSourceControl.setReturnValue( null );
394 
395         configSource.getFilters();
396         configSourceControl.setReturnValue( Collections.EMPTY_LIST );
397     }
398 
399     private void enableBasicFilteringConfiguration( final File basedir, final List<String> filterFilenames )
400         throws Exception
401     {
402         final MavenProject project = createBasicMavenProject();
403         if ( filterFilenames != null )
404         {
405             project.getBuild()
406                    .setFilters( filterFilenames );
407         }
408 
409         enableBasicFilteringConfiguration( project, basedir );
410     }
411 
412 }