Ensure that DBMS brand is detected before using the knowledge. Begin adding unit tests.

This commit is contained in:
Mark H. Wood
2014-09-05 19:55:35 -04:00
parent 69291af2d8
commit dfcf5a2491
3 changed files with 777 additions and 87 deletions

View File

@@ -40,7 +40,7 @@ import org.slf4j.LoggerFactory;
/**
* Executes SQL queries.
*
*
* @author Peter Breton
* @author Jim Downing
* @version $Revision$
@@ -67,15 +67,15 @@ public class DatabaseManager
/** Name to use for the pool */
private static String poolName = "dspacepool";
/**
* This regular expression is used to perform sanity checks
* on database names (i.e. tables and columns).
*
/**
* This regular expression is used to perform sanity checks
* on database names (i.e. tables and columns).
*
* FIXME: Regular expressions can be slow to solve this in the future we should
* probably create a system where we don't pass in column and table names to these low
* level database methods. This approach is highly exploitable for injection
* level database methods. This approach is highly exploitable for injection
* type attacks because we are unable to determine where the input came from. Instead
* we could pass in static integer constants which are then mapped to their sql name.
* we could pass in static integer constants which are then mapped to their sql name.
*/
private static final Pattern DB_SAFE_NAME = Pattern.compile("^[a-zA-Z_1-9.]+$");
@@ -94,12 +94,19 @@ public class DatabaseManager
public static boolean isOracle()
{
try
{
initialize();
} catch (SQLException ex)
{
log.error("Failed to initialize the database: ", ex);
}
return isOracle;
}
/**
* Set the constraint check to deferred (commit time)
*
*
* @param context
* The context object
* @param constraintName
@@ -133,7 +140,7 @@ public class DatabaseManager
/**
* Set the constraint check to immediate (every query)
*
*
* @param context
* The context object
* @param constraintName
@@ -164,12 +171,12 @@ public class DatabaseManager
}
}
}
/**
* Return an iterator with the results of the query. The table parameter
* indicates the type of result. If table is null, the column names are read
* from the ResultSetMetaData.
*
*
* @param context
* The context object
* @param table
@@ -177,8 +184,8 @@ public class DatabaseManager
* @param query
* The SQL query
* @param parameters
* A set of SQL parameters to be included in query. The order of
* the parameters must correspond to the order of their reference
* A set of SQL parameters to be included in query. The order of
* the parameters must correspond to the order of their reference
* within the query.
* @return A TableRowIterator with the results of the query
* @exception SQLException
@@ -199,7 +206,7 @@ public class DatabaseManager
}
log.debug(sb.toString());
}
PreparedStatement statement = context.getDBConnection().prepareStatement(query);
try
{
@@ -226,24 +233,24 @@ public class DatabaseManager
throw sqle;
}
}
/**
* Return an iterator with the results of the query.
*
*
* @param context
* The context object
* @param query
* The SQL query
* @param parameters
* A set of SQL parameters to be included in query. The order of
* the parameters must correspond to the order of their reference
* A set of SQL parameters to be included in query. The order of
* the parameters must correspond to the order of their reference
* within the query.
* @return A TableRowIterator with the results of the query
* @exception SQLException
* If a database error occurs
*/
public static TableRowIterator query(Context context, String query,
Object... parameters) throws SQLException
Object... parameters) throws SQLException
{
if (log.isDebugEnabled())
{
@@ -289,14 +296,14 @@ public class DatabaseManager
/**
* Return the single row result to this query, or null if no result. If more
* than one row results, only the first is returned.
*
*
* @param context
* Current DSpace context
* @param query
* The SQL query
* @param parameters
* A set of SQL parameters to be included in query. The order of
* the parameters must correspond to the order of their reference
* A set of SQL parameters to be included in query. The order of
* the parameters must correspond to the order of their reference
* within the query.
* @return A TableRow object, or null if no result
@@ -323,11 +330,11 @@ public class DatabaseManager
return (retRow);
}
/**
* Return the single row result to this query, or null if no result. If more
* than one row results, only the first is returned.
*
*
* @param context
* Current DSpace context
* @param table
@@ -335,8 +342,8 @@ public class DatabaseManager
* @param query
* The SQL query
* @param parameters
* A set of SQL parameters to be included in query. The order of
* the parameters must correspond to the order of their reference
* A set of SQL parameters to be included in query. The order of
* the parameters must correspond to the order of their reference
* within the query.
* @return A TableRow object, or null if no result
* @exception SQLException
@@ -361,18 +368,18 @@ public class DatabaseManager
}
return (retRow);
}
/**
* Execute an update, insert or delete query. Returns the number of rows
* affected by the query.
*
*
* @param context
* Current DSpace context
* @param query
* The SQL query to execute
* @param parameters
* A set of SQL parameters to be included in query. The order of
* the parameters must correspond to the order of their reference
* A set of SQL parameters to be included in query. The order of
* the parameters must correspond to the order of their reference
* within the query.
* @return The number of rows affected by the query.
* @exception SQLException
@@ -397,10 +404,10 @@ public class DatabaseManager
}
try
{
{
statement = context.getDBConnection().prepareStatement(query);
loadParameters(statement, parameters);
return statement.executeUpdate();
}
finally
@@ -420,7 +427,7 @@ public class DatabaseManager
/**
* Create a new row in the given table, and assigns a unique id.
*
*
* @param context
* Current DSpace context
* @param table
@@ -439,7 +446,7 @@ public class DatabaseManager
/**
* Find a table row by its primary key. Returns the row, or null if no row
* with that primary key value exists.
*
*
* @param context
* Current DSpace context
* @param table
@@ -464,7 +471,7 @@ public class DatabaseManager
* Find a table row by a unique value. Returns the row, or null if no row
* with that primary key value exists. If multiple rows with the value
* exist, one is returned.
*
*
* @param context
* Current DSpace context
* @param table
@@ -500,7 +507,7 @@ public class DatabaseManager
/**
* Delete a table row via its primary key. Returns the number of rows
* deleted.
*
*
* @param context
* Current DSpace context
* @param table
@@ -523,7 +530,7 @@ public class DatabaseManager
/**
* Delete all table rows with the given value. Returns the number of rows
* deleted.
*
*
* @param context
* Current DSpace context
* @param table
@@ -557,7 +564,7 @@ public class DatabaseManager
/**
* Obtain an RDBMS connection.
*
*
* @return A new database connection.
* @exception SQLException
* If a database error occurs, or a connection cannot be
@@ -592,7 +599,7 @@ public class DatabaseManager
/**
* Release resources associated with this connection.
*
*
* @param c
* The connection to release
*/
@@ -614,7 +621,7 @@ public class DatabaseManager
/**
* Create a table row object that can be passed into the insert method, not
* commonly used unless the table has a referential integrity constraint.
*
*
* @param table
* The RDBMS table in which to create the new row
* @return The newly created row
@@ -627,7 +634,7 @@ public class DatabaseManager
/**
* Insert a table row into the RDBMS.
*
*
* @param context
* Current DSpace context
* @param row
@@ -653,7 +660,7 @@ public class DatabaseManager
/**
* Update changes to the RDBMS. Note that if the update fails, the values in
* the row will NOT be reverted.
*
*
* @param context
* Current DSpace context
* @param row
@@ -702,7 +709,7 @@ public class DatabaseManager
/**
* Delete row from the RDBMS.
*
*
* @param context
* Current DSpace context
* @param row
@@ -730,7 +737,7 @@ public class DatabaseManager
/**
* Return metadata about a table.
*
*
* @param table
* The name of the table
* @return An array of ColumnInfo objects
@@ -746,7 +753,7 @@ public class DatabaseManager
/**
* Return info about column in table.
*
*
* @param table
* The name of the table
* @param column
@@ -765,7 +772,7 @@ public class DatabaseManager
/**
* Return the names of all the columns of the given table.
*
*
* @param table
* The name of the table
* @return The names of all the columns of the given table, as a List. Each
@@ -788,7 +795,7 @@ public class DatabaseManager
/**
* Return the names of all the columns of the ResultSet.
*
*
* @param meta
* The ResultSetMetaData
* @return The names of all the columns of the given table, as a List. Each
@@ -811,7 +818,7 @@ public class DatabaseManager
/**
* Return the canonical name for a table.
*
*
* @param table
* The name of the table.
* @return The canonical name of the table.
@@ -834,7 +841,7 @@ public class DatabaseManager
/**
* Load SQL into the RDBMS.
*
*
* @param sql
* The SQL to load.
* throws SQLException
@@ -853,7 +860,7 @@ public class DatabaseManager
/**
* Load SQL from a reader into the RDBMS.
*
*
* @param r
* The Reader from which to read the SQL.
* @throws SQLException
@@ -1011,7 +1018,7 @@ public class DatabaseManager
/**
* Convert the current row in a ResultSet into a TableRow object.
*
*
* @param results
* A ResultSet to process
* @param table
@@ -1160,7 +1167,7 @@ public class DatabaseManager
* Return the name of the primary key column. We assume there's only one
* primary key per table; if there are more, only the first one will be
* returned.
*
*
* @param row
* The TableRow to return the primary key for.
* @return The name of the primary key column, or null if the row has no
@@ -1177,7 +1184,7 @@ public class DatabaseManager
* Return the name of the primary key column in the given table. We assume
* there's only one primary key per table; if there are more, only the first
* one will be returned.
*
*
* @param table
* The name of the RDBMS table
* @return The name of the primary key column, or null if the table has no
@@ -1197,7 +1204,7 @@ public class DatabaseManager
* Return column information for the primary key column, or null if the
* table has no primary key. We assume there's only one primary key per
* table; if there are more, only the first one will be returned.
*
*
* @param table
* The name of the RDBMS table
* @return A ColumnInfo object, or null if the table has no primary key.
@@ -1222,7 +1229,7 @@ public class DatabaseManager
/**
* Execute SQL as a PreparedStatement on Connection. Bind parameters in
* columns to the values in the table row before executing.
*
*
* @param connection
* The SQL connection
* @param sql
@@ -1297,7 +1304,7 @@ public class DatabaseManager
/**
* Return metadata about a table.
*
*
* @param table
* The name of the table
* @return An map of info.
@@ -1322,7 +1329,7 @@ public class DatabaseManager
/**
* Read metadata about a table from the database.
*
*
* @param table
* The RDBMS table.
* @return A map of information about the columns. The key is the name of
@@ -1336,7 +1343,7 @@ public class DatabaseManager
Connection connection = null;
ResultSet pkcolumns = null;
ResultSet columns = null;
try
{
String schema = ConfigurationManager.getProperty("db.schema");
@@ -1344,8 +1351,8 @@ public class DatabaseManager
schema = null;
}
String catalog = null;
int dotIndex = table.indexOf('.');
int dotIndex = table.indexOf('.');
if (dotIndex > 0)
{
catalog = table.substring(0, dotIndex);
@@ -1353,7 +1360,7 @@ public class DatabaseManager
log.warn("catalog: " + catalog);
log.warn("table: " + table);
}
connection = getConnection();
DatabaseMetaData metadata = connection.getMetaData();
@@ -1362,9 +1369,9 @@ public class DatabaseManager
int max = metadata.getMaxTableNameLength();
String tname = ((max > 0) && (table.length() >= max)) ? table
.substring(0, max - 1) : table;
pkcolumns = metadata.getPrimaryKeys(catalog, schema, tname);
Set<String> pks = new HashSet<String>();
while (pkcolumns.next())
@@ -1519,11 +1526,16 @@ public class DatabaseManager
*/
public static String getDbName()
{
try {
initialize();
} catch (SQLException ex) {
log.error("Failed to initialize the database: ", ex);
}
return dbms;
}
/**
* Iterate over the given parameters and add them to the given prepared statement.
* Iterate over the given parameters and add them to the given prepared statement.
* Only a select number of datatypes are supported by the JDBC driver.
*
* @param statement
@@ -1731,7 +1743,7 @@ public class DatabaseManager
{
params.add(col);
}
}
}
}
PreparedStatement statement = null;

View File

@@ -0,0 +1,681 @@
/*
* Copyright 2014 Mark H. Wood.
*/
package org.dspace.storage.rdbms;
import org.dspace.AbstractUnitTest;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author mhwood
*/
public class DatabaseManagerTest
extends AbstractUnitTest
{
public DatabaseManagerTest()
{
}
@BeforeClass
public static void setUpClass()
{
}
@AfterClass
public static void tearDownClass()
{
}
@Before
public void setUp()
{
}
@After
public void tearDown()
{
}
/**
* Test of isOracle method, of class DatabaseManager.
*/
@Test
public void testIsOracle()
{
System.out.println("isOracle");
boolean expResult = true;
boolean result = DatabaseManager.isOracle();
assertEquals(expResult, result);
}
/**
* Test of setConstraintDeferred method, of class DatabaseManager.
*/
/*
@Test
public void testSetConstraintDeferred() throws Exception
{
System.out.println("setConstraintDeferred");
Context context = null;
String constraintName = "";
DatabaseManager.setConstraintDeferred(context, constraintName);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of setConstraintImmediate method, of class DatabaseManager.
*/
/*
@Test
public void testSetConstraintImmediate() throws Exception
{
System.out.println("setConstraintImmediate");
Context context = null;
String constraintName = "";
DatabaseManager.setConstraintImmediate(context, constraintName);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of queryTable method, of class DatabaseManager.
*/
/*
@Test
public void testQueryTable() throws Exception
{
System.out.println("queryTable");
Context context = null;
String table = "";
String query = "";
Object[] parameters = null;
TableRowIterator expResult = null;
TableRowIterator result = DatabaseManager.queryTable(context, table,
query, parameters);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of query method, of class DatabaseManager.
*/
/*
@Test
public void testQuery() throws Exception
{
System.out.println("query");
Context context = null;
String query = "";
Object[] parameters = null;
TableRowIterator expResult = null;
TableRowIterator result = DatabaseManager.query(context, query,
parameters);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of querySingle method, of class DatabaseManager.
*/
/*
@Test
public void testQuerySingle() throws Exception
{
System.out.println("querySingle");
Context context = null;
String query = "";
Object[] parameters = null;
TableRow expResult = null;
TableRow result = DatabaseManager.querySingle(context, query, parameters);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of querySingleTable method, of class DatabaseManager.
*/
/*
@Test
public void testQuerySingleTable() throws Exception
{
System.out.println("querySingleTable");
Context context = null;
String table = "";
String query = "";
Object[] parameters = null;
TableRow expResult = null;
TableRow result = DatabaseManager.querySingleTable(context, table, query,
parameters);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of updateQuery method, of class DatabaseManager.
*/
/*
@Test
public void testUpdateQuery() throws Exception
{
System.out.println("updateQuery");
Context context = null;
String query = "";
Object[] parameters = null;
int expResult = 0;
int result = DatabaseManager.updateQuery(context, query, parameters);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of create method, of class DatabaseManager.
*/
/*
@Test
public void testCreate() throws Exception
{
System.out.println("create");
Context context = null;
String table = "";
TableRow expResult = null;
TableRow result = DatabaseManager.create(context, table);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of find method, of class DatabaseManager.
*/
/*
@Test
public void testFind() throws Exception
{
System.out.println("find");
Context context = null;
String table = "";
int id = 0;
TableRow expResult = null;
TableRow result = DatabaseManager.find(context, table, id);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of findByUnique method, of class DatabaseManager.
*/
/*
@Test
public void testFindByUnique() throws Exception
{
System.out.println("findByUnique");
Context context = null;
String table = "";
String column = "";
Object value = null;
TableRow expResult = null;
TableRow result = DatabaseManager.findByUnique(context, table, column,
value);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of delete method, of class DatabaseManager.
*/
/*
@Test
public void testDelete_3args() throws Exception
{
System.out.println("delete");
Context context = null;
String table = "";
int id = 0;
int expResult = 0;
int result = DatabaseManager.delete(context, table, id);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of deleteByValue method, of class DatabaseManager.
*/
/*
@Test
public void testDeleteByValue() throws Exception
{
System.out.println("deleteByValue");
Context context = null;
String table = "";
String column = "";
Object value = null;
int expResult = 0;
int result = DatabaseManager.deleteByValue(context, table, column, value);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getConnection method, of class DatabaseManager.
*/
/*
@Test
public void testGetConnection() throws Exception
{
System.out.println("getConnection");
Connection expResult = null;
Connection result = DatabaseManager.getConnection();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getDataSource method, of class DatabaseManager.
*/
/*
@Test
public void testGetDataSource()
{
System.out.println("getDataSource");
DataSource expResult = null;
DataSource result = DatabaseManager.getDataSource();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of freeConnection method, of class DatabaseManager.
*/
/*
@Test
public void testFreeConnection()
{
System.out.println("freeConnection");
Connection c = null;
DatabaseManager.freeConnection(c);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of row method, of class DatabaseManager.
*/
/*
@Test
public void testRow() throws Exception
{
System.out.println("row");
String table = "";
TableRow expResult = null;
TableRow result = DatabaseManager.row(table);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of insert method, of class DatabaseManager.
*/
/*
@Test
public void testInsert() throws Exception
{
System.out.println("insert");
Context context = null;
TableRow row = null;
DatabaseManager.insert(context, row);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of update method, of class DatabaseManager.
*/
/*
@Test
public void testUpdate() throws Exception
{
System.out.println("update");
Context context = null;
TableRow row = null;
int expResult = 0;
int result = DatabaseManager.update(context, row);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of delete method, of class DatabaseManager.
*/
/*
@Test
public void testDelete_Context_TableRow() throws Exception
{
System.out.println("delete");
Context context = null;
TableRow row = null;
int expResult = 0;
int result = DatabaseManager.delete(context, row);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getColumnInfo method, of class DatabaseManager.
*/
/*
@Test
public void testGetColumnInfo_String() throws Exception
{
System.out.println("getColumnInfo");
String table = "";
Collection<ColumnInfo> expResult = null;
Collection<ColumnInfo> result = DatabaseManager.getColumnInfo(table);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getColumnInfo method, of class DatabaseManager.
*/
/*
@Test
public void testGetColumnInfo_String_String() throws Exception
{
System.out.println("getColumnInfo");
String table = "";
String column = "";
ColumnInfo expResult = null;
ColumnInfo result = DatabaseManager.getColumnInfo(table, column);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getColumnNames method, of class DatabaseManager.
*/
/*
@Test
public void testGetColumnNames_String() throws Exception
{
System.out.println("getColumnNames");
String table = "";
List<String> expResult = null;
List<String> result = DatabaseManager.getColumnNames(table);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getColumnNames method, of class DatabaseManager.
*/
/*
@Test
public void testGetColumnNames_ResultSetMetaData() throws Exception
{
System.out.println("getColumnNames");
ResultSetMetaData meta = null;
List<String> expResult = null;
List<String> result = DatabaseManager.getColumnNames(meta);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of canonicalize method, of class DatabaseManager.
*/
/*
@Test
public void testCanonicalize()
{
System.out.println("canonicalize");
String table = "";
String expResult = "";
String result = DatabaseManager.canonicalize(table);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of loadSql method, of class DatabaseManager.
*/
/*
@Test
public void testLoadSql_String() throws Exception
{
System.out.println("loadSql");
String sql = "";
DatabaseManager.loadSql(sql);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of loadSql method, of class DatabaseManager.
*/
/*
@Test
public void testLoadSql_Reader() throws Exception
{
System.out.println("loadSql");
Reader r = null;
DatabaseManager.loadSql(r);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of process method, of class DatabaseManager.
*/
/*
@Test
public void testProcess_ResultSet_String() throws Exception
{
System.out.println("process");
ResultSet results = null;
String table = "";
TableRow expResult = null;
TableRow result = DatabaseManager.process(results, table);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of process method, of class DatabaseManager.
*/
/*
@Test
public void testProcess_3args() throws Exception
{
System.out.println("process");
ResultSet results = null;
String table = "";
List<String> pColumnNames = null;
TableRow expResult = null;
TableRow result = DatabaseManager.process(results, table, pColumnNames);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getPrimaryKeyColumn method, of class DatabaseManager.
*/
/*
@Test
public void testGetPrimaryKeyColumn_TableRow() throws Exception
{
System.out.println("getPrimaryKeyColumn");
TableRow row = null;
String expResult = "";
String result = DatabaseManager.getPrimaryKeyColumn(row);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getPrimaryKeyColumn method, of class DatabaseManager.
*/
/*
@Test
public void testGetPrimaryKeyColumn_String() throws Exception
{
System.out.println("getPrimaryKeyColumn");
String table = "";
String expResult = "";
String result = DatabaseManager.getPrimaryKeyColumn(table);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getPrimaryKeyColumnInfo method, of class DatabaseManager.
*/
/*
@Test
public void testGetPrimaryKeyColumnInfo() throws Exception
{
System.out.println("getPrimaryKeyColumnInfo");
String table = "";
ColumnInfo expResult = null;
ColumnInfo result = DatabaseManager.getPrimaryKeyColumnInfo(table);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of shutdown method, of class DatabaseManager.
*/
/*
@Test
public void testShutdown() throws Exception
{
System.out.println("shutdown");
DatabaseManager.shutdown();
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of getDbName method, of class DatabaseManager.
*/
@Test
public void testGetDbName()
{
System.out.println("getDbName");
String expResult = "H2";
String result = DatabaseManager.getDbName();
assertEquals(expResult, result);
}
/**
* Test of loadParameters method, of class DatabaseManager.
*/
/*
@Test
public void testLoadParameters() throws Exception
{
System.out.println("loadParameters");
PreparedStatement statement = null;
Object[] parameters = null;
DatabaseManager.loadParameters(statement, parameters);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of main method, of class DatabaseManager.
*/
/*
@Test
public void testMain()
{
System.out.println("main");
String[] args = null;
DatabaseManager.main(args);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
/**
* Test of applyOffsetAndLimit method, of class DatabaseManager.
*/
/*
@Test
public void testApplyOffsetAndLimit()
{
System.out.println("applyOffsetAndLimit");
StringBuffer query = null;
List<Serializable> params = null;
int offset = 0;
int limit = 0;
DatabaseManager.applyOffsetAndLimit(query, params, offset, limit);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
*/
}

View File

@@ -14,7 +14,6 @@ import java.sql.Statement;
import java.sql.Types;
import java.util.List;
import mockit.Deencapsulation;
import mockit.Mock;
import mockit.MockUp;
@@ -35,16 +34,14 @@ public final class MockDatabaseManager
{
// Set our logger to specify the Mock class, so we know which logs are from the "real" vs "mock" class
private static final Logger log = Logger.getLogger(MockDatabaseManager.class);
// Get the values of private static variables 'isOracle' and 'isPostgres' from
// DatabaseManager itself (by using Deencapsulation)
private static final boolean isOracle = Deencapsulation.getField(DatabaseManager.class, "isOracle");
private static final boolean isPostgres = Deencapsulation.getField(DatabaseManager.class, "isPostgres");
/** Is our DBMS Oracle-like? */
private static final boolean isOracle = DatabaseManager.isOracle();
/**
* Override/Mock the default "setConstraintDeferred()" method in order to
* Override/Mock the default "setConstraintDeferred()" method in order to
* add some custom H2-specific code (look for the comments with "H2" in them).
*
*
* Set the constraint check to deferred (commit time)
*
* @param context
@@ -58,13 +55,13 @@ public final class MockDatabaseManager
String constraintName) throws SQLException
{
log.debug("Mocked setContraintDeferred() method for H2 database");
Statement statement = null;
try
{
statement = context.getDBConnection().createStatement();
//statement.execute("SET CONSTRAINTS " + constraintName + " DEFERRED");
// H2 does NOT support "SET CONSTRAINTS" syntax.
// H2 does NOT support "SET CONSTRAINTS" syntax.
// Instead it requires the following SQL
statement.execute("SET REFERENTIAL_INTEGRITY FALSE");
statement.close();
@@ -84,11 +81,11 @@ public final class MockDatabaseManager
}
}
}
/**
* Override/Mock the default "setConstraintImmediate()" method in order to
* Override/Mock the default "setConstraintImmediate()" method in order to
* add some custom H2-specific code (look for the comments with "H2" in them).
*
*
* Set the constraint check to immediate (every query)
*
* @param context
@@ -102,13 +99,13 @@ public final class MockDatabaseManager
String constraintName) throws SQLException
{
log.debug("Mocked setContraintImmediate() method for H2 database");
Statement statement = null;
try
{
statement = context.getDBConnection().createStatement();
//statement.execute("SET CONSTRAINTS " + constraintName + " IMMEDIATE");
// H2 does NOT support "SET CONSTRAINTS" syntax.
// H2 does NOT support "SET CONSTRAINTS" syntax.
// Instead it requires the following SQL
statement.execute("SET REFERENTIAL_INTEGRITY TRUE");
statement.close();
@@ -127,11 +124,11 @@ public final class MockDatabaseManager
}
}
}
/**
* Override/Mock the default "process()" method in order to add some custom
* Override/Mock the default "process()" method in order to add some custom
* H2-specific code (look for the comments with "H2" in them below).
*
*
* Convert the current row in a ResultSet into a TableRow object.
*
* @param results
@@ -149,7 +146,7 @@ public final class MockDatabaseManager
{
// Added for H2 debugging
log.debug("Mocked process() method for H2 database");
ResultSetMetaData meta = results.getMetaData();
int columns = meta.getColumnCount() + 1;
@@ -239,7 +236,7 @@ public final class MockDatabaseManager
{
log.error("Unable to parse text from database", e);
}*/
// H2 assumes that "getBytes()" should return hexidecimal.
// So, the above commented out code will throw a JdbcSQLException:
// "Hexadecimal string contains non-hex character"