Skip to content

Commit 68fca92

Browse files
committed
DATAREDIS-822 - Consider argument range in RedisCommand min/max arguments specification.
We now consider max argument specifications as upper bound and no longer as the exact number of required arguments. Original pull request: #335.
1 parent 5bfd739 commit 68fca92

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/main/java/org/springframework/data/redis/core/RedisCommand.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @author Christoph Strobl
3030
* @author Thomas Darimont
3131
* @author Ninad Divadkar
32+
* @author Mark Paluch
3233
* @since 1.3
3334
* @see Redis command list:
3435
* https://github.com/antirez/redis/blob/93e7a130fc9594e41ccfc996b5eca7626ae5356a/src/redis.c#L119
@@ -280,10 +281,10 @@ public boolean requiresArguments() {
280281
}
281282

282283
/**
283-
* @return {@literal true} if an exact number of arguments is expected
284+
* @return {@literal true} if an exact number of arguments is expected.
284285
*/
285286
public boolean requiresExactNumberOfArguments() {
286-
return maxArgs >= 0;
287+
return maxArgs == 0 || minArgs == maxArgs;
287288
}
288289

289290
/**
@@ -346,6 +347,11 @@ public void validateArgumentCount(int nrArguments) {
346347
throw new IllegalArgumentException(
347348
String.format("%s command requires at least %s arguments.", this.name(), this.minArgs));
348349
}
350+
351+
if (maxArgs != 0 && nrArguments > maxArgs) {
352+
throw new IllegalArgumentException(
353+
String.format("%s command requires at most %s arguments.", this.name(), this.maxArgs));
354+
}
349355
}
350356
}
351357

src/test/java/org/springframework/data/redis/core/RedisCommandUnitTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import org.junit.rules.ExpectedException;
2424

2525
/**
26+
* Unit tests for {@link RedisCommand}.
27+
*
2628
* @author Christoph Strobl
2729
* @author Thomas Darimont
30+
* @author Mark Paluch
2831
*/
2932
public class RedisCommandUnitTests {
3033

@@ -65,8 +68,25 @@ public void shouldNotThrowExceptionOnValidArgumentCount() {
6568
RedisCommand.AUTH.validateArgumentCount(1);
6669
}
6770

71+
@Test // DATAREDIS-822
72+
public void shouldConsiderMinMaxArguments() {
73+
74+
RedisCommand.BITPOS.validateArgumentCount(2);
75+
RedisCommand.BITPOS.validateArgumentCount(3);
76+
RedisCommand.BITPOS.validateArgumentCount(4);
77+
}
78+
79+
@Test // DATAREDIS-822
80+
public void shouldReportArgumentMismatchIfMaxArgumentsExceeded() {
81+
82+
expectedException.expect(IllegalArgumentException.class);
83+
expectedException.expectMessage("BITPOS command requires at most 4 arguments");
84+
85+
RedisCommand.BITPOS.validateArgumentCount(5);
86+
}
87+
6888
@Test // DATAREDIS-73
69-
public void shouldThrowExceptionOnInvalidArgumentCountWhenExpectedExcatMatch() {
89+
public void shouldThrowExceptionOnInvalidArgumentCountWhenExpectedExactMatch() {
7090

7191
expectedException.expect(IllegalArgumentException.class);
7292
expectedException.expectMessage("AUTH command requires 1 arguments");

0 commit comments

Comments
 (0)