RedisCommandExecutor implements CommandExecutor { public void execute(final ByteCommand command, final byte[]... args) throws ConnectionException, IOException { connection.open(); final RedisStreamWriter streamWriter = connection.getStreamWriter(); streamWriter.write(RedisBytes.ASTERISK_BYTE); streamWriter.write(args.length + 1); streamWriter.writeNewLine(); streamWriter.write(RedisBytes.DOLLAR_BYTE); streamWriter.write(command.getBytes().length); streamWriter.writeNewLine(); streamWriter.write(command.getBytes()); streamWriter.writeNewLine(); for (final byte[] arg : args) { streamWriter.write(RedisBytes.DOLLAR_BYTE); streamWriter.write(arg.length); streamWriter.writeNewLine(); streamWriter.write(arg); streamWriter.writeNewLine(); } streamWriter.flush(); } RedisCommandExecutor(final Connection connection); void execute(final ByteCommand command, final byte[]... args);  }
@Test @SuppressWarnings("rawtypes") public void execute() throws ConnectionException, IOException { byte[] key = {'1'}; byte[] value = {'1'}; byte[][] args = {key, value}; String expectedRedisCommandString = "*3\r\n$3\r\nSET\r\n$1\r\n1\r\n$1\r\n1\r\n"; final byte[] outputStreamBytes = new byte[100]; doAnswer(new Answer() { int position = 0; public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); byte[] bytes = (byte[]) args[0]; int offset = (Integer) args[1]; int length = (Integer) args[2]; System.arraycopy(bytes, offset, outputStreamBytes, position, length); position += length; return null; } }).when(outputStream).write((byte[]) any(), anyInt(), anyInt()); commandExecutor.execute(RedisCommands.SET, args); verify(streamWriter, times(7)).writeNewLine(); verify(streamWriter).write(RedisBytes.ASTERISK_BYTE); verify(streamWriter, times(3)).write(RedisBytes.DOLLAR_BYTE); verify(streamWriter, times(4)).write(anyInt()); verify(streamWriter).flush(); String actualRedisCommandString = new String(outputStreamBytes).substring(0, expectedRedisCommandString.length()); assertEquals(expectedRedisCommandString, actualRedisCommandString); }