View Javadoc

1   package org.apache.maven.shared.filtering;
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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import org.codehaus.plexus.PlexusTestCase;
28  import org.codehaus.plexus.util.FileUtils;
29  
30  /**
31   * @author <a href="mailto:olamy@apache.org">olamy</a>
32   * @version $Id: DefaultMavenFileFilterTest.java 1072705 2011-02-20 20:09:17Z bentmann $
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 }