Protocol implementation

This commit is contained in:
Jonathan Leibiusky
2010-06-11 02:57:11 -03:00
commit 7e6ed0af41
5 changed files with 370 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
package redis.clients.jedis.tests;
import java.io.IOException;
import java.net.UnknownHostException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Client;
public class ClientTest extends Assert {
private Client client;
@Before
public void setUp() throws Exception {
client = new Client();
}
@After
public void tearDown() throws Exception {
client.disconnect();
}
@Test
public void ping() throws UnknownHostException, IOException {
client.setHost("localhost");
client.connect();
assertTrue(client.isConnected());
String status = client.ping();
assertEquals("PONG", status);
}
@Test
public void setAndGet() throws UnknownHostException, IOException {
client.setHost("localhost");
client.connect();
String status = client.set("foo", "bar");
assertEquals("OK", status);
String value = client.get("foo");
assertEquals("bar", value);
}
@Test(expected = UnknownHostException.class)
public void checkUnkownHost() throws UnknownHostException, IOException {
client.setHost("someunknownhost");
client.connect();
}
@Test(expected = IOException.class)
public void checkWrongPort() throws UnknownHostException, IOException {
client.setHost("localhost");
client.setPort(55665);
client.connect();
}
}

View File

@@ -0,0 +1,72 @@
package redis.clients.jedis.tests;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.junit.Test;
import redis.clients.jedis.Protocol;
public class ProtocolTest extends Assert {
@Test
public void buildACommand() {
Protocol protocol = new Protocol();
String command = protocol.buildCommand("GET", "SOMEKEY");
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
assertEquals(expectedCommand, command);
}
@Test
public void bulkReply() {
InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes());
Protocol protocol = new Protocol();
String response = protocol.getBulkReply(is);
assertEquals("foobar", response);
}
@Test
public void nullBulkReply() {
InputStream is = new ByteArrayInputStream("$-1\r\n".getBytes());
Protocol protocol = new Protocol();
String response = protocol.getBulkReply(is);
assertEquals(null, response);
}
@Test
public void singleLineReply() {
InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes());
Protocol protocol = new Protocol();
String response = protocol.getSingleLineReply(is);
assertEquals("OK", response);
}
@Test
public void integerReply() {
InputStream is = new ByteArrayInputStream(":123\r\n".getBytes());
Protocol protocol = new Protocol();
int response = protocol.getIntegerReply(is);
assertEquals(123, response);
}
@Test
public void multiBulkReply() {
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());
Protocol protocol = new Protocol();
List<String> response = protocol.getMultiBulkReply(is);
List<String> expected = new ArrayList<String>();
expected.add("foo");
expected.add("bar");
expected.add("Hello");
expected.add("World");
assertEquals(expected, response);
}
}