1 package org.apache.maven.shared.filtering;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
30
31
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
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 }