Browse Source

DATAJDBC-412 - Polishing.

Added an integration test because you never can tell with databases.
pull/221/head
Jens Schauder 6 years ago
parent
commit
8e63084162
No known key found for this signature in database
GPG Key ID: 996B1389BA0721C3
  1. 1
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java
  2. 81
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCustomConversionIntegrationTests.java
  3. 2
      spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-db2.sql
  4. 3
      spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-h2.sql
  5. 4
      spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-hsql.sql
  6. 3
      spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-mariadb.sql
  7. 1
      spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-mssql.sql
  8. 3
      spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-mysql.sql
  9. 3
      spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-postgres.sql

1
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java

@ -311,6 +311,7 @@ public class BasicJdbcConverter extends BasicRelationalConverter implements Jdbc @@ -311,6 +311,7 @@ public class BasicJdbcConverter extends BasicRelationalConverter implements Jdbc
private JdbcValue tryToConvertToJdbcValue(@Nullable Object value) {
if (canWriteAsJdbcValue(value)) {
Object converted = writeValue(value, ClassTypeInformation.from(JdbcValue.class));
if(converted instanceof JdbcValue) {
return (JdbcValue) converted;

81
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCustomConversionIntegrationTests.java

@ -20,12 +20,12 @@ import static org.assertj.core.api.Assertions.*; @@ -20,12 +20,12 @@ import static org.assertj.core.api.Assertions.*;
import java.math.BigDecimal;
import java.sql.JDBCType;
import java.util.Optional;
import java.util.Date;
import org.assertj.core.api.SoftAssertions;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -72,7 +72,8 @@ public class JdbcRepositoryCustomConversionIntegrationTests { @@ -72,7 +72,8 @@ public class JdbcRepositoryCustomConversionIntegrationTests {
@Bean
JdbcCustomConversions jdbcCustomConversions() {
return new JdbcCustomConversions(asList(BigDecimalToString.INSTANCE, StringToBigDecimalConverter.INSTANCE));
return new JdbcCustomConversions(asList(StringToBigDecimalConverter.INSTANCE, BigDecimalToString.INSTANCE,
CustomIdReadingConverter.INSTANCE, CustomIdWritingConverter.INSTANCE));
}
}
@ -108,20 +109,62 @@ public class JdbcRepositoryCustomConversionIntegrationTests { @@ -108,20 +109,62 @@ public class JdbcRepositoryCustomConversionIntegrationTests {
repository.save(entity);
Optional<EntityWithStringyBigDecimal> reloaded = repository.findById(entity.id);
EntityWithStringyBigDecimal reloaded = repository.findById(entity.id).get();
// loading the number from the database might result in additional zeros at the end.
String stringyNumber = reloaded.get().stringyNumber;
String stringyNumber = reloaded.stringyNumber;
assertThat(stringyNumber).startsWith(entity.stringyNumber);
assertThat(stringyNumber.substring(entity.stringyNumber.length())).matches("0*");
}
interface EntityWithBooleanRepository extends CrudRepository<EntityWithStringyBigDecimal, Long> {}
@Test // DATAJDBC-412
public void saveAndLoadAnEntityWithReference() {
EntityWithStringyBigDecimal entity = new EntityWithStringyBigDecimal();
entity.stringyNumber = "123456.78910";
entity.reference = new OtherEntity();
entity.reference.created = new Date();
repository.save(entity);
EntityWithStringyBigDecimal reloaded = repository.findById(entity.id).get();
// loading the number from the database might result in additional zeros at the end.
SoftAssertions.assertSoftly(softly -> {
String stringyNumber = reloaded.stringyNumber;
softly.assertThat(stringyNumber).startsWith(entity.stringyNumber);
softly.assertThat(stringyNumber.substring(entity.stringyNumber.length())).matches("0*");
softly.assertThat(entity.id.value).isNotNull();
softly.assertThat(reloaded.id.value).isEqualTo(entity.id.value);
softly.assertThat(entity.reference.id.value).isNotNull();
softly.assertThat(reloaded.reference.id.value).isEqualTo(entity.reference.id.value);
});
}
interface EntityWithBooleanRepository extends CrudRepository<EntityWithStringyBigDecimal, CustomId> {}
private static class EntityWithStringyBigDecimal {
@Id Long id;
@Id CustomId id;
String stringyNumber;
OtherEntity reference;
}
private static class CustomId {
private final Long value;
CustomId(Long value) {
this.value = value;
}
}
private static class OtherEntity {
@Id CustomId id;
Date created;
}
@WritingConverter
@ -135,7 +178,6 @@ public class JdbcRepositoryCustomConversionIntegrationTests { @@ -135,7 +178,6 @@ public class JdbcRepositoryCustomConversionIntegrationTests {
Object value = new BigDecimal(source);
return JdbcValue.of(value, JDBCType.DECIMAL);
}
}
@ReadingConverter
@ -149,4 +191,27 @@ public class JdbcRepositoryCustomConversionIntegrationTests { @@ -149,4 +191,27 @@ public class JdbcRepositoryCustomConversionIntegrationTests {
return source.toString();
}
}
@WritingConverter
enum CustomIdWritingConverter implements Converter<CustomId, Number> {
INSTANCE;
@Override
public Number convert(CustomId source) {
return source.value.intValue();
}
}
@ReadingConverter
enum CustomIdReadingConverter implements Converter<Number, CustomId> {
INSTANCE;
@Override
public CustomId convert(Number source) {
return new CustomId(source.longValue());
}
}
}

2
spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-db2.sql

@ -1,3 +1,5 @@ @@ -1,3 +1,5 @@
DROP TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL;
DROP TABLE OTHER_ENTITY;
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, Stringy_number DECIMAL(20,10));
CREATE TABLE OTHER_ENTITY ( ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);

3
spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-h2.sql

@ -1 +1,2 @@ @@ -1 +1,2 @@
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id IDENTITY PRIMARY KEY, Stringy_number DECIMAL(20,10))
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id IDENTITY PRIMARY KEY, Stringy_number DECIMAL(20,10));
CREATE TABLE OTHER_ENTITY ( ID IDENTITY PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);

4
spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-hsql.sql

@ -1 +1,3 @@ @@ -1 +1,3 @@
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id IDENTITY PRIMARY KEY, Stringy_number DECIMAL(20,10))
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id IDENTITY PRIMARY KEY, Stringy_number DECIMAL(20,10));
CREATE TABLE OTHER_ENTITY ( ID IDENTITY PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);

