BrokenEvent nanotests

com.brokenevent.nanotests
Class DbAssert

java.lang.Object
  extended by com.brokenevent.nanotests.DbAssert

public final class DbAssert
extends java.lang.Object

Helper class for DataBase-related asserts. These methods can be used directly: DbAssert.assertQueryNotNull(...), however, they read better if they are referenced through static import:

 import static com.brokenevent.nanotests.DbAssert.*;
    ...
    assertQueryNotNull(...);
 

Author:
BrokenEvent

Constructor Summary
protected DbAssert()
          Protect constructor since it is a static only class
 
Method Summary
static void assertLastRow(java.lang.String table, java.lang.String id, java.lang.String field, boolean expected)
          Asserts that the value from the last row of the table equals given value.
static void assertLastRow(java.lang.String table, java.lang.String id, java.lang.String field, java.util.Date expected)
          Asserts that the value from the last row of the table equals given value.
static void assertLastRow(java.lang.String table, java.lang.String id, java.lang.String field, int expected)
          Asserts that the value from the last row of the table equals given value.
static void assertLastRow(java.lang.String table, java.lang.String id, java.lang.String field, java.lang.String expected)
          Asserts that the value from the last row of the table equals given value.
static void assertQueryNotNull(java.lang.String sql)
          Asserts that query result is not empty.
static void assertQueryNull(java.lang.String sql)
          Asserts that query result is empty.
static void assertRow(java.lang.String table, java.lang.String id, int idValue, java.lang.String field, boolean expected)
          Asserts that the value from one of the rows of the table equals the expected.
static void assertRow(java.lang.String table, java.lang.String id, int idValue, java.lang.String field, int expected)
          Asserts that the value from one of the rows of the table equals the expected.
static void assertRow(java.lang.String table, java.lang.String id, int idValue, java.lang.String field, java.lang.String expected)
          Asserts that the value from one of the rows of the table equals the expected.
static void assertRow(java.lang.String table, java.lang.String id, java.lang.String idValue, java.lang.String field, boolean expected)
          Asserts that the value from one of the rows of the table equals the expected.
static void assertRow(java.lang.String table, java.lang.String id, java.lang.String idValue, java.lang.String field, int expected)
          Asserts that the value from one of the rows of the table equals the expected.
static void assertRow(java.lang.String table, java.lang.String id, java.lang.String idValue, java.lang.String field, java.lang.String expected)
          Asserts that the value from one of the rows of the table equals the expected.
static void execute(java.lang.String sql)
          Helper method to execute non-query statement with the existing connection.
static int getLastIdNum(java.lang.String table, java.lang.String id)
          Helper method to get the last ID from the table where ID field is numeric.
static java.lang.String getLastIdStr(java.lang.String table, java.lang.String id)
          Helper method to get the last ID from the table where ID field is string.
static void initDbAssert(java.lang.String connectionString, java.lang.String login, java.lang.String password, java.lang.String driverClassName)
          Initialize DbAssert before usage.
static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> query(java.lang.String sql)
          Helper method to execute query with the existing connection.
static java.util.Map<java.lang.String,java.lang.Object> querySingle(java.lang.String sql)
          Helper method to execute query with a single-row result with the existing connection.
static
<T> java.util.List<T>
querySingleColumn(java.lang.String sql)
          Helper method to execute query with a single-column result with the existing connection.
static void shutdownDbAssert()
          Shutdown db connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DbAssert

protected DbAssert()
Protect constructor since it is a static only class

Method Detail

initDbAssert

public static void initDbAssert(java.lang.String connectionString,
                                java.lang.String login,
                                java.lang.String password,
                                java.lang.String driverClassName)
Initialize DbAssert before usage. Should be called from org.junit.Before or org.junit.BeforeClass method of the testcase.

Parameters:
connectionString - jdbc connection string
login - database login
password - database password
driverClassName - driver classname

shutdownDbAssert

public static void shutdownDbAssert()
Shutdown db connection. Should be called from org.junit.After or org.junit.AfterClass method of the testcase.


query

public static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> query(java.lang.String sql)
Helper method to execute query with the existing connection.

Parameters:
sql - query text
Returns:
query result

querySingle

public static java.util.Map<java.lang.String,java.lang.Object> querySingle(java.lang.String sql)
Helper method to execute query with a single-row result with the existing connection.

Parameters:
sql - query text
Returns:
query result

querySingleColumn

public static <T> java.util.List<T> querySingleColumn(java.lang.String sql)
Helper method to execute query with a single-column result with the existing connection.

Parameters:
sql - query text
Returns:
query result

execute

public static void execute(java.lang.String sql)
Helper method to execute non-query statement with the existing connection.

Parameters:
sql - query text

assertQueryNotNull

public static void assertQueryNotNull(java.lang.String sql)
Asserts that query result is not empty. If it is, an AssertionError is thrown.

Parameters:
sql - SQL query text

assertQueryNull

public static void assertQueryNull(java.lang.String sql)
Asserts that query result is empty. If it isn't, an AssertionError is thrown.

Parameters:
sql - SQL query text

assertLastRow

public static void assertLastRow(java.lang.String table,
                                 java.lang.String id,
                                 java.lang.String field,
                                 java.lang.String expected)
