com.mockrunner.jdbc
Class AbstractResultSetHandler

java.lang.Object
  extended by com.mockrunner.jdbc.AbstractResultSetHandler
Direct Known Subclasses:
AbstractParameterResultSetHandler, StatementResultSetHandler

public abstract class AbstractResultSetHandler
extends java.lang.Object

Abstract base class for all ResultSet handlers. Used to coordinate ResultSet objects for a statement. You can use this class to prepare ResultSet objects and update count values that are returned by the execute method of a statement, if the current SQL string matches. Furthermore it can be used to create ResultSet objects. Please note that the ResultSet objects you create and prepare with this handler are cloned when executing statements. So you cannot rely on object identity. You have to use the id of the ResultSet to identify it. The ResultSet objects returned by getReturnedResultSets() are actually the instances the executed statements returned.


Constructor Summary
AbstractResultSetHandler()
           
 
Method Summary
 void addExecutedStatement(java.lang.String sql)
          Collects all SQL strings that were executed.
 void addReturnedResultSet(MockResultSet resultSet)
          Collects all ResultSet objects that were returned by a Statement, PreparedStatement or CallableStatement.
 void addReturnedResultSets(MockResultSet[] resultSets)
          Collects all ResultSet[] objects that were returned by a Statement, PreparedStatement or CallableStatement.
 void clearGeneratedKeys()
          Clears the list of statements that return generated keys.
 void clearGlobalGeneratedKeys()
          Clears the prepared global generated keys ResultSet.
 void clearGlobalResultSet()
          Clears the prepared global ResultSet.
 void clearGlobalUpdateCount()
          Clears the prepared global update count.
 void clearResultSets()
          Clears all prepared ResultSet objects.
 void clearReturnsResultSet()
          Clears the definitions if statements return ResultSet objects or update counts.
 void clearThrowsSQLException()
          Clears the list of statements that should throw an exception.
 void clearUpdateCounts()
          Clears all prepared update counts.
 MockResultSet createResultSet()
          Creates a new ResultSet with a random id.
 MockResultSet createResultSet(ResultSetFactory factory)
          Returns a new ResultSet created by the specified factory.
 MockResultSet createResultSet(java.lang.String id)
          Creates a new ResultSet with the specified id.
 MockResultSet createResultSet(java.lang.String id, ResultSetFactory factory)
          Returns a new ResultSet created by the specified factory.
protected  boolean getCaseSensitive()
          Returns if specified SQL strings should be handled case sensitive.
 boolean getContinueProcessingOnBatchFailure()
          Returns if batch processing should be continued if one of the commands in the batch fails.
protected  boolean getExactMatch()
          Returns if specified SQL statements must match exactly.
 java.util.List getExecutedStatements()
          Returns the List of all executed SQL strings.
 MockResultSet getGeneratedKeys(java.lang.String sql)
          Returns the first generated keys ResultSet that matches the specified SQL string.
 java.util.Map getGeneratedKeysMap()
          Returns the Map of all generated keys ResultSet objects, that were added with prepareGeneratedKeys(String, MockResultSet).
 MockResultSet getGlobalGeneratedKeys()
          Returns the global generated keys ResultSet.
 MockResultSet getGlobalResultSet()
          Returns the global ResultSet.
 MockResultSet[] getGlobalResultSets()
          Returns the global ResultSet[].
 int getGlobalUpdateCount()
          Returns the global update count for executeUpdate calls.
 int[] getGlobalUpdateCounts()
          Returns the array of global update counts.
 MockResultSet getResultSet(java.lang.String sql)
          Returns the first ResultSet that matches the specified SQL string.
 java.util.Map getResultSetMap()
          Returns the Map of all ResultSet objects, that were added with prepareResultSet(String, MockResultSet).
 MockResultSet[] getResultSets(java.lang.String sql)
          Returns the first ResultSet[] that matches the specified SQL string.
 java.util.List getReturnedResultSets()
          Returns the List of all returned ResultSet or ResultSet[] objects.
 java.lang.Boolean getReturnsResultSet(java.lang.String sql)
          Returns if the specified SQL string is a select that returns a ResultSet.
 java.sql.SQLException getSQLException(java.lang.String sql)
          Returns the SQLException the specified SQL string should throw.
 boolean getThrowsSQLException(java.lang.String sql)
          Returns if the specified SQL string should raise an exception.
 java.lang.Integer getUpdateCount(java.lang.String sql)
          Returns the first update count that matches the specified SQL string.
 java.util.Map getUpdateCountMap()
          Returns the Map of all update counts, that were added with prepareUpdateCount(String, int).
 java.lang.Integer[] getUpdateCounts(java.lang.String sql)
          Returns the first update count array that matches the specified SQL string.
