1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import java .sql .Types ;
25
25
import java .util .ArrayList ;
26
26
import java .util .Arrays ;
27
+ import java .util .Collection ;
27
28
import java .util .HashMap ;
28
29
import java .util .List ;
29
30
import java .util .Map ;
@@ -56,8 +57,9 @@ public class NamedParameterQueryTests {
56
57
57
58
private NamedParameterJdbcTemplate template ;
58
59
60
+
59
61
@ Before
60
- public void setUp () throws Exception {
62
+ public void setup () throws Exception {
61
63
connection = mock (Connection .class );
62
64
dataSource = mock (DataSource .class );
63
65
preparedStatement = mock (PreparedStatement .class );
@@ -78,16 +80,17 @@ public void verifyClose() throws Exception {
78
80
verify (connection ).close ();
79
81
}
80
82
83
+
81
84
@ Test
82
85
public void testQueryForListWithParamMap () throws Exception {
83
86
given (resultSet .getMetaData ()).willReturn (resultSetMetaData );
84
87
given (resultSet .next ()).willReturn (true , true , false );
85
88
given (resultSet .getObject (1 )).willReturn (11 , 12 );
86
89
87
- MapSqlParameterSource parms = new MapSqlParameterSource ();
88
- parms .addValue ("id" , 3 );
90
+ MapSqlParameterSource params = new MapSqlParameterSource ();
91
+ params .addValue ("id" , 3 );
89
92
List <Map <String , Object >> li = template .queryForList (
90
- "SELECT AGE FROM CUSTMR WHERE ID < :id" , parms );
93
+ "SELECT AGE FROM CUSTMR WHERE ID < :id" , params );
91
94
92
95
assertEquals ("All rows returned" , 2 , li .size ());
93
96
assertEquals ("First row is Integer" , 11 ,
@@ -103,10 +106,10 @@ public void testQueryForListWithParamMap() throws Exception {
103
106
public void testQueryForListWithParamMapAndEmptyResult () throws Exception {
104
107
given (resultSet .next ()).willReturn (false );
105
108
106
- MapSqlParameterSource parms = new MapSqlParameterSource ();
107
- parms .addValue ("id" , 3 );
109
+ MapSqlParameterSource params = new MapSqlParameterSource ();
110
+ params .addValue ("id" , 3 );
108
111
List <Map <String , Object >> li = template .queryForList (
109
- "SELECT AGE FROM CUSTMR WHERE ID < :id" , parms );
112
+ "SELECT AGE FROM CUSTMR WHERE ID < :id" , params );
110
113
111
114
assertEquals ("All rows returned" , 0 , li .size ());
112
115
verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID < ?" );
@@ -119,10 +122,10 @@ public void testQueryForListWithParamMapAndSingleRowAndColumn() throws Exception
119
122
given (resultSet .next ()).willReturn (true , false );
120
123
given (resultSet .getObject (1 )).willReturn (11 );
121
124
122
- MapSqlParameterSource parms = new MapSqlParameterSource ();
123
- parms .addValue ("id" , 3 );
125
+ MapSqlParameterSource params = new MapSqlParameterSource ();
126
+ params .addValue ("id" , 3 );
124
127
List <Map <String , Object >> li = template .queryForList (
125
- "SELECT AGE FROM CUSTMR WHERE ID < :id" , parms );
128
+ "SELECT AGE FROM CUSTMR WHERE ID < :id" , params );
126
129
127
130
assertEquals ("All rows returned" , 1 , li .size ());
128
131
assertEquals ("First row is Integer" , 11 ,
@@ -138,10 +141,10 @@ public void testQueryForListWithParamMapAndIntegerElementAndSingleRowAndColumn()
138
141
given (resultSet .next ()).willReturn (true , false );
139
142
given (resultSet .getInt (1 )).willReturn (11 );
140
143
141
- MapSqlParameterSource parms = new MapSqlParameterSource ();
142
- parms .addValue ("id" , 3 );
144
+ MapSqlParameterSource params = new MapSqlParameterSource ();
145
+ params .addValue ("id" , 3 );
143
146
List <Integer > li = template .queryForList ("SELECT AGE FROM CUSTMR WHERE ID < :id" ,
144
- parms , Integer .class );
147
+ params , Integer .class );
145
148
146
149
assertEquals ("All rows returned" , 1 , li .size ());
147
150
assertEquals ("First row is Integer" , 11 , li .get (0 ).intValue ());
@@ -155,9 +158,9 @@ public void testQueryForMapWithParamMapAndSingleRowAndColumn() throws Exception
155
158
given (resultSet .next ()).willReturn (true , false );
156
159
given (resultSet .getObject (1 )).willReturn (11 );
157
160
158
- MapSqlParameterSource parms = new MapSqlParameterSource ();
159
- parms .addValue ("id" , 3 );
160
- Map <String , Object > map = template .queryForMap ("SELECT AGE FROM CUSTMR WHERE ID < :id" , parms );
161
+ MapSqlParameterSource params = new MapSqlParameterSource ();
162
+ params .addValue ("id" , 3 );
163
+ Map <String , Object > map = template .queryForMap ("SELECT AGE FROM CUSTMR WHERE ID < :id" , params );
161
164
162
165
assertEquals ("Row is Integer" , 11 , ((Integer ) map .get ("age" )).intValue ());
163
166
verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID < ?" );
@@ -169,10 +172,10 @@ public void testQueryForObjectWithParamMapAndRowMapper() throws Exception {
169
172
given (resultSet .next ()).willReturn (true , false );
170
173
given (resultSet .getInt (1 )).willReturn (22 );
171
174
172
- MapSqlParameterSource parms = new MapSqlParameterSource ();
173
- parms .addValue ("id" , 3 );
175
+ MapSqlParameterSource params = new MapSqlParameterSource ();
176
+ params .addValue ("id" , 3 );
174
177
Object o = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" ,
175
- parms , new RowMapper <Object >() {
178
+ params , new RowMapper <Object >() {
176
179
@ Override
177
180
public Object mapRow (ResultSet rs , int rowNum ) throws SQLException {
178
181
return rs .getInt (1 );
@@ -190,10 +193,10 @@ public void testQueryForObjectWithMapAndInteger() throws Exception {
190
193
given (resultSet .next ()).willReturn (true , false );
191
194
given (resultSet .getInt (1 )).willReturn (22 );
192
195
193
- Map <String , Object > parms = new HashMap <String , Object >();
194
- parms .put ("id" , 3 );
196
+ Map <String , Object > params = new HashMap <>();
197
+ params .put ("id" , 3 );
195
198
Object o = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" ,
196
- parms , Integer .class );
199
+ params , Integer .class );
197
200
198
201
assertTrue ("Correct result type" , o instanceof Integer );
199
202
verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID = ?" );
@@ -206,10 +209,10 @@ public void testQueryForObjectWithParamMapAndInteger() throws Exception {
206
209
given (resultSet .next ()).willReturn (true , false );
207
210
given (resultSet .getInt (1 )).willReturn (22 );
208
211
209
- MapSqlParameterSource parms = new MapSqlParameterSource ();
210
- parms .addValue ("id" , 3 );
212
+ MapSqlParameterSource params = new MapSqlParameterSource ();
213
+ params .addValue ("id" , 3 );
211
214
Object o = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" ,
212
- parms , Integer .class );
215
+ params , Integer .class );
213
216
214
217
assertTrue ("Correct result type" , o instanceof Integer );
215
218
verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID = ?" );
@@ -224,9 +227,9 @@ public void testQueryForObjectWithParamMapAndList() throws Exception {
224
227
given (resultSet .next ()).willReturn (true , false );
225
228
given (resultSet .getInt (1 )).willReturn (22 );
226
229
227
- MapSqlParameterSource parms = new MapSqlParameterSource ();
228
- parms .addValue ("ids" , Arrays .asList (3 , 4 ));
229
- Object o = template .queryForObject (sql , parms , Integer .class );
230
+ MapSqlParameterSource params = new MapSqlParameterSource ();
231
+ params .addValue ("ids" , Arrays .asList (3 , 4 ));
232
+ Object o = template .queryForObject (sql , params , Integer .class );
230
233
231
234
assertTrue ("Correct result type" , o instanceof Integer );
232
235
verify (connection ).prepareStatement (sqlToUse );
@@ -239,14 +242,14 @@ public void testQueryForObjectWithParamMapAndListOfExpressionLists() throws Exce
239
242
given (resultSet .next ()).willReturn (true , false );
240
243
given (resultSet .getInt (1 )).willReturn (22 );
241
244
242
- MapSqlParameterSource parms = new MapSqlParameterSource ();
243
- List <Object []> l1 = new ArrayList <Object [] >();
245
+ MapSqlParameterSource params = new MapSqlParameterSource ();
246
+ List <Object []> l1 = new ArrayList <>();
244
247
l1 .add (new Object [] {3 , "Rod" });
245
248
l1 .add (new Object [] {4 , "Juergen" });
246
- parms .addValue ("multiExpressionList" , l1 );
249
+ params .addValue ("multiExpressionList" , l1 );
247
250
Object o = template .queryForObject (
248
251
"SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)" ,
249
- parms , Integer .class );
252
+ params , Integer .class );
250
253
251
254
assertTrue ("Correct result type" , o instanceof Integer );
252
255
verify (connection ).prepareStatement (
@@ -260,9 +263,9 @@ public void testQueryForIntWithParamMap() throws Exception {
260
263
given (resultSet .next ()).willReturn (true , false );
261
264
given (resultSet .getInt (1 )).willReturn (22 );
262
265
263
- MapSqlParameterSource parms = new MapSqlParameterSource ();
264
- parms .addValue ("id" , 3 );
265
- int i = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" , parms , Integer .class ).intValue ();
266
+ MapSqlParameterSource params = new MapSqlParameterSource ();
267
+ params .addValue ("id" , 3 );
268
+ int i = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" , params , Integer .class ).intValue ();
266
269
267
270
assertEquals ("Return of an int" , 22 , i );
268
271
verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID = ?" );
@@ -275,19 +278,33 @@ public void testQueryForLongWithParamBean() throws Exception {
275
278
given (resultSet .next ()).willReturn (true , false );
276
279
given (resultSet .getLong (1 )).willReturn (87L );
277
280
278
- BeanPropertySqlParameterSource parms = new BeanPropertySqlParameterSource (
279
- new ParameterBean (3 ));
280
-
281
- long l = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" , parms , Long .class ).longValue ();
281
+ BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource (new ParameterBean (3 ));
282
+ long l = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" , params , Long .class ).longValue ();
282
283
283
284
assertEquals ("Return of a long" , 87 , l );
284
285
verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID = ?" );
285
286
verify (preparedStatement ).setObject (1 , 3 , Types .INTEGER );
286
287
}
287
288
289
+ @ Test
290
+ public void testQueryForLongWithParamBeanWithCollection () throws Exception {
291
+ given (resultSet .getMetaData ()).willReturn (resultSetMetaData );
292
+ given (resultSet .next ()).willReturn (true , false );
293
+ given (resultSet .getLong (1 )).willReturn (87L );
294
+
295
+ BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource (new ParameterCollectionBean (3 , 5 ));
296
+ long l = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID IN (:ids)" , params , Long .class ).longValue ();
297
+
298
+ assertEquals ("Return of a long" , 87 , l );
299
+ verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID IN (?, ?)" );
300
+ verify (preparedStatement ).setObject (1 , 3 );
301
+ verify (preparedStatement ).setObject (2 , 5 );
302
+ }
303
+
304
+
288
305
static class ParameterBean {
289
306
290
- private int id ;
307
+ private final int id ;
291
308
292
309
public ParameterBean (int id ) {
293
310
this .id = id ;
@@ -298,4 +315,18 @@ public int getId() {
298
315
}
299
316
}
300
317
318
+
319
+ static class ParameterCollectionBean {
320
+
321
+ private final Collection <Integer > ids ;
322
+
323
+ public ParameterCollectionBean (Integer ... ids ) {
324
+ this .ids = Arrays .asList (ids );
325
+ }
326
+
327
+ public Collection <Integer > getIds () {
328
+ return ids ;
329
+ }
330
+ }
331
+
301
332
}
0 commit comments