📄 jdbctemplatetests.java
字号:
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
int[] actualRowsAffected = template.batchUpdate(sql, setter);
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(rowsAffected[0], actualRowsAffected[0]);
assertEquals(rowsAffected[1], actualRowsAffected[1]);
ctrlPreparedStatement.verify();
ctrlDatabaseMetaData.verify();
}
public void testInterruptibleBatchUpdate() throws Exception {
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
final int[] ids = new int[] { 100, 200 };
final int[] rowsAffected = new int[] { 1, 2 };
MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
mockPreparedStatement.getConnection();
ctrlPreparedStatement.setReturnValue(mockConnection);
mockPreparedStatement.setInt(1, ids[0]);
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.addBatch();
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.setInt(1, ids[1]);
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.addBatch();
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.executeBatch();
ctrlPreparedStatement.setReturnValue(rowsAffected);
mockPreparedStatement.getWarnings();
ctrlPreparedStatement.setReturnValue(null);
mockPreparedStatement.close();
ctrlPreparedStatement.setVoidCallable();
MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
mockDatabaseMetaData.getDatabaseProductName();
ctrlDatabaseMetaData.setReturnValue("MySQL");
mockDatabaseMetaData.supportsBatchUpdates();
ctrlDatabaseMetaData.setReturnValue(true);
mockConnection.prepareStatement(sql);
ctrlConnection.setReturnValue(mockPreparedStatement);
mockConnection.getMetaData();
ctrlConnection.setReturnValue(mockDatabaseMetaData, 2);
ctrlPreparedStatement.replay();
ctrlDatabaseMetaData.replay();
replay();
BatchPreparedStatementSetter setter =
new InterruptibleBatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
if (i < ids.length) {
ps.setInt(1, ids[i]);
}
}
public int getBatchSize() {
return 1000;
}
public boolean isBatchExhausted(int i) {
return (i >= ids.length);
}
};
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
int[] actualRowsAffected = template.batchUpdate(sql, setter);
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(rowsAffected[0], actualRowsAffected[0]);
assertEquals(rowsAffected[1], actualRowsAffected[1]);
ctrlPreparedStatement.verify();
ctrlDatabaseMetaData.verify();
}
public void testInterruptibleBatchUpdateWithBaseClass() throws Exception {
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
final int[] ids = new int[] { 100, 200 };
final int[] rowsAffected = new int[] { 1, 2 };
MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
mockPreparedStatement.getConnection();
ctrlPreparedStatement.setReturnValue(mockConnection);
mockPreparedStatement.setInt(1, ids[0]);
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.addBatch();
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.setInt(1, ids[1]);
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.addBatch();
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.executeBatch();
ctrlPreparedStatement.setReturnValue(rowsAffected);
mockPreparedStatement.getWarnings();
ctrlPreparedStatement.setReturnValue(null);
mockPreparedStatement.close();
ctrlPreparedStatement.setVoidCallable();
MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
mockDatabaseMetaData.getDatabaseProductName();
ctrlDatabaseMetaData.setReturnValue("MySQL");
mockDatabaseMetaData.supportsBatchUpdates();
ctrlDatabaseMetaData.setReturnValue(true);
mockConnection.prepareStatement(sql);
ctrlConnection.setReturnValue(mockPreparedStatement);
mockConnection.getMetaData();
ctrlConnection.setReturnValue(mockDatabaseMetaData, 2);
ctrlPreparedStatement.replay();
ctrlDatabaseMetaData.replay();
replay();
BatchPreparedStatementSetter setter =
new AbstractInterruptibleBatchPreparedStatementSetter() {
protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
if (i < ids.length) {
ps.setInt(1, ids[i]);
return true;
}
else {
return false;
}
}
};
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
int[] actualRowsAffected = template.batchUpdate(sql, setter);
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(rowsAffected[0], actualRowsAffected[0]);
assertEquals(rowsAffected[1], actualRowsAffected[1]);
ctrlPreparedStatement.verify();
ctrlDatabaseMetaData.verify();
}
public void testBatchUpdateWithPreparedStatementAndNoBatchSupport() throws Exception {
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
final int[] ids = new int[] { 100, 200 };
final int[] rowsAffected = new int[] { 1, 2 };
MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
mockPreparedStatement.getConnection();
ctrlPreparedStatement.setReturnValue(mockConnection);
mockPreparedStatement.setInt(1, ids[0]);
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.executeUpdate();
ctrlPreparedStatement.setReturnValue(rowsAffected[0]);
mockPreparedStatement.setInt(1, ids[1]);
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.executeUpdate();
ctrlPreparedStatement.setReturnValue(rowsAffected[1]);
mockPreparedStatement.getWarnings();
ctrlPreparedStatement.setReturnValue(null);
mockPreparedStatement.close();
ctrlPreparedStatement.setVoidCallable();
mockConnection.prepareStatement(sql);
ctrlConnection.setReturnValue(mockPreparedStatement);
ctrlPreparedStatement.replay();
replay();
BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setInt(1, ids[i]);
}
public int getBatchSize() {
return ids.length;
}
};
JdbcTemplate template = new JdbcTemplate(mockDataSource);
int[] actualRowsAffected = template.batchUpdate(sql, setter);
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(rowsAffected[0], actualRowsAffected[0]);
assertEquals(rowsAffected[1], actualRowsAffected[1]);
ctrlPreparedStatement.verify();
}
public void testBatchUpdateFails() throws Exception {
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
final int[] ids = new int[] { 100, 200 };
SQLException sex = new SQLException();
MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
mockPreparedStatement.getConnection();
ctrlPreparedStatement.setReturnValue(mockConnection);
mockPreparedStatement.setInt(1, ids[0]);
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.addBatch();
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.setInt(1, ids[1]);
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.addBatch();
ctrlPreparedStatement.setVoidCallable();
mockPreparedStatement.executeBatch();
ctrlPreparedStatement.setThrowable(sex);
mockPreparedStatement.close();
ctrlPreparedStatement.setVoidCallable();
MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
mockDatabaseMetaData.getDatabaseProductName();
ctrlDatabaseMetaData.setReturnValue("MySQL");
mockDatabaseMetaData.supportsBatchUpdates();
ctrlDatabaseMetaData.setReturnValue(true);
ctrlConnection.reset();
mockConnection.prepareStatement(sql);
ctrlConnection.setReturnValue(mockPreparedStatement);
mockConnection.getMetaData();
ctrlConnection.setReturnValue(mockDatabaseMetaData, 2);
mockConnection.close();
ctrlConnection.setVoidCallable(2);
ctrlPreparedStatement.replay();
ctrlDatabaseMetaData.replay();
replay();
BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setInt(1, ids[i]);
}
public int getBatchSize() {
return ids.length;
}
};
try {
JdbcTemplate template = new JdbcTemplate(mockDataSource);
template.batchUpdate(sql, setter);
fail("Should have failed because of SQLException in bulk update");
}
catch (DataAccessException ex) {
assertTrue("Root cause is SQLException", ex.getCause() == sex);
}
ctrlPreparedStatement.verify();
ctrlDatabaseMetaData.verify();
}
public void testCouldntGetConnectionOrExceptionTranslator() throws SQLException {
SQLException sex = new SQLException("foo", "07xxx");
ctrlDataSource = MockControl.createControl(DataSource.class);
mockDataSource = (DataSource) ctrlDataSource.getMock();
mockDataSource.getConnection();
// Expect two calls (one call after caching data product name): make get metadata fail also
ctrlDataSource.setThrowable(sex, 2);
replay();
try {
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
RowCountCallbackHandler rcch = new RowCountCallbackHandler();
template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
fail("Shouldn't have executed query without a connection");
}
catch (CannotGetJdbcConnectionException ex) {
// pass
assertTrue("Check root cause", ex.getCause() == sex);
}
ctrlDataSource.verify();
}
public void testCouldntGetConnectionForOperationOrExceptionTranslator() throws SQLException {
SQLException sex = new SQLException("foo", "07xxx");
// Change behavior in setUp() because we only expect one call to getConnection():
// none is necessary to get metadata for exception translator
ctrlDataSource = MockControl.createControl(DataSource.class);
mockDataSource = (DataSource) ctrlDataSource.getMock();
mockDataSource.getConnection();
// Expect two calls (one call after caching data product name): make get Metadata fail also
ctrlDataSource.setThrowable(sex, 2);
replay();
try {
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
RowCountCallbackHandler rcch = new RowCountCallbackHandler();
template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
fail("Shouldn't have executed query without a connection");
}
catch (CannotGetJdbcConnectionException ex) {
// pass
assertTrue("Check root cause", ex.getCause() == sex);
}
ctrlDataSource.verify();
}
public void testCouldntGetConnectionForOperationWithLazyExceptionTranslator() throws SQLException {
SQLException sex = new SQLException("foo", "07xxx");
ctrlDataSource = MockControl.createControl(DataSource.class);
mockDataSource = (DataSource) ctrlDataSource.getMock();
mockDataSource.getConnection();
ctrlDataSource.setThrowable(sex, 1);
replay();
try {
JdbcTemplate template2 = new JdbcTemplate();
template2.setDataSource(mockDataSource);
template2.afterPropertiesSet();
RowCountCallbackHandler rcch = new RowCountCallbackHandler();
template2.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
fail("Shouldn't have executed query without a connection");
}
catch (CannotGetJdbcConnectionException ex) {
// pass
assertTrue("Check root cause", ex.getCause() == sex);
}
ctrlDataSource.verify();
}
/**
* Verify that afterPropertiesSet invokes exception translator.
*/
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitialized() throws SQLException {
SQLException sex = new SQLException("foo", "07xxx");
ctrlDataSource = MockControl.createControl(DataSource.class);
mockDataSource = (DataSource) ctrlDataSource.getMock();
//mockConnection.getMetaData();
//ctrlConnection.setReturnValue(null, 1);
//mockConnection.close();
//ctrlConnection.setVoidCallable(1);
ctrlConnection.replay();
// Change behaviour in setUp() because we only expect one call to getConnection():
// none is necessary to get metadata for exception translator
ctrlDataSource = MockControl.createControl(DataSource.class);
mockDataSource = (DataSource) ctrlDataSource.getMock();
// Upfront call for metadata - no longer the case
//mockDataSource.getConnection();
//ctrlDataSource.setReturnValue(mockConnection, 1);
// One call for operation
mockDataSource.getConnection();
ctrlDataSource.setThrowable(sex, 2);
ctrlDataSource.replay();
try {
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
RowCountCallbackHandler rcch = new RowCountCallbackHandler();
template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
fail("Shouldn't have executed query without a connection");
}
catch (CannotGetJdbcConnectionException ex) {
// pass
assertTrue("Check root cause", ex.getCause() == sex);
}
ctrlDataSource.verify();
ctrlConnection.verify();
}
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitializedViaBeanProperty()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -