From f58bf08c1552bb33c796a3074fd65f765bea6558 Mon Sep 17 00:00:00 2001 From: Oscar Cai Date: Wed, 17 Apr 2019 16:29:37 +0200 Subject: [PATCH] DATAREDIS-972 - Fix validation for RedisCommand argument count. Original pull request: #448. --- .../data/redis/core/RedisCommand.java | 2 +- .../data/redis/core/RedisCommandUnitTests.java | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/core/RedisCommand.java b/src/main/java/org/springframework/data/redis/core/RedisCommand.java index 0674c988a..17e16135f 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisCommand.java +++ b/src/main/java/org/springframework/data/redis/core/RedisCommand.java @@ -348,7 +348,7 @@ public enum RedisCommand { String.format("%s command requires at least %s arguments.", this.name(), this.minArgs)); } - if (maxArgs != 0 && nrArguments > maxArgs) { + if (maxArgs > 0 && nrArguments > maxArgs) { throw new IllegalArgumentException( String.format("%s command requires at most %s arguments.", this.name(), this.maxArgs)); } diff --git a/src/test/java/org/springframework/data/redis/core/RedisCommandUnitTests.java b/src/test/java/org/springframework/data/redis/core/RedisCommandUnitTests.java index 9460b2ae6..48c1edf09 100644 --- a/src/test/java/org/springframework/data/redis/core/RedisCommandUnitTests.java +++ b/src/test/java/org/springframework/data/redis/core/RedisCommandUnitTests.java @@ -66,11 +66,14 @@ public class RedisCommandUnitTests { @Test // DATAREDIS-73 public void shouldNotThrowExceptionOnValidArgumentCount() { RedisCommand.AUTH.validateArgumentCount(1); + RedisCommand.ZADD.validateArgumentCount(3); // DATAREDIS-972 + RedisCommand.ZADD.validateArgumentCount(4); // DATAREDIS-972 + RedisCommand.ZADD.validateArgumentCount(5); // DATAREDIS-972 + RedisCommand.ZADD.validateArgumentCount(100); // DATAREDIS-972 } @Test // DATAREDIS-822 public void shouldConsiderMinMaxArguments() { - RedisCommand.BITPOS.validateArgumentCount(2); RedisCommand.BITPOS.validateArgumentCount(3); RedisCommand.BITPOS.validateArgumentCount(4); @@ -78,16 +81,13 @@ public class RedisCommandUnitTests { @Test // DATAREDIS-822 public void shouldReportArgumentMismatchIfMaxArgumentsExceeded() { - expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("BITPOS command requires at most 4 arguments"); - RedisCommand.BITPOS.validateArgumentCount(5); } @Test // DATAREDIS-73 public void shouldThrowExceptionOnInvalidArgumentCountWhenExpectedExactMatch() { - expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("AUTH command requires 1 arguments"); RedisCommand.AUTH.validateArgumentCount(2); @@ -95,9 +95,12 @@ public class RedisCommandUnitTests { @Test // DATAREDIS-73 public void shouldThrowExceptionOnInvalidArgumentCountWhenExpectedMinimalMatch() { - expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("DEL command requires at least 1 arguments"); RedisCommand.DEL.validateArgumentCount(0); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("ZADD command requires at least 3 arguments"); + RedisCommand.ZADD.validateArgumentCount(2); // DATAREDIS-972 } }