Modified CveDB and Settings so that they are no longer singletons; first step in thread safety updates

This commit is contained in:
Jeremy Long
2017-08-30 06:47:45 -04:00
parent c4b67a1db2
commit 74a2326e0e
113 changed files with 1809 additions and 1400 deletions

View File

@@ -15,8 +15,8 @@
*/
package org.owasp.dependencycheck.utils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.After;
import org.junit.Before;
/**
*
@@ -24,13 +24,33 @@ import org.junit.BeforeClass;
*/
public class BaseTest {
@BeforeClass
public static void setUpClass() throws Exception {
Settings.initialize();
/**
* The configured settings.
*/
private Settings settings;
/**
* Initialize the {@link Settings}.
*/
@Before
public void setUp() {
settings = new Settings();
}
@AfterClass
public static void tearDownClass() throws Exception {
Settings.cleanup(true);
/**
* Clean the {@link Settings}.
*/
@After
public void tearDown() {
settings.cleanup(true);
}
/**
* Returns the settings for the test cases.
*
* @return
*/
protected Settings getSettings() {
return settings;
}
}

View File

@@ -40,16 +40,18 @@ public class DownloaderIT extends BaseTest {
// Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, "1000");
// Settings.setString(Settings.KEYS.PROXY_PORT, "8080");
// Settings.setString(Settings.KEYS.PROXY_SERVER, "127.0.0.1");
URL url = new URL(Settings.getString(Settings.KEYS.CVE_MODIFIED_20_URL));
URL url = new URL(getSettings().getString(Settings.KEYS.CVE_MODIFIED_20_URL));
File outputPath = new File("target/downloaded_cve.xml");
Downloader.fetchFile(url, outputPath);
Downloader downloader = new Downloader(getSettings());
downloader.fetchFile(url, outputPath);
assertTrue(outputPath.isFile());
}
@Test
public void testGetLastModified() throws Exception {
URL url = new URL(Settings.getString(Settings.KEYS.CVE_MODIFIED_20_URL));
long timestamp = Downloader.getLastModified(url);
URL url = new URL(getSettings().getString(Settings.KEYS.CVE_MODIFIED_20_URL));
Downloader downloader = new Downloader(getSettings());
long timestamp = downloader.getLastModified(url);
assertTrue("timestamp equal to zero?", timestamp > 0);
}
}

View File

@@ -25,11 +25,12 @@ import org.junit.Test;
*
* @author Jeremy Long
*/
public class DownloaderTest {
public class DownloaderTest extends BaseTest {
@Test
public void testGetLastModified_file() throws Exception {
long timestamp = Downloader.getLastModified(new File("target/test-classes/dependencycheck.properties").toURI().toURL());
Downloader instance = new Downloader(getSettings());
long timestamp = instance.getLastModified(new File("target/test-classes/dependencycheck.properties").toURI().toURL());
assertTrue("timestamp equal to zero?", timestamp > 0);
}
}

View File

@@ -50,7 +50,7 @@ public class FileUtilsTest extends BaseTest {
@Test
public void testDelete() throws Exception {
File file = File.createTempFile("tmp", "deleteme", Settings.getTempDirectory());
File file = File.createTempFile("tmp", "deleteme", getSettings().getTempDirectory());
if (!file.exists()) {
fail("Unable to create a temporary file.");
}

View File

@@ -26,9 +26,7 @@ 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;
/**
@@ -37,22 +35,6 @@ 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.
*/
@@ -60,7 +42,7 @@ public class SettingsTest extends BaseTest {
public void testGetString() {
String key = Settings.KEYS.CVE_MODIFIED_VALID_FOR_DAYS;
String expResult = "7";
String result = Settings.getString(key);
String result = getSettings().getString(key);
Assert.assertTrue(result.endsWith(expResult));
}
@@ -71,21 +53,23 @@ public class SettingsTest extends BaseTest {
public void testGetDataFile() throws IOException {
String key = Settings.KEYS.DATA_DIRECTORY;
String expResult = "data";
File result = Settings.getDataFile(key);
File result = getSettings().getDataFile(key);
Assert.assertTrue(result.getAbsolutePath().endsWith(expResult));
}
/**
* Test of mergeProperties method, of class Settings.
* @throws java.io.IOException thrown when the test fails
* @throws java.net.URISyntaxException thrown when the test fails
*/
@Test
public void testMergeProperties_String() throws IOException, URISyntaxException {
String key = Settings.KEYS.PROXY_PORT;
String expResult = Settings.getString(key);
String expResult = getSettings().getString(key);
File f = new File(this.getClass().getClassLoader().getResource("test.properties").toURI());
//InputStream in = this.getClass().getClassLoader().getResourceAsStream("test.properties");
Settings.mergeProperties(f.getAbsolutePath());
String result = Settings.getString(key);
getSettings().mergeProperties(f.getAbsolutePath());
String result = getSettings().getString(key);
Assert.assertTrue("setting didn't change?", (expResult == null && result != null) || !expResult.equals(result));
}
@@ -96,8 +80,8 @@ public class SettingsTest extends BaseTest {
public void testSetString() {
String key = "newProperty";
String value = "someValue";
Settings.setString(key, value);
String expResults = Settings.getString(key);
getSettings().setString(key, value);
String expResults = getSettings().getString(key);
Assert.assertEquals(expResults, value);
}
@@ -108,9 +92,9 @@ public class SettingsTest extends BaseTest {
public void testSetStringIfNotNull() {
String key = "nullableProperty";
String value = "someValue";
Settings.setString(key, value);
Settings.setStringIfNotNull(key, null); // NO-OP
String expResults = Settings.getString(key);
getSettings().setString(key, value);
getSettings().setStringIfNotNull(key, null); // NO-OP
String expResults = getSettings().getString(key);
Assert.assertEquals(expResults, value);
}
@@ -121,9 +105,9 @@ public class SettingsTest extends BaseTest {
public void testSetStringIfNotEmpty() {
String key = "optionalProperty";
String value = "someValue";
Settings.setString(key, value);
Settings.setStringIfNotEmpty(key, ""); // NO-OP
String expResults = Settings.getString(key);
getSettings().setString(key, value);
getSettings().setStringIfNotEmpty(key, ""); // NO-OP
String expResults = getSettings().getString(key);
Assert.assertEquals(expResults, value);
}
@@ -135,9 +119,9 @@ public class SettingsTest extends BaseTest {
String key = "key That Doesn't Exist";
String defaultValue = "blue bunny";
String expResult = "blue bunny";
String result = Settings.getString(key);
String result = getSettings().getString(key);
Assert.assertTrue(result == null);
result = Settings.getString(key, defaultValue);
result = getSettings().getString(key, defaultValue);
Assert.assertEquals(expResult, result);
}
@@ -147,7 +131,7 @@ public class SettingsTest extends BaseTest {
@Test
public void testGetString_String() {
String key = Settings.KEYS.CONNECTION_TIMEOUT;
String result = Settings.getString(key);
String result = getSettings().getString(key);
Assert.assertTrue(result == null);
}
@@ -158,8 +142,8 @@ public class SettingsTest extends BaseTest {
public void testGetInt() throws InvalidSettingException {
String key = "SomeNumber";
int expResult = 85;
Settings.setString(key, "85");
int result = Settings.getInt(key);
getSettings().setString(key, "85");
int result = getSettings().getInt(key);
Assert.assertEquals(expResult, result);
}
@@ -170,8 +154,8 @@ public class SettingsTest extends BaseTest {
public void testGetIntDefault() throws InvalidSettingException {
String key = "SomeKey";
int expResult = 85;
Settings.setString(key, "blue");
int result = Settings.getInt(key, expResult);
getSettings().setString(key, "blue");
int result = getSettings().getInt(key, expResult);
Assert.assertEquals(expResult, result);
}
@@ -182,8 +166,8 @@ public class SettingsTest extends BaseTest {
public void testGetLong() throws InvalidSettingException {
String key = "SomeNumber";
long expResult = 300L;
Settings.setString(key, "300");
long result = Settings.getLong(key);
getSettings().setString(key, "300");
long result = getSettings().getLong(key);
Assert.assertEquals(expResult, result);
}
@@ -193,14 +177,14 @@ public class SettingsTest extends BaseTest {
@Test
public void testGetBoolean() throws InvalidSettingException {
String key = "SomeBoolean";
Settings.setString(key, "false");
getSettings().setString(key, "false");
boolean expResult = false;
boolean result = Settings.getBoolean(key);
boolean result = getSettings().getBoolean(key);
Assert.assertEquals(expResult, result);
key = "something that does not exist";
expResult = true;
result = Settings.getBoolean(key, true);
result = getSettings().getBoolean(key, true);
Assert.assertEquals(expResult, result);
}
@@ -212,11 +196,11 @@ public class SettingsTest extends BaseTest {
String key = "SomeKey";
String value = "value";
String dfault = "default";
Settings.setString(key, value);
String ret = Settings.getString(key);
getSettings().setString(key, value);
String ret = getSettings().getString(key);
Assert.assertEquals(value, ret);
Settings.removeProperty(key);
ret = Settings.getString(key, dfault);
getSettings().removeProperty(key);
ret = getSettings().getString(key, dfault);
Assert.assertEquals(dfault, ret);
}
@@ -225,11 +209,11 @@ public class SettingsTest extends BaseTest {
*/
@Test
public void testGetConnectionString() throws Exception {
String value = Settings.getConnectionString(Settings.KEYS.DB_CONNECTION_STRING, Settings.KEYS.DB_FILE_NAME);
String value = getSettings().getConnectionString(Settings.KEYS.DB_CONNECTION_STRING, Settings.KEYS.DB_FILE_NAME);
Assert.assertNotNull(value);
String msg = null;
try {
value = Settings.getConnectionString("invalidKey", null);
value = getSettings().getConnectionString("invalidKey", null);
} catch (InvalidSettingException e) {
msg = e.getMessage();
}
@@ -241,7 +225,7 @@ public class SettingsTest extends BaseTest {
*/
@Test
public void testGetTempDirectory() throws Exception {
File tmp = Settings.getTempDirectory();
File tmp = getSettings().getTempDirectory();
Assert.assertTrue(tmp.exists());
}
@@ -253,10 +237,10 @@ public class SettingsTest extends BaseTest {
public void testGetArrayFromADelimitedString() {
// GIVEN a delimited string
final String delimitedString = "value1,value2";
Settings.setString("key", delimitedString);
getSettings().setString("key", delimitedString);
// WHEN getting the array
final String[] array = Settings.getArray("key");
final String[] array = getSettings().getArray("key");
// THEN the split array is returned
assertThat("Expected the array to be non-null", array, notNullValue());
@@ -272,7 +256,7 @@ public class SettingsTest extends BaseTest {
@Test
public void testGetArrayWhereThePropertyIsNotSet() {
// WHEN getting the array
final String[] array = Settings.getArray("key");
final String[] array = getSettings().getArray("key");
// THEN null is returned
assertThat("Expected the array to be null", array, nullValue());
@@ -288,10 +272,10 @@ public class SettingsTest extends BaseTest {
final String[] array = {};
// WHEN setting the array
Settings.setArrayIfNotEmpty("key", array);
getSettings().setArrayIfNotEmpty("key", array);
// THEN the property was not set
assertThat("Expected the property to not be set", Settings.getString("key"), nullValue());
assertThat("Expected the property to not be set", getSettings().getString("key"), nullValue());
}
/**
@@ -304,10 +288,10 @@ public class SettingsTest extends BaseTest {
final String[] array = null;
// WHEN setting the array
Settings.setArrayIfNotEmpty("key", array);
getSettings().setArrayIfNotEmpty("key", array);
// THEN the property was not set
assertThat("Expected the property to not be set", Settings.getString("key"), nullValue());
assertThat("Expected the property to not be set", getSettings().getString("key"), nullValue());
}
/**
@@ -320,10 +304,10 @@ public class SettingsTest extends BaseTest {
final String[] array = {"value1", "value2"};
// WHEN setting the array
Settings.setArrayIfNotEmpty("key", array);
getSettings().setArrayIfNotEmpty("key", array);
// THEN the property is set
assertThat("Expected the property to be set", Settings.getString("key"), is("value1,value2"));
assertThat("Expected the property to be set", getSettings().getString("key"), is("value1,value2"));
}
/**
@@ -336,9 +320,9 @@ public class SettingsTest extends BaseTest {
final String[] array = {"value1"};
// WHEN setting the array
Settings.setArrayIfNotEmpty("key", array);
getSettings().setArrayIfNotEmpty("key", array);
// THEN the property is set
assertThat("Expected the property to be set", Settings.getString("key"), is("value1"));
assertThat("Expected the property to be set", getSettings().getString("key"), is("value1"));
}
}