From 7f8dd819474632de777d8a7655f1c65be02655da Mon Sep 17 00:00:00 2001 From: raviu Date: Tue, 14 Dec 2021 00:56:50 +1100 Subject: [PATCH 1/2] Include AbstractJdbcConfiguration beans in @DataJdbcTest See gh-29003 --- .../data/jdbc/DataJdbcTypeExcludeFilter.java | 19 +++++- .../jdbc/DataJdbcTypeExcludeFilterTests.java | 65 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java index 989c8ebc300..3ce0b5d3385 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -18,17 +18,34 @@ package org.springframework.boot.test.autoconfigure.data.jdbc; import org.springframework.boot.context.TypeExcludeFilter; import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter; +import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; + +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; /** * {@link TypeExcludeFilter} for {@link DataJdbcTest @DataJdbcTest}. * * @author Andy Wilkinson + * @author Ravi Undupitiya * @since 2.2.1 */ public final class DataJdbcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter { + private static final Set> DEFAULT_INCLUDES; + static { + Set> includes = new LinkedHashSet<>(); + includes.add(AbstractJdbcConfiguration.class); + DEFAULT_INCLUDES = Collections.unmodifiableSet(includes); + } + DataJdbcTypeExcludeFilter(Class testClass) { super(testClass); } + @Override + protected Set> getDefaultIncludes() { + return DEFAULT_INCLUDES; + } } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java new file mode 100644 index 00000000000..547f801332d --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.boot.test.autoconfigure.data.jdbc; + +import org.junit.jupiter.api.Test; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; +import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link DataJdbcTypeExcludeFilter}. + * + * @author Ravi Undupitiya + */ +public class DataJdbcTypeExcludeFilterTests { + + private MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); + + @Test + void matchNotUsingDefaultFilters() throws Exception { + DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(NotUsingDefaultFilters.class); + assertThat(excludes(filter, AbstractJdbcConfiguration.class)).isTrue(); + } + + @Test + void matchUsingDefaultFilters() throws Exception { + DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(UsingDefaultFilters.class); + assertThat(excludes(filter, AbstractJdbcConfiguration.class)).isFalse(); + } + + private boolean excludes(DataJdbcTypeExcludeFilter filter, Class type) throws IOException { + MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(type.getName()); + return filter.match(metadataReader, this.metadataReaderFactory); + } + + @DataJdbcTest + static class UsingDefaultFilters { + + } + + @DataJdbcTest(useDefaultFilters = false) + static class NotUsingDefaultFilters { + + } + +} From 9b34c31916ab3d63ad581b44723054b7e12f54f3 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 4 Jan 2022 15:32:23 +0100 Subject: [PATCH 2/2] Polish "Include AbstractJdbcConfiguration beans in @DataJdbcTest" See gh-29003 --- .../src/docs/asciidoc/features/testing.adoc | 2 +- .../autoconfigure/data/jdbc/DataJdbcTest.java | 9 ++++--- .../data/jdbc/DataJdbcTypeExcludeFilter.java | 17 +++++------- .../jdbc/DataJdbcTypeExcludeFilterTests.java | 26 ++++++++++++------- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc index 78dbe81b58d..c54565f7557 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc @@ -595,7 +595,7 @@ If you prefer your test to run against a real database, you can use the `@AutoCo ==== Auto-configured Data JDBC Tests `@DataJdbcTest` is similar to `@JdbcTest` but is for tests that use Spring Data JDBC repositories. By default, it configures an in-memory embedded database, a `JdbcTemplate`, and Spring Data JDBC repositories. -Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@DataJdbcTest` annotation is used. +Only `AbstractJdbcConfiguration` sub-classes are scanned when the `@DataJdbcTest` annotation is used, regular `@Component` and `@ConfigurationProperties` beans are not scanned. `@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans. TIP: A list of the auto-configurations that are enabled by `@DataJdbcTest` can be <>. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java index be7d58b20be..bdcbc890a77 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java @@ -43,8 +43,9 @@ import org.springframework.transaction.annotation.Transactional; * Annotation that can be used for a Data JDBC test that focuses only on * Data JDBC components. *

- * Using this annotation will disable full auto-configuration and instead apply only - * configuration relevant to Data JDBC tests. + * Using this annotation will disable full auto-configuration, scan for + * {@code AbstractJdbcConfiguration} sub-classes, and apply only configuration relevant to + * Data JDBC tests. *

* By default, tests annotated with {@code @DataJdbcTest} are transactional and roll back * at the end of each test. They also use an embedded in-memory database (replacing any @@ -87,8 +88,8 @@ public @interface DataJdbcTest { /** * Determines if default filtering should be used with - * {@link SpringBootApplication @SpringBootApplication}. By default no beans are - * included. + * {@link SpringBootApplication @SpringBootApplication}. By default, only + * {@code AbstractJdbcConfiguration} beans are included. * @see #includeFilters() * @see #excludeFilters() * @return if default filters should be used diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java index 3ce0b5d3385..acae298e294 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 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. @@ -16,14 +16,13 @@ package org.springframework.boot.test.autoconfigure.data.jdbc; +import java.util.Collections; +import java.util.Set; + import org.springframework.boot.context.TypeExcludeFilter; import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter; import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; -import java.util.Collections; -import java.util.LinkedHashSet; -import java.util.Set; - /** * {@link TypeExcludeFilter} for {@link DataJdbcTest @DataJdbcTest}. * @@ -33,12 +32,7 @@ import java.util.Set; */ public final class DataJdbcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter { - private static final Set> DEFAULT_INCLUDES; - static { - Set> includes = new LinkedHashSet<>(); - includes.add(AbstractJdbcConfiguration.class); - DEFAULT_INCLUDES = Collections.unmodifiableSet(includes); - } + private static final Set> DEFAULT_INCLUDES = Collections.singleton(AbstractJdbcConfiguration.class); DataJdbcTypeExcludeFilter(Class testClass) { super(testClass); @@ -48,4 +42,5 @@ public final class DataJdbcTypeExcludeFilter extends StandardAnnotationCustomiza protected Set> getDefaultIncludes() { return DEFAULT_INCLUDES; } + } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java index 547f801332d..1e7a8bda018 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java @@ -16,35 +16,37 @@ package org.springframework.boot.test.autoconfigure.data.jdbc; +import java.io.IOException; + import org.junit.jupiter.api.Test; + import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; -import java.io.IOException; - import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link DataJdbcTypeExcludeFilter}. * * @author Ravi Undupitiya + * @author Stephane Nicoll */ -public class DataJdbcTypeExcludeFilterTests { +class DataJdbcTypeExcludeFilterTests { - private MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); + private final MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); @Test - void matchNotUsingDefaultFilters() throws Exception { - DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(NotUsingDefaultFilters.class); - assertThat(excludes(filter, AbstractJdbcConfiguration.class)).isTrue(); + void matchUsingDefaultFilters() throws Exception { + DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(UsingDefaultFilters.class); + assertThat(excludes(filter, TestJdbcConfiguration.class)).isFalse(); } @Test - void matchUsingDefaultFilters() throws Exception { - DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(UsingDefaultFilters.class); - assertThat(excludes(filter, AbstractJdbcConfiguration.class)).isFalse(); + void matchNotUsingDefaultFilters() throws Exception { + DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(NotUsingDefaultFilters.class); + assertThat(excludes(filter, TestJdbcConfiguration.class)).isTrue(); } private boolean excludes(DataJdbcTypeExcludeFilter filter, Class type) throws IOException { @@ -62,4 +64,8 @@ public class DataJdbcTypeExcludeFilterTests { } + static class TestJdbcConfiguration extends AbstractJdbcConfiguration { + + } + }