protected  boolean getUseRegularExpressions()
          Returns if regular expression matching is enabled
 boolean hasMultipleGlobalResultSets()
          Returns if multiple global result sets have been prepared, i.e. if an array of global result sets was prepared.
 boolean hasMultipleGlobalUpdateCounts()
          Returns if multiple global update counts have been prepared, i.e. if an array of global update counts was prepared.
 boolean hasMultipleResultSets(java.lang.String sql)
          Returns the if the specified SQL string returns multiple result sets.
 boolean hasMultipleUpdateCounts(java.lang.String sql)
          Returns the if the specified SQL string returns multiple update counts.
 void prepareGeneratedKeys(java.lang.String sql, MockResultSet generatedKeysResult)
          Prepare the generated keys ResultSet for a specified SQL string.
 void prepareGlobalGeneratedKeys(MockResultSet generatedKeysResult)
          Prepare the global generated keys ResultSet.
 void prepareGlobalResultSet(MockResultSet resultSet)
          Prepare the global ResultSet.
 void prepareGlobalResultSets(MockResultSet[] resultSets)
          Prepare an array of global ResultSet objects.
 void prepareGlobalUpdateCount(int updateCount)
          Prepare the global update count for executeUpdate calls.
 void prepareGlobalUpdateCounts(int[] updateCounts)
          Prepare an array of global update count values for executeUpdate calls.
 void prepareResultSet(java.lang.String sql, MockResultSet resultSet)
          Prepare a ResultSet for a specified SQL string.
 void prepareResultSets(java.lang.String sql, MockResultSet[] resultSets)
          Prepare an array of ResultSet objects for a specified SQL string.
 void prepareReturnsResultSet(java.lang.String sql, boolean returnsResultSet)
          Prepare if the specified SQL string is a select that returns a ResultSet.
 void prepareThrowsSQLException(java.lang.String sql)
          Prepare that the specified SQL string should raise an exception.
 void prepareThrowsSQLException(java.lang.String sql, java.sql.SQLException exc)
          Prepare that the specified SQL string should raise an exception.
 void prepareUpdateCount(java.lang.String sql, int updateCount)
          Prepare the update count for executeUpdate calls for a specified SQL string.
 void prepareUpdateCounts(java.lang.String sql, int[] updateCounts)
          Prepare an array update count values for executeUpdate calls for a specified SQL string.
 void setCaseSensitive(boolean caseSensitive)
          Set if specified SQL strings should be handled case sensitive.
 void setContinueProcessingOnBatchFailure(boolean continueProcessingOnBatchFailure)
          Set if batch processing should be continued if one of the commands in the batch fails.
 void setExactMatch(boolean exactMatch)
          Set if specified SQL statements must match exactly.
 void setUseRegularExpressions(boolean useRegularExpressions)
          Set if regular expressions should be used when matching SQL statements.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractResultSetHandler

public AbstractResultSetHandler()
Method Detail

createResultSet

public MockResultSet createResultSet()
Creates a new ResultSet with a random id.

Returns:
the new ResultSet

createResultSet

public MockResultSet createResultSet(java.lang.String id)
Creates a new ResultSet with the specified id.

Parameters:
id - the id
Returns:
the new ResultSet

createResultSet

public MockResultSet createResultSet(ResultSetFactory factory)
Returns a new ResultSet created by the specified factory. Creates a random id.

Parameters:
factory - the ResultSetFactory
Returns:
the new ResultSet

createResultSet

public MockResultSet createResultSet(java.lang.String id,
                                     ResultSetFactory factory)
Returns a new ResultSet created by the specified factory.

Parameters:
id - the id
factory - the ResultSetFactory
Returns:
the new ResultSet

setCaseSensitive

