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 <a href="mailto:olamy@apache.org">olamy</a>
31   * @since 1.0-beta-1
32   * @version $Id: TestReflectionProperties.java 1058704 2011-01-13 18:29:54Z dennisl $
33   */
34  public class TestReflectionProperties
35      extends PlexusTestCase
36  {
37  
38      public void testSimpleFiltering()
39          throws Exception
40      {
41          FileInputStream readFileInputStream = null;
42          try
43          {
44              MavenProject mavenProject = new MavenProject();
45              mavenProject.setVersion( "1.0" );
46              mavenProject.setGroupId( "org.apache" );
47              Properties executionProperties = new Properties();
48              executionProperties.setProperty( "foo", "bar" );
49              MavenFileFilter mavenFileFilter = (MavenFileFilter) lookup( MavenFileFilter.class.getName(), "default" );
50  
51              File from = new File( getBasedir() + "/src/test/units-files/reflection-test.properties" );
52              File to = new File( getBasedir() + "/target/reflection-test.properties" );
53  
54              if (to.exists())
55              {
56                  to.delete();
57              }            
58              
59              mavenFileFilter.copyFile( from, to, true, mavenProject, null, false, null,
60                                        new StubMavenSession( executionProperties ) );
61  
62              Properties reading = new Properties();
63              readFileInputStream = new FileInputStream( to );
64              reading.load( readFileInputStream );
65              assertEquals( "1.0", reading.get( "version" ) );
66              assertEquals( "org.apache", reading.get( "groupId" ) );
67              assertEquals( "bar", reading.get( "foo" ) );
68              assertEquals( "none filtered", reading.get( "none" ) );
69          }
70          finally
71          {
72              if ( readFileInputStream != null )
73              {
74                  readFileInputStream.close();
75              }
76          }
77  
78      }
79  
80      public void testSimpleNonFiltering()
81          throws Exception
82      {
83          FileInputStream readFileInputStream = null;
84          try
85          {
86              MavenProject mavenProject = new MavenProject();
87              mavenProject.setVersion( "1.0" );
88              mavenProject.setGroupId( "org.apache" );
89              Properties executionProperties = new Properties();
90              executionProperties.setProperty( "foo", "bar" );
91              MavenFileFilter mavenFileFilter = (MavenFileFilter) lookup( MavenFileFilter.class.getName(), "default" );
92  
93              File from = new File( getBasedir() + "/src/test/units-files/reflection-test.properties" );
94              File to = new File( getBasedir() + "/target/reflection-test.properties" );
95  
96              if (to.exists())
97              {
98                  to.delete();
99              }
100             
101             mavenFileFilter.copyFile( from, to, false, mavenProject, null, false, null,
102                                       new StubMavenSession( executionProperties ) );
103 
104             Properties reading = new Properties();
105             readFileInputStream = new FileInputStream( to );
106             reading.load( readFileInputStream );
107             assertEquals( "${pom.version}", reading.get( "version" ) );
108             assertEquals( "${pom.groupId}", reading.get( "groupId" ) );
109             assertEquals( "${foo}", reading.get( "foo" ) );
110             assertEquals( "none filtered", reading.get( "none" ) );
111         }
112         finally
113         {
114             if ( readFileInputStream != null )
115             {
116                 readFileInputStream.close();
117             }
118         }
119 
120     }    
121     
122 }