Skip to content

Commit 48b037f

Browse files
committed
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
1 parent 24d3584 commit 48b037f

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

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

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static java.util.Collections.*;
2020
import static org.assertj.core.api.Assertions.*;
2121
import static org.assertj.core.api.SoftAssertions.*;
22+
import static org.springframework.data.jdbc.testing.TestConfiguration.*;
2223
import static org.springframework.data.jdbc.testing.TestDatabaseFeatures.Feature.*;
2324
import static org.springframework.test.context.TestExecutionListeners.MergeMode.*;
2425

@@ -36,7 +37,6 @@
3637
import java.util.stream.IntStream;
3738

3839
import org.assertj.core.api.SoftAssertions;
39-
import org.junit.jupiter.api.BeforeEach;
4040
import org.junit.jupiter.api.Test;
4141
import org.junit.jupiter.api.extension.ExtendWith;
4242
import org.springframework.beans.factory.annotation.Autowired;
@@ -67,6 +67,7 @@
6767
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
6868
import org.springframework.data.relational.core.mapping.Table;
6969
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
70+
import org.springframework.test.context.ActiveProfiles;
7071
import org.springframework.test.context.ContextConfiguration;
7172
import org.springframework.test.context.TestExecutionListeners;
7273
import org.springframework.test.context.junit.jupiter.SpringExtension;
@@ -99,13 +100,6 @@ abstract class AbstractJdbcAggregateTemplateIntegrationTests {
99100

100101
LegoSet legoSet = createLegoSet("Star Destroyer");
101102

102-
@BeforeEach
103-
void beforeEach(){
104-
mappingContext.setSingleQueryLoadingEnabled(useSingleQuery());
105-
}
106-
107-
abstract boolean useSingleQuery();
108-
109103
/**
110104
* creates an instance of {@link NoIdListChain4} with the following properties:
111105
* <ul>
@@ -1879,16 +1873,10 @@ JdbcAggregateOperations operations(ApplicationEventPublisher publisher, Relation
18791873
}
18801874
}
18811875

1882-
static class JdbcAggregateTemplateIntegrationTests extends AbstractJdbcAggregateTemplateIntegrationTests {
1883-
@Override
1884-
boolean useSingleQuery() {
1885-
return false;
1886-
}
1887-
}
1888-
static class JdbcAggregateTemplateSqlIntegrationTests extends AbstractJdbcAggregateTemplateIntegrationTests {
1889-
@Override
1890-
boolean useSingleQuery() {
1891-
return true;
1892-
}
1876+
static class JdbcAggregateTemplateIntegrationTests extends AbstractJdbcAggregateTemplateIntegrationTests { }
1877+
1878+
@ActiveProfiles(PROFILE_SINGLE_QUERY_LOADING)
1879+
static class JdbcAggregateTemplateSingleQueryLoadingIntegrationTests extends AbstractJdbcAggregateTemplateIntegrationTests {
1880+
18931881
}
18941882
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @author Oliver Gierke
3232
*/
3333
@Configuration
34-
@Profile({ "hsql", "default" })
34+
@Profile({ "hsql", "!h2 && !mysql && !mariadb && !postgres && !oracle && !db2 && !mssql" })
3535
class HsqlDataSourceConfiguration {
3636

3737
@Autowired Class<?> context;

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.context.annotation.ComponentScan;
3232
import org.springframework.context.annotation.Configuration;
3333
import org.springframework.context.annotation.Lazy;
34+
import org.springframework.context.annotation.Profile;
3435
import org.springframework.data.convert.CustomConversions;
3536
import org.springframework.data.jdbc.core.convert.*;
3637
import org.springframework.data.jdbc.core.dialect.JdbcDialect;
@@ -67,6 +68,9 @@
6768
@ComponentScan // To pick up configuration classes (per activated profile)
6869
public class TestConfiguration {
6970

71+
public static final String PROFILE_SINGLE_QUERY_LOADING = "singleQueryLoading";
72+
public static final String PROFILE_NO_SINGLE_QUERY_LOADING = "!" + PROFILE_SINGLE_QUERY_LOADING;
73+
7074
@Autowired DataSource dataSource;
7175
@Autowired BeanFactory beanFactory;
7276
@Autowired ApplicationEventPublisher publisher;
@@ -107,11 +111,21 @@ template, new SqlParametersFactory(context, converter),
107111
new InsertStrategyFactory(template, new BatchJdbcOperations(template.getJdbcOperations()), dialect)).create();
108112
}
109113

110-
@Bean
111-
JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy, CustomConversions conversions) {
114+
@Bean("jdbcMappingContext")
115+
@Profile(PROFILE_NO_SINGLE_QUERY_LOADING)
116+
JdbcMappingContext jdbcMappingContextWithOutSingleQueryLoading(Optional<NamingStrategy> namingStrategy, CustomConversions conversions) {
117+
118+
JdbcMappingContext mappingContext = new JdbcMappingContext(namingStrategy.orElse(DefaultNamingStrategy.INSTANCE));
119+
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
120+
return mappingContext;
121+
}
122+
@Bean("jdbcMappingContext")
123+
@Profile(PROFILE_SINGLE_QUERY_LOADING)
124+
JdbcMappingContext jdbcMappingContextWithSingleQueryLoading(Optional<NamingStrategy> namingStrategy, CustomConversions conversions) {
112125

113126
JdbcMappingContext mappingContext = new JdbcMappingContext(namingStrategy.orElse(DefaultNamingStrategy.INSTANCE));
114127
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
128+
mappingContext.setSingleQueryLoadingEnabled(true);
115129
return mappingContext;
116130
}
117131

0 commit comments

Comments
 (0)