Introduce DataClassRowMapper with record-style constructor binding support

Closes gh-24695
This commit is contained in:
Juergen Hoeller
2020-08-28 18:52:35 +02:00
parent d4192b9d35
commit d37eaa5941
10 changed files with 388 additions and 80 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -25,7 +25,10 @@ import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.jdbc.core.test.ConcretePerson;
import org.springframework.jdbc.core.test.ConstructorPerson;
import org.springframework.jdbc.core.test.DatePerson;
import org.springframework.jdbc.core.test.Person;
import org.springframework.jdbc.core.test.SpacePerson;
@@ -48,32 +51,50 @@ import static org.mockito.Mockito.verify;
*/
public abstract class AbstractRowMapperTests {
protected void verifyPerson(Person bean) throws Exception {
assertThat(bean.getName()).isEqualTo("Bubba");
assertThat(bean.getAge()).isEqualTo(22L);
assertThat(bean.getBirth_date()).usingComparator(Date::compareTo).isEqualTo(new java.util.Date(1221222L));
assertThat(bean.getBalance()).isEqualTo(new BigDecimal("1234.56"));
protected void verifyPerson(Person person) {
assertThat(person.getName()).isEqualTo("Bubba");
assertThat(person.getAge()).isEqualTo(22L);
assertThat(person.getBirth_date()).usingComparator(Date::compareTo).isEqualTo(new java.util.Date(1221222L));
assertThat(person.getBalance()).isEqualTo(new BigDecimal("1234.56"));
verifyPersonViaBeanWrapper(person);
}
protected void verifyPerson(ConcretePerson bean) throws Exception {
assertThat(bean.getName()).isEqualTo("Bubba");
assertThat(bean.getAge()).isEqualTo(22L);
assertThat(bean.getBirth_date()).usingComparator(Date::compareTo).isEqualTo(new java.util.Date(1221222L));
assertThat(bean.getBalance()).isEqualTo(new BigDecimal("1234.56"));
protected void verifyPerson(ConcretePerson person) {
assertThat(person.getName()).isEqualTo("Bubba");
assertThat(person.getAge()).isEqualTo(22L);
assertThat(person.getBirth_date()).usingComparator(Date::compareTo).isEqualTo(new java.util.Date(1221222L));
assertThat(person.getBalance()).isEqualTo(new BigDecimal("1234.56"));
verifyPersonViaBeanWrapper(person);
}
protected void verifyPerson(SpacePerson bean) {
assertThat(bean.getLastName()).isEqualTo("Bubba");
assertThat(bean.getAge()).isEqualTo(22L);
assertThat(bean.getBirthDate()).isEqualTo(new Timestamp(1221222L).toLocalDateTime());
assertThat(bean.getBalance()).isEqualTo(new BigDecimal("1234.56"));
protected void verifyPerson(SpacePerson person) {
assertThat(person.getLastName()).isEqualTo("Bubba");
assertThat(person.getAge()).isEqualTo(22L);
assertThat(person.getBirthDate()).isEqualTo(new Timestamp(1221222L).toLocalDateTime());
assertThat(person.getBalance()).isEqualTo(new BigDecimal("1234.56"));
}
protected void verifyPerson(DatePerson bean) {
assertThat(bean.getLastName()).isEqualTo("Bubba");
assertThat(bean.getAge()).isEqualTo(22L);
assertThat(bean.getBirthDate()).isEqualTo(new java.sql.Date(1221222L).toLocalDate());
assertThat(bean.getBalance()).isEqualTo(new BigDecimal("1234.56"));
protected void verifyPerson(DatePerson person) {
assertThat(person.getLastName()).isEqualTo("Bubba");
assertThat(person.getAge()).isEqualTo(22L);
assertThat(person.getBirthDate()).isEqualTo(new java.sql.Date(1221222L).toLocalDate());
assertThat(person.getBalance()).isEqualTo(new BigDecimal("1234.56"));
}
protected void verifyPerson(ConstructorPerson person) {
assertThat(person.name()).isEqualTo("Bubba");
assertThat(person.age()).isEqualTo(22L);
assertThat(person.birth_date()).usingComparator(Date::compareTo).isEqualTo(new java.util.Date(1221222L));
assertThat(person.balance()).isEqualTo(new BigDecimal("1234.56"));
verifyPersonViaBeanWrapper(person);
}
private void verifyPersonViaBeanWrapper(Object person) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(person);
assertThat(bw.getPropertyValue("name")).isEqualTo("Bubba");
assertThat(bw.getPropertyValue("age")).isEqualTo(22L);
assertThat((Date) bw.getPropertyValue("birth_date")).usingComparator(Date::compareTo).isEqualTo(new java.util.Date(1221222L));
assertThat(bw.getPropertyValue("balance")).isEqualTo(new BigDecimal("1234.56"));
}
@@ -123,6 +144,11 @@ public abstract class AbstractRowMapperTests {
given(resultSetMetaData.getColumnLabel(3)).willReturn("birth_date");
given(resultSetMetaData.getColumnLabel(4)).willReturn("balance");
given(resultSet.findColumn("name")).willReturn(1);
given(resultSet.findColumn("age")).willReturn(2);
given(resultSet.findColumn("birth_date")).willReturn(3);
given(resultSet.findColumn("balance")).willReturn(4);
jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(new SingleConnectionDataSource(connection, false));
jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -37,9 +37,8 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/
public class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
public void testOverridingDifferentClassDefinedForMapping() {
BeanPropertyRowMapper mapper = new BeanPropertyRowMapper(Person.class);
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() ->

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2002-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.test.ConstructorPerson;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Juergen Hoeller
* @since 5.3
*/
public class DataClassRowMapperTests extends AbstractRowMapperTests {
@Test
public void testStaticQueryWithDataClass() throws Exception {
Mock mock = new Mock();
List<ConstructorPerson> result = mock.getJdbcTemplate().query(
"select name, age, birth_date, balance from people",
new DataClassRowMapper<>(ConstructorPerson.class));
assertThat(result.size()).isEqualTo(1);
verifyPerson(result.get(0));
mock.verifyClosed();
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2002-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.test;
import java.math.BigDecimal;
import java.util.Date;
/**
* @author Juergen Hoeller
*/
public class ConstructorPerson {
private String name;
private long age;
private java.util.Date birth_date;
private BigDecimal balance;
public ConstructorPerson(String name, long age, Date birth_date, BigDecimal balance) {
this.name = name;
this.age = age;
this.birth_date = birth_date;
this.balance = balance;
}
public String name() {
return name;
}
public long age() {
return age;
}
public Date birth_date() {
return birth_date;
}
public BigDecimal balance() {
return balance;
}
}