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 6c51c596b..e14083c8e 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisCommand.java +++ b/src/main/java/org/springframework/data/redis/core/RedisCommand.java @@ -28,6 +28,7 @@ import org.springframework.util.StringUtils; * @author Christoph Strobl * @author Thomas Darimont * @author Ninad Divadkar + * @author Mark Paluch * @since 1.3 * @see Redis command list: * https://github.com/antirez/redis/blob/93e7a130fc9594e41ccfc996b5eca7626ae5356a/src/redis.c#L119 @@ -279,10 +280,10 @@ public enum RedisCommand { } /** - * @return {@literal true} if an exact number of arguments is expected + * @return {@literal true} if an exact number of arguments is expected. */ public boolean requiresExactNumberOfArguments() { - return maxArgs >= 0; + return maxArgs == 0 || minArgs == maxArgs; } /** @@ -345,6 +346,11 @@ public enum RedisCommand { throw new IllegalArgumentException( String.format("%s command requires at least %s arguments.", this.name(), this.minArgs)); } + + 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 21aed19b7..f5aa8f95b 100644 --- a/src/test/java/org/springframework/data/redis/core/RedisCommandUnitTests.java +++ b/src/test/java/org/springframework/data/redis/core/RedisCommandUnitTests.java @@ -23,8 +23,11 @@ import org.junit.Test; import org.junit.rules.ExpectedException; /** + * Unit tests for {@link RedisCommand}. + * * @author Christoph Strobl * @author Thomas Darimont + * @author Mark Paluch */ public class RedisCommandUnitTests { @@ -65,8 +68,25 @@ public class RedisCommandUnitTests { RedisCommand.AUTH.validateArgumentCount(1); } + @Test // DATAREDIS-822 + public void shouldConsiderMinMaxArguments() { + + RedisCommand.BITPOS.validateArgumentCount(2); + RedisCommand.BITPOS.validateArgumentCount(3); + RedisCommand.BITPOS.validateArgumentCount(4); + } + + @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 shouldThrowExceptionOnInvalidArgumentCountWhenExpectedExcatMatch() { + public void shouldThrowExceptionOnInvalidArgumentCountWhenExpectedExactMatch() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("AUTH command requires 1 arguments");