1818import static org .assertj .core .api .Assertions .*;
1919import static org .mockito .Mockito .*;
2020
21+ import redis .clients .jedis .CommandArguments ;
2122import redis .clients .jedis .CommandObject ;
2223import redis .clients .jedis .Connection ;
2324import redis .clients .jedis .Jedis ;
25+ import redis .clients .jedis .args .Rawable ;
2426
2527import java .io .IOException ;
2628
2729import org .junit .jupiter .api .BeforeEach ;
2830import org .junit .jupiter .api .Disabled ;
2931import org .junit .jupiter .api .Nested ;
3032import org .junit .jupiter .api .Test ;
31- import org .mockito .ArgumentCaptor ;
3233import org .springframework .dao .InvalidDataAccessApiUsageException ;
3334import org .springframework .dao .InvalidDataAccessResourceUsageException ;
3435import org .springframework .data .redis .connection .AbstractConnectionUnitTestBase ;
4344 *
4445 * @author Christoph Strobl
4546 * @author Tihomir Mateev
47+ * @author Dongliang Xie
4648 */
4749class JedisConnectionUnitTests {
4850
@@ -61,23 +63,51 @@ public void setUp() {
6163 }
6264
6365 /**
64- * Captures the CommandObject sent via executeCommand and returns a string containing
65- * the command name and all arguments.
66+ * Captures the command sent to the mock connection and returns a string containing the
67+ * command name and all arguments.
6668 */
67- @ SuppressWarnings ("unchecked" )
6869 private String captureCommand () {
69- ArgumentCaptor <CommandObject <?>> captor = ArgumentCaptor .forClass (CommandObject .class );
70- verify (connectionMock , atLeastOnce ()).executeCommand (captor .capture ());
71- CommandObject <?> lastCommand = captor .getValue ();
72- // Build a string from all raw arguments
70+
71+ String command = null ;
72+
73+ for (var invocation : mockingDetails (connectionMock ).getInvocations ()) {
74+
75+ Object [] arguments = invocation .getArguments ();
76+
77+ if (arguments .length != 1 ) {
78+ continue ;
79+ }
80+
81+ if (arguments [0 ] instanceof CommandObject <?> commandObject ) {
82+ command = commandToString (commandObject .getArguments ());
83+ } else if (arguments [0 ] instanceof CommandArguments commandArguments ) {
84+ command = commandToString (commandArguments );
85+ }
86+ }
87+
88+ assertThat (command ).as ("captured Redis command" ).isNotNull ();
89+ return command ;
90+ }
91+
92+ private String commandToString (Iterable <? extends Rawable > arguments ) {
93+
7394 StringBuilder sb = new StringBuilder ();
74- for (var arg : lastCommand .getArguments ()) {
75- if (sb .length () > 0 ) sb .append (" " );
95+
96+ for (Rawable arg : arguments ) {
97+ if (sb .length () > 0 ) {
98+ sb .append (" " );
99+ }
76100 sb .append (new String (arg .getRaw ()));
77101 }
102+
78103 return sb .toString ();
79104 }
80105
106+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
107+ private void returnOkForCommands () {
108+ when (connectionMock .executeCommand (any (CommandObject .class ))).thenReturn ("OK" );
109+ }
110+
81111 @ Test // DATAREDIS-184, GH-2153
82112 void shutdownWithNullShouldDelegateCommandCorrectly () {
83113
@@ -166,10 +196,30 @@ void shouldThrowExceptionWhenAccessingRedisSentinelsCommandsWhenNoSentinelsConfi
166196 .isThrownBy (() -> connection .getSentinelConnection ());
167197 }
168198
169- @ Test // DATAREDIS-472
170- void restoreShouldThrowExceptionWhenTtlInMillisExceedsIntegerRange () {
171- assertThatIllegalArgumentException ()
172- .isThrownBy (() -> connection .restore ("foo" .getBytes (), (long ) Integer .MAX_VALUE + 1L , "bar" .getBytes ()));
199+ @ Test // GH-3386
200+ void restoreShouldPassLongTtlToJedis () {
201+
202+ long ttlInMillis = (long ) Integer .MAX_VALUE + 1L ;
203+
204+ returnOkForCommands ();
205+
206+ connection .restore ("foo" .getBytes (), ttlInMillis , "bar" .getBytes ());
207+
208+ String command = captureCommand ();
209+ assertThat (command ).contains ("RESTORE" ).contains (Long .toString (ttlInMillis ));
210+ }
211+
212+ @ Test // GH-3386
213+ void restoreWithReplaceShouldPassLongTtlToJedis () {
214+
215+ long ttlInMillis = (long ) Integer .MAX_VALUE + 1L ;
216+
217+ returnOkForCommands ();
218+
219+ connection .restore ("foo" .getBytes (), ttlInMillis , "bar" .getBytes (), true );
220+
221+ String command = captureCommand ();
222+ assertThat (command ).contains ("RESTORE" ).contains (Long .toString (ttlInMillis )).contains ("REPLACE" );
173223 }
174224
175225 @ Test // DATAREDIS-472
0 commit comments