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 org.owasp.dependencycheck.utils.Settings;
import static org.junit.Assert.assertTrue; 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 { public void setUp() throws Exception {
Settings.initialize(); Settings.initialize();
BaseDBTestCase.ensureDBExists(); BaseDBTestCase.ensureDBExists();
CveDB.getInstance().openDatabase();
final String buildFile = this.getClass().getClassLoader().getResource("build.xml").getPath(); final String buildFile = this.getClass().getClassLoader().getResource("build.xml").getPath();
buildFileRule.configureProject(buildFile); buildFileRule.configureProject(buildFile);
} }
@@ -57,6 +60,10 @@ public class DependencyCheckTaskTest {
//no cleanup... //no cleanup...
//executeTarget("cleanup"); //executeTarget("cleanup");
Settings.cleanup(true); Settings.cleanup(true);
try {
CveDB.getInstance().closeDatabase();
} catch (DatabaseException ex) {
}
} }
/** /**
@@ -73,7 +80,6 @@ public class DependencyCheckTaskTest {
buildFileRule.executeTarget("test.fileset"); buildFileRule.executeTarget("test.fileset");
assertTrue("DependencyCheck report was not generated", report.exists()); 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. * 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 * Database properties object containing the 'properties' from the database
* table. * table.
@@ -84,7 +84,7 @@ public final class CveDB {
/** /**
* The prepared statements. * 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 * 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 { private CveDB() throws DatabaseException {
openDatabase(); 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 { public synchronized void openDatabase() throws DatabaseException {
if (!isOpen()) { if (!isOpen()) {
connection = ConnectionFactory.getConnection(); 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); LOGGER.debug("", ex);
} }
connection = null; 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 * @return whether the database connection is open or closed
*/ */
private boolean isOpen() { private synchronized boolean isOpen() {
return connection != null; return connection != null;
} }
@@ -277,7 +278,7 @@ public final class CveDB {
* @throws DatabaseException thrown if there is an error preparing the * @throws DatabaseException thrown if there is an error preparing the
* statements * statements
*/ */
private EnumMap<PreparedStatementCveDb, PreparedStatement> prepareStatements() private synchronized EnumMap<PreparedStatementCveDb, PreparedStatement> prepareStatements()
throws DatabaseException { throws DatabaseException {
final EnumMap<PreparedStatementCveDb, PreparedStatement> result = new EnumMap<>(PreparedStatementCveDb.class); final EnumMap<PreparedStatementCveDb, PreparedStatement> result = new EnumMap<>(PreparedStatementCveDb.class);
@@ -301,7 +302,7 @@ public final class CveDB {
/** /**
* Closes all prepared statements. * Closes all prepared statements.
*/ */
private void closeStatements() { private synchronized void closeStatements() {
for (PreparedStatement preparedStatement : preparedStatements.values()) { for (PreparedStatement preparedStatement : preparedStatements.values()) {
DBUtils.closeStatement(preparedStatement); DBUtils.closeStatement(preparedStatement);
} }
@@ -315,7 +316,7 @@ public final class CveDB {
* @return the prepared statement * @return the prepared statement
* @throws SQLException thrown if a SQL Exception occurs * @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); final PreparedStatement preparedStatement = preparedStatements.get(key);
preparedStatement.clearParameters(); preparedStatement.clearParameters();
return preparedStatement; return preparedStatement;
@@ -351,7 +352,7 @@ public final class CveDB {
* *
* @return the value of databaseProperties * @return the value of databaseProperties
*/ */
public DatabaseProperties getDatabaseProperties() { public synchronized DatabaseProperties getDatabaseProperties() {
return databaseProperties; return databaseProperties;
} }
@@ -360,7 +361,7 @@ public final class CveDB {
* *
* @return the database properties * @return the database properties
*/ */
protected DatabaseProperties reloadProperties() { protected synchronized DatabaseProperties reloadProperties() {
databaseProperties = new DatabaseProperties(this); databaseProperties = new DatabaseProperties(this);
return databaseProperties; return databaseProperties;
} }

View File

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

View File

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