@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2020 the original author or authors .
* Copyright 2002 - 2022 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 .
@ -19,85 +19,91 @@ package org.springframework.jdbc.core;
@@ -19,85 +19,91 @@ package org.springframework.jdbc.core;
import java.util.List ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.params.ParameterizedTest ;
import org.junit.jupiter.params.provider.CsvSource ;
import org.springframework.beans.TypeMismatchException ;
import org.springframework.dao.InvalidDataAccessApiUsageException ;
import org.springframework.jdbc.core.test.ConcretePerson ;
import org.springframework.jdbc.core.test.DatePerson ;
import org.springframework.jdbc.core.test.EmailPerson ;
import org.springframework.jdbc.core.test.ExtendedPerson ;
import org.springframework.jdbc.core.test.Person ;
import org.springframework.jdbc.core.test.SpacePerson ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
import static org.assertj.core.api.Assertions.assertThatNoException ;
/ * *
* Tests for { @link BeanPropertyRowMapper } .
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @author Sam Brannen
* /
public class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
@Test
@SuppressWarnings ( { "unchecked" , "rawtypes" } )
public void testO verridingDifferentClassDefinedForMapping( ) {
void o verridingDifferentClassDefinedForMapping( ) {
BeanPropertyRowMapper mapper = new BeanPropertyRowMapper ( Person . class ) ;
assertThatExceptionOfType ( InvalidDataAccessApiUsageException . class ) . isThrownBy ( ( ) - >
mapper . setMappedClass ( Long . class ) ) ;
}
@Test
public void testO verridingSameClassDefinedForMapping( ) {
void o verridingSameClassDefinedForMapping( ) {
BeanPropertyRowMapper < Person > mapper = new BeanPropertyRowMapper < > ( Person . class ) ;
mapper . setMappedClass ( Person . class ) ;
assertThatNoException ( ) . isThrownBy ( ( ) - > mapper . setMappedClass ( Person . class ) ) ;
}
@Test
public void te stS taticQueryWithRowMapper( ) throws Exception {
void staticQueryWithRowMapper ( ) throws Exception {
Mock mock = new Mock ( ) ;
List < Person > result = mock . getJdbcTemplate ( ) . query (
"select name, age, birth_date, balance from people" ,
new BeanPropertyRowMapper < > ( Person . class ) ) ;
assertThat ( result . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( result ) . hasSize ( 1 ) ;
verifyPerson ( result . get ( 0 ) ) ;
mock . verifyClosed ( ) ;
}
@Test
public void testM appingWithInheritance( ) throws Exception {
void m appingWithInheritance( ) throws Exception {
Mock mock = new Mock ( ) ;
List < ConcretePerson > result = mock . getJdbcTemplate ( ) . query (
"select name, age, birth_date, balance from people" ,
new BeanPropertyRowMapper < > ( ConcretePerson . class ) ) ;
assertThat ( result . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( result ) . hasSize ( 1 ) ;
verifyPerson ( result . get ( 0 ) ) ;
mock . verifyClosed ( ) ;
}
@Test
public void testM appingWithNoUnpopulatedFieldsFound( ) throws Exception {
void m appingWithNoUnpopulatedFieldsFound( ) throws Exception {
Mock mock = new Mock ( ) ;
List < ConcretePerson > result = mock . getJdbcTemplate ( ) . query (
"select name, age, birth_date, balance from people" ,
new BeanPropertyRowMapper < > ( ConcretePerson . class , true ) ) ;
assertThat ( result . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( result ) . hasSize ( 1 ) ;
verifyPerson ( result . get ( 0 ) ) ;
mock . verifyClosed ( ) ;
}
@Test
public void testM appingWithUnpopulatedFieldsNotChecked( ) throws Exception {
void m appingWithUnpopulatedFieldsNotChecked( ) throws Exception {
Mock mock = new Mock ( ) ;
List < ExtendedPerson > result = mock . getJdbcTemplate ( ) . query (
"select name, age, birth_date, balance from people" ,
new BeanPropertyRowMapper < > ( ExtendedPerson . class ) ) ;
assertThat ( result . size ( ) ) . isEqualTo ( 1 ) ;
ExtendedPerson bean = result . get ( 0 ) ;
verifyPerson ( bean ) ;
assertThat ( result ) . hasSize ( 1 ) ;
verifyPerson ( result . get ( 0 ) ) ;
mock . verifyClosed ( ) ;
}
@Test
public void testM appingWithUnpopulatedFieldsNotAccepted( ) throws Exception {
void m appingWithUnpopulatedFieldsNotAccepted( ) throws Exception {
Mock mock = new Mock ( ) ;
assertThatExceptionOfType ( InvalidDataAccessApiUsageException . class ) . isThrownBy ( ( ) - >
mock . getJdbcTemplate ( ) . query ( "select name, age, birth_date, balance from people" ,
@ -105,7 +111,7 @@ public class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
@@ -105,7 +111,7 @@ public class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
}
@Test
public void testM appingNullValue( ) throws Exception {
void m appingNullValue( ) throws Exception {
BeanPropertyRowMapper < Person > mapper = new BeanPropertyRowMapper < > ( Person . class ) ;
Mock mock = new Mock ( MockType . TWO ) ;
assertThatExceptionOfType ( TypeMismatchException . class ) . isThrownBy ( ( ) - >
@ -113,25 +119,50 @@ public class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
@@ -113,25 +119,50 @@ public class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
}
@Test
public void testQ ueryWithSpaceInColumnNameAndLocalDateTime( ) throws Exception {
void q ueryWithSpaceInColumnNameAndLocalDateTime( ) throws Exception {
Mock mock = new Mock ( MockType . THREE ) ;
List < SpacePerson > result = mock . getJdbcTemplate ( ) . query (
"select last_name as \"Last Name\", age, birth_date, balance from people" ,
new BeanPropertyRowMapper < > ( SpacePerson . class ) ) ;
assertThat ( result . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( result ) . hasSize ( 1 ) ;
verifyPerson ( result . get ( 0 ) ) ;
mock . verifyClosed ( ) ;
}
@Test
public void testQ ueryWithSpaceInColumnNameAndLocalDate( ) throws Exception {
void q ueryWithSpaceInColumnNameAndLocalDate( ) throws Exception {
Mock mock = new Mock ( MockType . THREE ) ;
List < DatePerson > result = mock . getJdbcTemplate ( ) . query (
"select last_name as \"Last Name\", age, birth_date, balance from people" ,
new BeanPropertyRowMapper < > ( DatePerson . class ) ) ;
assertThat ( result . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( result ) . hasSize ( 1 ) ;
verifyPerson ( result . get ( 0 ) ) ;
mock . verifyClosed ( ) ;
}
@Test
void queryWithUnderscoreInColumnNameAndPersonWithMultipleAdjacentUppercaseLettersInPropertyName ( ) throws Exception {
Mock mock = new Mock ( ) ;
List < EmailPerson > result = mock . getJdbcTemplate ( ) . query (
"select name, age, birth_date, balance, e_mail from people" ,
new BeanPropertyRowMapper < > ( EmailPerson . class ) ) ;
assertThat ( result ) . hasSize ( 1 ) ;
verifyPerson ( result . get ( 0 ) ) ;
mock . verifyClosed ( ) ;
}
@ParameterizedTest
@CsvSource ( {
"age, age" ,
"lastName, last_name" ,
"Name, name" ,
"FirstName, first_name" ,
"EMail, e_mail" ,
"URL, u_r_l" , // likely undesirable, but that's the status quo
} )
void underscoreName ( String input , String expected ) {
BeanPropertyRowMapper < ? > mapper = new BeanPropertyRowMapper < > ( Object . class ) ;
assertThat ( mapper . underscoreName ( input ) ) . isEqualTo ( expected ) ;
}
}