From e5744dd63faee02148a22cdfc944ca763adf1669 Mon Sep 17 00:00:00 2001 From: Anthony Whitford Date: Sat, 31 Oct 2015 10:25:50 -0700 Subject: [PATCH] Added extra setters with null and empty checks. --- .../owasp/dependencycheck/utils/Settings.java | 48 +++++++++++++++++++ .../dependencycheck/utils/SettingsTest.java | 26 ++++++++++ 2 files changed, 74 insertions(+) diff --git a/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/Settings.java b/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/Settings.java index 166800463..b294b04cf 100644 --- a/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/Settings.java +++ b/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/Settings.java @@ -444,6 +444,30 @@ public final class Settings { LOGGER.debug("Setting: {}='{}'", key, value); } + /** + * Sets a property value only if the value is not null. + * + * @param key the key for the property + * @param value the value for the property + */ + public static void setStringIfNotNull(String key, String value) { + if (null != value) { + setString(key, value); + } + } + + /** + * Sets a property value only if the value is not null and not empty. + * + * @param key the key for the property + * @param value the value for the property + */ + public static void setStringIfNotEmpty(String key, String value) { + if (null != value && !value.isEmpty()) { + setString(key, value); + } + } + /** * Sets a property value. * @@ -454,6 +478,18 @@ public final class Settings { setString(key, Boolean.toString(value)); } + /** + * Sets a property value only if the value is not null. + * + * @param key the key for the property + * @param value the value for the property + */ + public static void setBooleanIfNotNull(String key, Boolean value) { + if (null != value) { + setBoolean(key, value); + } + } + /** * Sets a property value. * @@ -465,6 +501,18 @@ public final class Settings { LOGGER.debug("Setting: {}='{}'", key, value); } + /** + * Sets a property value only if the value is not null. + * + * @param key the key for the property + * @param value the value for the property + */ + public static void setIntIfNotNull(String key, Integer value) { + if (null != value) { + setInt(key, value); + } + } + /** * Merges a new properties file into the current properties. This method allows for the loading of a user provided properties * file.

diff --git a/dependency-check-utils/src/test/java/org/owasp/dependencycheck/utils/SettingsTest.java b/dependency-check-utils/src/test/java/org/owasp/dependencycheck/utils/SettingsTest.java index 63caba59a..03a545816 100644 --- a/dependency-check-utils/src/test/java/org/owasp/dependencycheck/utils/SettingsTest.java +++ b/dependency-check-utils/src/test/java/org/owasp/dependencycheck/utils/SettingsTest.java @@ -77,6 +77,32 @@ public class SettingsTest extends BaseTest { Assert.assertEquals(expResults, value); } + /** + * Test of setStringIfNotNull method, of class Settings. + */ + @Test + public void testSetStringIfNotNull() { + String key = "nullableProperty"; + String value = "someValue"; + Settings.setString(key, value); + Settings.setStringIfNotNull(key, null); // NO-OP + String expResults = Settings.getString(key); + Assert.assertEquals(expResults, value); + } + + /** + * Test of setStringIfNotNull method, of class Settings. + */ + @Test + public void testSetStringIfNotEmpty() { + String key = "optionalProperty"; + String value = "someValue"; + Settings.setString(key, value); + Settings.setStringIfNotEmpty(key, ""); // NO-OP + String expResults = Settings.getString(key); + Assert.assertEquals(expResults, value); + } + /** * Test of getString method, of class Settings. */