From 969f86baae68987c15e977d01e560cc1f28fa964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Tue, 26 Oct 2021 18:58:25 +0200 Subject: [PATCH] fix NPE in PropertyPlaceHolder --- .../java/org/xbib/settings/PropertyPlaceholder.java | 6 ++++-- .../org/xbib/settings/content/test/SettingsTest.java | 10 ++++++++++ .../settings/datastructures/test/SettingsTest.java | 10 ++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/settings-api/src/main/java/org/xbib/settings/PropertyPlaceholder.java b/settings-api/src/main/java/org/xbib/settings/PropertyPlaceholder.java index 3a6898e..b308c5f 100644 --- a/settings-api/src/main/java/org/xbib/settings/PropertyPlaceholder.java +++ b/settings-api/src/main/java/org/xbib/settings/PropertyPlaceholder.java @@ -46,6 +46,9 @@ public class PropertyPlaceholder { protected String parseStringValue(String value, PlaceholderResolver placeholderResolver, Set visitedPlaceholders) { + if (value == null) { + return null; + } StringBuilder sb = new StringBuilder(value); int startIndex = value.indexOf(this.placeholderPrefix); while (startIndex != -1) { @@ -53,8 +56,7 @@ public class PropertyPlaceholder { if (endIndex != -1) { String placeholder = sb.substring(startIndex + this.placeholderPrefix.length(), endIndex); if (!visitedPlaceholders.add(placeholder)) { - throw new IllegalArgumentException( - "Circular placeholder reference '" + placeholder + "' in property definitions"); + throw new IllegalArgumentException("Circular placeholder reference '" + placeholder + "' in property definitions"); } placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders); int defaultValueIdx = placeholder.indexOf(':'); diff --git a/settings-content/src/test/java/org/xbib/settings/content/test/SettingsTest.java b/settings-content/src/test/java/org/xbib/settings/content/test/SettingsTest.java index ce341fb..9ba6d8f 100644 --- a/settings-content/src/test/java/org/xbib/settings/content/test/SettingsTest.java +++ b/settings-content/src/test/java/org/xbib/settings/content/test/SettingsTest.java @@ -23,6 +23,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -116,6 +117,15 @@ public class SettingsTest { 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 public void testSystemEnvironment() { Settings settings = Settings.settingsBuilder() diff --git a/settings-datastructures/src/test/java/org/xbib/settings/datastructures/test/SettingsTest.java b/settings-datastructures/src/test/java/org/xbib/settings/datastructures/test/SettingsTest.java index 3bbd94b..d54b0bf 100644 --- a/settings-datastructures/src/test/java/org/xbib/settings/datastructures/test/SettingsTest.java +++ b/settings-datastructures/src/test/java/org/xbib/settings/datastructures/test/SettingsTest.java @@ -13,6 +13,7 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; public class SettingsTest { @@ -89,6 +90,15 @@ public class SettingsTest { 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 public void testSystemEnvironment() { Settings settings = Settings.settingsBuilder()