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.FileWriter;
24  import java.util.Properties;
25  
26  import org.codehaus.plexus.PlexusTestCase;
27  
28  /**
29   * @author <a href="mailto:olamy@apache.org">olamy</a>
30   * @since 1.0-beta-1
31   * @version $Id: PropertyUtilsTest.java 1058704 2011-01-13 18:29:54Z dennisl $
32   */
33  public class PropertyUtilsTest
34      extends PlexusTestCase
35  {
36      private static File testDirectory = new File( getBasedir(), "target/test-classes/" );
37  
38      public void testBasic()
39          throws Exception
40      {
41          File basicProp = new File( testDirectory, "basic.properties" );
42  
43          if ( basicProp.exists() )
44          {
45              basicProp.delete();
46          }
47  
48          basicProp.createNewFile();
49          FileWriter writer = new FileWriter( basicProp );
50  
51          writer.write( "ghost=${non_existent}\n" );
52          writer.write( "key=${untat_na_damgo}\n" );
53          writer.write( "untat_na_damgo=gani_man\n" );
54          writer.flush();
55          writer.close();
56  
57          Properties prop = PropertyUtils.loadPropertyFile( basicProp, false, false );
58          assertTrue( prop.getProperty( "key" ).equals( "gani_man" ) );
59          assertTrue( prop.getProperty( "ghost" ).equals( "${non_existent}" ) );
60      }
61  
62      public void testSystemProperties()
63          throws Exception
64      {
65          File systemProp = new File( testDirectory, "system.properties" );
66  
67          if ( systemProp.exists() )
68          {
69              systemProp.delete();
70          }
71  
72          systemProp.createNewFile();
73          FileWriter writer = new FileWriter( systemProp );
74  
75          writer.write( "key=${user.dir}" );
76          writer.flush();
77          writer.close();
78  
79          Properties prop = PropertyUtils.loadPropertyFile( systemProp, false, true );
80          assertTrue( prop.getProperty( "key" ).equals( System.getProperty( "user.dir" ) ) );
81      }
82  
83      public void testException()
84          throws Exception
85      {
86          File nonExistent = new File( testDirectory, "not_existent_file" );
87  
88          assertFalse( "property file exist: " + nonExistent.toString(), nonExistent.exists() );
89  
90          try
91          {
92              PropertyUtils.loadPropertyFile( nonExistent, true, false );
93              assertTrue( "Exception failed", false );
94          }
95          catch ( Exception ex )
96          {
97              // exception ok
98          }
99      }
100 
101     public void testloadpropertiesFile()
102         throws Exception
103     {
104         File propertyFile = new File( getBasedir() + "/src/test/units-files/propertyutils-test.properties" );
105         Properties baseProps = new Properties();
106         baseProps.put( "pom.version", "realVersion" );
107 
108         Properties interpolated = PropertyUtils.loadPropertyFile( propertyFile, baseProps );
109         assertEquals( "realVersion", interpolated.get( "version" ) );
110         assertEquals( "${foo}", interpolated.get( "foo" ) );
111         assertEquals( "realVersion", interpolated.get( "bar" ) );
112         assertEquals( "none filtered", interpolated.get( "none" ) );
113     }
114 }