Skip to content

Commit 123c696

Browse files
committed
Remove more hardcoded connection settings
1 parent 97b3666 commit 123c696

18 files changed

+175
-148
lines changed

src/test/java/redis/clients/jedis/ACLJedisPoolTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ACLJedisPoolTest {
2929
public static void prepare() throws Exception {
3030
// Use to check if the ACL test should be ran. ACL are available only in 6.0 and later
3131
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
32-
RedisVersionUtil.checkRedisMajorVersionNumber(6));
32+
RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint));
3333
}
3434

3535
@Test

src/test/java/redis/clients/jedis/ACLJedisSentinelPoolTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public class ACLJedisSentinelPoolTest {
3030

3131
@BeforeClass
3232
public static void prepare() throws Exception {
33+
EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone2-primary");
3334
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
34-
RedisVersionUtil.checkRedisMajorVersionNumber(6));
35+
RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint));
3536
}
3637

3738
@Before

src/test/java/redis/clients/jedis/ACLJedisTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ACLJedisTest extends JedisCommandsTestBase {
2828
@BeforeClass
2929
public static void prepare() throws Exception {
3030
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
31-
RedisVersionUtil.checkRedisMajorVersionNumber(6));
31+
RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint));
3232
}
3333

3434
public ACLJedisTest(RedisProtocol redisProtocol) {
Lines changed: 82 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,94 @@
11
package redis.clients.jedis;
2+
23
import com.google.gson.Gson;
34
import com.google.gson.reflect.TypeToken;
45
import redis.clients.jedis.util.JedisURIHelper;
56

67
import java.io.FileReader;
78
import java.net.URI;
8-
import java.util.HashMap;
9-
import java.util.List;
9+
import java.util.*;
1010

1111
public class EndpointConfig {
1212

13-
private boolean tls;
14-
private String username;
15-
private String password;
16-
private int bdbId;
17-
private Object rawEndpoints;
18-
19-
private List<URI> endpoints;
20-
21-
public EndpointConfig(boolean tls, String username, String password, int bdbId, Object rawEndpoints) {
22-
this.tls = tls;
23-
this.username = username;
24-
this.password = password;
25-
this.bdbId = bdbId;
26-
this.rawEndpoints = rawEndpoints;
27-
}
28-
29-
public HostAndPort getHostAndPort() {
30-
return JedisURIHelper.getHostAndPort(endpoints.get(0));
31-
}
32-
33-
public String getPassword() {
34-
return password;
35-
}
36-
37-
public String getUsername() {
38-
return username;
39-
}
40-
41-
public String getHost() {
42-
return getHostAndPort().getHost();
43-
}
44-
45-
public int getPort() {
46-
return getHostAndPort().getPort();
47-
}
48-
49-
public URI getURI() {
50-
return endpoints.get(0);
51-
}
52-
53-
public URI getCustomizedURI(boolean withCredentials, String path)
54-
{
55-
return getCustomizedURI(withCredentials ? username : "", withCredentials ? password : "", path);
56-
}
57-
58-
public URI getCustomizedURI(String u, String p, String path)
59-
{
60-
String userInfo = !(u.isEmpty() && p.isEmpty()) ? u + ":" + p + "@" : "";
61-
return URI.create((tls ? "rediss" : "redis") + "://" + userInfo + getHost() + ":" + getPort() + path);
62-
}
63-
64-
public Connection getConnection() {
65-
return new Connection(getHostAndPort(), getClientConfigBuilder().build());
66-
}
67-
68-
public Connection getConnection(int timeoutMillis) {
69-
return new Connection(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build());
70-
}
71-
72-
public Jedis getJedis() {
73-
return new Jedis(getHostAndPort(), getClientConfigBuilder().build());
74-
}
75-
76-
public Jedis getJedis(int timeoutMillis) {
77-
return new Jedis(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build());
78-
}
79-
80-
public DefaultJedisClientConfig.Builder getClientConfigBuilder() {
81-
return DefaultJedisClientConfig.builder().user(username).password(password);
82-
}
83-
84-
public static HashMap<String, EndpointConfig> loadFromJSON(String filePath) throws Exception {
85-
Gson gson = new Gson();
86-
HashMap<String, EndpointConfig> configs;
87-
try (FileReader reader = new FileReader(filePath)) {
88-
configs = gson.fromJson(reader, new TypeToken<HashMap<String, EndpointConfig>>() {
89-
}.getType());
13+
private boolean tls;
14+
private String username;
15+
private String password;
16+
private int bdbId;
17+
private Object rawEndpoints;
18+
private List<URI> endpoints;
19+
20+
21+
public EndpointConfig(HostAndPort hnp, String username, String password) {
22+
this.tls = false;
23+
this.username = username;
24+
this.password = password;
25+
this.bdbId = 0;
26+
this.rawEndpoints = null;
27+
this.endpoints = Collections.singletonList(
28+
URI.create("redis://" + hnp.getHost() + ":" + hnp.getPort())
29+
);
30+
}
31+
32+
public HostAndPort getHostAndPort() {
33+
return JedisURIHelper.getHostAndPort(endpoints.get(0));
34+
}
35+
36+
public String getPassword() {
37+
return password;
38+
}
39+
40+
public String getUsername() {
41+
return username;
42+
}
43+
44+
public String getHost() {
45+
return getHostAndPort().getHost();
46+
}
47+
48+
public int getPort() {
49+
return getHostAndPort().getPort();
50+
}
51+
52+
public URI getURI() {
53+
return endpoints.get(0);
54+
}
55+
56+
public URI getCustomizedURI(boolean withCredentials, String path) {
57+
return getCustomizedURI(withCredentials ? username : "", withCredentials ? password : "", path);
58+
}
59+
60+
public URI getCustomizedURI(String u, String p, String path) {
61+
String userInfo = !(u.isEmpty() && p.isEmpty()) ? u + ":" + p + "@" : "";
62+
return URI.create((tls ? "rediss" : "redis") + "://" + userInfo + getHost() + ":" + getPort() + path);
63+
}
64+
65+
public Connection getConnection() {
66+
return new Connection(getHostAndPort(), getClientConfigBuilder().build());
67+
}
68+
69+
public Connection getConnection(int timeoutMillis) {
70+
return new Connection(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build());
71+
}
72+
73+
public Jedis getJedis() {
74+
return new Jedis(getHostAndPort(), getClientConfigBuilder().build());
75+
}
76+
77+
public Jedis getJedis(int timeoutMillis) {
78+
return new Jedis(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build());
79+
}
80+
81+
public DefaultJedisClientConfig.Builder getClientConfigBuilder() {
82+
return DefaultJedisClientConfig.builder().user(username).password(password).ssl(tls);
83+
}
84+
85+
public static HashMap<String, EndpointConfig> loadFromJSON(String filePath) throws Exception {
86+
Gson gson = new Gson();
87+
HashMap<String, EndpointConfig> configs;
88+
try (FileReader reader = new FileReader(filePath)) {
89+
configs = gson.fromJson(reader, new TypeToken<HashMap<String, EndpointConfig>>() {
90+
}.getType());
91+
}
92+
return configs;
9093
}
91-
return configs;
92-
}
9394
}

src/test/java/redis/clients/jedis/JedisTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public JedisTest(RedisProtocol protocol) {
3636
@Test
3737
public void useWithoutConnecting() {
3838
try (Jedis j = new Jedis()) {
39-
j.auth("foobared");
39+
j.auth(endpoint.getPassword());
4040
j.dbSize();
4141
}
4242
}
@@ -240,15 +240,17 @@ public void shouldNotUpdateDbIndexIfSelectFails() {
240240

241241
@Test
242242
public void allowUrlWithNoDBAndNoPassword() {
243-
try (Jedis j1 = new Jedis("redis://localhost:6380")) {
244-
j1.auth("foobared");
243+
EndpointConfig endpoint1 = HostAndPorts.getRedisEndpoint("standalone1");
244+
245+
try (Jedis j1 = new Jedis(endpoint1.getURI().toString())) {
246+
j1.auth(endpoint1.getPassword());
245247
// assertEquals("localhost", j1.getClient().getHost());
246248
// assertEquals(6380, j1.getClient().getPort());
247249
assertEquals(0, j1.getDB());
248250
}
249251

250-
try (Jedis j2 = new Jedis("redis://localhost:6380/")) {
251-
j2.auth("foobared");
252+
try (Jedis j2 = new Jedis(endpoint1.getURI().toString())) {
253+
j2.auth(endpoint1.getPassword());
252254
// assertEquals("localhost", j2.getClient().getHost());
253255
// assertEquals(6380, j2.getClient().getPort());
254256
assertEquals(0, j2.getDB());
@@ -288,7 +290,7 @@ public void checkCloseableAfterConnect() {
288290
@Test
289291
public void checkCloseableAfterCommand() {
290292
Jedis bj = new Jedis();
291-
bj.auth("foobared");
293+
bj.auth(endpoint.getPassword());
292294
bj.close();
293295
}
294296

src/test/java/redis/clients/jedis/MigratePipeliningTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void setUp() throws Exception {
5757
dest.select(db);
5858

5959
destAuth = new Jedis(host, portAuth, 500);
60-
destAuth.auth("foobared");
60+
destAuth.auth(endpoint.getPassword());
6161
destAuth.flushAll();
6262
destAuth.select(dbAuth);
6363
}
@@ -258,7 +258,7 @@ public void migrateAuth() {
258258
Pipeline p = jedis.pipelined();
259259

260260
p.set("foo", "bar");
261-
p.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth("foobared"), "foo");
261+
p.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth(endpoint.getPassword()), "foo");
262262
p.get("foo");
263263

264264
assertThat(p.syncAndReturnAll(),
@@ -274,7 +274,7 @@ public void migrateAuthBinary() {
274274
Pipeline p = jedis.pipelined();
275275

276276
p.set(bfoo, bbar);
277-
p.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth("foobared"), bfoo);
277+
p.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth(endpoint.getPassword()), bfoo);
278278
p.get(bfoo);
279279

280280
assertThat(p.syncAndReturnAll(),
@@ -287,6 +287,8 @@ public void migrateAuthBinary() {
287287
public void migrateAuth2() {
288288
assertNull(jedis.get("foo"));
289289

290+
291+
290292
Pipeline p = destAuth.pipelined();
291293

292294
p.set("foo", "bar");

src/test/java/redis/clients/jedis/PipeliningTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ public void testSyncWithNoCommandQueued() {
547547
public void testCloseable() throws IOException {
548548
// we need to test with fresh instance of Jedis
549549
Jedis jedis2 = new Jedis(endpoint.getHost(), endpoint.getPort(), 500);
550-
jedis2.auth("foobared");
550+
jedis2.auth(endpoint.getPassword());
551551

552552
Pipeline pipeline = jedis2.pipelined();
553553
Response<String> retFuture1 = pipeline.set("a", "1");

src/test/java/redis/clients/jedis/SSLACLJedisClusterTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ public class SSLACLJedisClusterTest extends JedisClusterTestBase {
4343

4444
@BeforeClass
4545
public static void prepare() {
46+
// TODO(imalinovskyi): Remove hardcoded connection settings
47+
// once this test is refactored to support RE
4648
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
47-
RedisVersionUtil.checkRedisMajorVersionNumber(6));
49+
RedisVersionUtil.checkRedisMajorVersionNumber(6,
50+
new EndpointConfig(new HostAndPort("localhost", 8379),
51+
"default", "cluster")));
4852

4953
SSLJedisTest.setupTrustStore();
5054
}

src/test/java/redis/clients/jedis/SSLACLJedisTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.io.FileInputStream;
66
import java.io.InputStream;
7-
import java.net.URI;
87
import java.security.InvalidAlgorithmParameterException;
98
import java.security.KeyStore;
109
import java.security.SecureRandom;
@@ -23,50 +22,53 @@
2322
*/
2423
public class SSLACLJedisTest {
2524

25+
protected static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0-acl-tls");
26+
27+
protected static final EndpointConfig endpointWithDefaultUser = HostAndPorts.getRedisEndpoint("standalone0-tls");
28+
2629
@BeforeClass
2730
public static void prepare() {
2831
// Use to check if the ACL test should be ran. ACL are available only in 6.0 and later
29-
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
30-
RedisVersionUtil.checkRedisMajorVersionNumber(6));
31-
3232
SSLJedisTest.setupTrustStore();
33+
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
34+
RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint));
3335
}
3436

3537
@Test
3638
public void connectWithSsl() {
37-
try (Jedis jedis = new Jedis("localhost", 6390, true)) {
38-
jedis.auth("acljedis", "fizzbuzz");
39+
try (Jedis jedis = new Jedis(endpoint.getHost(), endpoint.getPort(), true)) {
40+
jedis.auth(endpoint.getUsername(), endpoint.getPassword());
3941
assertEquals("PONG", jedis.ping());
4042
}
4143
}
4244

4345
@Test
4446
public void connectWithConfig() {
45-
try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390),
47+
try (Jedis jedis = new Jedis(endpoint.getHostAndPort(),
4648
DefaultJedisClientConfig.builder().ssl(true).build())) {
47-
jedis.auth("acljedis", "fizzbuzz");
49+
jedis.auth(endpoint.getUsername(), endpoint.getPassword());
4850
assertEquals("PONG", jedis.ping());
4951
}
5052
}
5153

5254
@Test
5355
public void connectWithUrl() {
5456
// The "rediss" scheme instructs jedis to open a SSL/TLS connection.
55-
try (Jedis jedis = new Jedis("rediss://default:foobared@localhost:6390")) {
57+
try (Jedis jedis = new Jedis(endpointWithDefaultUser.getCustomizedURI(true, "").toString())) {
5658
assertEquals("PONG", jedis.ping());
5759
}
58-
try (Jedis jedis = new Jedis("rediss://acljedis:fizzbuzz@localhost:6390")) {
60+
try (Jedis jedis = new Jedis(endpoint.getCustomizedURI(true, "").toString())) {
5961
assertEquals("PONG", jedis.ping());
6062
}
6163
}
6264

6365
@Test
6466
public void connectWithUri() {
6567
// The "rediss" scheme instructs jedis to open a SSL/TLS connection.
66-
try (Jedis jedis = new Jedis(URI.create("rediss://default:foobared@localhost:6390"))) {
68+
try (Jedis jedis = new Jedis(endpointWithDefaultUser.getCustomizedURI(true, ""))) {
6769
assertEquals("PONG", jedis.ping());
6870
}
69-
try (Jedis jedis = new Jedis(URI.create("rediss://acljedis:fizzbuzz@localhost:6390"))) {
71+
try (Jedis jedis = new Jedis(endpoint.getCustomizedURI(true, ""))) {
7072
assertEquals("PONG", jedis.ping());
7173
}
7274
}

0 commit comments

Comments
 (0)