Skip to content

Commit 8c39941

Browse files
committed
DATAREDIS-1013 - Polishing.
Add test. Consider grammar depending on argument count. Original pull request: #468.
1 parent 9dc8244 commit 8c39941

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,25 @@ public void validateArgumentCount(int nrArguments) {
342342
if (requiresExactNumberOfArguments()) {
343343
if (nrArguments != maxArgs) {
344344
throw new IllegalArgumentException(
345-
String.format("%s command requires %s arguments.", this.name(), this.maxArgs));
345+
String.format("%s command requires %s %s.", this.name(), this.maxArgs, arguments(this.maxArgs)));
346346
}
347347
}
348348
if (nrArguments < minArgs) {
349349
throw new IllegalArgumentException(
350-
String.format("%s command requires at least %s arguments.", this.name(), this.minArgs));
350+
String.format("%s command requires at least %s %s.", this.name(), this.minArgs, arguments(this.maxArgs)));
351351
}
352352

353353
if (maxArgs > 0 && nrArguments > maxArgs) {
354354
throw new IllegalArgumentException(
355-
String.format("%s command requires at most %s arguments.", this.name(), this.maxArgs));
355+
String.format("%s command requires at most %s %s.", this.name(), this.maxArgs, arguments(this.maxArgs)));
356356
}
357357
}
358358
}
359359

360+
private static String arguments(int count) {
361+
return count == 1 ? "argument" : "arguments";
362+
}
363+
360364
/**
361365
* Returns the command represented by the given {@code key}. Returns {@link #UNKNOWN} if no matching command could be
362366
* found.

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ public void shouldReturnUnknownCommandForUnknownCommandString() {
6464
assertThat(RedisCommand.failsafeCommandLookup("strangecommand"), is(RedisCommand.UNKNOWN));
6565
}
6666

67-
@Test // DATAREDIS-73, DATAREDIS-972
67+
@Test // DATAREDIS-73, DATAREDIS-972, DATAREDIS-1013
6868
public void shouldNotThrowExceptionOnValidArgumentCount() {
6969

7070
RedisCommand.AUTH.validateArgumentCount(1);
7171
RedisCommand.ZADD.validateArgumentCount(3);
7272
RedisCommand.ZADD.validateArgumentCount(4);
7373
RedisCommand.ZADD.validateArgumentCount(5);
7474
RedisCommand.ZADD.validateArgumentCount(100);
75+
RedisCommand.SELECT.validateArgumentCount(1);
7576
}
7677

7778
@Test // DATAREDIS-822
@@ -86,16 +87,16 @@ public void shouldConsiderMinMaxArguments() {
8687
public void shouldReportArgumentMismatchIfMaxArgumentsExceeded() {
8788

8889
expectedException.expect(IllegalArgumentException.class);
89-
expectedException.expectMessage("BITPOS command requires at most 4 arguments");
90+
expectedException.expectMessage("SELECT command requires 1 argument");
9091

91-
RedisCommand.BITPOS.validateArgumentCount(5);
92+
RedisCommand.SELECT.validateArgumentCount(0);
9293
}
9394

9495
@Test // DATAREDIS-73
9596
public void shouldThrowExceptionOnInvalidArgumentCountWhenExpectedExactMatch() {
9697

9798
expectedException.expect(IllegalArgumentException.class);
98-
expectedException.expectMessage("AUTH command requires 1 arguments");
99+
expectedException.expectMessage("AUTH command requires 1 argument");
99100

100101
RedisCommand.AUTH.validateArgumentCount(2);
101102
}
@@ -104,7 +105,7 @@ public void shouldThrowExceptionOnInvalidArgumentCountWhenExpectedExactMatch() {
104105
public void shouldThrowExceptionOnInvalidArgumentCountForDelWhenExpectedMinimalMatch() {
105106

106107
expectedException.expect(IllegalArgumentException.class);
107-
expectedException.expectMessage("DEL command requires at least 1 arguments");
108+
expectedException.expectMessage("DEL command requires at least 1 argument");
108109

109110
RedisCommand.DEL.validateArgumentCount(0);
110111
}

0 commit comments

Comments
 (0)