Skip to content

Commit

Permalink
RedisURI: fixes + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirisa committed Nov 14, 2022
1 parent f1d738a commit 1b532f4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
11 changes: 5 additions & 6 deletions driver/src/main/java/jdbc/client/RedisJedisURIBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ protected RedisJedisURIBase(String url, Properties info) {
int questionIndex = uri.indexOf('?');
if (slashIndex >= 0) {
hostAndPortBlock = uri.substring(0, slashIndex);
uri = uri.substring(slashIndex);
uri = uri.substring(slashIndex + 1);
} else if (questionIndex >= 0) {
hostAndPortBlock = uri.substring(0, questionIndex);
uri = uri.substring(questionIndex);
uri = uri.substring(questionIndex + 1);
} else {
hostAndPortBlock = uri;
uri = "";
Expand All @@ -54,10 +54,10 @@ protected RedisJedisURIBase(String url, Properties info) {

String databaseBlock = "";
if (slashIndex >= 0) {
uri = uri.replaceFirst("/", "");
questionIndex = uri.indexOf('?');
if (questionIndex >= 0) {
databaseBlock = uri.substring(0, questionIndex);
uri = uri.substring(questionIndex);
uri = uri.substring(questionIndex + 1);
} else {
databaseBlock = uri;
uri = "";
Expand All @@ -67,7 +67,6 @@ protected RedisJedisURIBase(String url, Properties info) {

String parametersBlock = "";
if (questionIndex >= 0) {
uri = uri.replaceFirst("\\?", "");
parametersBlock = uri;
}
setParameters(parametersBlock, info);
Expand Down Expand Up @@ -118,7 +117,7 @@ private void setDatabase(@NotNull String databaseBlock, Properties info) {

private void setParameters(@NotNull String parametersBlock, Properties info) {
Map<String, String> parameters = new HashMap<>();
String[] parametersParts = parametersBlock.split(",");
String[] parametersParts = parametersBlock.split("&");
for (String parameterBlock : parametersParts) {
if (!parameterBlock.isEmpty()) {
String[] nodeParts = parameterBlock.split("=", 2);
Expand Down
50 changes: 50 additions & 0 deletions driver/src/test/jdbc/client/RedisURITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.util.Comparator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class RedisURITest {

@Test
public void testURI() {
RedisJedisURI uri = new RedisJedisURI("jdbc:redis://user:password@server:6380/7", null);
Expand All @@ -19,6 +21,54 @@ public void testURI() {
assertEquals(7, uri.getDatabase());
}

@Test
public void testURIEmptyUser() {
RedisJedisURI uri = new RedisJedisURI("jdbc:redis://password@server:6380/7", null);
assertNull(uri.getUser());
assertEquals("password", uri.getPassword());
}

@Test
public void testURIEmptyAuth() {
RedisJedisURI uri = new RedisJedisURI("jdbc:redis://server:6380/7", null);
assertNull(uri.getUser());
assertNull(uri.getPassword());
}

@Test
public void testURIEmptyDatabase() {
RedisJedisURI uri = new RedisJedisURI("jdbc:redis://server:6380", null);
assertEquals(0, uri.getDatabase());
}

@Test
public void testURIEmptyDatabaseAfterSlash() {
RedisJedisURI uri = new RedisJedisURI("jdbc:redis://user:password@server:6380/", null);
assertEquals(0, uri.getDatabase());
}

@Test
public void testURIParamsAfterDatabase() {
RedisJedisURI uri = new RedisJedisURI("jdbc:redis://user:password@server:6380/7?connectionTimeout=1000&socketTimeout=3000", null);
assertEquals(1000, uri.getConnectionTimeoutMillis());
assertEquals(3000, uri.getSocketTimeoutMillis());
}

@Test
public void testURIParamsAfterSlash() {
RedisJedisURI uri = new RedisJedisURI("jdbc:redis://user:password@server:6380/?connectionTimeout=1000&socketTimeout=3000", null);
assertEquals(1000, uri.getConnectionTimeoutMillis());
assertEquals(3000, uri.getSocketTimeoutMillis());
}


@Test
public void testURIParamsAfterHostAndPort() {
RedisJedisURI uri = new RedisJedisURI("jdbc:redis://user:password@server:6380?connectionTimeout=1000&socketTimeout=3000", null);
assertEquals(1000, uri.getConnectionTimeoutMillis());
assertEquals(3000, uri.getSocketTimeoutMillis());
}

@Test
public void testClusterURI() {
RedisJedisClusterURI uri = new RedisJedisClusterURI("jdbc:redis:cluster://user:password@host0:6379,host1:6380,host2:6381,host3:6382,host4:6383,host5:6384?maxAttempts=6", null);
Expand Down

0 comments on commit 1b532f4

Please sign in to comment.