View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.filtering;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  import java.nio.charset.StandardCharsets;
27  import java.util.Properties;
28  
29  import org.junit.jupiter.api.Test;
30  
31  import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
32  import static org.junit.jupiter.api.Assertions.*;
33  
34  /**
35   * @author Olivier Lamy
36   * @since 1.0-beta-1
37   */
38  class PropertyUtilsTest {
39      private static File testDirectory = new File(getBasedir(), "target/test-classes/");
40  
41      @Test
42      void basic() throws Exception {
43          File basicProperties = File.createTempFile("basic", ".properties");
44          basicProperties.deleteOnExit();
45  
46          try (Writer writer = new OutputStreamWriter(new FileOutputStream(basicProperties), StandardCharsets.UTF_8)) {
47              writer.write("ghost=${non_existent}\n");
48              writer.write("key=${untat_na_damgo}\n");
49              writer.write("untat_na_damgo=gani_man\n");
50              writer.flush();
51          }
52  
53          Properties properties = PropertyUtils.loadPropertyFile(basicProperties, false, false);
54          assertEquals("gani_man", properties.getProperty("key"));
55          assertEquals("${non_existent}", properties.getProperty("ghost"));
56      }
57  
58      @Test
59      void systemProperties() throws Exception {
60          File systemProperties = File.createTempFile("system", ".properties");
61          systemProperties.deleteOnExit();
62  
63          try (Writer writer = new OutputStreamWriter(new FileOutputStream(systemProperties), StandardCharsets.UTF_8)) {
64              writer.write("key=${user.dir}");
65              writer.flush();
66          }
67  
68          Properties properties = PropertyUtils.loadPropertyFile(systemProperties, false, true);
69          assertEquals(System.getProperty("user.dir"), properties.getProperty("key"));
70      }
71  
72      @Test
73      void exception() throws Exception {
74          File nonExistent = new File(testDirectory, "not_existent_file");
75  
76          assertFalse(nonExistent.exists(), "property file exist: " + nonExistent);
77  
78          try {
79              PropertyUtils.loadPropertyFile(nonExistent, true, false);
80              fail("Expected exception not thrown");
81          } catch (Exception ex) {
82              // exception ok
83          }
84      }
85  
86      @Test
87      void loadPropertiesFile() throws Exception {
88          File propertyFile = new File(getBasedir() + "/src/test/units-files/propertyutils-test.properties");
89          Properties baseProperties = new Properties();
90          baseProperties.put("pom.version", "realVersion");
91  
92          Properties interpolated = PropertyUtils.loadPropertyFile(propertyFile, baseProperties);
93          assertEquals("realVersion", interpolated.get("version"));
94          assertEquals("${foo}", interpolated.get("foo"));
95          assertEquals("realVersion", interpolated.get("bar"));
96          assertEquals("none filtered", interpolated.get("none"));
97      }
98  
99      /**
100      * Test case to reproduce MSHARED-417
101      *
102      * @throws IOException if problem writing file
103      */
104     @Test
105     void circularReferences() throws IOException {
106         File circularProperties = File.createTempFile("circular", ".properties");
107         circularProperties.deleteOnExit();
108 
109         try (Writer writer = new OutputStreamWriter(new FileOutputStream(circularProperties), StandardCharsets.UTF_8)) {
110             writer.write("test=${test2}\n");
111             writer.write("test2=${test2}\n");
112             writer.flush();
113         }
114 
115         Properties properties = PropertyUtils.loadPropertyFile(circularProperties, null);
116         assertEquals("${test2}", properties.getProperty("test"));
117         assertEquals("${test2}", properties.getProperty("test2"));
118     }
119 
120     /**
121      * Test case to reproduce MSHARED-417
122      *
123      * @throws IOException if problem writing file
124      */
125     @Test
126     void circularReferences3Vars() throws IOException {
127         File circularProperties = File.createTempFile("circular", ".properties");
128         circularProperties.deleteOnExit();
129 
130         try (Writer writer = new OutputStreamWriter(new FileOutputStream(circularProperties), StandardCharsets.UTF_8)) {
131             writer.write("test=${test2}\n");
132             writer.write("test2=${test3}\n");
133             writer.write("test3=${test}\n");
134             writer.flush();
135         }
136 
137         Properties properties = PropertyUtils.loadPropertyFile(circularProperties, null);
138         assertEquals("${test2}", properties.getProperty("test"));
139         assertEquals("${test3}", properties.getProperty("test2"));
140         assertEquals("${test}", properties.getProperty("test3"));
141     }
142 
143     @Test
144     void nonCircularReferences1Var3Times() throws IOException {
145         File nonCircularProperties = File.createTempFile("non-circular", ".properties");
146         nonCircularProperties.deleteOnExit();
147 
148         try (Writer writer =
149                 new OutputStreamWriter(new FileOutputStream(nonCircularProperties), StandardCharsets.UTF_8)) {
150             writer.write("depends=p1 >= ${version}, p2 >= ${version}, p3 >= ${version}\n");
151             writer.write("version=1.2.3\n");
152             writer.flush();
153         }
154 
155         Properties properties = PropertyUtils.loadPropertyFile(nonCircularProperties, null);
156         assertEquals("p1 >= 1.2.3, p2 >= 1.2.3, p3 >= 1.2.3", properties.getProperty("depends"));
157         assertEquals("1.2.3", properties.getProperty("version"));
158     }
159 
160     @Test
161     void nonCircularReferences2Vars2Times() throws IOException {
162         File nonCircularProperties = File.createTempFile("non-circular", ".properties");
163         nonCircularProperties.deleteOnExit();
164 
165         try (Writer writer =
166                 new OutputStreamWriter(new FileOutputStream(nonCircularProperties), StandardCharsets.UTF_8)) {
167             writer.write("test=${test2} ${test3} ${test2} ${test3}\n");
168             writer.write("test2=${test3} ${test3}\n");
169             writer.write("test3=test\n");
170             writer.flush();
171         }
172 
173         Properties properties = PropertyUtils.loadPropertyFile(nonCircularProperties, null);
174         assertEquals("test test test test test test", properties.getProperty("test"));
175         assertEquals("test test", properties.getProperty("test2"));
176         assertEquals("test", properties.getProperty("test3"));
177     }
178 }