From 1b5854e5abde2aa4dec5d2bcba6aa985a795780c Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 30 Oct 2018 16:30:35 +0100 Subject: [PATCH] DATAJDBC-258 - Polishing. Simplified id value retrieval. Simplified ignoring tests for a specific database. Fixed property name in pom.xml. Original pull request: #98. --- pom.xml | 2 +- spring-data-jdbc/pom.xml | 2 +- .../jdbc/core/DefaultDataAccessStrategy.java | 16 ++----- .../AggregateTemplateIntegrationTests.java | 15 +++--- ...oryPropertyConversionIntegrationTests.java | 13 ++++-- ...sitoryWithCollectionsIntegrationTests.java | 5 +- ...bcRepositoryWithListsIntegrationTests.java | 5 +- ...dbcRepositoryWithMapsIntegrationTests.java | 5 +- .../testing/DatabaseProfileValueSource.java | 46 +++++++++++++++++++ 9 files changed, 81 insertions(+), 28 deletions(-) create mode 100644 spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java diff --git a/pom.xml b/pom.xml index 2f0689ddd..bb3e4a50e 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ 0.1.4 2.2.8 - 7.0.0.jre8 + 7.0.0.jre8 3.4.6 1.3.2 5.1.41 diff --git a/spring-data-jdbc/pom.xml b/spring-data-jdbc/pom.xml index 7a29a087d..945823017 100644 --- a/spring-data-jdbc/pom.xml +++ b/spring-data-jdbc/pom.xml @@ -161,7 +161,7 @@ com.microsoft.sqlserver mssql-jdbc - ${mssql.verion} + ${mssql.version} test diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java index a029280c9..3ad8abf5a 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java @@ -319,13 +319,16 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy { || (idProperty.getType() == long.class && idValue.equals(0L)); } + @Nullable private Object getIdFromHolder(KeyHolder holder, RelationalPersistentEntity persistentEntity) { try { // MySQL just returns one value with a special name return holder.getKey(); - } catch (InvalidDataAccessApiUsageException e) { + } catch (DataRetrievalFailureException | InvalidDataAccessApiUsageException e) { // Postgres returns a value for each column + // MS SQL Server returns a value that might be null. + Map keys = holder.getKeys(); if (keys == null || persistentEntity.getIdProperty() == null) { @@ -333,17 +336,6 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy { } return keys.get(persistentEntity.getIdColumn()); - } catch (DataRetrievalFailureException e) { - // thomas.lang@th-deg.de - // mssql causes org.springframework.dao.DataRetrievalFailureException: - // The generated key is not of a supported numeric type. Unable to cast [null] to [java.lang.Number] - // see what happens here - - Map keys = holder.getKeys(); - if (keys == null || persistentEntity.getIdProperty() == null) { - return null; - } - return null; } } diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java index 1f8fb01ee..095f1915e 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java @@ -20,6 +20,9 @@ import static org.assertj.core.api.Assertions.*; import lombok.Data; +import java.util.ArrayList; +import java.util.List; + import org.assertj.core.api.SoftAssertions; import org.junit.ClassRule; import org.junit.Rule; @@ -30,19 +33,18 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.annotation.Id; +import org.springframework.data.jdbc.testing.DatabaseProfileValueSource; import org.springframework.data.jdbc.testing.TestConfiguration; import org.springframework.data.relational.core.conversion.RelationalConverter; import org.springframework.data.relational.core.mapping.Column; import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.test.annotation.IfProfileValue; +import org.springframework.test.annotation.ProfileValueSourceConfiguration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.rules.SpringClassRule; import org.springframework.test.context.junit4.rules.SpringMethodRule; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.List; - /** * Integration tests for {@link JdbcAggregateTemplate}. * @@ -51,6 +53,7 @@ import java.util.List; */ @ContextConfiguration @Transactional +@ProfileValueSourceConfiguration(DatabaseProfileValueSource.class) public class AggregateTemplateIntegrationTests { @ClassRule public static final SpringClassRule classRule = new SpringClassRule(); @@ -145,7 +148,7 @@ public class AggregateTemplateIntegrationTests { } @Test // DATAJDBC-112 - @IfProfileValue(name = "spring.profiles.active", values = {"mysql", "postgres", "mariadb", "default"}) // DATAJDBC-278 + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 public void updateReferencedEntityFromNull() { legoSet.setManual(null); @@ -204,7 +207,7 @@ public class AggregateTemplateIntegrationTests { } @Test // DATAJDBC-112 - @IfProfileValue(name = "spring.profiles.active", values = {"mysql", "postgres", "mariadb", "default"}) // DATAJDBC-278 + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 public void changeReferencedEntity() { template.save(legoSet); @@ -296,6 +299,7 @@ public class AggregateTemplateIntegrationTests { softly.assertAll(); } + @Test // DATAJDBC-276 public void saveAndLoadAnEntityWithListOfElementsWithoutId() { @@ -368,7 +372,6 @@ public class AggregateTemplateIntegrationTests { private String content; } - @Configuration @Import(TestConfiguration.class) static class Config { diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryPropertyConversionIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryPropertyConversionIntegrationTests.java index 5e99a00d5..47161cab4 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryPropertyConversionIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryPropertyConversionIntegrationTests.java @@ -40,10 +40,12 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.annotation.Id; import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; +import org.springframework.data.jdbc.testing.DatabaseProfileValueSource; import org.springframework.data.jdbc.testing.TestConfiguration; import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent; import org.springframework.data.repository.CrudRepository; import org.springframework.test.annotation.IfProfileValue; +import org.springframework.test.annotation.ProfileValueSourceConfiguration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.rules.SpringClassRule; import org.springframework.test.context.junit4.rules.SpringMethodRule; @@ -57,6 +59,7 @@ import org.springframework.transaction.annotation.Transactional; * @author Thomas Lang */ @ContextConfiguration +@ProfileValueSourceConfiguration(DatabaseProfileValueSource.class) @Transactional public class JdbcRepositoryPropertyConversionIntegrationTests { @@ -90,7 +93,7 @@ public class JdbcRepositoryPropertyConversionIntegrationTests { @Autowired DummyEntityRepository repository; @Test // DATAJDBC-95 - @IfProfileValue(name = "spring.profiles.active", values = {"mysql", "postgres", "mariadb", "default"}) // DATAJDBC-278 + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 public void saveAndLoadAnEntity() { EntityWithColumnsRequiringConversions entity = repository.save(createDummyEntity()); @@ -109,7 +112,7 @@ public class JdbcRepositoryPropertyConversionIntegrationTests { } @Test // DATAJDBC-95 - @IfProfileValue(name = "spring.profiles.active", values = {"mysql", "postgres", "mariadb", "default"}) // DATAJDBC-278 + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 public void existsById() { EntityWithColumnsRequiringConversions entity = repository.save(createDummyEntity()); @@ -118,7 +121,7 @@ public class JdbcRepositoryPropertyConversionIntegrationTests { } @Test // DATAJDBC-95 - @IfProfileValue(name = "spring.profiles.active", values = {"mysql", "postgres", "mariadb", "default"}) // DATAJDBC-278 + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 public void findAllById() { EntityWithColumnsRequiringConversions entity = repository.save(createDummyEntity()); @@ -127,7 +130,7 @@ public class JdbcRepositoryPropertyConversionIntegrationTests { } @Test // DATAJDBC-95 - @IfProfileValue(name = "spring.profiles.active", values = {"mysql", "postgres", "mariadb", "default"}) // DATAJDBC-278 + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 public void deleteAll() { EntityWithColumnsRequiringConversions entity = repository.save(createDummyEntity()); @@ -138,7 +141,7 @@ public class JdbcRepositoryPropertyConversionIntegrationTests { } @Test // DATAJDBC-95 - @IfProfileValue(name = "spring.profiles.active", values = {"mysql", "postgres", "mariadb", "default"}) // DATAJDBC-278 + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 public void deleteById() { EntityWithColumnsRequiringConversions entity = repository.save(createDummyEntity()); diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithCollectionsIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithCollectionsIntegrationTests.java index 5aafde897..3d9f8f190 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithCollectionsIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithCollectionsIntegrationTests.java @@ -34,10 +34,12 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.annotation.Id; import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; +import org.springframework.data.jdbc.testing.DatabaseProfileValueSource; import org.springframework.data.jdbc.testing.TestConfiguration; import org.springframework.data.repository.CrudRepository; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.test.annotation.IfProfileValue; +import org.springframework.test.annotation.ProfileValueSourceConfiguration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.rules.SpringClassRule; import org.springframework.test.context.junit4.rules.SpringMethodRule; @@ -50,6 +52,7 @@ import org.springframework.transaction.annotation.Transactional; * @author Thomas Lang */ @ContextConfiguration +@ProfileValueSourceConfiguration(DatabaseProfileValueSource.class) @Transactional public class JdbcRepositoryWithCollectionsIntegrationTests { @@ -136,7 +139,7 @@ public class JdbcRepositoryWithCollectionsIntegrationTests { } @Test // DATAJDBC-113 - @IfProfileValue(name = "spring.profiles.active", values = {"mysql", "postgres", "mariadb", "default"}) // DATAJDBC-278 + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 public void updateSet() { Element element1 = createElement("one"); diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithListsIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithListsIntegrationTests.java index 752fc7c6d..ba0aa8c62 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithListsIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithListsIntegrationTests.java @@ -34,10 +34,12 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.annotation.Id; import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; +import org.springframework.data.jdbc.testing.DatabaseProfileValueSource; import org.springframework.data.jdbc.testing.TestConfiguration; import org.springframework.data.repository.CrudRepository; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.test.annotation.IfProfileValue; +import org.springframework.test.annotation.ProfileValueSourceConfiguration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.rules.SpringClassRule; import org.springframework.test.context.junit4.rules.SpringMethodRule; @@ -50,6 +52,7 @@ import org.springframework.transaction.annotation.Transactional; * @author Thomas Lang */ @ContextConfiguration +@ProfileValueSourceConfiguration(DatabaseProfileValueSource.class) @Transactional public class JdbcRepositoryWithListsIntegrationTests { @@ -136,7 +139,7 @@ public class JdbcRepositoryWithListsIntegrationTests { } @Test // DATAJDBC-130 - @IfProfileValue(name = "spring.profiles.active", values = {"mysql", "postgres", "mariadb", "default"}) // DATAJDBC-278 + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 public void updateList() { Element element1 = createElement("one"); diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithMapsIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithMapsIntegrationTests.java index 70ede7515..6e670c352 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithMapsIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithMapsIntegrationTests.java @@ -33,10 +33,12 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.annotation.Id; import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; +import org.springframework.data.jdbc.testing.DatabaseProfileValueSource; import org.springframework.data.jdbc.testing.TestConfiguration; import org.springframework.data.repository.CrudRepository; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.test.annotation.IfProfileValue; +import org.springframework.test.annotation.ProfileValueSourceConfiguration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.rules.SpringClassRule; import org.springframework.test.context.junit4.rules.SpringMethodRule; @@ -49,6 +51,7 @@ import org.springframework.transaction.annotation.Transactional; * @author Thomas Lang */ @ContextConfiguration +@ProfileValueSourceConfiguration(DatabaseProfileValueSource.class) @Transactional public class JdbcRepositoryWithMapsIntegrationTests { @@ -135,7 +138,7 @@ public class JdbcRepositoryWithMapsIntegrationTests { } @Test // DATAJDBC-131 - @IfProfileValue(name = "spring.profiles.active", values = {"mysql", "postgres", "mariadb", "default"}) // DATAJDBC-278 + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 public void updateMap() { Element element1 = createElement("one"); diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java new file mode 100644 index 000000000..6ea7c5d47 --- /dev/null +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java @@ -0,0 +1,46 @@ +/* + * Copyright 2018 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 + * + * http://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.data.jdbc.testing; + +import org.springframework.test.annotation.ProfileValueSource; + +/** + * This {@link ProfileValueSource} offers a single set of keys {@code current.database.is.not.} where + * {@code } is a database as used in active profiles to enable integration tests to run with a certain + * database. The value returned for these keys is {@code "true"} or {@code "false"} depending on if the database is + * actually the one currently used by integration tests. + * + * @author Jens Schauder + */ +public class DatabaseProfileValueSource implements ProfileValueSource { + + private final String currentDatabase; + + DatabaseProfileValueSource() { + + currentDatabase = System.getProperty("spring.profiles.active", "hsqldb"); + } + + @Override + public String get(String key) { + + if (!key.startsWith("current.database.is.not.")) { + return null; + } + + return Boolean.toString(!key.endsWith(currentDatabase)).toLowerCase(); + } +}