Skip to content

Commit a8ffacd

Browse files
author
Jonathan Leibiusky
committed
Added bunch of missing commands and a test to check if Jedis is updated
1 parent 9b202c3 commit a8ffacd

File tree

7 files changed

+179
-2
lines changed

7 files changed

+179
-2
lines changed

src/main/java/redis/clients/jedis/Client.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,4 +563,28 @@ public void configGet(String pattern) {
563563
public void configSet(String parameter, String value) {
564564
sendCommand("CONFIG", "SET", parameter, value);
565565
}
566+
567+
public void strlen(String key) {
568+
sendCommand("STRLEN", key);
569+
}
570+
571+
public void sync() {
572+
sendCommand("SYNC");
573+
}
574+
575+
public void lpushx(String key, String string) {
576+
sendCommand("LPUSHX", key, string);
577+
}
578+
579+
public void persist(String key) {
580+
sendCommand("PERSIST", key);
581+
}
582+
583+
public void rpushx(String key, String string) {
584+
sendCommand("RPUSHX", key, string);
585+
}
586+
587+
public void echo(String string) {
588+
sendCommand("ECHO", string);
589+
}
566590
}

src/main/java/redis/clients/jedis/Jedis.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,4 +790,33 @@ public String configSet(String parameter, String value) {
790790
public boolean isConnected() {
791791
return client.isConnected();
792792
}
793+
794+
public int strlen(String key) {
795+
client.strlen(key);
796+
return client.getIntegerReply();
797+
}
798+
799+
public void sync() {
800+
client.sync();
801+
}
802+
803+
public int lpushx(String key, String string) {
804+
client.lpushx(key, string);
805+
return client.getIntegerReply();
806+
}
807+
808+
public int persist(String key) {
809+
client.persist(key);
810+
return client.getIntegerReply();
811+
}
812+
813+
public int rpushx(String key, String string) {
814+
client.rpushx(key, string);
815+
return client.getIntegerReply();
816+
}
817+
818+
public String echo(String string) {
819+
client.echo(string);
820+
return client.getBulkReply();
821+
}
793822
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package redis.clients.jedis.tests;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.DataInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.lang.reflect.Method;
8+
import java.net.MalformedURLException;
9+
import java.net.URL;
10+
import java.util.HashSet;
11+
import java.util.Set;
12+
13+
import org.junit.Assert;
14+
import org.junit.Ignore;
15+
import org.junit.Test;
16+
17+
import redis.clients.jedis.Jedis;
18+
import redis.clients.jedis.JedisPubSub;
19+
import redis.clients.jedis.Transaction;
20+
21+
public class JedisNewCommandsCheckTest extends Assert {
22+
@Test
23+
@Ignore(value = "Ignored because still missing information for DEBUG and LINSERT commands")
24+
public void checkJedisIsUpdated() throws IOException {
25+
String[] commands = getAvailableCommands();
26+
Set<String> implementedCommands = getImplementedCommands();
27+
28+
Set<String> missingCommands = new HashSet<String>();
29+
for (String command : commands) {
30+
if (!implementedCommands.contains(command.trim())) {
31+
missingCommands.add(command);
32+
}
33+
}
34+
35+
if (!missingCommands.isEmpty()) {
36+
fail("There are missing commands: " + missingCommands.toString());
37+
}
38+
}
39+
40+
private Set<String> getImplementedCommands() {
41+
Method[] methods = Jedis.class.getDeclaredMethods();
42+
Set<String> implementedCommands = new HashSet<String>();
43+
for (Method method : methods) {
44+
implementedCommands.add(method.getName().trim().toLowerCase());
45+
}
46+
47+
methods = JedisPubSub.class.getDeclaredMethods();
48+
for (Method method : methods) {
49+
implementedCommands.add(method.getName().trim().toLowerCase());
50+
}
51+
52+
methods = Transaction.class.getDeclaredMethods();
53+
for (Method method : methods) {
54+
implementedCommands.add(method.getName().trim().toLowerCase());
55+
}
56+
implementedCommands.add("config");
57+
return implementedCommands;
58+
}
59+
60+
private String[] getAvailableCommands() throws MalformedURLException,
61+
IOException {
62+
URL url = new URL("http://dimaion.com/redis/master");
63+
InputStream openStream = url.openStream();
64+
DataInputStream dis = new DataInputStream(new BufferedInputStream(
65+
openStream));
66+
byte[] all = new byte[dis.available()];
67+
dis.readFully(all);
68+
String commandList = new String(all);
69+
String[] commands = commandList.split("\n");
70+
return commands;
71+
}
72+
}

src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,20 @@ public void flushAll() {
219219
jedis.select(1);
220220
assertEquals(0, jedis.dbSize());
221221
}
222+
223+
@Test
224+
public void persist() {
225+
jedis.setex("foo", 60 * 60, "bar");
226+
assertTrue(jedis.ttl("foo") > 0);
227+
int status = jedis.persist("foo");
228+
assertEquals(1, status);
229+
assertEquals(-1, jedis.ttl("foo"));
230+
}
231+
232+
@Test
233+
public void echo() {
234+
String result = jedis.echo("hello world");
235+
assertEquals("hello world", result);
236+
}
237+
222238
}

src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ public void save() {
1616

1717
@Test
1818
public void bgsave() {
19-
String status = jedis.bgsave();
20-
assertEquals("Background saving started", status);
19+
try {
20+
String status = jedis.bgsave();
21+
assertEquals("Background saving started", status);
22+
} catch (JedisException e) {
23+
assertEquals("ERR background save already in progress", e
24+
.getMessage());
25+
}
2126
}
2227

2328
@Test
@@ -72,4 +77,10 @@ public void configSet() {
7277
assertEquals("OK", status);
7378
jedis.configSet("maxmemory", memory);
7479
}
80+
81+
@Test
82+
public void sync() {
83+
jedis.sync();
84+
}
85+
7586
}

src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,25 @@ public void run() {
249249
assertEquals(2, result.size());
250250
assertEquals("foo", result.get(0));
251251
assertEquals("bar", result.get(1));
252+
}
253+
254+
@Test
255+
public void lpushx() {
256+
int status = jedis.lpushx("foo", "bar");
257+
assertEquals(0, status);
252258

259+
jedis.lpush("foo", "a");
260+
status = jedis.lpushx("foo", "b");
261+
assertEquals(2, status);
262+
}
263+
264+
@Test
265+
public void rpushx() {
266+
int status = jedis.rpushx("foo", "bar");
267+
assertEquals(0, status);
268+
269+
jedis.lpush("foo", "a");
270+
status = jedis.rpushx("foo", "b");
271+
assertEquals(2, status);
253272
}
254273
}

src/test/java/redis/clients/jedis/tests/commands/StringValuesCommandsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,10 @@ public void substr() {
169169
assertEquals("This is a string", jedis.substr("s", 0, -1));
170170
assertEquals(" string", jedis.substr("s", 9, 100000));
171171
}
172+
173+
@Test
174+
public void strlen() {
175+
jedis.set("s", "This is a string");
176+
assertEquals("This is a string".length(), jedis.strlen("s"));
177+
}
172178
}

0 commit comments

Comments
 (0)