|
|
|
@ -75,18 +75,18 @@ import static org.mockito.Mockito.verify; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class JdbcTemplateTests { |
|
|
|
public class JdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
private Connection connection = mock(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private DataSource dataSource = mock(); |
|
|
|
private DataSource dataSource = mock(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Connection connection = mock(); |
|
|
|
|
|
|
|
|
|
|
|
private Statement statement = mock(); |
|
|
|
private Statement statement = mock(); |
|
|
|
|
|
|
|
|
|
|
|
private PreparedStatement preparedStatement = mock(); |
|
|
|
private PreparedStatement preparedStatement = mock(); |
|
|
|
|
|
|
|
|
|
|
|
private ResultSet resultSet = mock(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private CallableStatement callableStatement = mock(); |
|
|
|
private CallableStatement callableStatement = mock(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ResultSet resultSet = mock(); |
|
|
|
|
|
|
|
|
|
|
|
private JdbcTemplate template = new JdbcTemplate(this.dataSource); |
|
|
|
private JdbcTemplate template = new JdbcTemplate(this.dataSource); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -136,9 +136,9 @@ public class JdbcTemplateTests { |
|
|
|
given(this.preparedStatement.executeUpdate()).willThrow(sqlException); |
|
|
|
given(this.preparedStatement.executeUpdate()).willThrow(sqlException); |
|
|
|
|
|
|
|
|
|
|
|
Dispatcher d = new Dispatcher(idParam, sql); |
|
|
|
Dispatcher d = new Dispatcher(idParam, sql); |
|
|
|
assertThatExceptionOfType(UncategorizedSQLException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(UncategorizedSQLException.class) |
|
|
|
this.template.update(d)) |
|
|
|
.isThrownBy(() -> this.template.update(d)) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
verify(this.preparedStatement).setInt(1, idParam); |
|
|
|
verify(this.preparedStatement).setInt(1, idParam); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.connection, atLeastOnce()).close(); |
|
|
|
verify(this.connection, atLeastOnce()).close(); |
|
|
|
@ -371,9 +371,9 @@ public class JdbcTemplateTests { |
|
|
|
given(this.statement.executeUpdate(sql)).willThrow(sqlException); |
|
|
|
given(this.statement.executeUpdate(sql)).willThrow(sqlException); |
|
|
|
given(this.connection.createStatement()).willReturn(this.statement); |
|
|
|
given(this.connection.createStatement()).willReturn(this.statement); |
|
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(DataAccessException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(DataAccessException.class) |
|
|
|
this.template.update(sql)) |
|
|
|
.isThrownBy(() -> this.template.update(sql)) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
verify(this.statement).close(); |
|
|
|
verify(this.statement).close(); |
|
|
|
verify(this.connection, atLeastOnce()).close(); |
|
|
|
verify(this.connection, atLeastOnce()).close(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -672,9 +672,9 @@ public class JdbcTemplateTests { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
assertThatExceptionOfType(DataAccessException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(DataAccessException.class) |
|
|
|
this.template.batchUpdate(sql, setter)) |
|
|
|
.isThrownBy(() -> this.template.batchUpdate(sql, setter)) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
} |
|
|
|
} |
|
|
|
finally { |
|
|
|
finally { |
|
|
|
verify(this.preparedStatement, times(2)).addBatch(); |
|
|
|
verify(this.preparedStatement, times(2)).addBatch(); |
|
|
|
@ -776,9 +776,9 @@ public class JdbcTemplateTests { |
|
|
|
JdbcTemplate template = new JdbcTemplate(this.dataSource, false); |
|
|
|
JdbcTemplate template = new JdbcTemplate(this.dataSource, false); |
|
|
|
RowCountCallbackHandler rcch = new RowCountCallbackHandler(); |
|
|
|
RowCountCallbackHandler rcch = new RowCountCallbackHandler(); |
|
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(CannotGetJdbcConnectionException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(CannotGetJdbcConnectionException.class) |
|
|
|
template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch)) |
|
|
|
.isThrownBy(() -> template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch)) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -790,9 +790,9 @@ public class JdbcTemplateTests { |
|
|
|
this.template.afterPropertiesSet(); |
|
|
|
this.template.afterPropertiesSet(); |
|
|
|
RowCountCallbackHandler rcch = new RowCountCallbackHandler(); |
|
|
|
RowCountCallbackHandler rcch = new RowCountCallbackHandler(); |
|
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(CannotGetJdbcConnectionException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(CannotGetJdbcConnectionException.class) |
|
|
|
this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch)) |
|
|
|
.isThrownBy(() -> this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch)) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -830,9 +830,9 @@ public class JdbcTemplateTests { |
|
|
|
this.template.afterPropertiesSet(); |
|
|
|
this.template.afterPropertiesSet(); |
|
|
|
} |
|
|
|
} |
|
|
|
RowCountCallbackHandler rcch = new RowCountCallbackHandler(); |
|
|
|
RowCountCallbackHandler rcch = new RowCountCallbackHandler(); |
|
|
|
assertThatExceptionOfType(CannotGetJdbcConnectionException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(CannotGetJdbcConnectionException.class) |
|
|
|
this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch)) |
|
|
|
.isThrownBy(() -> this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch)) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -859,9 +859,9 @@ public class JdbcTemplateTests { |
|
|
|
given(this.preparedStatement.executeUpdate()).willThrow(sqlException); |
|
|
|
given(this.preparedStatement.executeUpdate()).willThrow(sqlException); |
|
|
|
|
|
|
|
|
|
|
|
PreparedStatementSetter pss = ps -> ps.setString(1, name); |
|
|
|
PreparedStatementSetter pss = ps -> ps.setString(1, name); |
|
|
|
assertThatExceptionOfType(DataAccessException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(DataAccessException.class) |
|
|
|
new JdbcTemplate(this.dataSource).update(sql, pss)) |
|
|
|
.isThrownBy(() -> new JdbcTemplate(this.dataSource).update(sql, pss)) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
verify(this.preparedStatement).setString(1, name); |
|
|
|
verify(this.preparedStatement).setString(1, name); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.connection, atLeastOnce()).close(); |
|
|
|
verify(this.connection, atLeastOnce()).close(); |
|
|
|
@ -897,9 +897,9 @@ public class JdbcTemplateTests { |
|
|
|
t.setIgnoreWarnings(false); |
|
|
|
t.setIgnoreWarnings(false); |
|
|
|
|
|
|
|
|
|
|
|
ResultSetExtractor<Byte> extractor = rs -> rs.getByte(1); |
|
|
|
ResultSetExtractor<Byte> extractor = rs -> rs.getByte(1); |
|
|
|
assertThatExceptionOfType(SQLWarningException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(SQLWarningException.class) |
|
|
|
t.query(sql, extractor)) |
|
|
|
.isThrownBy(() -> t.query(sql, extractor)) |
|
|
|
.withCause(warnings); |
|
|
|
.withCause(warnings); |
|
|
|
verify(this.resultSet).close(); |
|
|
|
verify(this.resultSet).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.connection).close(); |
|
|
|
verify(this.connection).close(); |
|
|
|
@ -940,7 +940,7 @@ public class JdbcTemplateTests { |
|
|
|
this.template.query(sql, (RowCallbackHandler) rs -> { |
|
|
|
this.template.query(sql, (RowCallbackHandler) rs -> { |
|
|
|
throw sqlException; |
|
|
|
throw sqlException; |
|
|
|
})) |
|
|
|
})) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
verify(this.resultSet).close(); |
|
|
|
verify(this.resultSet).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.connection, atLeastOnce()).close(); |
|
|
|
verify(this.connection, atLeastOnce()).close(); |
|
|
|
@ -960,7 +960,7 @@ public class JdbcTemplateTests { |
|
|
|
this.template.query(sql, (RowCallbackHandler) rs -> { |
|
|
|
this.template.query(sql, (RowCallbackHandler) rs -> { |
|
|
|
throw sqlException; |
|
|
|
throw sqlException; |
|
|
|
})) |
|
|
|
})) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
verify(this.resultSet).close(); |
|
|
|
verify(this.resultSet).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.connection).close(); |
|
|
|
verify(this.connection).close(); |
|
|
|
@ -990,7 +990,7 @@ public class JdbcTemplateTests { |
|
|
|
template.query(sql, (RowCallbackHandler) rs -> { |
|
|
|
template.query(sql, (RowCallbackHandler) rs -> { |
|
|
|
throw sqlException; |
|
|
|
throw sqlException; |
|
|
|
})) |
|
|
|
})) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
verify(this.resultSet).close(); |
|
|
|
verify(this.resultSet).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.connection).close(); |
|
|
|
verify(this.connection).close(); |
|
|
|
|