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.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Properties;
27  
28  import org.codehaus.plexus.PlexusTestCase;
29  import org.codehaus.plexus.logging.Logger;
30  
31  /**
32   * @author Olivier Lamy
33   * @since 1.0-beta-1
34   *
35   */
36  public class PropertyUtilsTest
37      extends PlexusTestCase
38  {
39      private static File testDirectory = new File( getBasedir(), "target/test-classes/" );
40  
41      public void testBasic()
42          throws Exception
43      {
44          File basicProp = new File( testDirectory, "basic.properties" );
45  
46          if ( basicProp.exists() )
47          {
48              basicProp.delete();
49          }
50  
51          basicProp.createNewFile();
52          try ( FileWriter writer = new FileWriter( basicProp ) )
53          {
54              writer.write( "ghost=${non_existent}\n" );
55              writer.write( "key=${untat_na_damgo}\n" );
56              writer.write( "untat_na_damgo=gani_man\n" );
57              writer.flush();
58          }
59  
60          Properties prop = PropertyUtils.loadPropertyFile( basicProp, false, false );
61          assertTrue( prop.getProperty( "key" ).equals( "gani_man" ) );
62          assertTrue( prop.getProperty( "ghost" ).equals( "${non_existent}" ) );
63      }
64  
65      public void testSystemProperties()
66          throws Exception
67      {
68          File systemProp = new File( testDirectory, "system.properties" );
69  
70          if ( systemProp.exists() )
71          {
72              systemProp.delete();
73          }
74  
75          systemProp.createNewFile();
76          try ( FileWriter writer = new FileWriter( systemProp ) )
77          {
78              writer.write( "key=${user.dir}" );
79              writer.flush();
80          }
81  
82          Properties prop = PropertyUtils.loadPropertyFile( systemProp, false, true );
83          assertTrue( prop.getProperty( "key" ).equals( System.getProperty( "user.dir" ) ) );
84      }
85  
86      public void testException()
87          throws Exception
88      {
89          File nonExistent = new File( testDirectory, "not_existent_file" );
90  
91          assertFalse( "property file exist: " + nonExistent.toString(), nonExistent.exists() );
92  
93          try
94          {
95              PropertyUtils.loadPropertyFile( nonExistent, true, false );
96              assertTrue( "Exception failed", false );
97          }
98          catch ( Exception ex )
99          {
100             // exception ok
101         }
102     }
103 
104     public void testloadpropertiesFile()
105         throws Exception
106     {
107         File propertyFile = new File( getBasedir() + "/src/test/units-files/propertyutils-test.properties" );
108         Properties baseProps = new Properties();
109         baseProps.put( "pom.version", "realVersion" );
110 
111         Properties interpolated = PropertyUtils.loadPropertyFile( propertyFile, baseProps );
112         assertEquals( "realVersion", interpolated.get( "version" ) );
113         assertEquals( "${foo}", interpolated.get( "foo" ) );
114         assertEquals( "realVersion", interpolated.get( "bar" ) );
115         assertEquals( "none filtered", interpolated.get( "none" ) );
116     }
117 
118     /**
119      * Test case to reproduce MSHARED-417
120      *
121      * @throws IOException if problem writing file
122      */
123     public void testCircularReferences()
124         throws IOException
125     {
126         File basicProp = new File( testDirectory, "circular.properties" );
127 
128         if ( basicProp.exists() )
129         {
130             basicProp.delete();
131         }
132 
133         basicProp.createNewFile();
134         try( FileWriter writer = new FileWriter( basicProp ) )
135         {
136             writer.write( "test=${test2}\n" );
137             writer.write( "test2=${test2}\n" );
138             writer.flush();
139         }
140 
141         MockLogger logger = new MockLogger();
142 
143         Properties prop = PropertyUtils.loadPropertyFile( basicProp, null, logger );
144         assertEquals( "${test2}", prop.getProperty( "test" ) );
145         assertEquals( "${test2}", prop.getProperty( "test2" ) );
146         assertEquals( 2, logger.warnMsgs.size() );
147         assertWarn( "Circular reference between properties detected: test2 => test2", logger );
148         assertWarn( "Circular reference between properties detected: test => test2 => test2", logger );
149     }
150 
151     /**
152      * Test case to reproduce MSHARED-417
153      *
154      * @throws IOException if problem writing file
155      */
156     public void testCircularReferences3Vars()
157         throws IOException
158     {
159         File basicProp = new File( testDirectory, "circular.properties" );
160 
161         if ( basicProp.exists() )
162         {
163             basicProp.delete();
164         }
165 
166         basicProp.createNewFile();
167         try ( FileWriter writer = new FileWriter( basicProp ) )
168         {
169             writer.write( "test=${test2}\n" );
170             writer.write( "test2=${test3}\n" );
171             writer.write( "test3=${test}\n" );
172             writer.flush();
173         }
174 
175         MockLogger logger = new MockLogger();
176 
177         Properties prop = PropertyUtils.loadPropertyFile( basicProp, null, logger );
178         assertEquals( "${test2}", prop.getProperty( "test" ) );
179         assertEquals( "${test3}", prop.getProperty( "test2" ) );
180         assertEquals( "${test}", prop.getProperty( "test3" ) );
181         assertEquals( 3, logger.warnMsgs.size() );
182         assertWarn( "Circular reference between properties detected: test3 => test => test2 => test3", logger );
183         assertWarn( "Circular reference between properties detected: test2 => test3 => test => test2", logger );
184         assertWarn( "Circular reference between properties detected: test => test2 => test3 => test", logger );
185     }
186 
187     private void assertWarn( String expected, MockLogger logger )
188     {
189         assertTrue( logger.warnMsgs.contains( expected ) );
190     }
191 
192     private static class MockLogger
193         implements Logger
194     {
195 
196         ArrayList<String> warnMsgs = new ArrayList<>();
197 
198         @Override
199         public void debug( String message )
200         {
201             // nothing
202         }
203 
204         @Override
205         public void debug( String message, Throwable throwable )
206         {
207             // nothing
208         }
209 
210         @Override
211         public boolean isDebugEnabled()
212         {
213             return false;
214         }
215 
216         @Override
217         public void info( String message )
218         {
219             // nothing
220         }
221 
222         @Override
223         public void info( String message, Throwable throwable )
224         {
225             // nothing
226         }
227 
228         @Override
229         public boolean isInfoEnabled()
230         {
231             return false;
232         }
233 
234         @Override
235         public void warn( String message )
236         {
237             warnMsgs.add( message );
238         }
239 
240         @Override
241         public void warn( String message, Throwable throwable )
242         {
243             // nothing
244         }
245 
246         @Override
247         public boolean isWarnEnabled()
248         {
249             return false;
250         }
251 
252         @Override
253         public void error( String message )
254         {
255             // nothing
256         }
257 
258         @Override
259         public void error( String message, Throwable throwable )
260         {
261             // nothing
262         }
263 
264         @Override
265         public boolean isErrorEnabled()
266         {
267             return false;
268         }
269 
270         @Override
271         public void fatalError( String message )
272         {
273             // nothing
274         }
275 
276         @Override
277         public void fatalError( String message, Throwable throwable )
278         {
279             // nothing
280         }
281 
282         @Override
283         public boolean isFatalErrorEnabled()
284         {
285             return false;
286         }
287 
288         @Override
289         public int getThreshold()
290         {
291             return 0;
292         }
293 
294         @Override
295         public void setThreshold( int threshold )
296         {
297             // nothing
298         }
299 
300         @Override
301         public Logger getChildLogger( String name )
302         {
303             return null;
304         }
305 
306         @Override
307         public String getName()
308         {
309             return null;
310         }
311     }
312 }