diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 4e1c42e57c..96ddac20c8 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -73,7 +73,7 @@ public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clie @Override public String toString() { - return "Connection{" + socketFactory + "}"; + return getClass().getSimpleName() + "{" + socketFactory + "}"; } @Experimental @@ -82,52 +82,22 @@ public String toIdentityString() { return strVal; } + String className = getClass().getSimpleName(); int id = hashCode(); - String classInfo = getClass().toString(); if (socket == null) { - StringBuilder buf = new StringBuilder(56) - .append("[") - .append(classInfo) - .append(", id: 0x") - .append(id) - .append(']'); - return buf.toString(); + return String.format("%s{id: 0x%X}", className, id); } SocketAddress remoteAddr = socket.getRemoteSocketAddress(); SocketAddress localAddr = socket.getLocalSocketAddress(); if (remoteAddr != null) { - StringBuilder buf = new StringBuilder(101) - .append("[") - .append(classInfo) - .append(", id: 0x") - .append(id) - .append(", L:") - .append(localAddr) - .append(broken? " ! " : " - ") - .append("R:") - .append(remoteAddr) - .append(']'); - strVal = buf.toString(); + strVal = String.format("%s{id: 0x%X, L:%s %c R:%s}", className, id, + localAddr, (broken ? '!' : '-'), remoteAddr); } else if (localAddr != null) { - StringBuilder buf = new StringBuilder(64) - .append("[") - .append(classInfo) - .append(", id: 0x") - .append(id) - .append(", L:") - .append(localAddr) - .append(']'); - strVal = buf.toString(); + strVal = String.format("%s{id: 0x%X, L:%s}", className, id, localAddr); } else { - StringBuilder buf = new StringBuilder(56) - .append("[") - .append(classInfo) - .append(", id: 0x") - .append(id) - .append(']'); - strVal = buf.toString(); + strVal = String.format("%s{id: 0x%X}", className, id); } strValActive = broken; @@ -156,7 +126,7 @@ public void setSoTimeout(int soTimeout) { try { this.socket.setSoTimeout(soTimeout); } catch (SocketException ex) { - broken = true; + setBroken(); throw new JedisConnectionException(ex); } } @@ -169,7 +139,7 @@ public void setTimeoutInfinite() { } socket.setSoTimeout(infiniteSoTimeout); } catch (SocketException ex) { - broken = true; + setBroken(); throw new JedisConnectionException(ex); } } @@ -178,7 +148,7 @@ public void rollbackTimeout() { try { socket.setSoTimeout(this.soTimeout); } catch (SocketException ex) { - broken = true; + setBroken(); throw new JedisConnectionException(ex); } } @@ -245,7 +215,7 @@ public void sendCommand(final CommandArguments args) { */ } // Any other exceptions related to connection? - broken = true; + setBroken(); throw ex; } } @@ -398,7 +368,7 @@ protected void flush() { try { outputStream.flush(); } catch (IOException ex) { - broken = true; + setBroken(); throw new JedisConnectionException(ex); } } @@ -414,7 +384,7 @@ protected Object readProtocolWithCheckingBroken() { // System.out.println(redis.clients.jedis.util.SafeEncoder.encodeObject(read)); // return read; } catch (JedisConnectionException exc) { - broken = true; + setBroken(); throw exc; } } diff --git a/src/test/java/redis/clients/jedis/ConnectionTest.java b/src/test/java/redis/clients/jedis/ConnectionTest.java index c57248db53..2fce9d62ac 100644 --- a/src/test/java/redis/clients/jedis/ConnectionTest.java +++ b/src/test/java/redis/clients/jedis/ConnectionTest.java @@ -1,7 +1,9 @@ package redis.clients.jedis; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.hamcrest.Matchers; import org.junit.After; -import org.junit.Assert; import org.junit.Test; import redis.clients.jedis.exceptions.JedisConnectionException; @@ -45,10 +47,28 @@ public void checkCloseable() { @Test public void checkIdentityString() { client = new Connection("localhost", 6379); - Assert.assertFalse(client.toIdentityString().contains("6379")); + + String idString = "id: 0x" + Integer.toHexString(client.hashCode()).toUpperCase(); + + String identityString = client.toIdentityString(); + assertThat(identityString, Matchers.startsWith("Connection{")); + assertThat(identityString, Matchers.endsWith("}")); + assertThat(identityString, Matchers.containsString(idString)); + client.connect(); - Assert.assertTrue(client.toIdentityString().contains("6379")); + identityString = client.toIdentityString(); + assertThat(identityString, Matchers.startsWith("Connection{")); + assertThat(identityString, Matchers.endsWith("}")); + assertThat(identityString, Matchers.containsString(idString)); + assertThat(identityString, Matchers.containsString(", L:")); + assertThat(identityString, Matchers.containsString(" - R:")); + client.close(); - Assert.assertTrue(client.toIdentityString().contains("6379")); + identityString = client.toIdentityString(); + assertThat(identityString, Matchers.startsWith("Connection{")); + assertThat(identityString, Matchers.endsWith("}")); + assertThat(identityString, Matchers.containsString(idString)); + assertThat(identityString, Matchers.containsString(", L:")); + assertThat(identityString, Matchers.containsString(" ! R:")); } }