|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.data.redis;
|
18 | 18 |
|
19 |
| -import java.util.Arrays; |
20 |
| -import java.util.List; |
21 |
| - |
22 |
| -import org.junit.After; |
23 | 19 | import org.junit.Test;
|
24 | 20 | import org.junit.runner.RunWith;
|
25 | 21 |
|
26 |
| -import org.springframework.boot.test.util.TestPropertyValues; |
| 22 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 23 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
27 | 24 | import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
|
28 | 25 | import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
|
29 |
| -import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
30 | 26 | import org.springframework.context.annotation.Bean;
|
31 | 27 | import org.springframework.context.annotation.Configuration;
|
32 | 28 | import org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder;
|
33 | 29 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
34 |
| -import org.springframework.util.StringUtils; |
35 | 30 |
|
36 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
37 | 32 |
|
|
45 | 40 | @ClassPathExclusions("lettuce-core-*.jar")
|
46 | 41 | public class RedisAutoConfigurationJedisTests {
|
47 | 42 |
|
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)); |
56 | 45 |
|
57 | 46 | @Test
|
58 | 47 | 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 | + }); |
65 | 57 | }
|
66 | 58 |
|
67 | 59 | @Test
|
68 | 60 | 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 | + }); |
72 | 65 | }
|
73 | 66 |
|
74 | 67 | @Test
|
75 | 68 | 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 | + }); |
83 | 80 | }
|
84 | 81 |
|
85 | 82 | @Test
|
86 | 83 | 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 | + }); |
95 | 96 | }
|
96 | 97 |
|
97 | 98 | @Test
|
98 | 99 | 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 | + }); |
106 | 111 | }
|
107 | 112 |
|
108 | 113 | @Test
|
109 | 114 | 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 | + }); |
117 | 127 | }
|
118 | 128 |
|
119 | 129 | @Test
|
120 | 130 | 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", |
122 | 133 | "spring.redis.jedis.pool.max-idle:4",
|
123 | 134 | "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 | + }); |
131 | 144 | }
|
132 | 145 |
|
133 | 146 | @Test
|
134 | 147 | 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 | + }); |
139 | 156 | }
|
140 | 157 |
|
141 | 158 | @Test
|
142 | 159 | 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()); |
149 | 165 | }
|
150 | 166 |
|
151 | 167 | @Test
|
152 | 168 | 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")); |
159 | 176 | }
|
160 | 177 |
|
161 | 178 | @Test
|
162 | 179 | 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()); |
184 | 185 | }
|
185 | 186 |
|
186 | 187 | @Configuration
|
|
0 commit comments