Skip to content

Commit 7f8dd81

Browse files
raviusnicoll
authored andcommitted
Include AbstractJdbcConfiguration beans in @DataJdbcTest
See gh-29003
1 parent 05fa0e2 commit 7f8dd81

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,17 +18,34 @@
1818

1919
import org.springframework.boot.context.TypeExcludeFilter;
2020
import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter;
21+
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
22+
23+
import java.util.Collections;
24+
import java.util.LinkedHashSet;
25+
import java.util.Set;
2126

2227
/**
2328
* {@link TypeExcludeFilter} for {@link DataJdbcTest @DataJdbcTest}.
2429
*
2530
* @author Andy Wilkinson
31+
* @author Ravi Undupitiya
2632
* @since 2.2.1
2733
*/
2834
public final class DataJdbcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter<DataJdbcTest> {
2935

36+
private static final Set<Class<?>> DEFAULT_INCLUDES;
37+
static {
38+
Set<Class<?>> includes = new LinkedHashSet<>();
39+
includes.add(AbstractJdbcConfiguration.class);
40+
DEFAULT_INCLUDES = Collections.unmodifiableSet(includes);
41+
}
42+
3043
DataJdbcTypeExcludeFilter(Class<?> testClass) {
3144
super(testClass);
3245
}
3346

47+
@Override
48+
protected Set<Class<?>> getDefaultIncludes() {
49+
return DEFAULT_INCLUDES;
50+
}
3451
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.data.jdbc;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.core.type.classreading.MetadataReader;
21+
import org.springframework.core.type.classreading.MetadataReaderFactory;
22+
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
23+
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
24+
25+
import java.io.IOException;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link DataJdbcTypeExcludeFilter}.
31+
*
32+
* @author Ravi Undupitiya
33+
*/
34+
public class DataJdbcTypeExcludeFilterTests {
35+
36+
private MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
37+
38+
@Test
39+
void matchNotUsingDefaultFilters() throws Exception {
40+
DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(NotUsingDefaultFilters.class);
41+
assertThat(excludes(filter, AbstractJdbcConfiguration.class)).isTrue();
42+
}
43+
44+
@Test
45+
void matchUsingDefaultFilters() throws Exception {
46+
DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(UsingDefaultFilters.class);
47+
assertThat(excludes(filter, AbstractJdbcConfiguration.class)).isFalse();
48+
}
49+
50+
private boolean excludes(DataJdbcTypeExcludeFilter filter, Class<?> type) throws IOException {
51+
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(type.getName());
52+
return filter.match(metadataReader, this.metadataReaderFactory);
53+
}
54+
55+
@DataJdbcTest
56+
static class UsingDefaultFilters {
57+
58+
}
59+
60+
@DataJdbcTest(useDefaultFilters = false)
61+
static class NotUsingDefaultFilters {
62+
63+
}
64+
65+
}

0 commit comments

Comments
 (0)