Skip to content

fixes #63; better toString representation of ArangoHost #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/com/arangodb/ArangoHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,20 @@ public boolean equals(Object obj) {
return true;
}

/**
* Returns a string representation in the form <code>host:port</code>,
* adding square brackets if needed
*/
@Override
public String toString() {
// "[]:12345" requires 8 extra bytes.
StringBuilder builder = new StringBuilder(host.length() + 8);
if (host.indexOf(':') >= 0) {
builder.append('[').append(host).append(']');
} else {
builder.append(host);
}
builder.append(':').append(port);
return builder.toString();
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/arangodb/ArangoHostTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.arangodb;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Test;

/**
* @author Anton Sarov - initial contribution
*
*/
public class ArangoHostTest {

@Test
public void testToString() {
ArangoHost arangoHost = new ArangoHost("127.0.0.1", 8529);
assertThat(arangoHost.toString(), is("127.0.0.1:8529"));
}

@Test
public void testToStringIPv6() {
ArangoHost arangoHost = new ArangoHost("2001:db8:1f70::999:de8:7648:6e8", 8529);
assertThat(arangoHost.toString(), is("[2001:db8:1f70::999:de8:7648:6e8]:8529"));
}
}