View Javadoc

1   package org.apache.maven.shared.filtering;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   import java.util.List;
6   import java.util.Properties;
7   
8   import org.codehaus.plexus.PlexusTestCase;
9   import org.codehaus.plexus.util.FileUtils;
10  
11  /*
12   * Licensed to the Apache Software Foundation (ASF) under one
13   * or more contributor license agreements.  See the NOTICE file
14   * distributed with this work for additional information
15   * regarding copyright ownership.  The ASF licenses this file
16   * to you under the Apache License, Version 2.0 (the
17   * "License"); you may not use this file except in compliance
18   * with the License.  You may obtain a copy of the License at
19   *
20   *    http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing,
23   * software distributed under the License is distributed on an
24   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25   * KIND, either express or implied.  See the License for the
26   * specific language governing permissions and limitations
27   * under the License.
28   */
29  
30  /**
31   * @author <a href="mailto:olamy@apache.org">olamy</a>
32   * @version $Id: DefaultMavenFileFilterTest.java 1065840 2011-01-31 22:52:32Z olamy $
33   */
34  public class DefaultMavenFileFilterTest
35      extends PlexusTestCase
36  {
37  
38      File to = new File( getBasedir(), "target/reflection-test.properties" );
39  
40      protected void setUp()
41          throws Exception
42      {
43          super.setUp();
44          if ( to.exists() )
45          {
46              FileUtils.forceDelete( to );
47          }
48      }
49  
50      public void testNotOverwriteFile()
51          throws Exception
52      {
53          MavenFileFilter mavenFileFilter = (MavenFileFilter) lookup( MavenFileFilter.class.getName(), "default" );
54  
55          File from = new File( getBasedir(), "src/test/units-files/reflection-test.properties" );
56  
57          mavenFileFilter.copyFile( from, to, false, null, null );
58  
59          from = new File( getBasedir(), "src/test/units-files/reflection-test-older.properties" );
60  
61          // very old file :-)
62          from.setLastModified( 1 );
63  
64          to.setLastModified( System.currentTimeMillis() );
65  
66          mavenFileFilter.copyFile( from, to, false, null, null );
67  
68          Properties properties = PropertyUtils.loadPropertyFile( to, null );
69          assertEquals( "${pom.version}", properties.getProperty( "version" ) );
70  
71      }
72  
73      public void testOverwriteFile()
74          throws Exception
75      {
76          MavenFileFilter mavenFileFilter = (MavenFileFilter) lookup( MavenFileFilter.class.getName(), "default" );
77  
78          File from = new File( getBasedir(), "src/test/units-files/reflection-test.properties" );
79  
80          mavenFileFilter.copyFile( from, to, false, null, null );
81  
82          from = new File( getBasedir(), "src/test/units-files/reflection-test-older.properties" );
83  
84          // very old file :-)
85          from.setLastModified( 1 );
86  
87          to.setLastModified( System.currentTimeMillis() );
88  
89          mavenFileFilter.copyFile( from, to, false, null, null, true );
90  
91          Properties properties = PropertyUtils.loadPropertyFile( to, null );
92          assertEquals( "older file", properties.getProperty( "version" ) );
93  
94      }
95  
96      public void testNullSafeDefaultFilterWrappers()
97          throws Exception
98      {
99          MavenFileFilter mavenFileFilter = (MavenFileFilter) lookup( MavenFileFilter.class.getName(), "default" );
100 
101         mavenFileFilter.getDefaultFilterWrappers( null, null, false, null, null );
102 
103         // shouldn't fail
104     }
105 
106     public void testMultiFilterFileInheritance()
107         throws Exception
108     {
109         DefaultMavenFileFilter mavenFileFilter = new DefaultMavenFileFilter();
110 
111         File testDir = new File(getBasedir(), "src/test/units-files/MSHARED-177");
112 
113         List filters = new ArrayList();
114 
115         filters.add(new File(testDir, "first_filter_file.properties").getAbsolutePath());
116         filters.add(new File(testDir, "second_filter_file.properties").getAbsolutePath());
117         filters.add(new File(testDir, "third_filter_file.properties").getAbsolutePath());
118 
119         final Properties filterProperties = new Properties();
120 
121         mavenFileFilter.loadProperties(filterProperties, filters, new Properties() );
122 
123         assertTrue( filterProperties.getProperty( "third_filter_key" ).equals( "first and second" ) );
124     }
125 }