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
This commit is contained in:
Jens Schauder
2023-09-08 12:08:24 +02:00
parent 24d3584488
commit 48b037fb9b
3 changed files with 24 additions and 22 deletions

View File

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

View File

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

View File

@@ -31,6 +31,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Profile;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.jdbc.core.convert.*;
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)
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 BeanFactory beanFactory;
@Autowired ApplicationEventPublisher publisher;
@@ -107,13 +111,23 @@ public class TestConfiguration {
new InsertStrategyFactory(template, new BatchJdbcOperations(template.getJdbcOperations()), dialect)).create();
}
@Bean
JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy, CustomConversions conversions) {
@Bean("jdbcMappingContext")
@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));
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
mappingContext.setSingleQueryLoadingEnabled(true);
return mappingContext;
}
@Bean
CustomConversions jdbcCustomConversions(Dialect dialect) {