Check jedis is connected before executing commands
This commit is contained in:
@@ -21,7 +21,11 @@ public class Client {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
protected Client sendCommand(String name, String... args) {
|
||||
protected Client sendCommand(String name, String... args)
|
||||
throws JedisException {
|
||||
if (!isConnected()) {
|
||||
throw new JedisException("Please connect Jedis before using it.");
|
||||
}
|
||||
protocol.sendCommand(outputStream, name, args);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class Jedis extends Client {
|
||||
return sendCommand("GET", key).getBulkReply();
|
||||
}
|
||||
|
||||
public void quit() {
|
||||
public void quit() throws JedisException {
|
||||
sendCommand("QUIT");
|
||||
}
|
||||
|
||||
|
||||
14
src/test/java/redis/clients/jedis/tests/JedisTest.java
Normal file
14
src/test/java/redis/clients/jedis/tests/JedisTest.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisException;
|
||||
|
||||
public class JedisTest {
|
||||
@Test(expected = JedisException.class)
|
||||
public void useWithoutConnecting() throws JedisException {
|
||||
Jedis jedis = new Jedis("localhost");
|
||||
jedis.dbSize();
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisException;
|
||||
|
||||
public class ConnectionHandlingCommandsTest extends Assert {
|
||||
private Jedis jedis;
|
||||
@@ -25,7 +26,7 @@ public class ConnectionHandlingCommandsTest extends Assert {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void quit() {
|
||||
public void quit() throws JedisException {
|
||||
jedis.quit();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user