Skip to content

Commit a59cf66

Browse files
committed
Merge pull request #44354 from quaff
* gh-44354: Polish "Introduce TestSliceTestContextBootstrapper for test slices" Introduce TestSliceTestContextBootstrapper for test slices Closes gh-44354
2 parents 8bb5c27 + 53464f2 commit a59cf66

20 files changed

+99
-177
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-present 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;
18+
19+
import java.lang.annotation.Annotation;
20+
21+
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
22+
import org.springframework.core.ResolvableType;
23+
import org.springframework.core.annotation.MergedAnnotation;
24+
import org.springframework.core.annotation.MergedAnnotations;
25+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
26+
import org.springframework.test.context.TestContextAnnotationUtils;
27+
import org.springframework.test.context.TestContextBootstrapper;
28+
import org.springframework.util.Assert;
29+
30+
/**
31+
* Base class for test slice {@link TestContextBootstrapper test context bootstrappers}.
32+
*
33+
* @param <T> the test slice annotation
34+
* @author Yanming Zhou
35+
* @since 4.0.0
36+
*/
37+
public abstract class TestSliceTestContextBootstrapper<T extends Annotation> extends SpringBootTestContextBootstrapper {
38+
39+
private final Class<T> annotationType;
40+
41+
@SuppressWarnings("unchecked")
42+
protected TestSliceTestContextBootstrapper() {
43+
this.annotationType = (Class<T>) ResolvableType.forClass(getClass())
44+
.as(TestSliceTestContextBootstrapper.class)
45+
.getGeneric(0)
46+
.resolve();
47+
Assert.notNull(this.annotationType, "'%s' doesn't contain type parameter of '%s'"
48+
.formatted(getClass().getName(), TestSliceTestContextBootstrapper.class.getName()));
49+
}
50+
51+
@Override
52+
protected String[] getProperties(Class<?> testClass) {
53+
MergedAnnotation<T> annotation = MergedAnnotations.search(SearchStrategy.TYPE_HIERARCHY)
54+
.withEnclosingClasses(TestContextAnnotationUtils::searchEnclosingClass)
55+
.from(testClass)
56+
.get(this.annotationType);
57+
return annotation.isPresent() ? annotation.getStringArray("properties") : null;
58+
}
59+
60+
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/cassandra/DataCassandraTestContextBootstrapper.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.cassandra;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
@@ -26,13 +25,6 @@
2625
*
2726
* @author Artsiom Yudovin
2827
*/
29-
class DataCassandraTestContextBootstrapper extends SpringBootTestContextBootstrapper {
30-
31-
@Override
32-
protected String[] getProperties(Class<?> testClass) {
33-
DataCassandraTest dataCassandraTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
34-
DataCassandraTest.class);
35-
return (dataCassandraTest != null) ? dataCassandraTest.properties() : null;
36-
}
28+
class DataCassandraTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataCassandraTest> {
3729

3830
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestContextBootstrapper.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.couchbase;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
@@ -26,13 +25,6 @@
2625
*
2726
* @author Eddú Meléndez
2827
*/
29-
class DataCouchbaseTestContextBootstrapper extends SpringBootTestContextBootstrapper {
30-
31-
@Override
32-
protected String[] getProperties(Class<?> testClass) {
33-
DataCouchbaseTest dataCouchbaseTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
34-
DataCouchbaseTest.class);
35-
return (dataCouchbaseTest != null) ? dataCouchbaseTest.properties() : null;
36-
}
28+
class DataCouchbaseTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataCouchbaseTest> {
3729

3830
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/elasticsearch/DataElasticsearchTestContextBootstrapper.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.elasticsearch;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
@@ -26,13 +25,6 @@
2625
*
2726
* @author Eddú Meléndez
2827
*/
29-
class DataElasticsearchTestContextBootstrapper extends SpringBootTestContextBootstrapper {
30-
31-
@Override
32-
protected String[] getProperties(Class<?> testClass) {
33-
DataElasticsearchTest dataElasticsearchTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
34-
DataElasticsearchTest.class);
35-
return (dataElasticsearchTest != null) ? dataElasticsearchTest.properties() : null;
36-
}
28+
class DataElasticsearchTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataElasticsearchTest> {
3729

3830
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.jdbc;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataJdbcTest @DataJdbcTest} support.
2524
*
2625
* @author Andy Wilkinson
2726
*/
28-
class DataJdbcTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataJdbcTest dataJdbcTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataJdbcTest.class);
33-
return (dataJdbcTest != null) ? dataJdbcTest.properties() : null;
34-
}
27+
class DataJdbcTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataJdbcTest> {
3528

3629
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTestContextBootstrapper.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.ldap;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataLdapTest @DataLdapTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class DataLdapTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataLdapTest dataLdapTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataLdapTest.class);
33-
return (dataLdapTest != null) ? dataLdapTest.properties() : null;
34-
}
27+
class DataLdapTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataLdapTest> {
3528

3629
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestContextBootstrapper.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.mongo;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataMongoTest @DataMongoTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class DataMongoTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataMongoTest dataMongoTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataMongoTest.class);
33-
return (dataMongoTest != null) ? dataMongoTest.properties() : null;
34-
}
27+
class DataMongoTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataMongoTest> {
3528

3629
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestContextBootstrapper.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.neo4j;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataNeo4jTest @DataNeo4jTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class DataNeo4jTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataNeo4jTest dataNeo4jTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataNeo4jTest.class);
33-
return (dataNeo4jTest != null) ? dataNeo4jTest.properties() : null;
34-
}
27+
class DataNeo4jTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataNeo4jTest> {
3528

3629
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/r2dbc/DataR2dbcTestContextBootstrapper.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.r2dbc;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataR2dbcTest @DataR2dbcTest} support.
2524
*
2625
* @author Mark Paluch
2726
*/
28-
class DataR2dbcTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataR2dbcTest dataR2dbcTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataR2dbcTest.class);
33-
return (dataR2dbcTest != null) ? dataR2dbcTest.properties() : null;
34-
}
27+
class DataR2dbcTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataR2dbcTest> {
3528

3629
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestContextBootstrapper.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.redis;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataRedisTest @DataRedisTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class DataRedisTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataRedisTest dataRedisTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataRedisTest.class);
33-
return (dataRedisTest != null) ? dataRedisTest.properties() : null;
34-
}
27+
class DataRedisTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataRedisTest> {
3528

3629
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/graphql/GraphQlTestContextBootstrapper.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.graphql;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link GraphQlTest @GraphQlTest}.
2524
*
2625
* @author Brian Clozel
2726
*/
28-
class GraphQlTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
GraphQlTest graphQlTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, GraphQlTest.class);
33-
return (graphQlTest != null) ? graphQlTest.properties() : null;
34-
}
27+
class GraphQlTestContextBootstrapper extends TestSliceTestContextBootstrapper<GraphQlTest> {
3528

3629
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,15 @@
1616

1717
package org.springframework.boot.test.autoconfigure.jdbc;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link JdbcTest @JdbcTest} support.
2524
*
2625
* @author Artsiom Yudovin
26+
* @author Yanming Zhou
2727
*/
28-
class JdbcTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
JdbcTest jdbcTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, JdbcTest.class);
33-
return (jdbcTest != null) ? jdbcTest.properties() : null;
34-
}
28+
class JdbcTestContextBootstrapper extends TestSliceTestContextBootstrapper<JdbcTest> {
3529

3630
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jooq/JooqTestContextBootstrapper.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.jooq;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link JooqTest @JooqTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class JooqTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
JooqTest jooqTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, JooqTest.class);
33-
return (jooqTest != null) ? jooqTest.properties() : null;
34-
}
27+
class JooqTestContextBootstrapper extends TestSliceTestContextBootstrapper<JooqTest> {
3528

3629
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTestContextBootstrapper.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.json;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link JsonTest @JsonTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class JsonTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
JsonTest jsonTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, JsonTest.class);
33-
return (jsonTest != null) ? jsonTest.properties() : null;
34-
}
27+
class JsonTestContextBootstrapper extends TestSliceTestContextBootstrapper<JsonTest> {
3528

3629
}

0 commit comments

Comments
 (0)