public void setCaseSensitive(boolean caseSensitive)
Set if specified SQL strings should be handled case sensitive. Defaults to to false, i.e. INSERT is the same as insert. Please note that this method controls SQL statement matching for prepared results and update counts, i.e. what statements the tested application has to execute to receive a specified result. Unlike JDBCTestModule.setCaseSensitive(boolean) it does not control the statement matching of JDBCTestModule methods.

Parameters:
caseSensitive - enable or disable case sensitivity

setExactMatch

public void setExactMatch(boolean exactMatch)
Set if specified SQL statements must match exactly. Defaults to false, i.e. the SQL string does not need to match exactly. If the original statement is insert into mytable values(?, ?, ?) the string insert into mytable will match this statement. Usually false is the best choice, so prepared ResultSet objects do not have to match exactly the current statements SQL string. The current SQL string just has to contain the SQL string for the prepared prepared ResultSet. Please note that this method controls SQL statement matching for prepared results and update counts, i.e. what statements the tested application has to execute to receive a specified result. Unlike JDBCTestModule.setExactMatch(boolean) it does not control the statement matching of JDBCTestModule methods.

Parameters:
exactMatch - enable or disable exact matching

setUseRegularExpressions

public void setUseRegularExpressions(boolean useRegularExpressions)
Set if regular expressions should be used when matching SQL statements. Irrelevant if exactMatch is true. Default is false, i.e. you cannot use regular expressions and matching is based on string comparison. Please note that this method controls SQL statement matching for prepared results and update counts, i.e. what statements the tested application has to execute to receive a specified result. Unlike JDBCTestModule.setUseRegularExpressions(boolean) it does not control the statement matching of JDBCTestModule methods.

Parameters:
useRegularExpressions - should regular expressions be used

setContinueProcessingOnBatchFailure

public void setContinueProcessingOnBatchFailure(boolean continueProcessingOnBatchFailure)
Set if batch processing should be continued if one of the commands in the batch fails. This behaviour is driver dependend. The default is false, i.e. if a command fails with an exception, batch processing will not continue and the remaining commands will not be executed.

Parameters:
continueProcessingOnBatchFailure - should batch processing be continued

addExecutedStatement

public void addExecutedStatement(java.lang.String sql)
Collects all SQL strings that were executed.

Parameters:
sql - the SQL string

addReturnedResultSet

public void addReturnedResultSet(MockResultSet resultSet)
Collects all ResultSet objects that were returned by a Statement, PreparedStatement or CallableStatement.

Parameters:
resultSet - the ResultSet

addReturnedResultSets

public void addReturnedResultSets(MockResultSet[] resultSets)
Collects all ResultSet[] objects that were returned by a Statement, PreparedStatement or CallableStatement. Called if a statement returns multiple result sets.

Parameters:
resultSets - the ResultSet[]

getExecutedStatements

public java.util.List getExecutedStatements()
Returns the List of all executed SQL strings.

Returns:
the List of executed SQL strings

getReturnedResultSets

public java.util.List getReturnedResultSets()
Returns the List of all returned ResultSet or ResultSet[] objects. The List contains arrays of result sets, if a query returned multiple result sets. If a query returned multiple result sets, the list will always contain the full array of ResultSet objects that were prepared, even if MockStatement.getMoreResults() was not called for all the result sets.

Returns:
the List of returned ResultSet or ResultSet[] objects

clearResultSets

public void clearResultSets()
Clears all prepared ResultSet objects.


clearUpdateCounts

public void clearUpdateCounts()
Clears all prepared update counts.


clearReturnsResultSet

public void clearReturnsResultSet()
Clears the definitions if statements return ResultSet objects or update counts.


clearThrowsSQLException

public void clearThrowsSQLException()
Clears the list of statements that should throw an exception.


clearGeneratedKeys

public void clearGeneratedKeys()
Clears the list of statements that return generated keys.


clearGlobalResultSet

public void clearGlobalResultSet()
Clears the prepared global ResultSet.


clearGlobalGeneratedKeys

public void clearGlobalGeneratedKeys()
Clears the prepared global generated keys ResultSet.


clearGlobalUpdateCount

public void clearGlobalUpdateCount()
Clears the prepared global update count.


getResultSetMap

public java.util.Map getResultSetMap()
Returns the Map of all ResultSet objects, that were added with prepareResultSet(String, MockResultSet). The SQL strings map to the corresponding ResultSet.

