initial attempt

This commit is contained in:
Jeremy Long
2017-03-15 07:36:28 -04:00
parent 97b2e1a4da
commit 947499726a
4 changed files with 26 additions and 18 deletions

View File

@@ -30,6 +30,8 @@ import org.owasp.dependencycheck.BaseDBTestCase;
import org.owasp.dependencycheck.utils.Settings;
import static org.junit.Assert.assertTrue;
import org.owasp.dependencycheck.data.nvdcve.CveDB;
import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
/**
@@ -48,6 +50,7 @@ public class DependencyCheckTaskTest {
public void setUp() throws Exception {
Settings.initialize();
BaseDBTestCase.ensureDBExists();
CveDB.getInstance().openDatabase();
final String buildFile = this.getClass().getClassLoader().getResource("build.xml").getPath();
buildFileRule.configureProject(buildFile);
}
@@ -57,6 +60,10 @@ public class DependencyCheckTaskTest {
//no cleanup...
//executeTarget("cleanup");
Settings.cleanup(true);
try {
CveDB.getInstance().closeDatabase();
} catch (DatabaseException ex) {
}
}
/**
@@ -73,7 +80,6 @@ public class DependencyCheckTaskTest {
buildFileRule.executeTarget("test.fileset");
assertTrue("DependencyCheck report was not generated", report.exists());
}
/**

View File

@@ -75,7 +75,7 @@ public final class CveDB {
/**
* The bundle of statements used when accessing the database.
*/
private final ResourceBundle statementBundle;
private ResourceBundle statementBundle;
/**
* Database properties object containing the 'properties' from the database
* table.
@@ -84,7 +84,7 @@ public final class CveDB {
/**
* The prepared statements.
*/
private final EnumMap<PreparedStatementCveDb, PreparedStatement> preparedStatements;
private EnumMap<PreparedStatementCveDb, PreparedStatement> preparedStatements;
/**
* The enum value names must match the keys of the statements in the
@@ -203,12 +203,6 @@ public final class CveDB {
*/
private CveDB() throws DatabaseException {
openDatabase();
final String databaseProductName = determineDatabaseProductName();
statementBundle = databaseProductName != null
? ResourceBundle.getBundle("data/dbStatements", new Locale(databaseProductName))
: ResourceBundle.getBundle("data/dbStatements");
preparedStatements = prepareStatements();
databaseProperties = new DatabaseProperties(this);
}
/**
@@ -237,6 +231,12 @@ public final class CveDB {
public synchronized void openDatabase() throws DatabaseException {
if (!isOpen()) {
connection = ConnectionFactory.getConnection();
final String databaseProductName = determineDatabaseProductName();
statementBundle = databaseProductName != null
? ResourceBundle.getBundle("data/dbStatements", new Locale(databaseProductName))
: ResourceBundle.getBundle("data/dbStatements");
preparedStatements = prepareStatements();
databaseProperties = new DatabaseProperties(this);
}
}
@@ -257,7 +257,8 @@ public final class CveDB {
LOGGER.debug("", ex);
}
connection = null;
instance = null;
preparedStatements = null;
databaseProperties = null;
}
}
@@ -266,7 +267,7 @@ public final class CveDB {
*
* @return whether the database connection is open or closed
*/
private boolean isOpen() {
private synchronized boolean isOpen() {
return connection != null;
}
@@ -277,7 +278,7 @@ public final class CveDB {
* @throws DatabaseException thrown if there is an error preparing the
* statements
*/
private EnumMap<PreparedStatementCveDb, PreparedStatement> prepareStatements()
private synchronized EnumMap<PreparedStatementCveDb, PreparedStatement> prepareStatements()
throws DatabaseException {
final EnumMap<PreparedStatementCveDb, PreparedStatement> result = new EnumMap<>(PreparedStatementCveDb.class);
@@ -301,7 +302,7 @@ public final class CveDB {
/**
* Closes all prepared statements.
*/
private void closeStatements() {
private synchronized void closeStatements() {
for (PreparedStatement preparedStatement : preparedStatements.values()) {
DBUtils.closeStatement(preparedStatement);
}
@@ -315,7 +316,7 @@ public final class CveDB {
* @return the prepared statement
* @throws SQLException thrown if a SQL Exception occurs
*/
private PreparedStatement getPreparedStatement(PreparedStatementCveDb key) throws SQLException {
private synchronized PreparedStatement getPreparedStatement(PreparedStatementCveDb key) throws SQLException {
final PreparedStatement preparedStatement = preparedStatements.get(key);
preparedStatement.clearParameters();
return preparedStatement;
@@ -351,7 +352,7 @@ public final class CveDB {
*
* @return the value of databaseProperties
*/
public DatabaseProperties getDatabaseProperties() {
public synchronized DatabaseProperties getDatabaseProperties() {
return databaseProperties;
}
@@ -360,7 +361,7 @@ public final class CveDB {
*
* @return the database properties
*/
protected DatabaseProperties reloadProperties() {
protected synchronized DatabaseProperties reloadProperties() {
databaseProperties = new DatabaseProperties(this);
return databaseProperties;
}

View File

@@ -51,6 +51,7 @@ public abstract class BaseDBTestCase extends BaseTest {
@Before
public void setUpDb() throws Exception {
ensureDBExists();
CveDB.getInstance().openDatabase();
}
@AfterClass

View File

@@ -20,7 +20,7 @@ package org.owasp.dependencycheck.data.update;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.owasp.dependencycheck.BaseTest;
import org.owasp.dependencycheck.BaseDBTestCase;
import org.owasp.dependencycheck.data.update.exception.UpdateException;
import org.owasp.dependencycheck.data.update.nvd.UpdateableNvdCve;
@@ -28,7 +28,7 @@ import org.owasp.dependencycheck.data.update.nvd.UpdateableNvdCve;
*
* @author Jeremy Long
*/
public class NvdCveUpdaterIntegrationTest extends BaseTest {
public class NvdCveUpdaterIntegrationTest extends BaseDBTestCase {
public NvdCveUpdater getUpdater() {
NvdCveUpdater instance = new NvdCveUpdater();