Polishing

This commit is contained in:
Sam Brannen
2023-06-06 16:03:22 +02:00
parent 66a1be2d86
commit b9e972c248
6 changed files with 103 additions and 94 deletions

View File

@@ -34,25 +34,30 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNoException;
class BeanPropertyRowMapperTests {
/**
* Tests for R2DBC-based {@link BeanPropertyRowMapper}.
*
* @since 6.1
*/
class R2dbcBeanPropertyRowMapperTests {
@Test
void mappingUnknownReadableRejected() {
final BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
assertThatIllegalArgumentException().isThrownBy(() -> mapper.apply(Mockito.mock(Readable.class)))
.withMessageStartingWith("Can only map Readable Row or OutParameters, got io.r2dbc.spi.Readable$MockitoMock$");
}
@Test
void mappingOutParametersAccepted() {
final BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
assertThatNoException().isThrownBy(() -> mapper.apply(MockOutParameters.empty()));
}
@Test
void mappingRowSimpleObject() {
MockRow mockRow = SIMPLE_PERSON_ROW;
final BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
Person result = mapper.apply(mockRow);
@@ -64,7 +69,7 @@ class BeanPropertyRowMapperTests {
@Test
void mappingRowMissingAttributeAccepted() {
MockRow mockRow = SIMPLE_PERSON_ROW;
final BeanPropertyRowMapper<ExtendedPerson> mapper = new BeanPropertyRowMapper<>(ExtendedPerson.class);
BeanPropertyRowMapper<ExtendedPerson> mapper = new BeanPropertyRowMapper<>(ExtendedPerson.class);
ExtendedPerson result = mapper.apply(mockRow);
@@ -77,7 +82,7 @@ class BeanPropertyRowMapperTests {
@Test
void mappingRowWithDifferentName() {
MockRow mockRow = EMAIL_PERSON_ROW;
final BeanPropertyRowMapper<EmailPerson> mapper = new BeanPropertyRowMapper<>(EmailPerson.class);
BeanPropertyRowMapper<EmailPerson> mapper = new BeanPropertyRowMapper<>(EmailPerson.class);
EmailPerson result = mapper.apply(mockRow);
@@ -89,21 +94,22 @@ class BeanPropertyRowMapperTests {
@Test
void mappingRowMissingAttributeRejected() {
Class<ExtendedPerson> mappedClass = ExtendedPerson.class;
MockRow mockRow = SIMPLE_PERSON_ROW;
final BeanPropertyRowMapper<ExtendedPerson> mapper = new BeanPropertyRowMapper<>(ExtendedPerson.class, true);
BeanPropertyRowMapper<ExtendedPerson> mapper = new BeanPropertyRowMapper<>(mappedClass, true);
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
.isThrownBy(() -> mapper.apply(mockRow))
.withMessage("Given readable does not contain all items necessary to populate object of class org.springframework."
+ "r2dbc.core.BeanPropertyRowMapperTests$ExtendedPerson: [firstName, lastName, address, age]");
.withMessage("Given readable does not contain all items necessary to populate object of %s"
+ ": [firstName, lastName, address, age]", mappedClass);
}
//TODO cannot trigger a mapping of a read-only property, as mappedProperties don't include properties without a setter.
// TODO cannot trigger a mapping of a read-only property, as mappedProperties don't include properties without a setter.
@Test
void rowTypeAndMappingTypeMisaligned() {
MockRow mockRow = EXTENDED_PERSON_ROW;
final BeanPropertyRowMapper<TypeMismatchExtendedPerson> mapper = new BeanPropertyRowMapper<>(TypeMismatchExtendedPerson.class);
BeanPropertyRowMapper<TypeMismatchExtendedPerson> mapper = new BeanPropertyRowMapper<>(TypeMismatchExtendedPerson.class);
assertThatExceptionOfType(TypeMismatchException.class)
.isThrownBy(() -> mapper.apply(mockRow))
@@ -124,7 +130,7 @@ class BeanPropertyRowMapperTests {
.identified(2, int.class, null)
.identified(3, String.class, "123 Sesame Street")
.build();
final BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
mapper.setPrimitivesDefaultedForNullValue(true);
Person result = mapper.apply(mockRow);
@@ -147,6 +153,7 @@ class BeanPropertyRowMapperTests {
}
@SuppressWarnings("unused")
private static class Person {
String firstName;
@@ -181,6 +188,7 @@ class BeanPropertyRowMapperTests {
}
@SuppressWarnings("unused")
private static class ExtendedPerson extends Person {
String address;
@@ -204,6 +212,7 @@ class BeanPropertyRowMapperTests {
}
@SuppressWarnings("unused")
private static class EmailPerson extends Person {
String email;

View File

@@ -27,12 +27,17 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class DataClassRowMapperTests {
/**
* Test for R2DBC-based {@link DataClassRowMapper}.
*
* @since 6.1
*/
class R2dbcDataClassRowMapperTests {
@Test
void staticQueryWithDataClass() {
MockRow mockRow = MOCK_ROW; // uses name, age, birth_date
final DataClassRowMapper<ConstructorPerson> mapper = new DataClassRowMapper<>(ConstructorPerson.class);
DataClassRowMapper<ConstructorPerson> mapper = new DataClassRowMapper<>(ConstructorPerson.class);
ConstructorPerson person = mapper.apply(mockRow);
@@ -44,8 +49,8 @@ class DataClassRowMapperTests {
@Test
void staticQueryWithDataClassAndGenerics() {
MockRow mockRow = buildMockRow("birth_date", true); // uses name, age, birth_date, balance (as list)
//TODO validate actual R2DBC Row implementations would return something for balance if asking a List
final DataClassRowMapper<ConstructorPersonWithGenerics> mapper = new DataClassRowMapper<>(ConstructorPersonWithGenerics.class);
// TODO validate actual R2DBC Row implementations would return something for balance if requesting a List
DataClassRowMapper<ConstructorPersonWithGenerics> mapper = new DataClassRowMapper<>(ConstructorPersonWithGenerics.class);
ConstructorPersonWithGenerics person = mapper.apply(mockRow);
assertThat(person.name()).isEqualTo("Bubba");
@@ -57,7 +62,7 @@ class DataClassRowMapperTests {
@Test
void staticQueryWithDataRecord() {
MockRow mockRow = MOCK_ROW; // uses name, age, birth_date, balance
final DataClassRowMapper<RecordPerson> mapper = new DataClassRowMapper<>(RecordPerson.class);
DataClassRowMapper<RecordPerson> mapper = new DataClassRowMapper<>(RecordPerson.class);
RecordPerson person = mapper.apply(mockRow);
assertThat(person.name()).isEqualTo("Bubba");
@@ -69,7 +74,7 @@ class DataClassRowMapperTests {
@Test
void staticQueryWithDataClassAndSetters() {
MockRow mockRow = buildMockRow("birthdate", false); // uses name, age, birthdate (no underscore), balance
final DataClassRowMapper<ConstructorPersonWithSetters> mapper = new DataClassRowMapper<>(ConstructorPersonWithSetters.class);
DataClassRowMapper<ConstructorPersonWithSetters> mapper = new DataClassRowMapper<>(ConstructorPersonWithSetters.class);
ConstructorPersonWithSetters person = mapper.apply(mockRow);
assertThat(person.name()).isEqualTo("BUBBA");
@@ -177,10 +182,10 @@ class DataClassRowMapperTests {
}
static MockRow MOCK_ROW = buildMockRow("birth_date", false);
static final MockRow MOCK_ROW = buildMockRow("birth_date", false);
private static MockRow buildMockRow(String birthDateColumnName, boolean balanceObjectIdentifier) {
final MockRow.Builder builder = MockRow.builder();
MockRow.Builder builder = MockRow.builder();
builder.metadata(MockRowMetadata.builder()
.columnMetadata(MockColumnMetadata.builder().name("name").javaType(String.class).build())
.columnMetadata(MockColumnMetadata.builder().name("age").javaType(long.class).build())