Skip to content

Commit 7f3d0b3

Browse files
committed
Test for BeanPropertySqlParameterSource with collection
Issue: SPR-15390 (cherry picked from commit af6f688)
1 parent 546d7dd commit 7f3d0b3

File tree

1 file changed

+72
-41
lines changed

1 file changed

+72
-41
lines changed

spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterQueryTests.java

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import java.sql.Types;
2525
import java.util.ArrayList;
2626
import java.util.Arrays;
27+
import java.util.Collection;
2728
import java.util.HashMap;
2829
import java.util.List;
2930
import java.util.Map;
@@ -56,8 +57,9 @@ public class NamedParameterQueryTests {
5657

5758
private NamedParameterJdbcTemplate template;
5859

60+
5961
@Before
60-
public void setUp() throws Exception {
62+
public void setup() throws Exception {
6163
connection = mock(Connection.class);
6264
dataSource = mock(DataSource.class);
6365
preparedStatement = mock(PreparedStatement.class);
@@ -78,16 +80,17 @@ public void verifyClose() throws Exception {
7880
verify(connection).close();
7981
}
8082

83+
8184
@Test
8285
public void testQueryForListWithParamMap() throws Exception {
8386
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
8487
given(resultSet.next()).willReturn(true, true, false);
8588
given(resultSet.getObject(1)).willReturn(11, 12);
8689

87-
MapSqlParameterSource parms = new MapSqlParameterSource();
88-
parms.addValue("id", 3);
90+
MapSqlParameterSource params = new MapSqlParameterSource();
91+
params.addValue("id", 3);
8992
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);
9194

9295
assertEquals("All rows returned", 2, li.size());
9396
assertEquals("First row is Integer", 11,
@@ -103,10 +106,10 @@ public void testQueryForListWithParamMap() throws Exception {
103106
public void testQueryForListWithParamMapAndEmptyResult() throws Exception {
104107
given(resultSet.next()).willReturn(false);
105108

106-
MapSqlParameterSource parms = new MapSqlParameterSource();
107-
parms.addValue("id", 3);
109+
MapSqlParameterSource params = new MapSqlParameterSource();
110+
params.addValue("id", 3);
108111
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);
110113

111114
assertEquals("All rows returned", 0, li.size());
112115
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?");
@@ -119,10 +122,10 @@ public void testQueryForListWithParamMapAndSingleRowAndColumn() throws Exception
119122
given(resultSet.next()).willReturn(true, false);
120123
given(resultSet.getObject(1)).willReturn(11);
121124

122-
MapSqlParameterSource parms = new MapSqlParameterSource();
123-
parms.addValue("id", 3);
125+
MapSqlParameterSource params = new MapSqlParameterSource();
126+
params.addValue("id", 3);
124127
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);
126129

127130
assertEquals("All rows returned", 1, li.size());
128131
assertEquals("First row is Integer", 11,
@@ -138,10 +141,10 @@ public void testQueryForListWithParamMapAndIntegerElementAndSingleRowAndColumn()
138141
given(resultSet.next()).willReturn(true, false);
139142
given(resultSet.getInt(1)).willReturn(11);
140143

141-
MapSqlParameterSource parms = new MapSqlParameterSource();
142-
parms.addValue("id", 3);
144+
MapSqlParameterSource params = new MapSqlParameterSource();
145+
params.addValue("id", 3);
143146
List<Integer> li = template.queryForList("SELECT AGE FROM CUSTMR WHERE ID < :id",
144-
parms, Integer.class);
147+
params, Integer.class);
145148

146149
assertEquals("All rows returned", 1, li.size());
147150
assertEquals("First row is Integer", 11, li.get(0).intValue());
@@ -155,9 +158,9 @@ public void testQueryForMapWithParamMapAndSingleRowAndColumn() throws Exception
155158
given(resultSet.next()).willReturn(true, false);
156159
given(resultSet.getObject(1)).willReturn(11);
157160

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);
161164

162165
assertEquals("Row is Integer", 11, ((Integer) map.get("age")).intValue());
163166
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?");
@@ -169,10 +172,10 @@ public void testQueryForObjectWithParamMapAndRowMapper() throws Exception {
169172
given(resultSet.next()).willReturn(true, false);
170173
given(resultSet.getInt(1)).willReturn(22);
171174

172-
MapSqlParameterSource parms = new MapSqlParameterSource();
173-
parms.addValue("id", 3);
175+
MapSqlParameterSource params = new MapSqlParameterSource();
176+
params.addValue("id", 3);
174177
Object o = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
175-
parms, new RowMapper<Object>() {
178+
params, new RowMapper<Object>() {
176179
@Override
177180
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
178181
return rs.getInt(1);
@@ -190,10 +193,10 @@ public void testQueryForObjectWithMapAndInteger() throws Exception {
190193
given(resultSet.next()).willReturn(true, false);
191194
given(resultSet.getInt(1)).willReturn(22);
192195

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);
195198
Object o = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
196-
parms, Integer.class);
199+
params, Integer.class);
197200

198201
assertTrue("Correct result type", o instanceof Integer);
199202
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@@ -206,10 +209,10 @@ public void testQueryForObjectWithParamMapAndInteger() throws Exception {
206209
given(resultSet.next()).willReturn(true, false);
207210
given(resultSet.getInt(1)).willReturn(22);
208211

209-
MapSqlParameterSource parms = new MapSqlParameterSource();
210-
parms.addValue("id", 3);
212+
MapSqlParameterSource params = new MapSqlParameterSource();
213+
params.addValue("id", 3);
211214
Object o = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
212-
parms, Integer.class);
215+
params, Integer.class);
213216

214217
assertTrue("Correct result type", o instanceof Integer);
215218
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@@ -224,9 +227,9 @@ public void testQueryForObjectWithParamMapAndList() throws Exception {
224227
given(resultSet.next()).willReturn(true, false);
225228
given(resultSet.getInt(1)).willReturn(22);
226229

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);
230233

231234
assertTrue("Correct result type", o instanceof Integer);
232235
verify(connection).prepareStatement(sqlToUse);
@@ -239,14 +242,14 @@ public void testQueryForObjectWithParamMapAndListOfExpressionLists() throws Exce
239242
given(resultSet.next()).willReturn(true, false);
240243
given(resultSet.getInt(1)).willReturn(22);
241244

242-
MapSqlParameterSource parms = new MapSqlParameterSource();
243-
List<Object[]> l1 = new ArrayList<Object[]>();
245+
MapSqlParameterSource params = new MapSqlParameterSource();
246+
List<Object[]> l1 = new ArrayList<>();
244247
l1.add(new Object[] {3, "Rod"});
245248
l1.add(new Object[] {4, "Juergen"});
246-
parms.addValue("multiExpressionList", l1);
249+
params.addValue("multiExpressionList", l1);
247250
Object o = template.queryForObject(
248251
"SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)",
249-
parms, Integer.class);
252+
params, Integer.class);
250253

251254
assertTrue("Correct result type", o instanceof Integer);
252255
verify(connection).prepareStatement(
@@ -260,9 +263,9 @@ public void testQueryForIntWithParamMap() throws Exception {
260263
given(resultSet.next()).willReturn(true, false);
261264
given(resultSet.getInt(1)).willReturn(22);
262265

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();
266269

267270
assertEquals("Return of an int", 22, i);
268271
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@@ -275,19 +278,33 @@ public void testQueryForLongWithParamBean() throws Exception {
275278
given(resultSet.next()).willReturn(true, false);
276279
given(resultSet.getLong(1)).willReturn(87L);
277280

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();
282283

283284
assertEquals("Return of a long", 87, l);
284285
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
285286
verify(preparedStatement).setObject(1, 3, Types.INTEGER);
286287
}
287288

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+
288305
static class ParameterBean {
289306

290-
private int id;
307+
private final int id;
291308

292309
public ParameterBean(int id) {
293310
this.id = id;
@@ -298,4 +315,18 @@ public int getId() {
298315
}
299316
}
300317

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+
301332
}

0 commit comments

Comments
 (0)