Skip to content

Commit dd0ce54

Browse files
wilkinsonasnicoll
authored andcommitted
Improve the type-safety of ContextLoader for servlet and reactive web
1 parent 19ddfad commit dd0ce54

File tree

18 files changed

+1240
-960
lines changed

18 files changed

+1240
-960
lines changed

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.springframework.boot.context.properties.EnableConfigurationProperties;
5959
import org.springframework.boot.test.context.ContextConsumer;
6060
import org.springframework.boot.test.context.ContextLoader;
61+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6162
import org.springframework.context.annotation.Bean;
6263
import org.springframework.context.annotation.Configuration;
6364
import org.springframework.data.cassandra.core.CassandraOperations;
@@ -79,8 +80,9 @@
7980
*/
8081
public class HealthIndicatorAutoConfigurationTests {
8182

82-
public final ContextLoader contextLoader = new ContextLoader().autoConfig(
83-
HealthIndicatorAutoConfiguration.class, ManagementServerProperties.class);
83+
public final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
84+
.standard().autoConfig(HealthIndicatorAutoConfiguration.class,
85+
ManagementServerProperties.class);
8486

8587
@Test
8688
public void defaultHealthIndicator() {
@@ -378,7 +380,7 @@ public void notNeo4jHealthIndicator() throws Exception {
378380
.load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
379381
}
380382

381-
private ContextConsumer hasSingleHealthIndicator(
383+
private ContextConsumer<AnnotationConfigApplicationContext> hasSingleHealthIndicator(
382384
Class<? extends HealthIndicator> type) {
383385
return context -> {
384386
Map<String, HealthIndicator> beans = context

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java

Lines changed: 310 additions & 273 deletions
Large diffs are not rendered by default.

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import org.springframework.beans.factory.BeanCreationException;
3131
import org.springframework.boot.test.context.ContextLoader;
32+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3233
import org.springframework.context.annotation.Bean;
3334
import org.springframework.context.annotation.Configuration;
3435

@@ -59,29 +60,36 @@ public static void close() {
5960
}
6061
}
6162

62-
private final ContextLoader contextLoader = new ContextLoader()
63-
.autoConfig(HazelcastAutoConfiguration.class);
63+
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
64+
.standard().autoConfig(HazelcastAutoConfiguration.class);
6465

6566
@Test
6667
public void systemProperty() throws IOException {
67-
this.contextLoader.systemProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY,
68-
"classpath:org/springframework/boot/autoconfigure/hazelcast/"
69-
+ "hazelcast-client-specific.xml").load(context -> {
70-
HazelcastInstance hazelcastInstance = context
71-
.getBean(HazelcastInstance.class);
72-
assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class);
73-
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
74-
});
68+
this.contextLoader
69+
.systemProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY,
70+
"classpath:org/springframework/boot/autoconfigure/hazelcast/"
71+
+ "hazelcast-client-specific.xml")
72+
.load(context -> {
73+
HazelcastInstance hazelcastInstance = context
74+
.getBean(HazelcastInstance.class);
75+
assertThat(hazelcastInstance)
76+
.isInstanceOf(HazelcastClientProxy.class);
77+
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
78+
});
7579
}
7680

7781
@Test
7882
public void explicitConfigFile() throws IOException {
79-
this.contextLoader.env("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
80-
+ "hazelcast/hazelcast-client-specific.xml").load(context -> {
81-
HazelcastInstance hazelcastInstance = context.getBean(HazelcastInstance.class);
82-
assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class);
83-
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
84-
});
83+
this.contextLoader
84+
.env("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
85+
+ "hazelcast/hazelcast-client-specific.xml")
86+
.load(context -> {
87+
HazelcastInstance hazelcastInstance = context
88+
.getBean(HazelcastInstance.class);
89+
assertThat(hazelcastInstance)
90+
.isInstanceOf(HazelcastClientProxy.class);
91+
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
92+
});
8593
}
8694

8795
@Test
@@ -90,26 +98,28 @@ public void explicitConfigUrl() throws IOException {
9098
.load(context -> {
9199
HazelcastInstance hazelcastInstance = context
92100
.getBean(HazelcastInstance.class);
93-
assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class);
101+
assertThat(hazelcastInstance)
102+
.isInstanceOf(HazelcastClientProxy.class);
94103
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
95104
});
96105
}
97106

