From 8b06c54246844be2c415dac3f2981cbe644430f6 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 7 Sep 2017 09:31:24 +0200 Subject: [PATCH] DATAREDIS-679 - Polishing. Use IllegalState- instead of IllegalArgumentException and utilize ExpectedException rule in tests. Original Pull Request: #270 --- .gitignore | 1 + .../connection/jedis/JedisClusterConnection.java | 7 ++++--- .../jedis/JedisClusterConnectionUnitTests.java | 11 +++++------ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index b0c75b8ac..f1fc2fba4 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ build out work *.rdb +*.aof diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterConnection.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterConnection.java index 2d2c88308..b7602a18f 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterConnection.java @@ -4107,8 +4107,8 @@ public class JedisClusterConnection implements RedisClusterConnection { this.topologyProvider = topologyProvider; if (cluster != null) { - PropertyAccessor accessor = new DirectFieldAccessFallbackBeanWrapper(cluster); + PropertyAccessor accessor = new DirectFieldAccessFallbackBeanWrapper(cluster); this.connectionHandler = accessor.isReadableProperty("connectionHandler") ? (JedisClusterConnectionHandler) accessor.getPropertyValue("connectionHandler") : null; } else { @@ -4137,7 +4137,7 @@ public class JedisClusterConnection implements RedisClusterConnection { return connection; } - throw new IllegalArgumentException(String.format("Node %s is unknown to cluster", node)); + throw new IllegalStateException(String.format("Node %s is unknown to cluster", node)); } private JedisPool getResourcePoolForSpecificNode(RedisClusterNode node) { @@ -4153,7 +4153,8 @@ public class JedisClusterConnection implements RedisClusterConnection { private Jedis getConnectionForSpecificNode(RedisClusterNode node) { RedisClusterNode member = topologyProvider.getTopology().lookup(node); - if (connectionHandler != null && member != null) { + + if (member != null && connectionHandler != null) { return connectionHandler.getConnectionFromNode(new HostAndPort(member.getHost(), member.getPort())); } diff --git a/src/test/java/org/springframework/data/redis/connection/jedis/JedisClusterConnectionUnitTests.java b/src/test/java/org/springframework/data/redis/connection/jedis/JedisClusterConnectionUnitTests.java index 0686e93cd..57ee613e1 100644 --- a/src/test/java/org/springframework/data/redis/connection/jedis/JedisClusterConnectionUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/jedis/JedisClusterConnectionUnitTests.java @@ -272,12 +272,10 @@ public class JedisClusterConnectionUnitTests { nodes.remove(CLUSTER_HOST + ":" + MASTER_NODE_3_PORT); - try { - connection.dbSize(new RedisClusterNode(CLUSTER_HOST, MASTER_NODE_3_PORT)); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), - containsString("Node " + CLUSTER_HOST + ":" + MASTER_NODE_3_PORT + " is unknown to cluster")); - } + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Node " + CLUSTER_HOST + ":" + MASTER_NODE_3_PORT + " is unknown to cluster"); + + connection.dbSize(new RedisClusterNode(CLUSTER_HOST, MASTER_NODE_3_PORT)); } @Test // DATAREDIS-679 @@ -288,6 +286,7 @@ public class JedisClusterConnectionUnitTests { when(connectionHandlerMock.getConnectionFromNode(new HostAndPort(CLUSTER_HOST, MASTER_NODE_3_PORT))) .thenReturn(con3Mock); when(con3Mock.dbSize()).thenAnswer(new Answer() { + @Override public Long answer(InvocationOnMock invocation) throws Throwable {