diff --git a/Makefile b/Makefile index c5d745babd..10352457b9 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ daemonize yes protected-mode no port 6379 requirepass foobared +user acljedis on allcommands allkeys >fizzbuzz pidfile /tmp/redis1.pid logfile /tmp/redis1.log save "" diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index cd0b00e312..1f962a2d54 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -76,6 +76,15 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE); } + public JedisPool(final String host, int port, String user, final String password) { + this(new GenericObjectPoolConfig(), host, port, user, password); + } + + public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, + String user, final String password) { + this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, user, password, Protocol.DEFAULT_DATABASE); + } + public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, int timeout, final String user, final String password) { this(poolConfig, host, port, timeout, user, password, Protocol.DEFAULT_DATABASE); diff --git a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java index 4124f20d5a..d010382bac 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java @@ -8,7 +8,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.concurrent.atomic.AtomicInteger; - import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.DefaultPooledObject; @@ -21,78 +20,80 @@ import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.Transaction; import redis.clients.jedis.exceptions.InvalidURIException; -import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.exceptions.JedisExhaustedPoolException; public class JedisPoolTest { - private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); + private static final HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); @Test public void checkConnections() { JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - jedis.close(); - pool.destroy(); + try (Jedis jedis = pool.getResource()) { + jedis.auth("foobared"); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + } + pool.close(); assertTrue(pool.isClosed()); } @Test public void checkCloseableConnections() throws Exception { JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - jedis.close(); + try (Jedis jedis = pool.getResource()) { + jedis.auth("foobared"); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + } pool.close(); assertTrue(pool.isClosed()); } @Test public void checkConnectionWithDefaultPort() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort()); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - jedis.close(); - pool.destroy(); + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost()); + try (Jedis jedis = pool.getResource()) { + jedis.auth("foobared"); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + } + pool.close(); assertTrue(pool.isClosed()); } @Test - public void checkJedisIsReusedWhenReturned() { + public void checkResourceIsClosableAndReusable() { + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); + config.setMaxTotal(1); + config.setBlockWhenExhausted(false); + try (JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared", 0, + "closable-resuable-pool", false, null, null, null)) { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort()); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "0"); - jedis.close(); - - jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.incr("foo"); - jedis.close(); - pool.destroy(); - assertTrue(pool.isClosed()); + Jedis jedis = pool.getResource(); + jedis.set("hello", "jedis"); + jedis.close(); + + Jedis jedis2 = pool.getResource(); + assertEquals(jedis, jedis2); + assertEquals("jedis", jedis2.get("hello")); + jedis2.close(); + } } @Test public void checkPoolRepairedWhenJedisIsBroken() { JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort()); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.quit(); - jedis.close(); - - jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.incr("foo"); - jedis.close(); - pool.destroy(); + try (Jedis jedis = pool.getResource()) { + jedis.auth("foobared"); + jedis.set("foo", "0"); + jedis.quit(); + } + + try (Jedis jedis = pool.getResource()) { + jedis.auth("foobared"); + jedis.incr("foo"); + } + pool.close(); assertTrue(pool.isClosed()); } @@ -101,14 +102,14 @@ public void checkPoolOverflow() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort()); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "0"); + try (JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort()); + Jedis jedis = pool.getResource()) { + jedis.auth("foobared"); - Jedis newJedis = pool.getResource(); - newJedis.auth("foobared"); - newJedis.incr("foo"); + try (Jedis jedis2 = pool.getResource()) { + jedis2.auth("foobared"); + } + } } @Test @@ -116,107 +117,96 @@ public void securePool() { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared"); - Jedis jedis = pool.getResource(); - jedis.set("foo", "bar"); - jedis.close(); - pool.destroy(); + try (Jedis jedis = pool.getResource()) { + jedis.set("foo", "bar"); + } + pool.close(); assertTrue(pool.isClosed()); } @Test public void nonDefaultDatabase() { - JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared"); - Jedis jedis0 = pool0.getResource(); - jedis0.set("foo", "bar"); - assertEquals("bar", jedis0.get("foo")); - jedis0.close(); - pool0.destroy(); - assertTrue(pool0.isClosed()); - - JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared", 1); - Jedis jedis1 = pool1.getResource(); - assertNull(jedis1.get("foo")); - jedis1.close(); - pool1.destroy(); - assertTrue(pool1.isClosed()); + try (JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "foobared"); + Jedis jedis0 = pool0.getResource()) { + jedis0.set("foo", "bar"); + assertEquals("bar", jedis0.get("foo")); + } + + try (JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "foobared", 1); + Jedis jedis1 = pool1.getResource()) { + assertNull(jedis1.get("foo")); + } } @Test public void startWithUrlString() { - Jedis j = new Jedis("localhost", 6380); - j.auth("foobared"); - j.select(2); - j.set("foo", "bar"); - j.close(); + try (Jedis j = new Jedis("localhost", 6380)) { + j.auth("foobared"); + j.select(2); + j.set("foo", "bar"); + } - JedisPool pool = new JedisPool("redis://:foobared@localhost:6380/2"); - Jedis jedis = pool.getResource(); - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); - jedis.close(); + try (JedisPool pool = new JedisPool("redis://:foobared@localhost:6380/2"); + Jedis jedis = pool.getResource()) { + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); + } } @Test public void startWithUrl() throws URISyntaxException { - Jedis j = new Jedis("localhost", 6380); - j.auth("foobared"); - j.select(2); - j.set("foo", "bar"); - j.close(); + try (Jedis j = new Jedis("localhost", 6380)) { + j.auth("foobared"); + j.select(2); + j.set("foo", "bar"); + } - JedisPool pool = new JedisPool(new URI("redis://:foobared@localhost:6380/2")); - Jedis jedis = pool.getResource(); - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); + try (JedisPool pool = new JedisPool(new URI("redis://:foobared@localhost:6380/2")); + Jedis jedis = pool.getResource()) { + assertEquals("bar", jedis.get("foo")); + } } @Test(expected = InvalidURIException.class) public void shouldThrowInvalidURIExceptionForInvalidURI() throws URISyntaxException { - JedisPool pool = new JedisPool(new URI("localhost:6380")); + new JedisPool(new URI("localhost:6380")).close(); } @Test public void allowUrlWithNoDBAndNoPassword() throws URISyntaxException { - new JedisPool("redis://localhost:6380"); - new JedisPool(new URI("redis://localhost:6380")); + new JedisPool("redis://localhost:6380").close(); + new JedisPool(new URI("redis://localhost:6380")).close(); } @Test public void selectDatabaseOnActivation() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared"); - - Jedis jedis0 = pool.getResource(); - assertEquals(0, jedis0.getDB()); - - jedis0.select(1); - assertEquals(1, jedis0.getDB()); - - jedis0.close(); - - Jedis jedis1 = pool.getResource(); - assertTrue("Jedis instance was not reused", jedis1 == jedis0); - assertEquals(0, jedis1.getDB()); - - jedis1.close(); - pool.destroy(); - assertTrue(pool.isClosed()); + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, + "foobared")) { + + Jedis jedis0 = pool.getResource(); + assertEquals(0, jedis0.getDB()); + + jedis0.select(1); + assertEquals(1, jedis0.getDB()); + + jedis0.close(); + + Jedis jedis1 = pool.getResource(); + assertTrue("Jedis instance was not reused", jedis1 == jedis0); + assertEquals(0, jedis1.getDB()); + + jedis1.close(); + } } @Test public void customClientName() { - JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "foobared", 0, "my_shiny_client_name"); + Jedis jedis = pool.getResource()) { - Jedis jedis = pool0.getResource(); - - assertEquals("my_shiny_client_name", jedis.clientGetname()); - - jedis.close(); - pool0.destroy(); - assertTrue(pool0.isClosed()); + assertEquals("my_shiny_client_name", jedis.clientGetname()); + } } @Test @@ -295,115 +285,97 @@ public void returnResourceShouldResetState() { jedis2.close(); } - pool.destroy(); + pool.close(); assertTrue(pool.isClosed()); } - @Test - public void checkResourceIsCloseable() { - GenericObjectPoolConfig config = new GenericObjectPoolConfig(); - config.setMaxTotal(1); - config.setBlockWhenExhausted(false); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared"); - - Jedis jedis = pool.getResource(); - try { - jedis.set("hello", "jedis"); - } finally { - jedis.close(); - } - - Jedis jedis2 = pool.getResource(); - try { - assertEquals(jedis, jedis2); - } finally { - jedis2.close(); - } - } - @Test public void getNumActiveIsNegativeWhenPoolIsClosed() { JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "foobared", 0, "my_shiny_client_name"); - pool.destroy(); + try (Jedis j = pool.getResource()) { + j.ping(); + } + + pool.close(); assertTrue(pool.getNumActive() < 0); } @Test public void getNumActiveReturnsTheCorrectNumber() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - - assertEquals(1, pool.getNumActive()); - - Jedis jedis2 = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "bar"); - - assertEquals(2, pool.getNumActive()); - - jedis.close(); - assertEquals(1, pool.getNumActive()); - - jedis2.close(); - - assertEquals(0, pool.getNumActive()); - - pool.destroy(); + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + Jedis jedis = pool.getResource(); + jedis.auth("foobared"); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + + assertEquals(1, pool.getNumActive()); + + Jedis jedis2 = pool.getResource(); + jedis.auth("foobared"); + jedis.set("foo", "bar"); + + assertEquals(2, pool.getNumActive()); + + jedis.close(); + assertEquals(1, pool.getNumActive()); + + jedis2.close(); + + assertEquals(0, pool.getNumActive()); + } } @Test public void testAddObject() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - pool.addObjects(1); - assertEquals(1, pool.getNumIdle()); - pool.destroy(); + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + pool.addObjects(1); + assertEquals(1, pool.getNumIdle()); + } } @Test public void closeResourceTwice() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - Jedis j = pool.getResource(); - j.auth("foobared"); - j.ping(); - j.close(); - j.close(); + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + Jedis j = pool.getResource(); + j.auth("foobared"); + j.ping(); + j.close(); + j.close(); + } } @Test public void closeBrokenResourceTwice() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - Jedis j = pool.getResource(); - try { - // make connection broken - j.getClient().getOne(); - fail(); - } catch (Exception e) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + Jedis j = pool.getResource(); + try { + // make connection broken + j.getClient().getOne(); + fail(); + } catch (Exception e) { + } + assertTrue(j.getClient().isBroken()); + j.close(); + j.close(); } - assertTrue(j.getClient().isBroken()); - j.close(); - j.close(); } @Test public void testCloseConnectionOnMakeObject() { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "wrong pass"); - Jedis jedis = new Jedis("redis://:foobared@localhost:6379/"); - int currentClientCount = getClientCount(jedis.clientList()); - try { - pool.getResource(); - fail("Should throw exception as password is incorrect."); - } catch (Exception e) { - assertEquals(currentClientCount, getClientCount(jedis.clientList())); + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "wrong pass"); + Jedis jedis = new Jedis("redis://:foobared@localhost:6379/")) { + int currentClientCount = getClientCount(jedis.clientList()); + try { + pool.getResource(); + fail("Should throw exception as password is incorrect."); + } catch (Exception e) { + assertEquals(currentClientCount, getClientCount(jedis.clientList())); + } } - jedis.close(); } private int getClientCount(final String clientList) { diff --git a/src/test/java/redis/clients/jedis/tests/JedisPoolWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/JedisPoolWithCompleteCredentialsTest.java index 17a13293d3..9994ccddb5 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisPoolWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisPoolWithCompleteCredentialsTest.java @@ -1,30 +1,32 @@ package redis.clients.jedis.tests; -import org.apache.commons.pool2.PooledObject; -import org.apache.commons.pool2.PooledObjectFactory; -import org.apache.commons.pool2.impl.DefaultPooledObject; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.net.URI; +import java.net.URISyntaxException; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Before; import org.junit.Test; -import redis.clients.jedis.*; + +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; +import redis.clients.jedis.Protocol; import redis.clients.jedis.exceptions.InvalidURIException; import redis.clients.jedis.exceptions.JedisExhaustedPoolException; import redis.clients.jedis.tests.utils.RedisVersionUtil; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.concurrent.atomic.AtomicInteger; - -import static org.junit.Assert.*; - /** - * This test class is a copy of @JedisPoolTest where all authentications are made with - * default:foobared credentialsinformation + * This test class is a copy of {@link JedisPoolTest}. * * This test is only executed when the server/cluster is Redis 6. or more. */ public class JedisPoolWithCompleteCredentialsTest { - private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); + private static final HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); /** * Use to check if the ACL test should be ran. ACL are available only in 6.0 and later @@ -45,70 +47,60 @@ public void setUp() throws Exception { @Test public void checkConnections() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - Jedis jedis = pool.getResource(); - jedis.auth("default","foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - jedis.close(); - pool.destroy(); + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), "acljedis", "fizzbuzz"); + try (Jedis jedis = pool.getResource()) { + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + } + pool.close(); assertTrue(pool.isClosed()); } @Test public void checkCloseableConnections() throws Exception { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - Jedis jedis = pool.getResource(); - jedis.auth("default","foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - jedis.close(); + JedisPool pool = new JedisPool(hnp.getHost(), hnp.getPort(), "acljedis", "fizzbuzz"); + try (Jedis jedis = pool.getResource()) { + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + } pool.close(); assertTrue(pool.isClosed()); } @Test - public void checkConnectionWithDefaultPort() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort()); - Jedis jedis = pool.getResource(); - jedis.auth("default","foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - jedis.close(); - pool.destroy(); - assertTrue(pool.isClosed()); - } - - @Test - public void checkJedisIsReusedWhenReturned() { + public void checkResourceIsClosableAndReusable() { + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); + config.setMaxTotal(1); + config.setBlockWhenExhausted(false); + try (JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), + Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, 0 /*infinite*/, "acljedis", "fizzbuzz", + Protocol.DEFAULT_DATABASE, "closable-resuable-pool", false, null, null, null)) { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort()); - Jedis jedis = pool.getResource(); - jedis.auth("default","foobared"); - jedis.set("foo", "0"); - jedis.close(); + Jedis jedis = pool.getResource(); + jedis.set("hello", "jedis"); + jedis.close(); - jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.incr("foo"); - jedis.close(); - pool.destroy(); - assertTrue(pool.isClosed()); + Jedis jedis2 = pool.getResource(); + assertEquals(jedis, jedis2); + assertEquals("jedis", jedis2.get("hello")); + jedis2.close(); + } } @Test public void checkPoolRepairedWhenJedisIsBroken() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort()); - Jedis jedis = pool.getResource(); - jedis.auth("default","foobared"); - jedis.quit(); - jedis.close(); - - jedis = pool.getResource(); - jedis.auth("default", "foobared"); - jedis.incr("foo"); - jedis.close(); - pool.destroy(); + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), + Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, 0 /*infinite*/, "acljedis", "fizzbuzz", + Protocol.DEFAULT_DATABASE, "repairable-pool"); + try (Jedis jedis = pool.getResource()) { + jedis.set("foo", "0"); + jedis.quit(); + } + + try (Jedis jedis = pool.getResource()) { + jedis.incr("foo"); + } + pool.close(); assertTrue(pool.isClosed()); } @@ -117,25 +109,25 @@ public void checkPoolOverflow() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort()); - Jedis jedis = pool.getResource(); - jedis.auth("default", "foobared"); - jedis.set("foo", "0"); - - Jedis newJedis = pool.getResource(); - newJedis.auth("default", "foobared"); - newJedis.incr("foo"); + try (JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort()); + Jedis jedis = pool.getResource()) { + jedis.auth("acljedis", "fizzbuzz"); + + try (Jedis jedis2 = pool.getResource()) { + jedis2.auth("acljedis", "fizzbuzz"); + } + } } @Test public void securePool() { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "default","foobared"); - Jedis jedis = pool.getResource(); - jedis.set("foo", "bar"); - jedis.close(); - pool.destroy(); + JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "acljedis", "fizzbuzz"); + try (Jedis jedis = pool.getResource()) { + jedis.set("foo", "bar"); + } + pool.close(); assertTrue(pool.isClosed()); } @@ -143,333 +135,130 @@ public void securePool() { public void securePoolNonSSL() { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "default","foobared", false); - Jedis jedis = pool.getResource(); - jedis.set("foo", "bar"); - jedis.close(); - pool.destroy(); + JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "acljedis", "fizzbuzz", false); + try (Jedis jedis = pool.getResource()) { + jedis.set("foo", "bar"); + } + pool.close(); assertTrue(pool.isClosed()); } @Test public void nonDefaultDatabase() { - JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "default", - "foobared"); - Jedis jedis0 = pool0.getResource(); - jedis0.set("foo", "bar"); - assertEquals("bar", jedis0.get("foo")); - jedis0.close(); - pool0.destroy(); - assertTrue(pool0.isClosed()); - - JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared", 1); - Jedis jedis1 = pool1.getResource(); - assertNull(jedis1.get("foo")); - jedis1.close(); - pool1.destroy(); - assertTrue(pool1.isClosed()); - } + try (JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, + "acljedis", "fizzbuzz"); + Jedis jedis0 = pool0.getResource()) { + jedis0.set("foo", "bar"); + assertEquals("bar", jedis0.get("foo")); + } - @Test - public void nonDefaultDatabaseNonSSL() { - JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "default", - "foobared", false); - Jedis jedis0 = pool0.getResource(); - jedis0.set("foo", "bar"); - assertEquals("bar", jedis0.get("foo")); - jedis0.close(); - pool0.destroy(); - assertTrue(pool0.isClosed()); - - JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared", 1, false); - Jedis jedis1 = pool1.getResource(); - assertNull(jedis1.get("foo")); - jedis1.close(); - pool1.destroy(); - assertTrue(pool1.isClosed()); + try (JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, + "acljedis", "fizzbuzz", 1); + Jedis jedis1 = pool1.getResource()) { + assertNull(jedis1.get("foo")); + } } @Test public void startWithUrlString() { - Jedis j = new Jedis("localhost", 6380); - j.auth("default", "foobared"); - j.select(2); - j.set("foo", "bar"); - JedisPool pool = new JedisPool("redis://default:foobared@localhost:6380/2"); - Jedis jedis = pool.getResource(); - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); + try (Jedis j = new Jedis("localhost", 6379)) { + j.auth("acljedis", "fizzbuzz"); + j.select(2); + j.set("foo", "bar"); + } + + try (JedisPool pool = new JedisPool("redis://acljedis:fizzbuzz@localhost:6379/2"); + Jedis jedis = pool.getResource()) { + assertEquals("bar", jedis.get("foo")); + } } @Test public void startWithUrl() throws URISyntaxException { - Jedis j = new Jedis("localhost", 6380); - j.auth("default", "foobared"); - j.select(2); - j.set("foo", "bar"); - JedisPool pool = new JedisPool(new URI("redis://default:foobared@localhost:6380/2")); - Jedis jedis = pool.getResource(); - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); + try (Jedis j = new Jedis("localhost", 6379)) { + j.auth("acljedis", "fizzbuzz"); + j.select(2); + j.set("foo", "bar"); + } + + try (JedisPool pool = new JedisPool(new URI("redis://acljedis:fizzbuzz@localhost:6379/2")); + Jedis jedis = pool.getResource()) { + assertEquals("bar", jedis.get("foo")); + } + + try (JedisPool pool = new JedisPool(new URI("redis://default:foobared@localhost:6379/2")); + Jedis jedis = pool.getResource()) { + assertEquals("bar", jedis.get("foo")); + } } @Test(expected = InvalidURIException.class) public void shouldThrowInvalidURIExceptionForInvalidURI() throws URISyntaxException { - JedisPool pool = new JedisPool(new URI("localhost:6380")); - } - - @Test - public void connectWithURICredentials() throws URISyntaxException { - JedisPool pool = new JedisPool("localhost", 6380); - Jedis j = pool.getResource(); - - j.auth("default", "foobared"); - j.set("foo", "bar"); - - // create new user - j.aclSetUser("alice", "on", ">alicePassword", "~*", "+@all"); - - Jedis jedis = new Jedis(new URI("redis://default:foobared@localhost:6380")); - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); - - Jedis jedis2 = new Jedis(new URI("redis://alice:alicePassword@localhost:6380")); - assertEquals("PONG", jedis2.ping()); - assertEquals("bar", jedis2.get("foo")); - - // delete user - j.aclDelUser("alice"); + new JedisPool(new URI("localhost:6379")).close(); } @Test public void allowUrlWithNoDBAndNoPassword() throws URISyntaxException { - new JedisPool("redis://localhost:6380"); - new JedisPool(new URI("redis://localhost:6380")); + new JedisPool("redis://localhost:6379").close(); + new JedisPool(new URI("redis://localhost:6379")).close(); } @Test public void selectDatabaseOnActivation() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "default", "foobared"); - - Jedis jedis0 = pool.getResource(); - assertEquals(0, jedis0.getDB()); - - jedis0.select(1); - assertEquals(1, jedis0.getDB()); - - jedis0.close(); - - Jedis jedis1 = pool.getResource(); - assertTrue("Jedis instance was not reused", jedis1 == jedis0); - assertEquals(0, jedis1.getDB()); - - jedis1.close(); - pool.destroy(); - assertTrue(pool.isClosed()); - } - - @Test - public void customClientName() { - JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "default", "foobared", 0, "my_shiny_client_name"); - - Jedis jedis = pool0.getResource(); - - assertEquals("my_shiny_client_name", jedis.clientGetname()); - - jedis.close(); - pool0.destroy(); - assertTrue(pool0.isClosed()); - } - - @Test - public void customClientNameNoSSL() { - JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "default", "foobared", 0, "my_shiny_client_name_no_ssl", false); - - Jedis jedis = pool0.getResource(); - - assertEquals("my_shiny_client_name_no_ssl", jedis.clientGetname()); - - jedis.close(); - pool0.destroy(); - assertTrue(pool0.isClosed()); - } - - @Test - public void returnResourceDestroysResourceOnException() { - - class CrashingJedis extends Jedis { - @Override - public void resetState() { - throw new RuntimeException(); - } + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, + "acljedis", "fizzbuzz")) { + + Jedis jedis0 = pool.getResource(); + assertEquals(0, jedis0.getDB()); + + jedis0.select(1); + assertEquals(1, jedis0.getDB()); + + jedis0.close(); + + Jedis jedis1 = pool.getResource(); + assertTrue("Jedis instance was not reused", jedis1 == jedis0); + assertEquals(0, jedis1.getDB()); + + jedis1.close(); } - - final AtomicInteger destroyed = new AtomicInteger(0); - - class CrashingJedisPooledObjectFactory implements PooledObjectFactory { - - @Override - public PooledObject makeObject() throws Exception { - return new DefaultPooledObject(new CrashingJedis()); - } - - @Override - public void destroyObject(PooledObject p) throws Exception { - destroyed.incrementAndGet(); - } - - @Override - public boolean validateObject(PooledObject p) { - return true; - } - - @Override - public void activateObject(PooledObject p) throws Exception { - } - - @Override - public void passivateObject(PooledObject p) throws Exception { - } - } - - GenericObjectPoolConfig config = new GenericObjectPoolConfig(); - config.setMaxTotal(1); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "user", "foobared"); - pool.initPool(config, new CrashingJedisPooledObjectFactory()); - Jedis crashingJedis = pool.getResource(); - - try { - crashingJedis.close(); - } catch (Exception ignored) { - } - - assertEquals(1, destroyed.get()); } @Test - public void returnResourceShouldResetState() { - GenericObjectPoolConfig config = new GenericObjectPoolConfig(); - config.setMaxTotal(1); - config.setBlockWhenExhausted(false); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "default", "foobared"); - - Jedis jedis = pool.getResource(); - try { - jedis.set("hello", "jedis"); - Transaction t = jedis.multi(); - t.set("hello", "world"); - } finally { - jedis.close(); - } + public void customClientName() { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, + "acljedis", "fizzbuzz", 0, "my_shiny_client_name"); + Jedis jedis = pool.getResource()) { - Jedis jedis2 = pool.getResource(); - try { - assertTrue(jedis == jedis2); - assertEquals("jedis", jedis2.get("hello")); - } finally { - jedis2.close(); + assertEquals("my_shiny_client_name", jedis.clientGetname()); } - - pool.destroy(); - assertTrue(pool.isClosed()); } @Test - public void checkResourceIsCloseable() { - GenericObjectPoolConfig config = new GenericObjectPoolConfig(); - config.setMaxTotal(1); - config.setBlockWhenExhausted(false); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "default", "foobared"); - - Jedis jedis = pool.getResource(); - try { - jedis.set("hello", "jedis"); - } finally { - jedis.close(); - } - - Jedis jedis2 = pool.getResource(); - try { - assertEquals(jedis, jedis2); - } finally { - jedis2.close(); + public void customClientNameNoSSL() { + try (JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, + "acljedis", "fizzbuzz", 0, "my_shiny_client_name_no_ssl", false); + Jedis jedis = pool0.getResource()) { + + assertEquals("my_shiny_client_name_no_ssl", jedis.clientGetname()); } } - @Test - public void getNumActiveIsNegativeWhenPoolIsClosed() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "user", "foobared", 0, "my_shiny_client_name"); - - pool.destroy(); - assertTrue(pool.getNumActive() < 0); - } - - @Test - public void getNumActiveReturnsTheCorrectNumber() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - Jedis jedis = pool.getResource(); - jedis.auth("default","foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - - assertEquals(1, pool.getNumActive()); - - Jedis jedis2 = pool.getResource(); - jedis.auth("default","foobared"); - jedis.set("foo", "bar"); - - assertEquals(2, pool.getNumActive()); - - jedis.close(); - assertEquals(1, pool.getNumActive()); - - jedis2.close(); - - assertEquals(0, pool.getNumActive()); - - pool.destroy(); - } - - @Test - public void testAddObject() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - pool.addObjects(1); - assertEquals(1, pool.getNumIdle()); - pool.destroy(); - } - - @Test - public void closeResourceTwice() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); - Jedis j = pool.getResource(); - j.auth("default", "foobared"); - j.ping(); - j.close(); - j.close(); - } - - @Test public void testCloseConnectionOnMakeObject() { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "default", "wrong pass"); - Jedis jedis = new Jedis("redis://default:foobared@localhost:6379/"); - int currentClientCount = getClientCount(jedis.clientList()); - try { - pool.getResource(); - fail("Should throw exception as password is incorrect."); - } catch (Exception e) { - assertEquals(currentClientCount, getClientCount(jedis.clientList())); + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, + "acljedis", "foobared"); + Jedis jedis = new Jedis("redis://:foobared@localhost:6379/")) { + int currentClientCount = getClientCount(jedis.clientList()); + try { + pool.getResource(); + fail("Should throw exception as password is incorrect."); + } catch (Exception e) { + assertEquals(currentClientCount, getClientCount(jedis.clientList())); + } } - } private int getClientCount(final String clientList) { diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index 42436e716a..eaccdca09b 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -28,10 +28,10 @@ public class JedisTest extends JedisCommandTestBase { @Test public void useWithoutConnecting() { - Jedis jedis = new Jedis("localhost"); - jedis.auth("foobared"); - jedis.dbSize(); - jedis.close(); + try (Jedis j = new Jedis()) { + j.auth("foobared"); + j.dbSize(); + } } @Test @@ -135,16 +135,16 @@ public void shouldReconnectToSameDB() throws IOException { @Test public void startWithUrlString() { - Jedis j = new Jedis("localhost", 6380); - j.auth("foobared"); - j.select(2); - j.set("foo", "bar"); - j.close(); + try (Jedis j = new Jedis("localhost", 6380)) { + j.auth("foobared"); + j.select(2); + j.set("foo", "bar"); + } - Jedis jedis = new Jedis("redis://:foobared@localhost:6380/2"); - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); - jedis.close(); + try (Jedis j2 = new Jedis("redis://:foobared@localhost:6380/2")) { + assertEquals("PONG", j2.ping()); + assertEquals("bar", j2.get("foo")); + } } @Test @@ -175,19 +175,19 @@ public void shouldNotUpdateDbIndexIfSelectFails() { @Test public void allowUrlWithNoDBAndNoPassword() { - Jedis jedis = new Jedis("redis://localhost:6380"); - jedis.auth("foobared"); - assertEquals("localhost", jedis.getClient().getHost()); - assertEquals(6380, jedis.getClient().getPort()); - assertEquals(0, jedis.getDB()); - jedis.close(); + try (Jedis j1 = new Jedis("redis://localhost:6380")) { + j1.auth("foobared"); + assertEquals("localhost", j1.getClient().getHost()); + assertEquals(6380, j1.getClient().getPort()); + assertEquals(0, j1.getDB()); + } - jedis = new Jedis("redis://localhost:6380/"); - jedis.auth("foobared"); - assertEquals("localhost", jedis.getClient().getHost()); - assertEquals(6380, jedis.getClient().getPort()); - assertEquals(0, jedis.getDB()); - jedis.close(); + try (Jedis j2 = new Jedis("redis://localhost:6380/")) { + j2.auth("foobared"); + assertEquals("localhost", j2.getClient().getHost()); + assertEquals(6380, j2.getClient().getPort()); + assertEquals(0, j2.getDB()); + } } @Test diff --git a/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java index 5fa249e6ac..0bf609bef9 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java @@ -1,30 +1,20 @@ package redis.clients.jedis.tests; +import static org.junit.Assert.assertEquals; + +import java.net.URI; +import java.net.URISyntaxException; import org.junit.Before; import org.junit.Test; -import redis.clients.jedis.BinaryJedis; + import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.Protocol; -import redis.clients.jedis.exceptions.InvalidURIException; -import redis.clients.jedis.exceptions.JedisConnectionException; -import redis.clients.jedis.exceptions.JedisDataException; -import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.tests.commands.JedisCommandTestBase; import redis.clients.jedis.tests.utils.RedisVersionUtil; -import redis.clients.jedis.util.SafeEncoder; - -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.*; /** - * This test class is a copy of @JedisTest where all authentications are made with - * default:foobared credentialsinformation + * This test class is a copy of {@link JedisTest}. * * This test is only executed when the server/cluster is Redis 6. or more. */ @@ -45,192 +35,78 @@ public void setUp() throws Exception { @Test public void useWithoutConnecting() { - try(Jedis jedis = new Jedis("localhost")){ - assertEquals("OK", jedis.auth("default", "foobared")); - jedis.dbSize(); - } - } - - @Test - public void checkBinaryData() { - byte[] bigdata = new byte[1777]; - for (int b = 0; b < bigdata.length; b++) { - bigdata[b] = (byte) ((byte) b % 255); + try(Jedis j = new Jedis()){ + assertEquals("OK", j.auth("acljedis", "fizzbuzz")); + j.dbSize(); } - Map hash = new HashMap<>(); - hash.put("data", SafeEncoder.encode(bigdata)); - - String status = jedis.hmset("foo", hash); - assertEquals("OK", status); - assertEquals(hash, jedis.hgetAll("foo")); } @Test public void connectWithShardInfo() { JedisShardInfo shardInfo = new JedisShardInfo("localhost", Protocol.DEFAULT_PORT); - shardInfo.setUser("default"); - shardInfo.setPassword("foobared"); + shardInfo.setUser("acljedis"); + shardInfo.setPassword("fizzbuzz"); try(Jedis jedis = new Jedis(shardInfo)){ jedis.get("foo"); } } - @Test - public void timeoutConnection() throws Exception { - - String timeout = null; - try (Jedis jedis = new Jedis("localhost", 6379, 15000)){ - assertEquals("OK", jedis.auth("default", "foobared")); - timeout = jedis.configGet("timeout").get(1); - assertEquals("OK", jedis.configSet("timeout", "1")); - Thread.sleep(2000); - - jedis.hmget("foobar", "foo"); - fail("Operation should throw JedisConnectionException"); - } catch(JedisConnectionException jce) { - // expected - } - - // reset config - try(Jedis jedis2 = new Jedis("localhost", 6379)){ - assertEquals("OK", jedis2.auth("default", "foobared")); - assertEquals("OK", jedis2.configSet("timeout", timeout)); - } - } - - @Test - public void timeoutConnectionWithURI() throws Exception { - - String timeout = null; - try (Jedis jedis = new Jedis(new URI("redis://default:foobared@localhost:6380/2"), 15000)){ - timeout = jedis.configGet("timeout").get(1); - jedis.configSet("timeout", "1"); - Thread.sleep(2000); - - jedis.hmget("foobar", "foo"); - fail("Operation should throw JedisConnectionException"); - } catch(JedisConnectionException jce) { - // expected - } - - // reset config - try(Jedis jedis2 = new Jedis(new URI("redis://default:foobared@localhost:6380/2"))){ - jedis2.configSet("timeout", timeout); - } - } - - @Test(expected = JedisDataException.class) - public void failWhenSendingNullValues() { - jedis.set("foo", null); - } - - @Test(expected = InvalidURIException.class) - public void shouldThrowInvalidURIExceptionForInvalidURI() throws URISyntaxException { - try(Jedis j = new Jedis(new URI("localhost:6380"))){ - j.ping(); - } - } - - @Test - public void shouldReconnectToSameDB() throws IOException { - jedis.select(1); - jedis.set("foo", "bar"); - jedis.getClient().getSocket().shutdownInput(); - jedis.getClient().getSocket().shutdownOutput(); - assertEquals("bar", jedis.get("foo")); - } - @Test public void startWithUrlString() { - try(Jedis j = new Jedis("localhost", 6380)){ - assertEquals("OK", j.auth("default", "foobared")); + try(Jedis j = new Jedis("localhost", 6379)){ + assertEquals("OK", j.auth("acljedis", "fizzbuzz")); assertEquals("OK", j.select(2)); j.set("foo", "bar"); } - try(Jedis jedis = new Jedis("redis://default:foobared@localhost:6380/2")){ - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); + try(Jedis j2 = new Jedis("redis://acljedis:fizzbuzz@localhost:6379/2")){ + assertEquals("PONG", j2.ping()); + assertEquals("bar", j2.get("foo")); } } @Test public void startWithUrl() throws URISyntaxException { - try(Jedis j = new Jedis("localhost", 6380)){ - assertEquals("OK", j.auth("default","foobared")); + try(Jedis j = new Jedis("localhost", 6379)){ + assertEquals("OK", j.auth("acljedis", "fizzbuzz")); assertEquals("OK", j.select(2)); j.set("foo", "bar"); } - try(Jedis jedis = new Jedis(new URI("redis://default:foobared@localhost:6380/2"))){ - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); + try(Jedis j2 = new Jedis(new URI("redis://acljedis:fizzbuzz@localhost:6379/2"))){ + assertEquals("PONG", j2.ping()); + assertEquals("bar", j2.get("foo")); } } @Test - public void shouldNotUpdateDbIndexIfSelectFails() throws URISyntaxException { - int currentDb = jedis.getDB(); - try { - int invalidDb = -1; - jedis.select(invalidDb); - - fail("Should throw an exception if tried to select invalid db"); - } catch (JedisException e) { - assertEquals(currentDb, jedis.getDB()); + public void connectWithURICredentials() throws URISyntaxException { + jedis.set("foo", "bar"); + + try (Jedis j1 = new Jedis(new URI("redis://default:foobared@localhost:6379"))) { + assertEquals("PONG", j1.ping()); + assertEquals("bar", j1.get("foo")); } - } - @Test - public void connectWithURICredentials() throws URISyntaxException { - try(Jedis j = new Jedis("localhost")){ - j.auth("default", "foobared"); - j.set("foo", "bar"); - - // create new user - j.aclSetUser("alice", "on", ">alicePassword", "~*", "+@all"); - - try(Jedis jedis = new Jedis(new URI("redis://default:foobared@localhost:6379"))){ - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); - } - - try(Jedis jedis = new Jedis(new URI("redis://alice:alicePassword@localhost:6379"))){ - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); - } - - // delete user - j.aclDelUser("alice"); + try (Jedis j2 = new Jedis(new URI("redis://acljedis:fizzbuzz@localhost:6379"))) { + assertEquals("PONG", j2.ping()); + assertEquals("bar", j2.get("foo")); } } @Test public void allowUrlWithNoDBAndNoPassword() { - try(Jedis jedis = new Jedis("redis://localhost:6380")){ - assertEquals("OK", jedis.auth("default", "foobared")); - assertEquals("localhost", jedis.getClient().getHost()); - assertEquals(6380, jedis.getClient().getPort()); - assertEquals(0, jedis.getDB()); + try(Jedis j1 = new Jedis("redis://localhost:6379")){ + assertEquals("OK", j1.auth("acljedis", "fizzbuzz")); + assertEquals("localhost", j1.getClient().getHost()); + assertEquals(6379, j1.getClient().getPort()); + assertEquals(0, j1.getDB()); } - try(Jedis jedis = new Jedis("redis://localhost:6380/")) { - assertEquals("OK", jedis.auth("default", "foobared")); - assertEquals("localhost", jedis.getClient().getHost()); - assertEquals(6380, jedis.getClient().getPort()); - assertEquals(0, jedis.getDB()); - } - } - @Test - public void checkCloseable() { - jedis.close(); - try(BinaryJedis bj = new BinaryJedis("localhost")){ - bj.connect(); + try(Jedis j2 = new Jedis("redis://localhost:6379/")) { + assertEquals("OK", j2.auth("acljedis", "fizzbuzz")); + assertEquals("localhost", j2.getClient().getHost()); + assertEquals(6379, j2.getClient().getPort()); + assertEquals(0, j2.getDB()); } } - @Test - public void checkDisconnectOnQuit() { - jedis.quit(); - assertFalse(jedis.getClient().isConnected()); - } - } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java b/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java index b7e5cfb0d0..72f6b6a9ef 100644 --- a/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java @@ -53,10 +53,10 @@ private static void setJvmTrustStore(String trustStoreFilePath, String trustStor @Test public void connectWithUrl() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. - Jedis jedis = new Jedis("rediss://localhost:6390"); - jedis.auth("foobared"); - assertEquals("PONG", jedis.ping()); - jedis.close(); + try (Jedis jedis = new Jedis("rediss://localhost:6390")) { + jedis.auth("foobared"); + assertEquals("PONG", jedis.ping()); + } } /** @@ -65,10 +65,10 @@ public void connectWithUrl() { @Test public void connectWithoutShardInfo() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. - Jedis jedis = new Jedis(URI.create("rediss://localhost:6390")); - jedis.auth("foobared"); - assertEquals("PONG", jedis.ping()); - jedis.close(); + try (Jedis jedis = new Jedis(URI.create("rediss://localhost:6390"))) { + jedis.auth("foobared"); + assertEquals("PONG", jedis.ping()); + } } /** diff --git a/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java index 47efd7ace3..ecb729c5d4 100644 --- a/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java @@ -23,9 +23,7 @@ import static org.junit.Assert.*; /** - * This test class is a copy of @SSLJedisTest - * where all authentications are made with - * default:foobared credentialsinformation + * This test class is a copy of {@link SSLJedisTest}. * * This test is only executed when the server/cluster is Redis 6. or more. */ @@ -67,10 +65,14 @@ private static void setJvmTrustStore(String trustStoreFilePath, String trustStor @Test public void connectWithUrl() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. - Jedis jedis = new Jedis("rediss://localhost:6390"); - jedis.auth("default", "foobared"); - assertEquals("PONG", jedis.ping()); - jedis.close(); + try (Jedis jedis = new Jedis("rediss://localhost:6390")) { + jedis.auth("default", "foobared"); + assertEquals("PONG", jedis.ping()); + } + try (Jedis jedis = new Jedis("rediss://localhost:6390")) { + jedis.auth("acljedis", "fizzbuzz"); + assertEquals("PONG", jedis.ping()); + } } /** @@ -79,17 +81,12 @@ public void connectWithUrl() { @Test public void connectWithUrlAndCompleteCredentials() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. - Jedis jedis = new Jedis("rediss://default:foobared@localhost:6390"); - assertEquals("PONG", jedis.ping()); - - // create user - jedis.aclSetUser("alice", "on", ">alicePassword", "~*", "+@all"); - - Jedis jedis2 = new Jedis("rediss://alice:alicePassword@localhost:6390"); - assertEquals("PONG", jedis2.ping()); - - jedis.aclDelUser("alice"); - jedis.close(); + try (Jedis jedis = new Jedis("rediss://default:foobared@localhost:6390")) { + assertEquals("PONG", jedis.ping()); + } + try (Jedis jedis = new Jedis("rediss://acljedis:fizzbuzz@localhost:6390")) { + assertEquals("PONG", jedis.ping()); + } } @@ -99,10 +96,10 @@ public void connectWithUrlAndCompleteCredentials() { @Test public void connectWithoutShardInfo() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. - Jedis jedis = new Jedis(URI.create("rediss://localhost:6390")); - jedis.auth("default", "foobared"); - assertEquals("PONG", jedis.ping()); - jedis.close(); + try (Jedis jedis = new Jedis(URI.create("rediss://localhost:6390"))) { + jedis.auth("acljedis", "fizzbuzz"); + assertEquals("PONG", jedis.ping()); + } } /** @@ -122,8 +119,8 @@ public void connectWithShardInfo() throws Exception { sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null); - shardInfo.setUser("default"); - shardInfo.setPassword("foobared"); + shardInfo.setUser("acljedis"); + shardInfo.setPassword("fizzbuzz"); Jedis jedis = new Jedis(shardInfo); assertEquals("PONG", jedis.ping()); @@ -152,8 +149,8 @@ public void connectWithShardInfoByIpAddress() throws Exception { sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null); - shardInfo.setUser("default"); - shardInfo.setPassword("foobared"); + shardInfo.setUser("acljedis"); + shardInfo.setPassword("fizzbuzz"); Jedis jedis = new Jedis(shardInfo); try { @@ -185,8 +182,8 @@ public void connectWithShardInfoAndCustomHostnameVerifier() { HostnameVerifier hostnameVerifier = new BasicHostnameVerifier(); JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); - shardInfo.setUser("default"); - shardInfo.setPassword("foobared"); + shardInfo.setUser("acljedis"); + shardInfo.setPassword("fizzbuzz"); Jedis jedis = new Jedis(shardInfo); assertEquals("PONG", jedis.ping()); @@ -205,8 +202,8 @@ public void connectWithShardInfoAndCustomSocketFactory() throws Exception { HostnameVerifier hostnameVerifier = new BasicHostnameVerifier(); JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); - shardInfo.setUser("default"); - shardInfo.setPassword("foobared"); + shardInfo.setUser("acljedis"); + shardInfo.setPassword("fizzbuzz"); Jedis jedis = new Jedis(shardInfo); assertEquals("PONG", jedis.ping()); @@ -228,8 +225,8 @@ public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() { HostnameVerifier hostnameVerifier = new BasicHostnameVerifier(); JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); - shardInfo.setUser("default"); - shardInfo.setPassword("foobared"); + shardInfo.setUser("acljedis"); + shardInfo.setPassword("fizzbuzz"); Jedis jedis = new Jedis(shardInfo); try { @@ -261,8 +258,8 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception { final SSLSocketFactory sslSocketFactory = createTrustNoOneSslSocketFactory(); JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, null, null); - shardInfo.setUser("default"); - shardInfo.setPassword("foobared"); + shardInfo.setUser("acljedis"); + shardInfo.setPassword("fizzbuzz"); Jedis jedis = new Jedis(shardInfo); try { diff --git a/src/test/java/redis/clients/jedis/tests/commands/AccessControlListCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/AccessControlListCommandsTest.java index b95677a7fc..f018d926e7 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AccessControlListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AccessControlListCommandsTest.java @@ -1,22 +1,23 @@ package redis.clients.jedis.tests.commands; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.List; +import org.junit.Before; +import org.junit.Test; + import redis.clients.jedis.AccessControlUser; import redis.clients.jedis.Jedis; import redis.clients.jedis.Transaction; import redis.clients.jedis.exceptions.JedisAccessControlException; import redis.clients.jedis.tests.utils.RedisVersionUtil; - -import java.util.List; - -import static org.junit.Assert.*; +import redis.clients.jedis.util.SafeEncoder; // TODO :properly define and test exceptions - public class AccessControlListCommandsTest extends JedisCommandTestBase { - public static String USER_YYY = "yyy"; public static String USER_ZZZ = "zzz"; public static String USER_ZZZ_PASSWORD = "secret"; @@ -36,36 +37,40 @@ public void setUp() throws Exception { @Test public void aclWhoAmI() { - String returnValue = jedis.aclWhoAmI(); - assertEquals("default", returnValue); - } + String string = jedis.aclWhoAmI(); + assertEquals("default", string); - @Test - public void aclWhoAmIBinary() { - byte[] returnValue = jedis.aclWhoAmIBinary(); - assertNotNull(returnValue); + byte[] binary = jedis.aclWhoAmIBinary(); + assertArrayEquals(SafeEncoder.encode("default"), binary); } @Test public void aclListDefault() { - assertEquals(1, jedis.aclList().size()); - } - - @Test - public void aclListBinaryDefault() { - assertEquals(1, jedis.aclListBinary().size()); + assertTrue(jedis.aclList().size() > 0); + assertTrue(jedis.aclListBinary().size() > 0); } @Test public void addAndRemoveUser() { + int existingUsers = jedis.aclList().size(); + String status = jedis.aclSetUser(USER_ZZZ); assertEquals("OK", status); - assertEquals(2, jedis.aclList().size()); - assertEquals(2, jedis.aclListBinary().size()); // test binary + assertEquals(existingUsers + 1, jedis.aclList().size()); + assertEquals(existingUsers + 1, jedis.aclListBinary().size()); // test binary jedis.aclDelUser(USER_ZZZ); - assertEquals(1, jedis.aclList().size()); - assertEquals(1, jedis.aclListBinary().size()); // test binary + assertEquals(existingUsers, jedis.aclList().size()); + assertEquals(existingUsers, jedis.aclListBinary().size()); // test binary + } + + @Test + public void aclUsers() { + List users = jedis.aclUsers(); + assertEquals(2, users.size()); + assertTrue(users.contains("default")); + + assertEquals(2, jedis.aclUsersBinary().size()); // Test binary } @Test @@ -94,9 +99,9 @@ public void aclGetUser() { jedis.aclSetUser(USER_ZZZ, "reset", "+@all", "~*", "-@string", "+incr", "-debug", "+debug|digest"); userInfo = jedis.aclGetUser(USER_ZZZ); - Assert.assertThat(userInfo.getCommands(), containsString("+@all")); - Assert.assertThat(userInfo.getCommands(), containsString("-@string")); - Assert.assertThat(userInfo.getCommands(), containsString("+debug|digest")); + assertThat(userInfo.getCommands(), containsString("+@all")); + assertThat(userInfo.getCommands(), containsString("-@string")); + assertThat(userInfo.getCommands(), containsString("+debug|digest")); jedis.aclDelUser(USER_ZZZ); @@ -433,35 +438,12 @@ public void aclGenPassBinary() { assertNotNull( jedis.aclGenPassBinary() ); } - @Test - public void aclUsers() { - List users = jedis.aclUsers(); - assertEquals( 1, users.size() ); - assertEquals( "default", users.get(0) ); - - assertEquals( 1, jedis.aclUsersBinary().size() ); // Test binary - - //add new user - jedis.aclSetUser(USER_ZZZ); - users = jedis.aclUsers(); - assertEquals( 2, users.size() ); - assertEquals( "default", users.get(0) ); - assertEquals( USER_ZZZ, users.get(1) ); - - assertEquals( 2, jedis.aclUsersBinary().size() ); // Test binary - - //delete user - jedis.aclDelUser(USER_ZZZ); - - } - @Test public void aclBinaryCommandsTest() { jedis.aclSetUser(USER_ZZZ.getBytes()); - assertEquals(2, jedis.aclList().size()); - assertNotNull( jedis.aclGetUser(USER_ZZZ) ); + assertNotNull(jedis.aclGetUser(USER_ZZZ)); - assertEquals( new Long(1) , jedis.aclDelUser(USER_ZZZ.getBytes()) ); + assertEquals(Long.valueOf(1L), jedis.aclDelUser(USER_ZZZ.getBytes())); jedis.aclSetUser(USER_ZZZ.getBytes(), "reset".getBytes(), @@ -474,9 +456,9 @@ public void aclBinaryCommandsTest() { AccessControlUser userInfo = jedis.aclGetUser(USER_ZZZ.getBytes()); - Assert.assertThat(userInfo.getCommands(), containsString("+@all")); - Assert.assertThat(userInfo.getCommands(), containsString("-@string")); - Assert.assertThat(userInfo.getCommands(), containsString("+debug|digest")); + assertThat(userInfo.getCommands(), containsString("+@all")); + assertThat(userInfo.getCommands(), containsString("-@string")); + assertThat(userInfo.getCommands(), containsString("+debug|digest")); jedis.aclDelUser(USER_ZZZ.getBytes()); }