Added commands operating on all kind of values
This commit is contained in:
@@ -13,6 +13,7 @@ import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.JedisException;
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
public class ProtocolTest extends Assert {
|
||||
@@ -38,7 +39,7 @@ public class ProtocolTest extends Assert {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bulkReply() {
|
||||
public void bulkReply() throws JedisException {
|
||||
InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
String response = protocol.getBulkReply(is);
|
||||
@@ -46,7 +47,7 @@ public class ProtocolTest extends Assert {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullBulkReply() {
|
||||
public void nullBulkReply() throws JedisException {
|
||||
InputStream is = new ByteArrayInputStream("$-1\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
String response = protocol.getBulkReply(is);
|
||||
@@ -54,7 +55,7 @@ public class ProtocolTest extends Assert {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleLineReply() {
|
||||
public void singleLineReply() throws JedisException {
|
||||
InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
String response = protocol.getSingleLineReply(is);
|
||||
@@ -62,7 +63,7 @@ public class ProtocolTest extends Assert {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void integerReply() {
|
||||
public void integerReply() throws JedisException {
|
||||
InputStream is = new ByteArrayInputStream(":123\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
int response = protocol.getIntegerReply(is);
|
||||
@@ -70,7 +71,7 @@ public class ProtocolTest extends Assert {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiBulkReply() {
|
||||
public void multiBulkReply() throws JedisException {
|
||||
InputStream is = new ByteArrayInputStream(
|
||||
"*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n"
|
||||
.getBytes());
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisException;
|
||||
|
||||
public class AllKindOfValuesCommandsTest extends Assert {
|
||||
private Jedis jedis;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
jedis = new Jedis("localhost");
|
||||
jedis.connect();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
jedis.flushDB();
|
||||
jedis.disconnect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ping() throws JedisException {
|
||||
String status = jedis.ping();
|
||||
assertEquals("PONG", status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exists() throws JedisException {
|
||||
String status = jedis.set("foo", "bar");
|
||||
assertEquals("OK", status);
|
||||
|
||||
int reply = jedis.exists("foo");
|
||||
assertEquals(1, reply);
|
||||
|
||||
reply = jedis.del("foo");
|
||||
assertEquals(1, reply);
|
||||
|
||||
reply = jedis.exists("foo");
|
||||
assertEquals(0, reply);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void del() throws JedisException {
|
||||
jedis.set("foo1", "bar1");
|
||||
jedis.set("foo2", "bar2");
|
||||
jedis.set("foo3", "bar3");
|
||||
|
||||
int reply = jedis.del("foo1", "foo2", "foo3");
|
||||
assertEquals(3, reply);
|
||||
|
||||
reply = jedis.exists("foo1");
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.exists("foo2");
|
||||
assertEquals(0, reply);
|
||||
reply = jedis.exists("foo3");
|
||||
assertEquals(0, reply);
|
||||
|
||||
jedis.set("foo1", "bar1");
|
||||
|
||||
reply = jedis.del("foo1", "foo2");
|
||||
assertEquals(1, reply);
|
||||
|
||||
reply = jedis.del("foo1", "foo2");
|
||||
assertEquals(0, reply);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void type() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
String status = jedis.type("foo");
|
||||
assertEquals("string", status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void keys() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.set("foobar", "bar");
|
||||
|
||||
List<String> keys = jedis.keys("foo*");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("foo");
|
||||
expected.add("foobar");
|
||||
assertEquals(expected, keys);
|
||||
|
||||
expected = new ArrayList<String>();
|
||||
keys = jedis.keys("bar*");
|
||||
|
||||
assertEquals(expected, keys);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomKey() throws JedisException {
|
||||
assertEquals("", jedis.randomKey());
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
|
||||
assertEquals("foo", jedis.randomKey());
|
||||
|
||||
jedis.set("bar", "foo");
|
||||
|
||||
String randomkey = jedis.randomKey();
|
||||
assertTrue(randomkey.equals("foo") || randomkey.equals("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rename() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
String status = jedis.rename("foo", "bar");
|
||||
assertEquals("OK", status);
|
||||
|
||||
String value = jedis.get("foo");
|
||||
assertEquals(null, value);
|
||||
|
||||
value = jedis.get("bar");
|
||||
assertEquals("bar", value);
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void renameOldAndNewAreTheSame() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.rename("foo", "foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renamenx() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
int status = jedis.renamenx("foo", "bar");
|
||||
assertEquals(1, status);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
status = jedis.renamenx("foo", "bar");
|
||||
assertEquals(0, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dbSize() throws JedisException {
|
||||
int size = jedis.dbSize();
|
||||
assertEquals(0, size);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
size = jedis.dbSize();
|
||||
assertEquals(1, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expire() throws JedisException {
|
||||
int status = jedis.expire("foo", 20);
|
||||
assertEquals(0, status);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
status = jedis.expire("foo", 20);
|
||||
assertEquals(1, status);
|
||||
|
||||
status = jedis.expire("foo", 20);
|
||||
assertEquals(0, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expireAt() throws JedisException {
|
||||
long unixTime = (System.currentTimeMillis() / 1000L) + 20;
|
||||
|
||||
int status = jedis.expireAt("foo", unixTime);
|
||||
assertEquals(0, status);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
unixTime = (System.currentTimeMillis() / 1000L) + 20;
|
||||
status = jedis.expireAt("foo", unixTime);
|
||||
assertEquals(1, status);
|
||||
|
||||
unixTime = (System.currentTimeMillis() / 1000L) + 20;
|
||||
status = jedis.expireAt("foo", unixTime);
|
||||
assertEquals(0, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ttl() throws JedisException {
|
||||
int ttl = jedis.ttl("foo");
|
||||
assertEquals(-1, ttl);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
ttl = jedis.ttl("foo");
|
||||
assertEquals(-1, ttl);
|
||||
|
||||
jedis.expire("foo", 20);
|
||||
ttl = jedis.ttl("foo");
|
||||
assertTrue(ttl >= 0 && ttl <= 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
String status = jedis.select(1);
|
||||
assertEquals("OK", status);
|
||||
assertEquals(null, jedis.get("foo"));
|
||||
status = jedis.select(0);
|
||||
assertEquals("OK", status);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void move() throws JedisException {
|
||||
int status = jedis.move("foo", 1);
|
||||
assertEquals(0, status);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
status = jedis.move("foo", 1);
|
||||
assertEquals(1, status);
|
||||
assertEquals(null, jedis.get("foo"));
|
||||
|
||||
jedis.select(1);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void flushDB() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals(1, jedis.dbSize());
|
||||
jedis.set("bar", "foo");
|
||||
jedis.move("bar", 1);
|
||||
String status = jedis.flushDB();
|
||||
assertEquals("OK", status);
|
||||
assertEquals(0, jedis.dbSize());
|
||||
jedis.select(1);
|
||||
assertEquals(1, jedis.dbSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void flushAll() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals(1, jedis.dbSize());
|
||||
jedis.set("bar", "foo");
|
||||
jedis.move("bar", 1);
|
||||
String status = jedis.flushAll();
|
||||
assertEquals("OK", status);
|
||||
assertEquals(0, jedis.dbSize());
|
||||
jedis.select(1);
|
||||
assertEquals(0, jedis.dbSize());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
public class ConnectionHandlingCommandsTest extends Assert {
|
||||
private Jedis jedis;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
jedis = new Jedis("localhost");
|
||||
jedis.connect();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
jedis.flushDB();
|
||||
jedis.disconnect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void quit() {
|
||||
jedis.quit();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore(value = "Need to implement!")
|
||||
public void auth() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,4 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
@@ -10,8 +7,9 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisException;
|
||||
|
||||
public class JedisTest extends Assert {
|
||||
public class StringValuesCommandsTest extends Assert {
|
||||
private Jedis jedis;
|
||||
|
||||
@Before
|
||||
@@ -22,21 +20,17 @@ public class JedisTest extends Assert {
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
jedis.flushDB();
|
||||
jedis.disconnect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ping() throws UnknownHostException, IOException {
|
||||
String status = jedis.ping();
|
||||
assertEquals("PONG", status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setAndGet() throws UnknownHostException, IOException {
|
||||
public void setAndGet() throws JedisException {
|
||||
String status = jedis.set("foo", "bar");
|
||||
assertEquals("OK", status);
|
||||
|
||||
String value = jedis.get("foo");
|
||||
assertEquals("bar", value);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user