Skip to content

Commit 9a87424

Browse files
committed
Use ApplicationContextRunner in RedisAutoConfigurationJedisTests
1 parent 8a123d3 commit 9a87424

File tree

1 file changed

+99
-98
lines changed

1 file changed

+99
-98
lines changed

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

Lines changed: 99 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,17 @@
1616

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

19-
import java.util.Arrays;
20-
import java.util.List;
21-
22-
import org.junit.After;
2319
import org.junit.Test;
2420
import org.junit.runner.RunWith;
2521

26-
import org.springframework.boot.test.util.TestPropertyValues;
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2724
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
2825
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
29-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3026
import org.springframework.context.annotation.Bean;
3127
import org.springframework.context.annotation.Configuration;
3228
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder;
3329
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
34-
import org.springframework.util.StringUtils;
3530

3631
import static org.assertj.core.api.Assertions.assertThat;
3732

@@ -45,142 +40,148 @@
4540
@ClassPathExclusions("lettuce-core-*.jar")
4641
public class RedisAutoConfigurationJedisTests {
4742

48-
private AnnotationConfigApplicationContext context;
49-
50-
@After
51-
public void close() {
52-
if (this.context != null) {
53-
this.context.close();
54-
}
55-
}
43+
private final ApplicationContextRunner runner = new ApplicationContextRunner()
44+
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class));
5645

5746
@Test
5847
public void testOverrideRedisConfiguration() {
59-
load("spring.redis.host:foo", "spring.redis.database:1");
60-
JedisConnectionFactory cf = this.context.getBean(JedisConnectionFactory.class);
61-
assertThat(cf.getHostName()).isEqualTo("foo");
62-
assertThat(cf.getDatabase()).isEqualTo(1);
63-
assertThat(cf.getPassword()).isNull();
64-
assertThat(cf.isUseSsl()).isFalse();
48+
this.runner.withPropertyValues("spring.redis.host:foo", "spring.redis.database:1")
49+
.run((context) -> {
50+
JedisConnectionFactory cf = context
51+
.getBean(JedisConnectionFactory.class);
52+
assertThat(cf.getHostName()).isEqualTo("foo");
53+
assertThat(cf.getDatabase()).isEqualTo(1);
54+
assertThat(cf.getPassword()).isNull();
55+
assertThat(cf.isUseSsl()).isFalse();
56+
});
6557
}
6658

6759
@Test
6860
public void testCustomizeRedisConfiguration() {
69-
load(CustomConfiguration.class);
70-
JedisConnectionFactory cf = this.context.getBean(JedisConnectionFactory.class);
71-
assertThat(cf.isUseSsl()).isTrue();
61+
this.runner.withUserConfiguration(CustomConfiguration.class).run((context) -> {
62+
JedisConnectionFactory cf = context.getBean(JedisConnectionFactory.class);
63+
assertThat(cf.isUseSsl()).isTrue();
64+
});
7265
}
7366

7467
@Test
7568
public void testRedisUrlConfiguration() {
76-
load("spring.redis.host:foo",
77-
"spring.redis.url:redis://user:password@example:33");
78-
JedisConnectionFactory cf = this.context.getBean(JedisConnectionFactory.class);
79-
assertThat(cf.getHostName()).isEqualTo("example");
80-
assertThat(cf.getPort()).isEqualTo(33);
81-
assertThat(cf.getPassword()).isEqualTo("password");
82-
assertThat(cf.isUseSsl()).isFalse();
69+
this.runner
70+
.withPropertyValues("spring.redis.host:foo",
71+
"spring.redis.url:redis://user:password@example:33")
72+
.run((context) -> {
73+
JedisConnectionFactory cf = context
74+
.getBean(JedisConnectionFactory.class);
75+
assertThat(cf.getHostName()).isEqualTo("example");
76+
assertThat(cf.getPort()).isEqualTo(33);
77+
assertThat(cf.getPassword()).isEqualTo("password");
78+
assertThat(cf.isUseSsl()).isFalse();
79+
});
8380
}
8481

