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.io.FileInputStream;
24  import java.util.Properties;
25  
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.PlexusTestCase;
28  
29  /**
30   * @author Olivier Lamy
31   * @since 1.0-beta-1
32   *
33   */
34  public class TestReflectionProperties
35      extends PlexusTestCase
36  {
37  
38      public void testSimpleFiltering()
39          throws Exception
40      {
41          MavenProject mavenProject = new MavenProject();
42          mavenProject.setVersion( "1.0" );
43          mavenProject.setGroupId( "org.apache" );
44          Properties userProperties = new Properties();
45          userProperties.setProperty( "foo", "bar" );
46          MavenFileFilter mavenFileFilter = lookup( MavenFileFilter.class );
47  
48          File from = new File( getBasedir() + "/src/test/units-files/reflection-test.properties" );
49          File to = new File( getBasedir() + "/target/reflection-test.properties" );
50  
51          if ( to.exists() )
52          {
53              to.delete();
54          }
55          
56          mavenFileFilter.copyFile( from, to, true, mavenProject, null, false, null,
57                                    new StubMavenSession( userProperties ) );
58  
59          Properties reading = new Properties();
60  
61          try ( FileInputStream readFileInputStream = new FileInputStream( to ) )
62          {
63              reading.load( readFileInputStream );
64          }
65  
66          assertEquals( "1.0", reading.get( "version" ) );
67          assertEquals( "org.apache", reading.get( "groupId" ) );
68          assertEquals( "bar", reading.get( "foo" ) );
69          assertEquals( "none filtered", reading.get( "none" ) );
70      }
71  
72      public void testSimpleNonFiltering()
73          throws Exception
74      {
75          
76          MavenProject mavenProject = new MavenProject();
77          mavenProject.setVersion( "1.0" );
78          mavenProject.setGroupId( "org.apache" );
79          Properties userProperties = new Properties();
80          userProperties.setProperty( "foo", "bar" );
81          MavenFileFilter mavenFileFilter = lookup( MavenFileFilter.class );
82  
83          File from = new File( getBasedir() + "/src/test/units-files/reflection-test.properties" );
84          File to = new File( getBasedir() + "/target/reflection-test.properties" );
85  
86          if ( to.exists() )
87          {
88              to.delete();
89          }
90  
91          mavenFileFilter.copyFile( from, to, false, mavenProject, null, false, null,
92                                    new StubMavenSession( userProperties ) );
93  
94          Properties reading = new Properties();
95  
96          try ( FileInputStream readFileInputStream = new FileInputStream( to ); )
97          {
98              reading.load( readFileInputStream );
99          }
100 
101         assertEquals( "${pom.version}", reading.get( "version" ) );
102         assertEquals( "${pom.groupId}", reading.get( "groupId" ) );
103         assertEquals( "${foo}", reading.get( "foo" ) );
104         assertEquals( "none filtered", reading.get( "none" ) );
105     }
106 
107 }