|
|
|
@ -45,6 +45,7 @@ import org.springframework.jdbc.CannotGetJdbcConnectionException; |
|
|
|
import org.springframework.jdbc.SQLWarningException; |
|
|
|
import org.springframework.jdbc.SQLWarningException; |
|
|
|
import org.springframework.jdbc.UncategorizedSQLException; |
|
|
|
import org.springframework.jdbc.UncategorizedSQLException; |
|
|
|
import org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter; |
|
|
|
import org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter; |
|
|
|
|
|
|
|
import org.springframework.jdbc.datasource.ConnectionProxy; |
|
|
|
import org.springframework.jdbc.datasource.SingleConnectionDataSource; |
|
|
|
import org.springframework.jdbc.datasource.SingleConnectionDataSource; |
|
|
|
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator; |
|
|
|
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator; |
|
|
|
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; |
|
|
|
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; |
|
|
|
@ -65,17 +66,24 @@ import static org.springframework.tests.Matchers.*; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class JdbcTemplateTests { |
|
|
|
public class JdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
@Rule |
|
|
|
|
|
|
|
public ExpectedException thrown = ExpectedException.none(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Connection connection; |
|
|
|
private Connection connection; |
|
|
|
|
|
|
|
|
|
|
|
private DataSource dataSource; |
|
|
|
private DataSource dataSource; |
|
|
|
|
|
|
|
|
|
|
|
private PreparedStatement preparedStatement; |
|
|
|
private PreparedStatement preparedStatement; |
|
|
|
|
|
|
|
|
|
|
|
private Statement statement; |
|
|
|
private Statement statement; |
|
|
|
|
|
|
|
|
|
|
|
private ResultSet resultSet; |
|
|
|
private ResultSet resultSet; |
|
|
|
|
|
|
|
|
|
|
|
private JdbcTemplate template; |
|
|
|
private JdbcTemplate template; |
|
|
|
|
|
|
|
|
|
|
|
private CallableStatement callableStatement; |
|
|
|
private CallableStatement callableStatement; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Rule |
|
|
|
|
|
|
|
public ExpectedException thrown = ExpectedException.none(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
@Before |
|
|
|
public void setup() throws Exception { |
|
|
|
public void setup() throws Exception { |
|
|
|
this.connection = mock(Connection.class); |
|
|
|
this.connection = mock(Connection.class); |
|
|
|
@ -96,6 +104,7 @@ public class JdbcTemplateTests { |
|
|
|
given(this.callableStatement.getResultSet()).willReturn(this.resultSet); |
|
|
|
given(this.callableStatement.getResultSet()).willReturn(this.resultSet); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testBeanProperties() throws Exception { |
|
|
|
public void testBeanProperties() throws Exception { |
|
|
|
assertTrue("datasource ok", this.template.getDataSource() == this.dataSource); |
|
|
|
assertTrue("datasource ok", this.template.getDataSource() == this.dataSource); |
|
|
|
@ -141,10 +150,9 @@ public class JdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testStringsWithStaticSql() throws Exception { |
|
|
|
public void testStringsWithStaticSql() throws Exception { |
|
|
|
doTestStrings(false, null, null, null, null, new JdbcTemplateCallback() { |
|
|
|
doTestStrings(null, null, null, null, new JdbcTemplateCallback() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) { |
|
|
|
RowCallbackHandler rch) { |
|
|
|
|
|
|
|
template.query(sql, rch); |
|
|
|
template.query(sql, rch); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
@ -152,10 +160,9 @@ public class JdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testStringsWithStaticSqlAndFetchSizeAndMaxRows() throws Exception { |
|
|
|
public void testStringsWithStaticSqlAndFetchSizeAndMaxRows() throws Exception { |
|
|
|
doTestStrings(false, 10, 20, 30, null, new JdbcTemplateCallback() { |
|
|
|
doTestStrings(10, 20, 30, null, new JdbcTemplateCallback() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) { |
|
|
|
RowCallbackHandler rch) { |
|
|
|
|
|
|
|
template.query(sql, rch); |
|
|
|
template.query(sql, rch); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
@ -163,10 +170,9 @@ public class JdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testStringsWithEmptyPreparedStatementSetter() throws Exception { |
|
|
|
public void testStringsWithEmptyPreparedStatementSetter() throws Exception { |
|
|
|
doTestStrings(true, null, null, null, null, new JdbcTemplateCallback() { |
|
|
|
doTestStrings(null, null, null, null, new JdbcTemplateCallback() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) { |
|
|
|
RowCallbackHandler rch) { |
|
|
|
|
|
|
|
template.query(sql, (PreparedStatementSetter) null, rch); |
|
|
|
template.query(sql, (PreparedStatementSetter) null, rch); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
@ -175,10 +181,9 @@ public class JdbcTemplateTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testStringsWithPreparedStatementSetter() throws Exception { |
|
|
|
public void testStringsWithPreparedStatementSetter() throws Exception { |
|
|
|
final Integer argument = 99; |
|
|
|
final Integer argument = 99; |
|
|
|
doTestStrings(true, null, null, null, argument, new JdbcTemplateCallback() { |
|
|
|
doTestStrings(null, null, null, argument, new JdbcTemplateCallback() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) { |
|
|
|
RowCallbackHandler rch) { |
|
|
|
|
|
|
|
template.query(sql, new PreparedStatementSetter() { |
|
|
|
template.query(sql, new PreparedStatementSetter() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void setValues(PreparedStatement ps) throws SQLException { |
|
|
|
public void setValues(PreparedStatement ps) throws SQLException { |
|
|
|
@ -191,10 +196,9 @@ public class JdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testStringsWithEmptyPreparedStatementArgs() throws Exception { |
|
|
|
public void testStringsWithEmptyPreparedStatementArgs() throws Exception { |
|
|
|
doTestStrings(true, null, null, null, null, new JdbcTemplateCallback() { |
|
|
|
doTestStrings(null, null, null, null, new JdbcTemplateCallback() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) { |
|
|
|
RowCallbackHandler rch) { |
|
|
|
|
|
|
|
template.query(sql, (Object[]) null, rch); |
|
|
|
template.query(sql, (Object[]) null, rch); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
@ -203,20 +207,16 @@ public class JdbcTemplateTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testStringsWithPreparedStatementArgs() throws Exception { |
|
|
|
public void testStringsWithPreparedStatementArgs() throws Exception { |
|
|
|
final Integer argument = 99; |
|
|
|
final Integer argument = 99; |
|
|
|
doTestStrings(true, null, null, null, argument, new JdbcTemplateCallback() { |
|
|
|
doTestStrings(null, null, null, argument, new JdbcTemplateCallback() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, |
|
|
|
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) { |
|
|
|
RowCallbackHandler rch) { |
|
|
|
|
|
|
|
template.query(sql, new Object[] { argument }, rch); |
|
|
|
template.query(sql, new Object[] { argument }, rch); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void doTestStrings( |
|
|
|
private void doTestStrings(Integer fetchSize, Integer maxRows, Integer queryTimeout, |
|
|
|
boolean usePreparedStatement, |
|
|
|
Object argument, JdbcTemplateCallback jdbcTemplateCallback) throws Exception { |
|
|
|
Integer fetchSize, Integer maxRows, Integer queryTimeout, 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" }; |
|
|
|
@ -293,6 +293,19 @@ public class JdbcTemplateTests { |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
verify(this.preparedStatement).close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testConnectionCallback() throws Exception { |
|
|
|
|
|
|
|
String result = this.template.execute(new ConnectionCallback<String>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public String doInConnection(Connection con) { |
|
|
|
|
|
|
|
assertTrue(con instanceof ConnectionProxy); |
|
|
|
|
|
|
|
assertSame(JdbcTemplateTests.this.connection, ((ConnectionProxy) con).getTargetConnection()); |
|
|
|
|
|
|
|
return "test"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
assertEquals("test", result); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testConnectionCallbackWithStatementSettings() throws Exception { |
|
|
|
public void testConnectionCallbackWithStatementSettings() throws Exception { |
|
|
|
String result = this.template.execute(new ConnectionCallback<String>() { |
|
|
|
String result = this.template.execute(new ConnectionCallback<String>() { |
|
|
|
@ -724,7 +737,6 @@ public class JdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
@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 = ?"; |
|
|
|
final List<Object[]> ids = new ArrayList<>(); |
|
|
|
final List<Object[]> ids = new ArrayList<>(); |
|
|
|
ids.add(new Object[] {100}); |
|
|
|
ids.add(new Object[] {100}); |
|
|
|
@ -835,13 +847,15 @@ public class JdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitializedViaBeanProperty() |
|
|
|
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitializedViaBeanProperty() |
|
|
|
throws Exception { |
|
|
|
throws SQLException { |
|
|
|
|
|
|
|
|
|
|
|
doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(true); |
|
|
|
doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitializedInAfterPropertiesSet() |
|
|
|
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitializedInAfterPropertiesSet() |
|
|
|
throws Exception { |
|
|
|
throws SQLException { |
|
|
|
|
|
|
|
|
|
|
|
doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(false); |
|
|
|
doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -851,6 +865,7 @@ public class JdbcTemplateTests { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty) |
|
|
|
private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty) |
|
|
|
throws SQLException { |
|
|
|
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); |
|
|
|
|