8582
@Test
8683
public void testOverrideUrlRedisConfiguration() {
87-
load("spring.redis.host:foo", "spring.redis.password:xyz",
88-
"spring.redis.port:1000", "spring.redis.ssl:false",
89-
"spring.redis.url:rediss://user:password@example:33");
90-
JedisConnectionFactory cf = this.context.getBean(JedisConnectionFactory.class);
91-
assertThat(cf.getHostName()).isEqualTo("example");
92-
assertThat(cf.getPort()).isEqualTo(33);
93-
assertThat(cf.getPassword()).isEqualTo("password");
94-
assertThat(cf.isUseSsl()).isTrue();
84+
this.runner
85+
.withPropertyValues("spring.redis.host:foo", "spring.redis.password:xyz",
86+
"spring.redis.port:1000", "spring.redis.ssl:false",
87+
"spring.redis.url:rediss://user:password@example:33")
88+
.run((context) -> {
89+
JedisConnectionFactory cf = context
90+
.getBean(JedisConnectionFactory.class);
91+
assertThat(cf.getHostName()).isEqualTo("example");
92+
assertThat(cf.getPort()).isEqualTo(33);
93+
assertThat(cf.getPassword()).isEqualTo("password");
94+
assertThat(cf.isUseSsl()).isTrue();
95+
});
9596
}
9697

9798
@Test
9899
public void testPasswordInUrlWithColon() {
99-
load("spring.redis.url:redis://:pass:word@example:33");
100-
assertThat(this.context.getBean(JedisConnectionFactory.class).getHostName())
101-
.isEqualTo("example");
102-
assertThat(this.context.getBean(JedisConnectionFactory.class).getPort())
103-
.isEqualTo(33);
104-
assertThat(this.context.getBean(JedisConnectionFactory.class).getPassword())
105-
.isEqualTo("pass:word");
100+
this.runner.withPropertyValues("spring.redis.url:redis://:pass:word@example:33")
101+
.run((context) -> {
102+
assertThat(
103+
context.getBean(JedisConnectionFactory.class).getHostName())
104+
.isEqualTo("example");
105+
assertThat(context.getBean(JedisConnectionFactory.class).getPort())
106+
.isEqualTo(33);
107+
assertThat(
108+
context.getBean(JedisConnectionFactory.class).getPassword())
109+
.isEqualTo("pass:word");
110+
});
106111
}
107112

108113
@Test
109114
public void testPasswordInUrlStartsWithColon() {
110-
load("spring.redis.url:redis://user::pass:word@example:33");
111-
assertThat(this.context.getBean(JedisConnectionFactory.class).getHostName())
112-
.isEqualTo("example");
113-
assertThat(this.context.getBean(JedisConnectionFactory.class).getPort())
114-
.isEqualTo(33);
115-
assertThat(this.context.getBean(JedisConnectionFactory.class).getPassword())
116-
.isEqualTo(":pass:word");
115+
this.runner
116+
.withPropertyValues("spring.redis.url:redis://user::pass:word@example:33")
117+
.run((context) -> {
118+
assertThat(
119+
context.getBean(JedisConnectionFactory.class).getHostName())
120+
.isEqualTo("example");
121+
assertThat(context.getBean(JedisConnectionFactory.class).getPort())
122+
.isEqualTo(33);
123+
assertThat(
124+
context.getBean(JedisConnectionFactory.class).getPassword())
125+
.isEqualTo(":pass:word");
126+
});
117127
}
118128

119129
@Test
120130
public void testRedisConfigurationWithPool() {
121-
load("spring.redis.host:foo", "spring.redis.jedis.pool.min-idle:1",
131+
this.runner.withPropertyValues("spring.redis.host:foo",
132+
"spring.redis.jedis.pool.min-idle:1",
122133
"spring.redis.jedis.pool.max-idle:4",
123134
"spring.redis.jedis.pool.max-active:16",
124-
"spring.redis.jedis.pool.max-wait:2000");
125-
JedisConnectionFactory cf = this.context.getBean(JedisConnectionFactory.class);
126-
assertThat(cf.getHostName()).isEqualTo("foo");
127-
assertThat(cf.getPoolConfig().getMinIdle()).isEqualTo(1);
128-
assertThat(cf.getPoolConfig().getMaxIdle()).isEqualTo(4);
129-
assertThat(cf.getPoolConfig().getMaxTotal()).isEqualTo(16);
130-
assertThat(cf.getPoolConfig().getMaxWaitMillis()).isEqualTo(2000);
135+
"spring.redis.jedis.pool.max-wait:2000").run((context) -> {
136+
JedisConnectionFactory cf = context
137+
.getBean(JedisConnectionFactory.class);
138+
assertThat(cf.getHostName()).isEqualTo("foo");
139+
assertThat(cf.getPoolConfig().getMinIdle()).isEqualTo(1);
140+
assertThat(cf.getPoolConfig().getMaxIdle()).isEqualTo(4);
141+
assertThat(cf.getPoolConfig().getMaxTotal()).isEqualTo(16);
142+
assertThat(cf.getPoolConfig().getMaxWaitMillis()).isEqualTo(2000);
143+
});
131144
}
132145

