Issue #730: Allow multiple args for CLI suppresion

The core has not been extended but the CLI is able to parse and pass to the Settings singleton
This change to the CLI is backwards compatible
This commit is contained in:
Phillip Whittlesea
2017-06-11 15:05:24 +01:00
parent 869c9c0114
commit 76218da8d1
6 changed files with 165 additions and 12 deletions

View File

@@ -32,6 +32,8 @@ import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
/**
* A simple settings container that wraps the dependencycheck.properties file.
*
@@ -47,6 +49,10 @@ public final class Settings {
* The properties file location.
*/
private static final String PROPERTIES_FILE = "dependencycheck.properties";
/**
* Array separator.
*/
private static final String ARRAY_SEP = ",";
/**
* Thread local settings.
*/
@@ -558,6 +564,18 @@ public final class Settings {
LOGGER.debug("Setting: {}='{}'", key, value);
}
/**
* Sets a property value from an array.
* <p>
* Note: each value of the array will be joined by the delimiter {@link Settings#ARRAY_SEP}.
*
* @param key the key for the property
* @param value the value for the property
*/
static void setArray(String key, String[] value) {
setString(key, StringUtils.join(value, ARRAY_SEP));
}
/**
* Sets a property value only if the value is not null.
*
@@ -582,6 +600,18 @@ public final class Settings {
}
}
/**
* Sets a property value only if the array value is not null and not empty.
*
* @param key the key for the property
* @param value the value for the property
*/
public static void setArrayIfNotEmpty(String key, String[] value) {
if (null != value && value.length > 0) {
setArray(key, value);
}
}
/**
* Sets a property value.
*

View File

@@ -17,10 +17,17 @@
*/
package org.owasp.dependencycheck.utils;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
@@ -29,6 +36,22 @@ import org.junit.Test;
*/
public class SettingsTest extends BaseTest {
/**
* Initialize the {@link Settings} singleton.
*/
@Before
public void setUp() {
Settings.initialize();
}
/**
* Clean the {@link Settings} singleton.
*/
@After
public void tearDown() {
Settings.cleanup();
}
/**
* Test of getString method, of class Settings.
*/
@@ -220,4 +243,80 @@ public class SettingsTest extends BaseTest {
File tmp = Settings.getTempDirectory();
Assert.assertTrue(tmp.exists());
}
/**
* Assert {@link Settings#setArrayIfNotEmpty(String, String[])} with an empty array is ignored.
*/
@Test
public void testSetArrayNotEmptyIgnoresAnEmptyArray() {
// GIVEN an empty array
final String[] array = {};
// WHEN setting the array
Settings.setArrayIfNotEmpty("key", array);
// THEN the property was not set
assertThat("Expected the property to not be set", Settings.getString("key"), nullValue());
}
/**
* Assert {@link Settings#setArrayIfNotEmpty(String, String[])} with a null array is ignored.
*/
@Test
public void testSetArrayNotEmptyIgnoresAnNullArray() {
// GIVEN a null array
final String[] array = null;
// WHEN setting the array
Settings.setArrayIfNotEmpty("key", array);
// THEN the property was not set
assertThat("Expected the property to not be set", Settings.getString("key"), nullValue());
}
/**
* Assert {@link Settings#setArrayIfNotEmpty(String, String[])} with multiple values sets a delimited string.
*/
@Test
public void testSetArrayNotEmptySetsADelimitedString() {
// GIVEN an array with values
final String[] array = { "value1", "value2" };
// WHEN setting the array
Settings.setArrayIfNotEmpty("key", array);
// THEN the property is set
assertThat("Expected the property to be set", Settings.getString("key"), is("value1,value2"));
}
/**
* Assert {@link Settings#setArrayIfNotEmpty(String, String[])} with a single values sets a string.
*/
@Test
public void testSetArrayNotEmptyWithSingleValueSetsAString() {
// GIVEN an array with a value
final String[] array = { "value1" };
// WHEN setting the array
Settings.setArrayIfNotEmpty("key", array);
// THEN the property is set
assertThat("Expected the property to be set", Settings.getString("key"), is("value1"));
}
/**
* Assert {@link Settings#setArray(String, String[])} with multiple values sets a delimited string.
*/
@Test
public void testSetArraySetsADelimitedString() {
// GIVEN an array with values
final String[] array = { "value1", "value2" };
// WHEN setting the array
Settings.setArray("key", array);
// THEN the property is set
assertThat("Expected the property to be set", Settings.getString("key"), is("value1,value2"));
}
}