com.mockrunner.jdbc
Class AbstractResultSetHandler

java.lang.Object
  extended bycom.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 clearGlobalResultSet()
          Clears the global ResultSet.
 void clearResultSets()
          Clears the 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 the 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.
protected  boolean getExactMatch()
          Returns if specified SQL statements must match exactly.
 java.util.List getExecutedStatements()
          Returns the List of all executed SQL strings.
 MockResultSet getGlobalResultSet()
          Returns the global ResultSet.
 int getGlobalUpdateCount()
          Returns the global update count for executeUpdate calls.
 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).
 java.util.List getReturnedResultSets()
          Returns the List of all returned ResultSet objects.
 java.lang.Boolean getReturnsResultSet(java.lang.String sql)
          Returns if the specified SQL string is a select that returns a ResultSet.
 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).
protected  boolean getUseRegularExpressions()
          Returns if regular expression matching is enabled
 void prepareGlobalResultSet(MockResultSet resultSet)
          Prepare the global ResultSet.
 void prepareGlobalUpdateCount(int updateCount)
          Prepare the global update count for executeUpdate calls.
 void prepareResultSet(java.lang.String sql, MockResultSet resultSet)
          Prepare a ResultSet 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 if 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 setCaseSensitive(boolean caseSensitive)
          Set if specified SQL strings should be handled case sensitive.
 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. Currently there's only the FileResultSetFactory to create ResultSet objects based on CSV files but you can implement your own factories.

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. Currently there's only the FileResultSetFactory to create ResultSet objects based on CSV files but you can implement your own factories. Uses a random id.

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. 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 (which is much faster). Enable this feature only if necessary. 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

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

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 objects.

Returns:
the List of returned ResultSet objects

clearResultSets

public void clearResultSets()
Clears the ResultSet objects.


clearUpdateCounts

public void clearUpdateCounts()
Clears the 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.


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

getResultSet

public MockResultSet getResultSet(java.lang.String sql)
Returns the first 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 MockResultSet

getGlobalResultSet

public MockResultSet getGlobalResultSet()
Returns the global ResultSet. The statement returns the global ResultSet if no ResultSet can be found for the current SQL string.

Returns:
the global MockResultSet

getUpdateCount

public java.lang.Integer getUpdateCount(java.lang.String sql)
Returns the first update count 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 update count

getGlobalUpdateCount

public int getGlobalUpdateCount()
Returns the global update count for executeUpdate calls. The statement returns the global update count if no update count can be found for the current SQL string.

Returns:
the global update count

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 SELECT. 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

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

prepareGlobalResultSet

public void prepareGlobalResultSet(MockResultSet resultSet)
Prepare the global ResultSet. The statement returns the global ResultSet if no ResultSet can be found for the current SQL string.

Parameters:
resultSet - the 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

prepareGlobalUpdateCount

public void prepareGlobalUpdateCount(int updateCount)
Prepare the global update count for executeUpdate calls. The statement returns the global update count if no update count can be found for the current SQL string.

Parameters:
updateCount - the update count

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 SELECT. 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 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

clearGlobalResultSet

public void clearGlobalResultSet()
Clears the global ResultSet.


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