fix NPE in PropertyPlaceHolder

This commit is contained in:
Jörg Prante 2021-10-26 18:58:25 +02:00
parent 2b26886b17
commit 969f86baae
3 changed files with 24 additions and 2 deletions

View file

@ -46,6 +46,9 @@ public class PropertyPlaceholder {
protected String parseStringValue(String value, protected String parseStringValue(String value,
PlaceholderResolver placeholderResolver, PlaceholderResolver placeholderResolver,
Set<String> visitedPlaceholders) { Set<String> visitedPlaceholders) {
if (value == null) {
return null;
}
StringBuilder sb = new StringBuilder(value); StringBuilder sb = new StringBuilder(value);
int startIndex = value.indexOf(this.placeholderPrefix); int startIndex = value.indexOf(this.placeholderPrefix);
while (startIndex != -1) { while (startIndex != -1) {
@ -53,8 +56,7 @@ public class PropertyPlaceholder {
if (endIndex != -1) { if (endIndex != -1) {
String placeholder = sb.substring(startIndex + this.placeholderPrefix.length(), endIndex); String placeholder = sb.substring(startIndex + this.placeholderPrefix.length(), endIndex);
if (!visitedPlaceholders.add(placeholder)) { if (!visitedPlaceholders.add(placeholder)) {
throw new IllegalArgumentException( throw new IllegalArgumentException("Circular placeholder reference '" + placeholder + "' in property definitions");
"Circular placeholder reference '" + placeholder + "' in property definitions");
} }
placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders); placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
int defaultValueIdx = placeholder.indexOf(':'); int defaultValueIdx = placeholder.indexOf(':');

View file

@ -23,6 +23,7 @@ import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
@ -116,6 +117,15 @@ public class SettingsTest {
assertEquals(LocalDate.now().getYear(), Integer.parseInt(settings.get("date"))); assertEquals(LocalDate.now().getYear(), Integer.parseInt(settings.get("date")));
} }
@Test
public void testPropertyReplaceNull() {
Settings settings = Settings.settingsBuilder()
.put("null", null)
.replacePropertyPlaceholders()
.build();
assertNull(settings.get("null"));
}
@Test @Test
public void testSystemEnvironment() { public void testSystemEnvironment() {
Settings settings = Settings.settingsBuilder() Settings settings = Settings.settingsBuilder()

View file

@ -13,6 +13,7 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class SettingsTest { public class SettingsTest {
@ -89,6 +90,15 @@ public class SettingsTest {
assertEquals(LocalDate.now().getYear(), Integer.parseInt(settings.get("date"))); assertEquals(LocalDate.now().getYear(), Integer.parseInt(settings.get("date")));
} }
@Test
public void testPropertyReplaceNull() {
Settings settings = Settings.settingsBuilder()
.put("null", null)
.replacePropertyPlaceholders()
.build();
assertNull(settings.get("null"));
}
@Test @Test
public void testSystemEnvironment() { public void testSystemEnvironment() {
Settings settings = Settings.settingsBuilder() Settings settings = Settings.settingsBuilder()