45
45
import org .springframework .jdbc .SQLWarningException ;
46
46
import org .springframework .jdbc .UncategorizedSQLException ;
47
47
import org .springframework .jdbc .core .support .AbstractInterruptibleBatchPreparedStatementSetter ;
48
+ import org .springframework .jdbc .datasource .ConnectionProxy ;
48
49
import org .springframework .jdbc .datasource .SingleConnectionDataSource ;
49
50
import org .springframework .jdbc .support .SQLErrorCodeSQLExceptionTranslator ;
50
51
import org .springframework .jdbc .support .SQLStateSQLExceptionTranslator ;
65
66
*/
66
67
public class JdbcTemplateTests {
67
68
68
- @ Rule
69
- public ExpectedException thrown = ExpectedException .none ();
70
-
71
69
private Connection connection ;
70
+
72
71
private DataSource dataSource ;
72
+
73
73
private PreparedStatement preparedStatement ;
74
+
74
75
private Statement statement ;
76
+
75
77
private ResultSet resultSet ;
78
+
76
79
private JdbcTemplate template ;
80
+
77
81
private CallableStatement callableStatement ;
78
82
83
+ @ Rule
84
+ public ExpectedException thrown = ExpectedException .none ();
85
+
86
+
79
87
@ Before
80
88
public void setup () throws Exception {
81
89
this .connection = mock (Connection .class );
@@ -96,6 +104,7 @@ public void setup() throws Exception {
96
104
given (this .callableStatement .getResultSet ()).willReturn (this .resultSet );
97
105
}
98
106
107
+
99
108
@ Test
100
109
public void testBeanProperties () throws Exception {
101
110
assertTrue ("datasource ok" , this .template .getDataSource () == this .dataSource );
@@ -141,32 +150,29 @@ public void testBogusUpdate() throws Exception {
141
150
142
151
@ Test
143
152
public void testStringsWithStaticSql () throws Exception {
144
- doTestStrings (false , null , null , null , null , new JdbcTemplateCallback () {
153
+ doTestStrings (null , null , null , null , new JdbcTemplateCallback () {
145
154
@ Override
146
- public void doInJdbcTemplate (JdbcTemplate template , String sql ,
147
- RowCallbackHandler rch ) {
155
+ public void doInJdbcTemplate (JdbcTemplate template , String sql , RowCallbackHandler rch ) {
148
156
template .query (sql , rch );
149
157
}
150
158
});
151
159
}
152
160
153
161
@ Test
154
162
public void testStringsWithStaticSqlAndFetchSizeAndMaxRows () throws Exception {
155
- doTestStrings (false , 10 , 20 , 30 , null , new JdbcTemplateCallback () {
163
+ doTestStrings (10 , 20 , 30 , null , new JdbcTemplateCallback () {
156
164
@ Override
157
- public void doInJdbcTemplate (JdbcTemplate template , String sql ,
158
- RowCallbackHandler rch ) {
165
+ public void doInJdbcTemplate (JdbcTemplate template , String sql , RowCallbackHandler rch ) {
159
166
template .query (sql , rch );
160
167
}
161
168
});
162
169
}
163
170
164
171
@ Test
165
172
public void testStringsWithEmptyPreparedStatementSetter () throws Exception {
166
- doTestStrings (true , null , null , null , null , new JdbcTemplateCallback () {
173
+ doTestStrings (null , null , null , null , new JdbcTemplateCallback () {
167
174
@ Override
168
- public void doInJdbcTemplate (JdbcTemplate template , String sql ,
169
- RowCallbackHandler rch ) {
175
+ public void doInJdbcTemplate (JdbcTemplate template , String sql , RowCallbackHandler rch ) {
170
176
template .query (sql , (PreparedStatementSetter ) null , rch );
171
177
}
172
178
});
@@ -175,10 +181,9 @@ public void doInJdbcTemplate(JdbcTemplate template, String sql,
175
181
@ Test
176
182
public void testStringsWithPreparedStatementSetter () throws Exception {
177
183
final Integer argument = 99 ;
178
- doTestStrings (true , null , null , null , argument , new JdbcTemplateCallback () {
184
+ doTestStrings (null , null , null , argument , new JdbcTemplateCallback () {
179
185
@ Override
180
- public void doInJdbcTemplate (JdbcTemplate template , String sql ,
181
- RowCallbackHandler rch ) {
186
+ public void doInJdbcTemplate (JdbcTemplate template , String sql , RowCallbackHandler rch ) {
182
187
template .query (sql , new PreparedStatementSetter () {
183
188
@ Override
184
189
public void setValues (PreparedStatement ps ) throws SQLException {
@@ -191,10 +196,9 @@ public void setValues(PreparedStatement ps) throws SQLException {
191
196
192
197
@ Test
193
198
public void testStringsWithEmptyPreparedStatementArgs () throws Exception {
194
- doTestStrings (true , null , null , null , null , new JdbcTemplateCallback () {
199
+ doTestStrings (null , null , null , null , new JdbcTemplateCallback () {
195
200
@ Override
196
- public void doInJdbcTemplate (JdbcTemplate template , String sql ,
197
- RowCallbackHandler rch ) {
201
+ public void doInJdbcTemplate (JdbcTemplate template , String sql , RowCallbackHandler rch ) {
198
202
template .query (sql , (Object []) null , rch );
199
203
}
200
204
});
@@ -203,20 +207,16 @@ public void doInJdbcTemplate(JdbcTemplate template, String sql,
203
207
@ Test
204
208
public void testStringsWithPreparedStatementArgs () throws Exception {
205
209
final Integer argument = 99 ;
206
- doTestStrings (true , null , null , null , argument , new JdbcTemplateCallback () {
210
+ doTestStrings (null , null , null , argument , new JdbcTemplateCallback () {
207
211
@ Override
208
- public void doInJdbcTemplate (JdbcTemplate template , String sql ,
209
- RowCallbackHandler rch ) {
212
+ public void doInJdbcTemplate (JdbcTemplate template , String sql , RowCallbackHandler rch ) {
210
213
template .query (sql , new Object [] { argument }, rch );
211
214
}
212
215
});
213
216
}
214
217
215
- private void doTestStrings (
216
- boolean usePreparedStatement ,
217
- Integer fetchSize , Integer maxRows , Integer queryTimeout , Object argument ,
218
- JdbcTemplateCallback jdbcTemplateCallback )
219
- throws Exception {
218
+ private void doTestStrings (Integer fetchSize , Integer maxRows , Integer queryTimeout ,
219
+ Object argument , JdbcTemplateCallback jdbcTemplateCallback ) throws Exception {
220
220
221
221
String sql = "SELECT FORENAME FROM CUSTMR" ;
222
222
String [] results = { "rod" , "gary" , " portia" };
@@ -293,6 +293,19 @@ public void testLeaveConnectionOpenOnRequest() throws Exception {
293
293
verify (this .preparedStatement ).close ();
294
294
}
295
295
296
+ @ Test
297
+ public void testConnectionCallback () throws Exception {
298
+ String result = this .template .execute (new ConnectionCallback <String >() {
299
+ @ Override
300
+ public String doInConnection (Connection con ) {
301
+ assertTrue (con instanceof ConnectionProxy );
302
+ assertSame (JdbcTemplateTests .this .connection , ((ConnectionProxy ) con ).getTargetConnection ());
303
+ return "test" ;
304
+ }
305
+ });
306
+ assertEquals ("test" , result );
307
+ }
308
+
296
309
@ Test
297
310
public void testConnectionCallbackWithStatementSettings () throws Exception {
298
311
String result = this .template .execute (new ConnectionCallback <String >() {
@@ -724,7 +737,6 @@ public int getBatchSize() {
724
737
725
738
@ Test
726
739
public void testBatchUpdateWithListOfObjectArrays () throws Exception {
727
-
728
740
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?" ;
729
741
final List <Object []> ids = new ArrayList <>();
730
742
ids .add (new Object [] {100 });
@@ -835,13 +847,15 @@ public void testCouldntGetConnectionForOperationWithLazyExceptionTranslator() th
835
847
836
848
@ Test
837
849
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitializedViaBeanProperty ()
838
- throws Exception {
850
+ throws SQLException {
851
+
839
852
doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized (true );
840
853
}
841
854
842
855
@ Test
843
856
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitializedInAfterPropertiesSet ()
844
- throws Exception {
857
+ throws SQLException {
858
+
845
859
doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized (false );
846
860
}
847
861
@@ -851,6 +865,7 @@ public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitialize
851
865
*/
852
866
private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized (boolean beanProperty )
853
867
throws SQLException {
868
+
854
869
SQLException sqlException = new SQLException ("foo" , "07xxx" );
855
870
this .dataSource = mock (DataSource .class );
856
871
given (this .dataSource .getConnection ()).willThrow (sqlException );
0 commit comments