Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-1290] Fix PropertyUtils cycle detection results in false positives #78

Merged
merged 7 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ private static String getPropertyValue(String k, Properties p, Logger logger) {
// else prefix the original string with the
// resolved property ( so it can be parsed further )
// taking recursion into account.
if (nv == null || nv.equals(k) || k.equals(nk)) {
if (nv == null || k.equals(nk)) {
ret.append("${").append(nk).append("}");
} else {
v = nv + v;
v = nv + v.replace("${" + nk + "}", nv);
}
}
}
Expand Down
177 changes: 98 additions & 79 deletions src/test/java/org/apache/maven/shared/filtering/PropertyUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@
package org.apache.maven.shared.filtering;

import java.io.File;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import org.mockito.ArgumentCaptor;
import org.slf4j.Logger;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
* @author Olivier Lamy
Expand All @@ -39,40 +38,40 @@ public class PropertyUtilsTest extends TestSupport {
private static File testDirectory = new File(getBasedir(), "target/test-classes/");

public void testBasic() throws Exception {
File basicProp = new File(testDirectory, "basic.properties");
File basicProp = File.createTempFile("basic", ".properties");

if (basicProp.exists()) {
try {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(basicProp), StandardCharsets.UTF_8)) {
writer.write("ghost=${non_existent}\n");
writer.write("key=${untat_na_damgo}\n");
writer.write("untat_na_damgo=gani_man\n");
writer.flush();
}

Logger logger = mock(Logger.class);
Properties prop = PropertyUtils.loadPropertyFile(basicProp, false, false, logger);
wborn marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(prop.getProperty("key").equals("gani_man"));
wborn marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(prop.getProperty("ghost").equals("${non_existent}"));
} finally {
basicProp.delete();
}

basicProp.createNewFile();
try (FileWriter writer = new FileWriter(basicProp)) {
writer.write("ghost=${non_existent}\n");
writer.write("key=${untat_na_damgo}\n");
writer.write("untat_na_damgo=gani_man\n");
writer.flush();
}

Properties prop = PropertyUtils.loadPropertyFile(basicProp, false, false);
assertTrue(prop.getProperty("key").equals("gani_man"));
assertTrue(prop.getProperty("ghost").equals("${non_existent}"));
}

public void testSystemProperties() throws Exception {
File systemProp = new File(testDirectory, "system.properties");
File systemProp = File.createTempFile("system", ".properties");

if (systemProp.exists()) {
try {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(systemProp), StandardCharsets.UTF_8)) {
writer.write("key=${user.dir}");
writer.flush();
}

Logger logger = mock(Logger.class);
Properties prop = PropertyUtils.loadPropertyFile(systemProp, false, true, logger);
wborn marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(prop.getProperty("key").equals(System.getProperty("user.dir")));
wborn marked this conversation as resolved.
Show resolved Hide resolved
} finally {
wborn marked this conversation as resolved.
Show resolved Hide resolved
systemProp.delete();
}

systemProp.createNewFile();
try (FileWriter writer = new FileWriter(systemProp)) {
writer.write("key=${user.dir}");
writer.flush();
}

Properties prop = PropertyUtils.loadPropertyFile(systemProp, false, true);
assertTrue(prop.getProperty("key").equals(System.getProperty("user.dir")));
}

public void testException() throws Exception {
Expand All @@ -88,12 +87,13 @@ public void testException() throws Exception {
}
}

