Browse Source

Fix test setup for Single Query Loading.

The construction of the DataAccessStrategy happened before the MappingContext was properly setup.
This is now fixed.
A test utilizes Single Query Loading, if it activates the profile singleQueryLoading

Closes #1606
pull/1616/head
Jens Schauder 2 years ago
parent
commit
48b037fb9b
No known key found for this signature in database
GPG Key ID: 9537B67540F0A581
  1. 26
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AbstractJdbcAggregateTemplateIntegrationTests.java
  2. 2
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/HsqlDataSourceConfiguration.java
  3. 18
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestConfiguration.java

26
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AbstractJdbcAggregateTemplateIntegrationTests.java

@ -19,6 +19,7 @@ import static java.util.Arrays.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.SoftAssertions.*; import static org.assertj.core.api.SoftAssertions.*;
import static org.springframework.data.jdbc.testing.TestConfiguration.*;
import static org.springframework.data.jdbc.testing.TestDatabaseFeatures.Feature.*; import static org.springframework.data.jdbc.testing.TestDatabaseFeatures.Feature.*;
import static org.springframework.test.context.TestExecutionListeners.MergeMode.*; import static org.springframework.test.context.TestExecutionListeners.MergeMode.*;
@ -36,7 +37,6 @@ import java.util.function.Function;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import org.assertj.core.api.SoftAssertions; import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -67,6 +67,7 @@ import org.springframework.data.relational.core.mapping.MappedCollection;
import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.Table; import org.springframework.data.relational.core.mapping.Table;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
@ -99,13 +100,6 @@ abstract class AbstractJdbcAggregateTemplateIntegrationTests {
LegoSet legoSet = createLegoSet("Star Destroyer"); LegoSet legoSet = createLegoSet("Star Destroyer");
@BeforeEach
void beforeEach(){
mappingContext.setSingleQueryLoadingEnabled(useSingleQuery());
}
abstract boolean useSingleQuery();
/** /**
* creates an instance of {@link NoIdListChain4} with the following properties: * creates an instance of {@link NoIdListChain4} with the following properties:
* <ul> * <ul>
@ -1879,16 +1873,10 @@ abstract class AbstractJdbcAggregateTemplateIntegrationTests {
} }
} }
static class JdbcAggregateTemplateIntegrationTests extends AbstractJdbcAggregateTemplateIntegrationTests { static class JdbcAggregateTemplateIntegrationTests extends AbstractJdbcAggregateTemplateIntegrationTests { }
@Override
boolean useSingleQuery() { @ActiveProfiles(PROFILE_SINGLE_QUERY_LOADING)
return false; static class JdbcAggregateTemplateSingleQueryLoadingIntegrationTests extends AbstractJdbcAggregateTemplateIntegrationTests {
}
}
static class JdbcAggregateTemplateSqlIntegrationTests extends AbstractJdbcAggregateTemplateIntegrationTests {
@Override
boolean useSingleQuery() {
return true;
}
} }
} }

2
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/HsqlDataSourceConfiguration.java

@ -31,7 +31,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
* @author Oliver Gierke * @author Oliver Gierke
*/ */
@Configuration @Configuration
@Profile({ "hsql", "default" }) @Profile({ "hsql", "!h2 && !mysql && !mariadb && !postgres && !oracle && !db2 && !mssql" })
class HsqlDataSourceConfiguration { class HsqlDataSourceConfiguration {
@Autowired Class<?> context; @Autowired Class<?> context;

18
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestConfiguration.java

@ -31,6 +31,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Profile;
import org.springframework.data.convert.CustomConversions; import org.springframework.data.convert.CustomConversions;
import org.springframework.data.jdbc.core.convert.*; import org.springframework.data.jdbc.core.convert.*;
import org.springframework.data.jdbc.core.dialect.JdbcDialect; import org.springframework.data.jdbc.core.dialect.JdbcDialect;
@ -67,6 +68,9 @@ import org.springframework.transaction.PlatformTransactionManager;
@ComponentScan // To pick up configuration classes (per activated profile) @ComponentScan // To pick up configuration classes (per activated profile)
public class TestConfiguration { public class TestConfiguration {
public static final String PROFILE_SINGLE_QUERY_LOADING = "singleQueryLoading";
public static final String PROFILE_NO_SINGLE_QUERY_LOADING = "!" + PROFILE_SINGLE_QUERY_LOADING;
@Autowired DataSource dataSource; @Autowired DataSource dataSource;
@Autowired BeanFactory beanFactory; @Autowired BeanFactory beanFactory;
@Autowired ApplicationEventPublisher publisher; @Autowired ApplicationEventPublisher publisher;
@ -107,11 +111,21 @@ public class TestConfiguration {
new InsertStrategyFactory(template, new BatchJdbcOperations(template.getJdbcOperations()), dialect)).create(); new InsertStrategyFactory(template, new BatchJdbcOperations(template.getJdbcOperations()), dialect)).create();
} }
@Bean @Bean("jdbcMappingContext")
JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy, CustomConversions conversions) { @Profile(PROFILE_NO_SINGLE_QUERY_LOADING)
JdbcMappingContext jdbcMappingContextWithOutSingleQueryLoading(Optional<NamingStrategy> namingStrategy, CustomConversions conversions) {
JdbcMappingContext mappingContext = new JdbcMappingContext(namingStrategy.orElse(DefaultNamingStrategy.INSTANCE));
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
return mappingContext;
}
@Bean("jdbcMappingContext")
@Profile(PROFILE_SINGLE_QUERY_LOADING)
JdbcMappingContext jdbcMappingContextWithSingleQueryLoading(Optional<NamingStrategy> namingStrategy, CustomConversions conversions) {
JdbcMappingContext mappingContext = new JdbcMappingContext(namingStrategy.orElse(DefaultNamingStrategy.INSTANCE)); JdbcMappingContext mappingContext = new JdbcMappingContext(namingStrategy.orElse(DefaultNamingStrategy.INSTANCE));
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder()); mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
mappingContext.setSingleQueryLoadingEnabled(true);
return mappingContext; return mappingContext;
} }

Loading…
Cancel
Save