Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
bclozel committed Jan 23, 2018
1 parent 1f4a32f commit 145d46e
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 387 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,11 +21,11 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.After;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Test;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisNode;
Expand All @@ -52,138 +52,169 @@
*/
public class RedisAutoConfigurationTests {

private AnnotationConfigApplicationContext context;

@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class));

@Test
public void testDefaultRedisConfiguration() {
load();
assertThat(this.context.getBean("redisTemplate", RedisOperations.class))
.isNotNull();
assertThat(this.context.getBean(StringRedisTemplate.class)).isNotNull();
this.contextRunner.run((context) -> {
assertThat(context.getBean("redisTemplate", RedisOperations.class))
.isNotNull();
assertThat(context.getBean(StringRedisTemplate.class)).isNotNull();
});
}

@Test
public void testOverrideRedisConfiguration() {
load("spring.redis.host:foo", "spring.redis.database:1",
"spring.redis.lettuce.shutdown-timeout:500");
LettuceConnectionFactory cf = this.context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getDatabase()).isEqualTo(1);
assertThat(cf.getPassword()).isNull();
assertThat(cf.isUseSsl()).isFalse();
assertThat(cf.getShutdownTimeout()).isEqualTo(500);
this.contextRunner
.withPropertyValues("spring.redis.host:foo",
"spring.redis.database:1",
"spring.redis.lettuce.shutdown-timeout:500")
.run((context) -> {
LettuceConnectionFactory cf = context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getDatabase()).isEqualTo(1);
assertThat(cf.getPassword()).isNull();
assertThat(cf.isUseSsl()).isFalse();
assertThat(cf.getShutdownTimeout()).isEqualTo(500);
});
}

@Test
public void testCustomizeRedisConfiguration() {
load(CustomConfiguration.class);
LettuceConnectionFactory cf = this.context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.isUseSsl()).isTrue();
this.contextRunner
.withUserConfiguration(CustomConfiguration.class)
.run((context) -> {
LettuceConnectionFactory cf = context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.isUseSsl()).isTrue();
});
}

@Test
public void testRedisUrlConfiguration() {
load("spring.redis.host:foo",
"spring.redis.url:redis://user:password@example:33");
LettuceConnectionFactory cf = this.context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("example");
assertThat(cf.getPort()).isEqualTo(33);
assertThat(cf.getPassword()).isEqualTo("password");
assertThat(cf.isUseSsl()).isFalse();
this.contextRunner
.withPropertyValues("spring.redis.host:foo",
"spring.redis.url:redis://user:password@example:33")
.run((context) -> {
LettuceConnectionFactory cf = context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("example");
assertThat(cf.getPort()).isEqualTo(33);
assertThat(cf.getPassword()).isEqualTo("password");
assertThat(cf.isUseSsl()).isFalse();
});
}

@Test
public void testOverrideUrlRedisConfiguration() {
load("spring.redis.host:foo", "spring.redis.password:xyz",
"spring.redis.port:1000", "spring.redis.ssl:false",
"spring.redis.url:rediss://user:password@example:33");
LettuceConnectionFactory cf = this.context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("example");
assertThat(cf.getPort()).isEqualTo(33);
assertThat(cf.getPassword()).isEqualTo("password");
assertThat(cf.isUseSsl()).isTrue();
this.contextRunner
.withPropertyValues("spring.redis.host:foo", "spring.redis.password:xyz",
"spring.redis.port:1000", "spring.redis.ssl:false",
"spring.redis.url:rediss://user:password@example:33")
.run((context) -> {
LettuceConnectionFactory cf = context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("example");
assertThat(cf.getPort()).isEqualTo(33);
assertThat(cf.getPassword()).isEqualTo("password");
assertThat(cf.isUseSsl()).isTrue();
});
}

@Test
public void testRedisConfigurationWithPool() {
load("spring.redis.host:foo", "spring.redis.lettuce.pool.min-idle:1",
"spring.redis.lettuce.pool.max-idle:4",
"spring.redis.lettuce.pool.max-active:16",
"spring.redis.lettuce.pool.max-wait:2000",
"spring.redis.lettuce.shutdown-timeout:1000");
LettuceConnectionFactory cf = this.context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(getPoolingClientConfiguration(cf).getPoolConfig().getMinIdle())
.isEqualTo(1);
assertThat(getPoolingClientConfiguration(cf).getPoolConfig().getMaxIdle())
.isEqualTo(4);
assertThat(getPoolingClientConfiguration(cf).getPoolConfig().getMaxTotal())
.isEqualTo(16);
assertThat(getPoolingClientConfiguration(cf).getPoolConfig().getMaxWaitMillis())
.isEqualTo(2000);
assertThat(cf.getShutdownTimeout()).isEqualTo(1000);
this.contextRunner
.withPropertyValues("spring.redis.host:foo",
"spring.redis.lettuce.pool.min-idle:1",
"spring.redis.lettuce.pool.max-idle:4",
"spring.redis.lettuce.pool.max-active:16",
"spring.redis.lettuce.pool.max-wait:2000",
"spring.redis.lettuce.shutdown-timeout:1000")
.run((context) -> {
LettuceConnectionFactory cf = context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
GenericObjectPoolConfig poolConfig =
getPoolingClientConfiguration(cf).getPoolConfig();
assertThat(poolConfig.getMinIdle()).isEqualTo(1);
assertThat(poolConfig.getMaxIdle()).isEqualTo(4);
assertThat(poolConfig.getMaxTotal()).isEqualTo(16);
assertThat(poolConfig.getMaxWaitMillis()).isEqualTo(2000);
assertThat(cf.getShutdownTimeout()).isEqualTo(1000);
});
}