public void testloadpropertiesFile() throws Exception {
public void testLoadPropertiesFile() throws Exception {
File propertyFile = new File(getBasedir() + "/src/test/units-files/propertyutils-test.properties");
Properties baseProps = new Properties();
baseProps.put("pom.version", "realVersion");

Properties interpolated = PropertyUtils.loadPropertyFile(propertyFile, baseProps);
Logger logger = mock(Logger.class);
Properties interpolated = PropertyUtils.loadPropertyFile(propertyFile, baseProps, logger);
assertEquals("realVersion", interpolated.get("version"));
assertEquals("${foo}", interpolated.get("foo"));
assertEquals("realVersion", interpolated.get("bar"));
Expand All @@ -106,28 +106,22 @@ public void testloadpropertiesFile() throws Exception {
* @throws IOException if problem writing file
*/
public void testCircularReferences() throws IOException {
File basicProp = new File(testDirectory, "circular.properties");

if (basicProp.exists()) {
basicProp.delete();
}
File circularProp = File.createTempFile("circular", ".properties");

basicProp.createNewFile();
try (FileWriter writer = new FileWriter(basicProp)) {
writer.write("test=${test2}\n");
writer.write("test2=${test2}\n");
writer.flush();
try {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(circularProp), StandardCharsets.UTF_8)) {
writer.write("test=${test2}\n");
writer.write("test2=${test2}\n");
writer.flush();
}

Logger logger = mock(Logger.class);
Properties prop = PropertyUtils.loadPropertyFile(circularProp, null, logger);
assertEquals("${test2}", prop.getProperty("test"));
assertEquals("${test2}", prop.getProperty("test2"));
} finally {
circularProp.delete();
}

Logger logger = mock(Logger.class);

Properties prop = PropertyUtils.loadPropertyFile(basicProp, null, logger);
assertEquals("${test2}", prop.getProperty("test"));
assertEquals("${test2}", prop.getProperty("test2"));
assertWarn(
logger,
"Circular reference between properties detected: test2 => test2",
"Circular reference between properties detected: test => test2 => test2");
}

/**
Expand All @@ -136,39 +130,64 @@ public void testCircularReferences() throws IOException {
* @throws IOException if problem writing file
*/
public void testCircularReferences3Vars() throws IOException {
File basicProp = new File(testDirectory, "circular.properties");
File circularProp = File.createTempFile("circular", ".properties");

if (basicProp.exists()) {
basicProp.delete();
}

basicProp.createNewFile();
try (FileWriter writer = new FileWriter(basicProp)) {
writer.write("test=${test2}\n");
writer.write("test2=${test3}\n");
writer.write("test3=${test}\n");
writer.flush();
try {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(circularProp), StandardCharsets.UTF_8)) {
writer.write("test=${test2}\n");
writer.write("test2=${test3}\n");
writer.write("test3=${test}\n");
writer.flush();
}

Logger logger = mock(Logger.class);
Properties prop = PropertyUtils.loadPropertyFile(circularProp, null, logger);
assertEquals("${test2}", prop.getProperty("test"));
assertEquals("${test3}", prop.getProperty("test2"));
assertEquals("${test}", prop.getProperty("test3"));
} finally {
circularProp.delete();
}
}

Logger logger = mock(Logger.class);
public void testNonCircularReferences1Var3Times() throws IOException {
File circularProp = File.createTempFile("non-circular", ".properties");

Properties prop = PropertyUtils.loadPropertyFile(basicProp, null, logger);
assertEquals("${test2}", prop.getProperty("test"));
assertEquals("${test3}", prop.getProperty("test2"));
assertEquals("${test}", prop.getProperty("test3"));
assertWarn(
logger,
"Circular reference between properties detected: test3 => test => test2 => test3",
"Circular reference between properties detected: test2 => test3 => test => test2",
"Circular reference between properties detected: test => test2 => test3 => test");
try {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(circularProp), StandardCharsets.UTF_8)) {
writer.write("depends=p1 >= ${version}, p2 >= ${version}, p3 >= ${version}\n");
writer.write("version=1.2.3\n");
writer.flush();
}

Logger logger = mock(Logger.class);
Properties prop = PropertyUtils.loadPropertyFile(circularProp, null, logger);
assertEquals("p1 >= 1.2.3, p2 >= 1.2.3, p3 >= 1.2.3", prop.getProperty("depends"));
assertEquals("1.2.3", prop.getProperty("version"));
} finally {
circularProp.delete();
}
}

private void assertWarn(Logger mock, String... expected) {
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
verify(mock, times(expected.length)).warn(argument.capture());
List<String> messages = argument.getAllValues();
for (String str : expected) {
assertTrue(messages.contains(str));
public void testNonCircularReferences2Vars2Times() throws IOException {
File nonCircularProp = File.createTempFile("non-circular", ".properties");

try {
try (Writer writer =
new OutputStreamWriter(new FileOutputStream(nonCircularProp), StandardCharsets.UTF_8)) {
writer.write("test=${test2} ${test3} ${test2} ${test3}\n");
writer.write("test2=${test3} ${test3}\n");
writer.write("test3=test\n");
writer.flush();
}

Logger logger = mock(Logger.class);
Properties prop = PropertyUtils.loadPropertyFile(nonCircularProp, null, logger);
assertEquals("test test test test test test", prop.getProperty("test"));
assertEquals("test test", prop.getProperty("test2"));
assertEquals("test", prop.getProperty("test3"));
} finally {
nonCircularProp.delete();
}
}
}