Asserts that the value from the last row of the table equals given value. If it isn't, an AssertionError is thrown.
The last ID will be got by a query:
SELECT MAX(id) FROM table
Beware to use this assertion when running multiple tests interconnected with a single table at once. Some tests can fail due to thread operations intersection.

Parameters:
table - table name to query from
id - table primary key field name
field - field name to assert value from
expected - expected value

assertLastRow

public static void assertLastRow(java.lang.String table,
                                 java.lang.String id,
                                 java.lang.String field,
                                 int expected)
Asserts that the value from the last row of the table equals given value. If it isn't, an AssertionError is thrown.
The last ID will be got by a query:
SELECT MAX(id) FROM table
Beware to use this assertion when running multiple tests interconnected with a single table at once. Some tests can fail due to thread operations intersection.

Parameters:
table - table name to query from
id - table primary key field name
field - field name to assert value from
expected - expected value

assertLastRow

public static void assertLastRow(java.lang.String table,
                                 java.lang.String id,
                                 java.lang.String field,
                                 java.util.Date expected)
Asserts that the value from the last row of the table equals given value. If it isn't, an AssertionError is thrown.
The last ID will be got by a query:
SELECT MAX(id) FROM table
Beware to use this assertion when running multiple tests interconnected with a single table at once. Some tests can fail due to thread operations intersection.

Parameters:
table - table name to query from
id - table primary key field name
field - field name to assert value from
expected - expected value

assertLastRow

public static void assertLastRow(java.lang.String table,
                                 java.lang.String id,
                                 java.lang.String field,
                                 boolean expected)
Asserts that the value from the last row of the table equals given value. If it isn't, an AssertionError is thrown.
The last ID will be got by a query:
SELECT MAX(id) FROM table
Beware to use this assertion when running multiple tests interconnected with a single table at once. Some tests can fail due to thread operations intersection.

Parameters:
table - table name to query from
id - table primary key field name
field - field name to assert value from
expected - expected value

getLastIdNum

public static int getLastIdNum(java.lang.String table,
                               java.lang.String id)
Helper method to get the last ID from the table where ID field is numeric. Uses query:
SELECT MAX(id) FROM table

Parameters:
table - table name to query from
id - table primary key field name
Returns:
last ID number

getLastIdStr

public static java.lang.String getLastIdStr(java.lang.String table,
                                            java.lang.String id)
Helper method to get the last ID from the table where ID field is string. Uses query:
SELECT MAX(id) FROM table

Parameters:
table - table name to query from
id - table primary key field name
Returns:
last ID string value

assertRow

public static void assertRow(java.lang.String table,
                             java.lang.String id,
                             java.lang.String idValue,
                             java.lang.String field,
                             java.lang.String expected)
Asserts that the value from one of the rows of the table equals the expected. If it isn't, an AssertionError is thrown.

Parameters:
table - table name to query from
id - table primary key field name (or other field to use in WHERE clause)
idValue - value of the primary key field (or other field to use on comparison of the WHERE clause)
field - field name to assert value from
expected - expected value

assertRow

public static void assertRow(java.lang.String table,
                             java.lang.String id,
                             java.lang.String idValue,
                             java.lang.String field,
                             int expected)
Asserts that the value from one of the rows of the table equals the expected. If it isn't, an AssertionError is thrown.

Parameters:
table - table name to query from
id - table primary key field name (or other field to use in WHERE clause)
idValue - value of the primary key field (or other field to use on comparison of the WHERE clause)
field - field name to assert value from
expected - expected value

assertRow

public static void assertRow(java.lang.String table,
                             java.lang.String id,
                             java.lang.String idValue,
                             java.lang.String field,
                             boolean expected)
Asserts that the value from one of the rows of the table equals the expected. If it isn't, an AssertionError is thrown.

Parameters:
table - table name to query from
id - table primary key field name (or other field to use in WHERE clause)
idValue - value of the primary key field (or other field to use on comparison of the WHERE clause)
field - field name to assert value from
expected - expected value

assertRow

public static void assertRow(java.lang.String table,
                             java.lang.String id,
                             int idValue,
                             java.lang.String field,
                             java.lang.String expected)
Asserts that the value from one of the rows of the table equals the expected. If it isn't, an AssertionError is thrown.

Parameters:
table - table name to query from
id - table primary key field name (or other field to use in WHERE clause)
idValue - value of the primary key field (or other field to use on comparison of the WHERE clause)
field - field name to assert value from
expected - expected value

assertRow

public static void assertRow(java.lang.String table,
                             java.lang.String id,
                             int idValue,
                             java.lang.String field,
                             int expected)
Asserts that the value from one of the rows of the table equals the expected. If it isn't, an AssertionError is thrown.

Parameters:
table - table name to query from
id - table primary key field name (or other field to use in WHERE clause)
idValue - value of the primary key field (or other field to use on comparison of the WHERE clause)
field - field name to assert value from
expected - expected value

assertRow

public static void assertRow(java.lang.String table,
                             java.lang.String id,
                             int idValue,
                             java.lang.String field,
                             boolean expected)
Asserts that the value from one of the rows of the table equals the expected. If it isn't, an AssertionError is thrown.

Parameters:
table - table name to query from
id - table primary key field name (or other field to use in WHERE clause)
idValue - value of the primary key field (or other field to use on comparison of the WHERE clause)
field - field name to assert value from
expected - expected value

BrokenEvent nanotests