@Test
public void testRedisConfigurationWithTimeout() {
load("spring.redis.host:foo", "spring.redis.timeout:100");
LettuceConnectionFactory cf = this.context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getTimeout()).isEqualTo(100);
this.contextRunner
.withPropertyValues("spring.redis.host:foo",
"spring.redis.timeout:100")
.run((context) -> {
LettuceConnectionFactory cf = context
.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getTimeout()).isEqualTo(100);
});
}

@Test
public void testRedisConfigurationWithSentinel() {
List<String> sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");
load("spring.redis.sentinel.master:mymaster", "spring.redis.sentinel.nodes:"
+ StringUtils.collectionToCommaDelimitedString(sentinels));
assertThat(this.context.getBean(LettuceConnectionFactory.class)
.isRedisSentinelAware()).isTrue();
this.contextRunner
.withPropertyValues("spring.redis.sentinel.master:mymaster",
"spring.redis.sentinel.nodes:"
+ StringUtils.collectionToCommaDelimitedString(sentinels))
.run((context) -> {
assertThat(context.getBean(LettuceConnectionFactory.class)
.isRedisSentinelAware()).isTrue();

});
}

@Test
public void testRedisConfigurationWithSentinelAndPassword() {
load("spring.redis.password=password", "spring.redis.sentinel.master:mymaster",
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380");
LettuceConnectionFactory connectionFactory = this.context
.getBean(LettuceConnectionFactory.class);
assertThat(connectionFactory.getPassword()).isEqualTo("password");
Set<RedisNode> sentinels = connectionFactory.getSentinelConfiguration()
.getSentinels();
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
.contains("127.0.0.1:26379", "127.0.0.1:26380");
this.contextRunner
.withPropertyValues("spring.redis.password=password",
"spring.redis.sentinel.master:mymaster",
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380")
.run((context) -> {
LettuceConnectionFactory connectionFactory = context
.getBean(LettuceConnectionFactory.class);
assertThat(connectionFactory.getPassword()).isEqualTo("password");
Set<RedisNode> sentinels = connectionFactory.getSentinelConfiguration()
.getSentinels();
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
.contains("127.0.0.1:26379", "127.0.0.1:26380");
});
}

@Test
public void testRedisConfigurationWithCluster() {
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
load("spring.redis.cluster.nodes[0]:" + clusterNodes.get(0),
"spring.redis.cluster.nodes[1]:" + clusterNodes.get(1));
assertThat(this.context.getBean(LettuceConnectionFactory.class)
.getClusterConnection()).isNotNull();
this.contextRunner
.withPropertyValues("spring.redis.cluster.nodes[0]:" + clusterNodes.get(0),
"spring.redis.cluster.nodes[1]:" + clusterNodes.get(1))
.run((context) -> {
assertThat(context.getBean(LettuceConnectionFactory.class)
.getClusterConnection()).isNotNull();

});
}

@Test
public void testRedisConfigurationWithClusterAndPassword() {
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
load("spring.redis.password=password",
"spring.redis.cluster.nodes[0]:" + clusterNodes.get(0),
"spring.redis.cluster.nodes[1]:" + clusterNodes.get(1));
assertThat(this.context.getBean(LettuceConnectionFactory.class).getPassword())
.isEqualTo("password");
this.contextRunner
.withPropertyValues("spring.redis.password=password",
"spring.redis.cluster.nodes[0]:" + clusterNodes.get(0),
"spring.redis.cluster.nodes[1]:" + clusterNodes.get(1))
.run((context) -> {
assertThat(context.getBean(LettuceConnectionFactory.class).getPassword())
.isEqualTo("password");

});
}

private LettucePoolingClientConfiguration getPoolingClientConfiguration(
Expand All @@ -192,21 +223,6 @@ private LettucePoolingClientConfiguration getPoolingClientConfiguration(
"clientConfiguration");
}

private void load(String... environment) {
load(null, environment);
}

private void load(Class<?> config, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
TestPropertyValues.of(environment).applyTo(ctx);
if (config != null) {
ctx.register(config);
}
ctx.register(RedisAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}

@Configuration
static class CustomConfiguration {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,11 +18,10 @@

import java.util.Map;

import org.junit.After;
import org.junit.Test;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.data.redis.core.ReactiveRedisTemplate;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -34,28 +33,16 @@
*/
public class RedisReactiveAutoConfigurationTests {

private AnnotationConfigApplicationContext context;

@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations
.of(RedisAutoConfiguration.class, RedisReactiveAutoConfiguration.class));

@Test
public void testDefaultRedisConfiguration() {
load();
Map<String, ?> beans = this.context.getBeansOfType(ReactiveRedisTemplate.class);
assertThat(beans).containsOnlyKeys("reactiveRedisTemplate");
}

private void load(String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
TestPropertyValues.of(environment).applyTo(ctx);
ctx.register(RedisAutoConfiguration.class, RedisReactiveAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
this.contextRunner.run((context) -> {
Map<String, ?> beans = context.getBeansOfType(ReactiveRedisTemplate.class);
assertThat(beans).containsOnlyKeys("reactiveRedisTemplate");
});
}

}

0 comments on commit 145d46e

Please sign in to comment.