3
spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-mariadb.sql

@ -1 +1,2 @@ @@ -1 +1,2 @@
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id BIGINT AUTO_INCREMENT PRIMARY KEY, Stringy_number DECIMAL(20,10))
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id BIGINT AUTO_INCREMENT PRIMARY KEY, Stringy_number DECIMAL(20,10));
CREATE TABLE OTHER_ENTITY ( ID BIGINT AUTO_INCREMENT PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);

1
spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-mssql.sql

@ -1 +1,2 @@ @@ -1 +1,2 @@
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id BIGINT IDENTITY PRIMARY KEY, Stringy_number DECIMAL(20,10))
CREATE TABLE OTHER_ENTITY ( ID BIGINT IDENTITY PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);

3
spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-mysql.sql

@ -1 +1,2 @@ @@ -1 +1,2 @@
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id BIGINT AUTO_INCREMENT PRIMARY KEY, Stringy_number DECIMAL(20,10))
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( ID BIGINT AUTO_INCREMENT PRIMARY KEY, Stringy_number DECIMAL(20,10));
CREATE TABLE OTHER_ENTITY ( ID BIGINT AUTO_INCREMENT PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);

3
spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCustomConversionIntegrationTests-postgres.sql

@ -1 +1,2 @@ @@ -1 +1,2 @@
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id SERIAL PRIMARY KEY, Stringy_number DECIMAL(20,10))
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id SERIAL PRIMARY KEY, Stringy_number DECIMAL(20,10));
CREATE TABLE OTHER_ENTITY ( ID SERIAL PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);

Loading…
Cancel
Save