98107
@Test
99108
public void unknownConfigFile() {
100-
this.contextLoader.env("spring.hazelcast.config=foo/bar/unknown.xml")
101-
.loadAndFail(BeanCreationException.class, ex ->
102-
assertThat(ex.getMessage()).contains("foo/bar/unknown.xml"));
109+
this.contextLoader.env("spring.hazelcast.config=foo/bar/unknown.xml").loadAndFail(
110+
BeanCreationException.class,
111+
ex -> assertThat(ex.getMessage()).contains("foo/bar/unknown.xml"));
103112
}
104113

105114
@Test
106115
public void clientConfigTakesPrecedence() {
107116
this.contextLoader.config(HazelcastServerAndClientConfig.class)
108117
.env("spring.hazelcast.config=this-is-ignored.xml").load(context -> {
109-
HazelcastInstance hazelcastInstance = context
110-
.getBean(HazelcastInstance.class);
111-
assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class);
112-
});
118+
HazelcastInstance hazelcastInstance = context
119+
.getBean(HazelcastInstance.class);
120+
assertThat(hazelcastInstance)
121+
.isInstanceOf(HazelcastClientProxy.class);
122+
});
113123
}
114124

115125
@Configuration

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.boot.test.context.ContextLoader;
3131
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
3232
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
33+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3334
import org.springframework.context.annotation.Bean;
3435
import org.springframework.context.annotation.Configuration;
3536
import org.springframework.core.io.ClassPathResource;
@@ -45,8 +46,8 @@
4546
@ClassPathExclusions("hazelcast-client-*.jar")
4647
public class HazelcastAutoConfigurationServerTests {
4748

48-
private final ContextLoader contextLoader = new ContextLoader()
49-
.autoConfig(HazelcastAutoConfiguration.class);
49+
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
50+
.standard().autoConfig(HazelcastAutoConfiguration.class);
5051

5152
@Test
5253
public void defaultConfigFile() throws IOException {
@@ -61,8 +62,9 @@ public void defaultConfigFile() throws IOException {
6162

6263
@Test
6364
public void systemProperty() throws IOException {
64-
this.contextLoader.systemProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY,
65-
"classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml")
65+
this.contextLoader
66+
.systemProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY,
67+
"classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml")
6668
.load(context -> {
6769

6870
HazelcastInstance hazelcastInstance = context
@@ -75,32 +77,36 @@ public void systemProperty() throws IOException {
7577

7678
@Test
7779
public void explicitConfigFile() throws IOException {
78-
this.contextLoader.env("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
79-
+ "hazelcast-specific.xml").load(context -> {
80-
HazelcastInstance hazelcastInstance = context
81-
.getBean(HazelcastInstance.class);
82-
assertThat(hazelcastInstance.getConfig().getConfigurationFile()).isEqualTo(
83-
new ClassPathResource("org/springframework/boot/autoconfigure/hazelcast"
84-
+ "/hazelcast-specific.xml").getFile());
85-
});
80+
this.contextLoader
81+
.env("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
82+
+ "hazelcast-specific.xml")
83+
.load(context -> {
84+
HazelcastInstance hazelcastInstance = context
85+
.getBean(HazelcastInstance.class);
86+
assertThat(hazelcastInstance.getConfig().getConfigurationFile())
87+
.isEqualTo(new ClassPathResource(
88+
"org/springframework/boot/autoconfigure/hazelcast"
89+
+ "/hazelcast-specific.xml").getFile());
90+
});
8691
}
8792

8893
@Test
8994
public void explicitConfigUrl() throws IOException {
90-
this.contextLoader
91-
.env("spring.hazelcast.config=hazelcast-default.xml").load(context -> {
92-
HazelcastInstance hazelcastInstance = context
93-
.getBean(HazelcastInstance.class);
94-
assertThat(hazelcastInstance.getConfig().getConfigurationUrl())
95-
.isEqualTo(new ClassPathResource("hazelcast-default.xml").getURL());
96-
});
95+
this.contextLoader.env("spring.hazelcast.config=hazelcast-default.xml")
96+
.load(context -> {
97+
HazelcastInstance hazelcastInstance = context
98+
.getBean(HazelcastInstance.class);
99+
assertThat(hazelcastInstance.getConfig().getConfigurationUrl())
100+
.isEqualTo(new ClassPathResource("hazelcast-default.xml")
101+
.getURL());
102+
});
97103
}
98104

99105
@Test
100106
public void unknownConfigFile() {
101-
this.contextLoader.env("spring.hazelcast.config=foo/bar/unknown.xml")
102-
.loadAndFail(BeanCreationException.class, ex ->
103-
assertThat(ex.getMessage()).contains("foo/bar/unknown.xml"));
107+
this.contextLoader.env("spring.hazelcast.config=foo/bar/unknown.xml").loadAndFail(
108+
BeanCreationException.class,
109+
ex -> assertThat(ex.getMessage()).contains("foo/bar/unknown.xml"));
104110
}
105111

106112
@Test
@@ -109,15 +115,16 @@ public void configInstanceWithName() {
109115
HazelcastInstance existingHazelcastInstance = Hazelcast
110116
.newHazelcastInstance(config);
111117
try {
112-
this.contextLoader.config(HazelcastConfigWithName.class).env(
113-
"spring.hazelcast.config=this-is-ignored.xml").load(context -> {
114-
HazelcastInstance hazelcastInstance = context
115-
.getBean(HazelcastInstance.class);
116-
assertThat(hazelcastInstance.getConfig().getInstanceName())
117-
.isEqualTo("my-test-instance");
118-
// Should reuse any existing instance by default.
119-
assertThat(hazelcastInstance).isEqualTo(existingHazelcastInstance);
120-
});
118+
this.contextLoader.config(HazelcastConfigWithName.class)
119+
.env("spring.hazelcast.config=this-is-ignored.xml").load(context -> {
120+
HazelcastInstance hazelcastInstance = context
121+
.getBean(HazelcastInstance.class);
122+
assertThat(hazelcastInstance.getConfig().getInstanceName())
123+
.isEqualTo("my-test-instance");
124+
// Should reuse any existing instance by default.
125+
assertThat(hazelcastInstance)
126+
.isEqualTo(existingHazelcastInstance);
127+
});
121128
}
122129
finally {
123130
existingHazelcastInstance.shutdown();
@@ -128,12 +135,12 @@ public void configInstanceWithName() {
128135
public void configInstanceWithoutName() {
129136
this.contextLoader.config(HazelcastConfigNoName.class)
130137
.env("spring.hazelcast.config=this-is-ignored.xml").load(context -> {
131-
HazelcastInstance hazelcastInstance = context
132-
.getBean(HazelcastInstance.class);
133-
Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig()
134-
.getQueueConfigs();
135-
assertThat(queueConfigs).hasSize(1).containsKey("another-queue");
136-
});
138+
HazelcastInstance hazelcastInstance = context
139+
.getBean(HazelcastInstance.class);
140+
Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig()
141+
.getQueueConfigs();
142+
assertThat(queueConfigs).hasSize(1).containsKey("another-queue");
143+
});
137144
}
138145

139146
@Configuration

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Test;
2323

2424
import org.springframework.boot.test.context.ContextLoader;
25+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2526
import org.springframework.core.io.ClassPathResource;
2627

2728
import static org.assertj.core.api.Assertions.assertThat;
@@ -33,14 +34,15 @@
3334
*/
3435
public class HazelcastAutoConfigurationTests {
3536

36-
private final ContextLoader contextLoader = new ContextLoader()
37-
.autoConfig(HazelcastAutoConfiguration.class);
37+
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
38+
.standard().autoConfig(HazelcastAutoConfiguration.class);
3839

3940
@Test
4041
public void defaultConfigFile() throws IOException {
4142
// no hazelcast-client.xml and hazelcast.xml is present in root classpath
4243
this.contextLoader.load(context -> {
43-
HazelcastInstance hazelcastInstance = context.getBean(HazelcastInstance.class);
44+
HazelcastInstance hazelcastInstance = context
45+
.getBean(HazelcastInstance.class);
4446
assertThat(hazelcastInstance.getConfig().getConfigurationUrl())
4547
.isEqualTo(new ClassPathResource("hazelcast.xml").getURL());
4648
});

0 commit comments

Comments
 (0)