|
|
|
@ -191,7 +191,7 @@ public class JdbcTemplateTests { |
|
|
|
Object argument, JdbcTemplateCallback jdbcTemplateCallback) throws Exception { |
|
|
|
Object argument, JdbcTemplateCallback jdbcTemplateCallback) throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
String sql = "SELECT FORENAME FROM CUSTMR"; |
|
|
|
String sql = "SELECT FORENAME FROM CUSTMR"; |
|
|
|
String[] results = { "rod", "gary", " portia" }; |
|
|
|
String[] results = {"rod", "gary", " portia"}; |
|
|
|
|
|
|
|
|
|
|
|
class StringHandler implements RowCallbackHandler { |
|
|
|
class StringHandler implements RowCallbackHandler { |
|
|
|
private List<String> list = new LinkedList<>(); |
|
|
|
private List<String> list = new LinkedList<>(); |
|
|
|
@ -490,8 +490,8 @@ public class JdbcTemplateTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testBatchUpdateWithPreparedStatement() throws Exception { |
|
|
|
public void testBatchUpdateWithPreparedStatement() throws Exception { |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final int[] ids = new int[] { 100, 200 }; |
|
|
|
final int[] ids = new int[] {100, 200}; |
|
|
|
final int[] rowsAffected = new int[] { 1, 2 }; |
|
|
|
final int[] rowsAffected = new int[] {1, 2}; |
|
|
|
|
|
|
|
|
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected); |
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
@ -524,8 +524,8 @@ public class JdbcTemplateTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testInterruptibleBatchUpdate() throws Exception { |
|
|
|
public void testInterruptibleBatchUpdate() throws Exception { |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final int[] ids = new int[] { 100, 200 }; |
|
|
|
final int[] ids = new int[] {100, 200}; |
|
|
|
final int[] rowsAffected = new int[] { 1, 2 }; |
|
|
|
final int[] rowsAffected = new int[] {1, 2}; |
|
|
|
|
|
|
|
|
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected); |
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
@ -565,8 +565,8 @@ public class JdbcTemplateTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testInterruptibleBatchUpdateWithBaseClass() throws Exception { |
|
|
|
public void testInterruptibleBatchUpdateWithBaseClass() throws Exception { |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final int[] ids = new int[] { 100, 200 }; |
|
|
|
final int[] ids = new int[] {100, 200}; |
|
|
|
final int[] rowsAffected = new int[] { 1, 2 }; |
|
|
|
final int[] rowsAffected = new int[] {1, 2}; |
|
|
|
|
|
|
|
|
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected); |
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
@ -602,8 +602,8 @@ public class JdbcTemplateTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport() throws Exception { |
|
|
|
public void testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport() throws Exception { |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final int[] ids = new int[] { 100, 200 }; |
|
|
|
final int[] ids = new int[] {100, 200}; |
|
|
|
final int[] rowsAffected = new int[] { 1, 2 }; |
|
|
|
final int[] rowsAffected = new int[] {1, 2}; |
|
|
|
|
|
|
|
|
|
|
|
given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected[0], rowsAffected[1]); |
|
|
|
given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected[0], rowsAffected[1]); |
|
|
|
mockDatabaseMetaData(false); |
|
|
|
mockDatabaseMetaData(false); |
|
|
|
@ -639,8 +639,8 @@ public class JdbcTemplateTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testBatchUpdateWithPreparedStatementAndNoBatchSupport() throws Exception { |
|
|
|
public void testBatchUpdateWithPreparedStatementAndNoBatchSupport() throws Exception { |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final int[] ids = new int[] { 100, 200 }; |
|
|
|
final int[] ids = new int[] {100, 200}; |
|
|
|
final int[] rowsAffected = new int[] { 1, 2 }; |
|
|
|
final int[] rowsAffected = new int[] {1, 2}; |
|
|
|
|
|
|
|
|
|
|
|
given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected[0], rowsAffected[1]); |
|
|
|
given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected[0], rowsAffected[1]); |
|
|
|
|
|
|
|
|
|
|
|
@ -670,7 +670,7 @@ public class JdbcTemplateTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testBatchUpdateFails() throws Exception { |
|
|
|
public void testBatchUpdateFails() throws Exception { |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final int[] ids = new int[] { 100, 200 }; |
|
|
|
final int[] ids = new int[] {100, 200}; |
|
|
|
SQLException sqlException = new SQLException(); |
|
|
|
SQLException sqlException = new SQLException(); |
|
|
|
|
|
|
|
|
|
|
|
given(this.preparedStatement.executeBatch()).willThrow(sqlException); |
|
|
|
given(this.preparedStatement.executeBatch()).willThrow(sqlException); |
|
|
|
@ -701,6 +701,15 @@ public class JdbcTemplateTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testBatchUpdateWithEmptyList() throws Exception { |
|
|
|
|
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
|
|
|
|
JdbcTemplate template = new JdbcTemplate(this.dataSource, false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int[] actualRowsAffected = template.batchUpdate(sql, Collections.emptyList()); |
|
|
|
|
|
|
|
assertTrue("executed 0 updates", actualRowsAffected.length == 0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testBatchUpdateWithListOfObjectArrays() throws Exception { |
|
|
|
public void testBatchUpdateWithListOfObjectArrays() throws Exception { |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
@ -711,11 +720,9 @@ public class JdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected); |
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
|
|
|
|
|
|
|
|
JdbcTemplate template = new JdbcTemplate(this.dataSource, false); |
|
|
|
JdbcTemplate template = new JdbcTemplate(this.dataSource, false); |
|
|
|
|
|
|
|
|
|
|
|
int[] actualRowsAffected = template.batchUpdate(sql, ids); |
|
|
|
int[] actualRowsAffected = template.batchUpdate(sql, ids); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue("executed 2 updates", actualRowsAffected.length == 2); |
|
|
|
assertTrue("executed 2 updates", actualRowsAffected.length == 2); |
|
|
|
assertEquals(rowsAffected[0], actualRowsAffected[0]); |
|
|
|
assertEquals(rowsAffected[0], actualRowsAffected[0]); |
|
|
|
assertEquals(rowsAffected[1], actualRowsAffected[1]); |
|
|
|
assertEquals(rowsAffected[1], actualRowsAffected[1]); |
|
|
|
@ -738,10 +745,9 @@ public class JdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected); |
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
|
|
|
|
|
|
|
|
this.template = new JdbcTemplate(this.dataSource, false); |
|
|
|
this.template = new JdbcTemplate(this.dataSource, false); |
|
|
|
int[] actualRowsAffected = this.template.batchUpdate(sql, ids, sqlTypes); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int[] actualRowsAffected = this.template.batchUpdate(sql, ids, sqlTypes); |
|
|
|
assertTrue("executed 2 updates", actualRowsAffected.length == 2); |
|
|
|
assertTrue("executed 2 updates", actualRowsAffected.length == 2); |
|
|
|
assertEquals(rowsAffected[0], actualRowsAffected[0]); |
|
|
|
assertEquals(rowsAffected[0], actualRowsAffected[0]); |
|
|
|
assertEquals(rowsAffected[1], actualRowsAffected[1]); |
|
|
|
assertEquals(rowsAffected[1], actualRowsAffected[1]); |
|
|
|
@ -756,8 +762,8 @@ public class JdbcTemplateTests { |
|
|
|
public void testBatchUpdateWithCollectionOfObjects() throws Exception { |
|
|
|
public void testBatchUpdateWithCollectionOfObjects() throws Exception { |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; |
|
|
|
final List<Integer> ids = Arrays.asList(100, 200, 300); |
|
|
|
final List<Integer> ids = Arrays.asList(100, 200, 300); |
|
|
|
final int[] rowsAffected1 = new int[] { 1, 2 }; |
|
|
|
final int[] rowsAffected1 = new int[] {1, 2}; |
|
|
|
final int[] rowsAffected2 = new int[] { 3 }; |
|
|
|
final int[] rowsAffected2 = new int[] {3}; |
|
|
|
|
|
|
|
|
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected1, rowsAffected2); |
|
|
|
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected1, rowsAffected2); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
mockDatabaseMetaData(true); |
|
|
|
@ -780,19 +786,20 @@ public class JdbcTemplateTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCouldntGetConnectionForOperationOrExceptionTranslator() throws SQLException { |
|
|
|
public void testCouldNotGetConnectionForOperationOrExceptionTranslator() throws SQLException { |
|
|
|
SQLException sqlException = new SQLException("foo", "07xxx"); |
|
|
|
SQLException sqlException = new SQLException("foo", "07xxx"); |
|
|
|
this.dataSource = mock(DataSource.class); |
|
|
|
this.dataSource = mock(DataSource.class); |
|
|
|
given(this.dataSource.getConnection()).willThrow(sqlException); |
|
|
|
given(this.dataSource.getConnection()).willThrow(sqlException); |
|
|
|
JdbcTemplate template = new JdbcTemplate(this.dataSource, false); |
|
|
|
JdbcTemplate template = new JdbcTemplate(this.dataSource, false); |
|
|
|
RowCountCallbackHandler rcch = new RowCountCallbackHandler(); |
|
|
|
RowCountCallbackHandler rcch = new RowCountCallbackHandler(); |
|
|
|
|
|
|
|
|
|
|
|
this.thrown.expect(CannotGetJdbcConnectionException.class); |
|
|
|
this.thrown.expect(CannotGetJdbcConnectionException.class); |
|
|
|
this.thrown.expect(exceptionCause(sameInstance(sqlException))); |
|
|
|
this.thrown.expect(exceptionCause(sameInstance(sqlException))); |
|
|
|
template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); |
|
|
|
template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCouldntGetConnectionForOperationWithLazyExceptionTranslator() throws SQLException { |
|
|
|
public void testCouldNotGetConnectionForOperationWithLazyExceptionTranslator() throws SQLException { |
|
|
|
SQLException sqlException = new SQLException("foo", "07xxx"); |
|
|
|
SQLException sqlException = new SQLException("foo", "07xxx"); |
|
|
|
this.dataSource = mock(DataSource.class); |
|
|
|
this.dataSource = mock(DataSource.class); |
|
|
|
given(this.dataSource.getConnection()).willThrow(sqlException); |
|
|
|
given(this.dataSource.getConnection()).willThrow(sqlException); |
|
|
|
@ -800,30 +807,31 @@ public class JdbcTemplateTests { |
|
|
|
this.template.setDataSource(this.dataSource); |
|
|
|
this.template.setDataSource(this.dataSource); |
|
|
|
this.template.afterPropertiesSet(); |
|
|
|
this.template.afterPropertiesSet(); |
|
|
|
RowCountCallbackHandler rcch = new RowCountCallbackHandler(); |
|
|
|
RowCountCallbackHandler rcch = new RowCountCallbackHandler(); |
|
|
|
|
|
|
|
|
|
|
|
this.thrown.expect(CannotGetJdbcConnectionException.class); |
|
|
|
this.thrown.expect(CannotGetJdbcConnectionException.class); |
|
|
|
this.thrown.expect(exceptionCause(sameInstance(sqlException))); |
|
|
|
this.thrown.expect(exceptionCause(sameInstance(sqlException))); |
|
|
|
this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); |
|
|
|
this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitializedViaBeanProperty() |
|
|
|
public void testCouldNotGetConnectionInOperationWithExceptionTranslatorInitializedViaBeanProperty() |
|
|
|
throws SQLException { |
|
|
|
throws SQLException { |
|
|
|
|
|
|
|
|
|
|
|
doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(true); |
|
|
|
doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized(true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitializedInAfterPropertiesSet() |
|
|
|
public void testCouldNotGetConnectionInOperationWithExceptionTranslatorInitializedInAfterPropertiesSet() |
|
|
|
throws SQLException { |
|
|
|
throws SQLException { |
|
|
|
|
|
|
|
|
|
|
|
doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(false); |
|
|
|
doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* If beanProperty is true, initialize via exception translator bean property; |
|
|
|
* If beanProperty is true, initialize via exception translator bean property; |
|
|
|
* if false, use afterPropertiesSet(). |
|
|
|
* if false, use afterPropertiesSet(). |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty) |
|
|
|
private void doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty) |
|
|
|
throws SQLException { |
|
|
|
throws SQLException { |
|
|
|
|
|
|
|
|
|
|
|
SQLException sqlException = new SQLException("foo", "07xxx"); |
|
|
|
SQLException sqlException = new SQLException("foo", "07xxx"); |
|
|
|
@ -883,7 +891,7 @@ public class JdbcTemplateTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCouldntClose() throws Exception { |
|
|
|
public void testCouldNotClose() throws Exception { |
|
|
|
SQLException sqlException = new SQLException("bar"); |
|
|
|
SQLException sqlException = new SQLException("bar"); |
|
|
|
given(this.connection.createStatement()).willReturn(this.statement); |
|
|
|
given(this.connection.createStatement()).willReturn(this.statement); |
|
|
|
given(this.resultSet.next()).willReturn(false); |
|
|
|
given(this.resultSet.next()).willReturn(false); |
|
|
|
|