Returns:
the Map of ResultSet objects

getUpdateCountMap

public java.util.Map getUpdateCountMap()
Returns the Map of all update counts, that were added with prepareUpdateCount(String, int). The SQL strings map to the corresponding update count as Integer object.

Returns:
the Map of ResultSet objects

getGeneratedKeysMap

public java.util.Map getGeneratedKeysMap()
Returns the Map of all generated keys ResultSet objects, that were added with prepareGeneratedKeys(String, MockResultSet). The SQL strings map to the corresponding generated keys ResultSet.

Returns:
the Map of generated keys ResultSet objects

getResultSet

public MockResultSet getResultSet(java.lang.String sql)
Returns the first ResultSet that matches the specified SQL string. If the specified SQL string was prepared to return multiple result sets, the first one will be returned. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
Returns:
the corresponding MockResultSet

getResultSets

public MockResultSet[] getResultSets(java.lang.String sql)
Returns the first ResultSet[] that matches the specified SQL string. If the specified SQL string was prepared to return one single result set, this ResultSet will be wrapped in an array with one element. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
Returns:
the corresponding MockResultSet[]

hasMultipleResultSets

public boolean hasMultipleResultSets(java.lang.String sql)
Returns the if the specified SQL string returns multiple result sets. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
Returns:
true if the query returns multiple result sets, false otherwise

getGlobalResultSet

public MockResultSet getGlobalResultSet()
Returns the global ResultSet. If an array of global result sets was prepared, the first one will be returned.

Returns:
the global MockResultSet

getGlobalResultSets

public MockResultSet[] getGlobalResultSets()
Returns the global ResultSet[]. If one single ResultSet was prepared, this ResultSet will be wrapped in an array with one element.

Returns:
the global MockResultSet[]

hasMultipleGlobalResultSets

public boolean hasMultipleGlobalResultSets()
Returns if multiple global result sets have been prepared, i.e. if an array of global result sets was prepared.

Returns:
true if an array of global result sets was prepared, false otherwise

getUpdateCount

public java.lang.Integer getUpdateCount(java.lang.String sql)
Returns the first update count that matches the specified SQL string. If the specified SQL string was prepared to return multiple update counts, the first one will be returned. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
Returns:
the corresponding update count

getUpdateCounts

public java.lang.Integer[] getUpdateCounts(java.lang.String sql)
Returns the first update count array that matches the specified SQL string. If the specified SQL string was prepared to return one update count, this value will be wrapped in an array with one element. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
Returns:
the corresponding update count array

hasMultipleUpdateCounts

public boolean hasMultipleUpdateCounts(java.lang.String sql)
Returns the if the specified SQL string returns multiple update counts. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
Returns:
true if the SQL string returns multiple update counts, false otherwise

getGlobalUpdateCount

public int getGlobalUpdateCount()
Returns the global update count for executeUpdate calls. If an array of global update counts was prepared, the first one will be returned.

Returns:
the global update count

getGlobalUpdateCounts

public int[] getGlobalUpdateCounts()
Returns the array of global update counts. If one single update count value was prepared, this value will be wrapped in an array with one element.

Returns:
the array of global update counts

hasMultipleGlobalUpdateCounts

public boolean hasMultipleGlobalUpdateCounts()
Returns if multiple global update counts have been prepared, i.e. if an array of global update counts was prepared.

Returns:
true if an array of global update counts was prepared, false otherwise

getGeneratedKeys

public MockResultSet getGeneratedKeys(java.lang.String sql)
Returns the first generated keys ResultSet that matches the specified SQL string. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
Returns:
the corresponding generated keys MockResultSet

getGlobalGeneratedKeys

public MockResultSet getGlobalGeneratedKeys()
Returns the global generated keys ResultSet.

Returns:
the global generated keys MockResultSet

getReturnsResultSet

public java.lang.Boolean getReturnsResultSet(java.lang.String sql)
Returns if the specified SQL string is a select that returns a ResultSet. Usually you do not have to specify this. It is assumed that an SQL string returns a ResultSet if it contains the string select (case insensitive). Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
Returns:
true if the SQL string returns a ResultSet

getThrowsSQLException

public boolean getThrowsSQLException(java.lang.String sql)
Returns if the specified SQL string should raise an exception. This can be used to simulate database exceptions. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
Returns:
true if the specified SQL string should raise an exception, false otherwise

getSQLException

public java.sql.SQLException getSQLException(java.lang.String sql)
Returns the SQLException the specified SQL string should throw. Returns null if the specified SQL string should not throw an exception. This can be used to simulate database exceptions. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
Returns:
the SQLException or null

prepareResultSet

public void prepareResultSet(java.lang.String sql,
                             MockResultSet resultSet)
Prepare a ResultSet for a specified SQL string. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
resultSet - the corresponding MockResultSet

prepareResultSets

public void prepareResultSets(java.lang.String sql,
                              MockResultSet[] resultSets)
Prepare an array of ResultSet objects for a specified SQL string. This method can be used for queries that return multiple result sets. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
resultSets - the corresponding MockResultSet[]

prepareGlobalResultSet

public void prepareGlobalResultSet(MockResultSet resultSet)
Prepare the global ResultSet.

Parameters:
resultSet - the MockResultSet

prepareGlobalResultSets

public void prepareGlobalResultSets(MockResultSet[] resultSets)
Prepare an array of global ResultSet objects.

Parameters:
resultSets - the corresponding MockResultSet[]

prepareUpdateCount

public void prepareUpdateCount(java.lang.String sql,
                               int updateCount)
Prepare the update count for executeUpdate calls for a specified SQL string. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
updateCount - the update count

prepareUpdateCounts

public void prepareUpdateCounts(java.lang.String sql,
                                int[] updateCounts)
Prepare an array update count values for executeUpdate calls for a specified SQL string. This method can be used if multiple update counts are returned. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
updateCounts - the update count array

prepareGlobalUpdateCount

public void prepareGlobalUpdateCount(int updateCount)
Prepare the global update count for executeUpdate calls.

Parameters:
updateCount - the update count

prepareGlobalUpdateCounts

public void prepareGlobalUpdateCounts(int[] updateCounts)
Prepare an array of global update count values for executeUpdate calls.

Parameters:
updateCounts - the update count array

prepareGeneratedKeys

public void prepareGeneratedKeys(java.lang.String sql,
                                 MockResultSet generatedKeysResult)
Prepare the generated keys ResultSet for a specified SQL string. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
generatedKeysResult - the generated keys MockResultSet

prepareGlobalGeneratedKeys

public void prepareGlobalGeneratedKeys(MockResultSet generatedKeysResult)
Prepare the global generated keys ResultSet.

Parameters:
generatedKeysResult - the generated keys MockResultSet

prepareReturnsResultSet

public void prepareReturnsResultSet(java.lang.String sql,
                                    boolean returnsResultSet)
Prepare if the specified SQL string is a select that returns a ResultSet. Usually you do not have to specify this. It is assumed that an SQL string returns a ResultSet if it contains the string select (case insensitive). Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
returnsResultSet - specify if the SQL string returns a ResultSet

prepareThrowsSQLException

public void prepareThrowsSQLException(java.lang.String sql)
Prepare that the specified SQL string should raise an exception. This can be used to simulate database exceptions. This method creates an SQLException and will throw this exception. With prepareThrowsSQLException(String, SQLException) you can specify the exception. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string

prepareThrowsSQLException

public void prepareThrowsSQLException(java.lang.String sql,
                                      java.sql.SQLException exc)
Prepare that the specified SQL string should raise an exception. This can be used to simulate database exceptions. This method takes an exception object that will be thrown. Please note that you can modify the match parameters with setCaseSensitive(boolean), setExactMatch(boolean) and setUseRegularExpressions(boolean).

Parameters:
sql - the SQL string
exc - the SQLException that should be thrown

getCaseSensitive

protected boolean getCaseSensitive()
Returns if specified SQL strings should be handled case sensitive.

Returns:
is case sensitivity enabled or disabled

getExactMatch

protected boolean getExactMatch()
Returns if specified SQL statements must match exactly.

Returns:
is exact matching enabled or disabled

getUseRegularExpressions

protected boolean getUseRegularExpressions()
Returns if regular expression matching is enabled

Returns:
if regular expression matching is enabled

getContinueProcessingOnBatchFailure

public boolean getContinueProcessingOnBatchFailure()
Returns if batch processing should be continued if one of the commands in the batch fails.

Returns:
if batch processing should be continued