Extension Guidelines
The design is based on the following classification of operations: * Methods that change the state of the container (e.g connection.commit()) * Methods that provide container specific components (e.g connection.createStatement()) * Methods that are simple getter/setter types (e.g connection.getHoldability(), connection.setHoldability()) * Methods that prepare the container/component for use (e.g connection.setSavePoint()) * Methods that the users directly interact with and expect certain output (e.g resultSet.getInt())
Based on the above classification we have followed the simple rules given below. These standardizes how the mock containers behave and if you are looking for creating extensions, you would follow the same classification.
Method types | Examples | Has Implementation | Needs Mock Implementation |
Container Specific |
connection.commit() connection.createStatement() |
Yes | No |
Getter/ Setters |
connection.getHoldability() connection.setHoldability() |
Yes | No |
Prepare methods | connection.setSavePoint() | Yes | No |
Integration | resultSet.getInt() | No | Yes |
Some of the cases are explained in detail
public boolean getAutoCommit() throws SQLException {
assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
return _getAutoCommit();
}
public boolean _getAutoCommit() {
return autoCommitState \=\= AutoCommitState.ENABLED?true:false;
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
_setAutoCommit(autoCommit); }
public void _setAutoCommit(boolean autoCommit) {
autoCommitState = autoCommit? AutoCommitState.ENABLED:AutoCommitState.DISABLED;
}
Another Example
public boolean last() throws SQLException {
assertThat(mockState, AnyOf.anyOf(IsNot.not(ResultSetState.CLOSE), IsNot.not(ResultSetState.AUTOCLOSE)));
return _last();
}
public boolean _last() {
throw new NeedsMockDefinitionException(); \ }
public Statement createStatement() throws SQLException {
assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
MockStatement st= _createStatement();
mockStatements.add(st);
return st;
}
public MockStatement _createStatement() {
return new MockStatement(this);
}
Another example
public void commit() throws SQLException {
try{
assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
assertThat(autoCommitState, Is.is(AutoCommitState.DISABLED));
mockTransactionState = TransactionState.COMMIT;
_commit();
} catch (SQLException e){
mockTransactionState = TransactionState.AUTOROLLBACK;
throw e;
}
}
public void _commit() throws SQLException {
}
public void rollback(Savepoint savepoint) throws SQLException {
assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
assertThat(autoCommitState, Is.is(AutoCommitState.DISABLED));
mockTransactionState = TransactionState.ROLLBACK;
_rollback(savepoint);
}
public void _rollback(Savepoint savepoint) throws SQLException {
}
public void clearBatch() throws SQLException {
assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
assertThat("Auto Commit State", parent.autoCommitState, Is.is(MockConnection.AutoCommitState.DISABLED));
_clearBatch();
}
public void _clearBatch() {
batchSQLs.clear();
}