Browse Source

DATAJDBC-258 - Polishing.

Simplified id value retrieval.
Simplified ignoring tests for a specific database.
Fixed property name in pom.xml.

Original pull request: #98.
pull/99/head
Jens Schauder 7 years ago
parent
commit
1b5854e5ab
  1. 2
      pom.xml
  2. 2
      spring-data-jdbc/pom.xml
  3. 16
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java
  4. 15
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java
  5. 13
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryPropertyConversionIntegrationTests.java
  6. 5
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithCollectionsIntegrationTests.java
  7. 5
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithListsIntegrationTests.java
  8. 5
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithMapsIntegrationTests.java
  9. 46
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java

2
pom.xml

@ -29,7 +29,7 @@ @@ -29,7 +29,7 @@
<degraph-check.version>0.1.4</degraph-check.version>
<hsqldb.version>2.2.8</hsqldb.version>
<mssql.verion>7.0.0.jre8</mssql.verion>
<mssql.version>7.0.0.jre8</mssql.version>
<mybatis.version>3.4.6</mybatis.version>
<mybatis-spring.version>1.3.2</mybatis-spring.version>
<mysql-connector-java.version>5.1.41</mysql-connector-java.version>

2
spring-data-jdbc/pom.xml

@ -161,7 +161,7 @@ @@ -161,7 +161,7 @@
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>${mssql.verion}</version>
<version>${mssql.version}</version>
<scope>test</scope>
</dependency>

16
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java

@ -319,13 +319,16 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy { @@ -319,13 +319,16 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
|| (idProperty.getType() == long.class && idValue.equals(0L));
}
@Nullable
private <S> Object getIdFromHolder(KeyHolder holder, RelationalPersistentEntity<S> 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<String, Object> keys = holder.getKeys();
if (keys == null || persistentEntity.getIdProperty() == null) {
@ -333,17 +336,6 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy { @@ -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<String, Object> keys = holder.getKeys();
if (keys == null || persistentEntity.getIdProperty() == null) {
return null;
}
return null;
}
}

15
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java

@ -20,6 +20,9 @@ import static org.assertj.core.api.Assertions.*; @@ -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; @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -296,6 +299,7 @@ public class AggregateTemplateIntegrationTests {
softly.assertAll();
}
@Test // DATAJDBC-276
public void saveAndLoadAnEntityWithListOfElementsWithoutId() {
@ -368,7 +372,6 @@ public class AggregateTemplateIntegrationTests { @@ -368,7 +372,6 @@ public class AggregateTemplateIntegrationTests {
private String content;
}
@Configuration
@Import(TestConfiguration.class)
static class Config {

13
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryPropertyConversionIntegrationTests.java

@ -40,10 +40,12 @@ import org.springframework.context.annotation.Configuration; @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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());

5
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithCollectionsIntegrationTests.java

@ -34,10 +34,12 @@ import org.springframework.context.annotation.Configuration; @@ -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; @@ -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 { @@ -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");

5
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithListsIntegrationTests.java

@ -34,10 +34,12 @@ import org.springframework.context.annotation.Configuration; @@ -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; @@ -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 { @@ -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");

5
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryWithMapsIntegrationTests.java

@ -33,10 +33,12 @@ import org.springframework.context.annotation.Configuration; @@ -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; @@ -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 { @@ -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");

46
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java

@ -0,0 +1,46 @@ @@ -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.<database>} where
* {@code <database> } 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();
}
}
Loading…
Cancel
Save