133146
@Test
134147
public void testRedisConfigurationWithTimeout() {
135-
load("spring.redis.host:foo", "spring.redis.timeout:100");
136-
JedisConnectionFactory cf = this.context.getBean(JedisConnectionFactory.class);
137-
assertThat(cf.getHostName()).isEqualTo("foo");
138-
assertThat(cf.getTimeout()).isEqualTo(100);
148+
this.runner
149+
.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:100")
150+
.run((context) -> {
151+
JedisConnectionFactory cf = context
152+
.getBean(JedisConnectionFactory.class);
153+
assertThat(cf.getHostName()).isEqualTo("foo");
154+
assertThat(cf.getTimeout()).isEqualTo(100);
155+
});
139156
}
140157

141158
@Test
142159
public void testRedisConfigurationWithSentinel() {
143-
List<String> sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");
144-
load("spring.redis.sentinel.master:mymaster", "spring.redis.sentinel.nodes:"
145-
+ StringUtils.collectionToCommaDelimitedString(sentinels));
146-
assertThat(
147-
this.context.getBean(JedisConnectionFactory.class).isRedisSentinelAware())
148-
.isTrue();
160+
this.runner
161+
.withPropertyValues("spring.redis.sentinel.master:mymaster",
162+
"spring.redis.sentinel.nodes:127.0.0.1:26379,127.0.0.1:26380")
163+
.run((context) -> assertThat(context.getBean(JedisConnectionFactory.class)
164+
.isRedisSentinelAware()).isTrue());
149165
}
150166

151167
@Test
152168
public void testRedisConfigurationWithSentinelAndPassword() {
153-
List<String> sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");
154-
load("spring.redis.password=password", "spring.redis.sentinel.master:mymaster",
155-
"spring.redis.sentinel.nodes:"
156-
+ StringUtils.collectionToCommaDelimitedString(sentinels));
157-
assertThat(this.context.getBean(JedisConnectionFactory.class).getPassword())
158-
.isEqualTo("password");
169+
this.runner
170+
.withPropertyValues("spring.redis.password=password",
171+
"spring.redis.sentinel.master:mymaster",
172+
"spring.redis.sentinel.nodes:127.0.0.1:26379,127.0.0.1:26380")
173+
.run((context) -> assertThat(
174+
context.getBean(JedisConnectionFactory.class).getPassword())
175+
.isEqualTo("password"));
159176
}
160177

161178
@Test
162179
public void testRedisConfigurationWithCluster() {
163-
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
164-
load("spring.redis.cluster.nodes[0]:" + clusterNodes.get(0),
165-
"spring.redis.cluster.nodes[1]:" + clusterNodes.get(1));
166-
assertThat(
167-
this.context.getBean(JedisConnectionFactory.class).getClusterConnection())
168-
.isNotNull();
169-
}
170-
171-
private void load(String... environment) {
172-
load(null, environment);
173-
}
174-
175-
private void load(Class<?> config, String... environment) {
176-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
177-
TestPropertyValues.of(environment).applyTo(context);
178-
if (config != null) {
179-
context.register(config);
180-
}
181-
context.register(RedisAutoConfiguration.class);
182-
context.refresh();
183-
this.context = context;
180+
this.runner
181+
.withPropertyValues(
182+
"spring.redis.cluster.nodes=127.0.0.1:27379,127.0.0.1:27380")
183+
.run((context) -> assertThat(context.getBean(JedisConnectionFactory.class)
184+
.getClusterConnection()).isNotNull());
184185
}
185186

186187
@Configuration

0 commit comments

Comments
 (0)