Skip to content

Commit f8d63f4

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 abb7d9f commit f8d63f4

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
@@ -28,6 +28,7 @@
2828
* @author Christoph Strobl
2929
* @author Thomas Darimont
3030
* @author Ninad Divadkar
31+
* @author Mark Paluch
3132
* @since 1.3
3233
* @see Redis command list:
3334
* https://github.com/antirez/redis/blob/93e7a130fc9594e41ccfc996b5eca7626ae5356a/src/redis.c#L119
@@ -279,10 +280,10 @@ public boolean requiresArguments() {
279280
}
280281

281282
/**
282-
* @return {@literal true} if an exact number of arguments is expected
283+
* @return {@literal true} if an exact number of arguments is expected.
283284
*/
284285
public boolean requiresExactNumberOfArguments() {
285-
return maxArgs >= 0;
286+
return maxArgs == 0 || minArgs == maxArgs;
286287
}
287288

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

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)