Browse Source

Test for BeanPropertySqlParameterSource with collection

Issue: SPR-15390
pull/1374/head
Juergen Hoeller 8 years ago
parent
commit
af6f688196
  1. 111
      spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterQueryTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,6 +24,7 @@ import java.sql.SQLException; @@ -24,6 +24,7 @@ import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -56,8 +57,9 @@ public class NamedParameterQueryTests { @@ -56,8 +57,9 @@ public class NamedParameterQueryTests {
private NamedParameterJdbcTemplate template;
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
connection = mock(Connection.class);
dataSource = mock(DataSource.class);
preparedStatement = mock(PreparedStatement.class);
@ -78,16 +80,17 @@ public class NamedParameterQueryTests { @@ -78,16 +80,17 @@ public class NamedParameterQueryTests {
verify(connection).close();
}
@Test
public void testQueryForListWithParamMap() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getObject(1)).willReturn(11, 12);
MapSqlParameterSource parms = new MapSqlParameterSource();
parms.addValue("id", 3);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("id", 3);
List<Map<String, Object>> li = template.queryForList(
"SELECT AGE FROM CUSTMR WHERE ID < :id", parms);
"SELECT AGE FROM CUSTMR WHERE ID < :id", params);
assertEquals("All rows returned", 2, li.size());
assertEquals("First row is Integer", 11,
@ -103,10 +106,10 @@ public class NamedParameterQueryTests { @@ -103,10 +106,10 @@ public class NamedParameterQueryTests {
public void testQueryForListWithParamMapAndEmptyResult() throws Exception {
given(resultSet.next()).willReturn(false);
MapSqlParameterSource parms = new MapSqlParameterSource();
parms.addValue("id", 3);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("id", 3);
List<Map<String, Object>> li = template.queryForList(
"SELECT AGE FROM CUSTMR WHERE ID < :id", parms);
"SELECT AGE FROM CUSTMR WHERE ID < :id", params);
assertEquals("All rows returned", 0, li.size());
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?");
@ -119,10 +122,10 @@ public class NamedParameterQueryTests { @@ -119,10 +122,10 @@ public class NamedParameterQueryTests {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(11);
MapSqlParameterSource parms = new MapSqlParameterSource();
parms.addValue("id", 3);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("id", 3);
List<Map<String, Object>> li = template.queryForList(
"SELECT AGE FROM CUSTMR WHERE ID < :id", parms);
"SELECT AGE FROM CUSTMR WHERE ID < :id", params);
assertEquals("All rows returned", 1, li.size());
assertEquals("First row is Integer", 11,
@ -138,10 +141,10 @@ public class NamedParameterQueryTests { @@ -138,10 +141,10 @@ public class NamedParameterQueryTests {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(11);
MapSqlParameterSource parms = new MapSqlParameterSource();
parms.addValue("id", 3);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("id", 3);
List<Integer> li = template.queryForList("SELECT AGE FROM CUSTMR WHERE ID < :id",
parms, Integer.class);
params, Integer.class);
assertEquals("All rows returned", 1, li.size());
assertEquals("First row is Integer", 11, li.get(0).intValue());
@ -155,9 +158,9 @@ public class NamedParameterQueryTests { @@ -155,9 +158,9 @@ public class NamedParameterQueryTests {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getObject(1)).willReturn(11);
MapSqlParameterSource parms = new MapSqlParameterSource();
parms.addValue("id", 3);
Map<String, Object> map = template.queryForMap("SELECT AGE FROM CUSTMR WHERE ID < :id", parms);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("id", 3);
Map<String, Object> map = template.queryForMap("SELECT AGE FROM CUSTMR WHERE ID < :id", params);
assertEquals("Row is Integer", 11, ((Integer) map.get("age")).intValue());
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?");
@ -169,10 +172,10 @@ public class NamedParameterQueryTests { @@ -169,10 +172,10 @@ public class NamedParameterQueryTests {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
MapSqlParameterSource parms = new MapSqlParameterSource();
parms.addValue("id", 3);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("id", 3);
Object o = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
parms, new RowMapper<Object>() {
params, new RowMapper<Object>() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getInt(1);
@ -190,10 +193,10 @@ public class NamedParameterQueryTests { @@ -190,10 +193,10 @@ public class NamedParameterQueryTests {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
Map<String, Object> parms = new HashMap<>();
parms.put("id", 3);
Map<String, Object> params = new HashMap<>();
params.put("id", 3);
Object o = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
parms, Integer.class);
params, Integer.class);
assertTrue("Correct result type", o instanceof Integer);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@ -206,10 +209,10 @@ public class NamedParameterQueryTests { @@ -206,10 +209,10 @@ public class NamedParameterQueryTests {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
MapSqlParameterSource parms = new MapSqlParameterSource();
parms.addValue("id", 3);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("id", 3);
Object o = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
parms, Integer.class);
params, Integer.class);
assertTrue("Correct result type", o instanceof Integer);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@ -224,9 +227,9 @@ public class NamedParameterQueryTests { @@ -224,9 +227,9 @@ public class NamedParameterQueryTests {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
MapSqlParameterSource parms = new MapSqlParameterSource();
parms.addValue("ids", Arrays.asList(3, 4));
Object o = template.queryForObject(sql, parms, Integer.class);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("ids", Arrays.asList(3, 4));
Object o = template.queryForObject(sql, params, Integer.class);
assertTrue("Correct result type", o instanceof Integer);
verify(connection).prepareStatement(sqlToUse);
@ -239,14 +242,14 @@ public class NamedParameterQueryTests { @@ -239,14 +242,14 @@ public class NamedParameterQueryTests {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
MapSqlParameterSource parms = new MapSqlParameterSource();
MapSqlParameterSource params = new MapSqlParameterSource();
List<Object[]> l1 = new ArrayList<>();
l1.add(new Object[] {3, "Rod"});
l1.add(new Object[] {4, "Juergen"});
parms.addValue("multiExpressionList", l1);
params.addValue("multiExpressionList", l1);
Object o = template.queryForObject(
"SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)",
parms, Integer.class);
params, Integer.class);
assertTrue("Correct result type", o instanceof Integer);
verify(connection).prepareStatement(
@ -260,9 +263,9 @@ public class NamedParameterQueryTests { @@ -260,9 +263,9 @@ public class NamedParameterQueryTests {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt(1)).willReturn(22);
MapSqlParameterSource parms = new MapSqlParameterSource();
parms.addValue("id", 3);
int i = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id", parms, Integer.class).intValue();
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("id", 3);
int i = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id", params, Integer.class).intValue();
assertEquals("Return of an int", 22, i);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
@ -275,19 +278,33 @@ public class NamedParameterQueryTests { @@ -275,19 +278,33 @@ public class NamedParameterQueryTests {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getLong(1)).willReturn(87L);
BeanPropertySqlParameterSource parms = new BeanPropertySqlParameterSource(
new ParameterBean(3));
long l = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id", parms, Long.class).longValue();
BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(new ParameterBean(3));
long l = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id", params, Long.class).longValue();
assertEquals("Return of a long", 87, l);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
verify(preparedStatement).setObject(1, 3, Types.INTEGER);
}
@Test
public void testQueryForLongWithParamBeanWithCollection() throws Exception {
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
given(resultSet.next()).willReturn(true, false);
given(resultSet.getLong(1)).willReturn(87L);
BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(new ParameterCollectionBean(3, 5));
long l = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID IN (:ids)", params, Long.class).longValue();
assertEquals("Return of a long", 87, l);
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID IN (?, ?)");
verify(preparedStatement).setObject(1, 3);
verify(preparedStatement).setObject(2, 5);
}
static class ParameterBean {
private int id;
private final int id;
public ParameterBean(int id) {
this.id = id;
@ -298,4 +315,18 @@ public class NamedParameterQueryTests { @@ -298,4 +315,18 @@ public class NamedParameterQueryTests {
}
}
static class ParameterCollectionBean {
private final Collection<Integer> ids;
public ParameterCollectionBean(Integer... ids) {
this.ids = Arrays.asList(ids);
}
public Collection<Integer> getIds() {
return ids;
}
}
}

Loading…
Cancel
Save