SGF-881 - Fix parsing issue that mishandles whitespace in ConnectionEndpoint.parse(..).

This commit is contained in:
John Blum
2019-08-30 16:14:08 -07:00
parent bdd72fdc32
commit f5ca5929d6
3 changed files with 329 additions and 286 deletions

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.support;
import java.net.InetSocketAddress;
@@ -77,12 +76,13 @@ public class ConnectionEndpoint implements Cloneable, Comparable<ConnectionEndpo
* @see #ConnectionEndpoint(String, int)
*/
public static ConnectionEndpoint parse(String hostPort, int defaultPort) {
Assert.hasText(hostPort, "'hostPort' must be specified");
String host = StringUtils.trimAllWhitespace(hostPort);
int port = defaultPort;
int portIndex = hostPort.indexOf("[");
int portIndex = host.indexOf("[");
if (portIndex > -1) {
port = parsePort(parseDigits(host.substring(portIndex)), defaultPort);
@@ -92,8 +92,8 @@ public class ConnectionEndpoint implements Cloneable, Comparable<ConnectionEndpo
return new ConnectionEndpoint(host, port);
}
/* (non-Javadoc) */
static String parseDigits(String value) {
StringBuilder digits = new StringBuilder();
if (StringUtils.hasText(value)) {
@@ -107,8 +107,8 @@ public class ConnectionEndpoint implements Cloneable, Comparable<ConnectionEndpo
return digits.toString();
}
/* (non-Javadoc) */
static int parsePort(String port, int defaultPort) {
try {
return Integer.parseInt(port);
}
@@ -127,15 +127,15 @@ public class ConnectionEndpoint implements Cloneable, Comparable<ConnectionEndpo
* @see ConnectionEndpoint#DEFAULT_HOST
*/
public ConnectionEndpoint(String host, int port) {
Assert.isTrue(isValidPort(port), String.format("port number [%d] must be between 0 and 65535", port));
this.host = SpringUtils.defaultIfEmpty(host, DEFAULT_HOST);
this.port = port;
}
/* (non-Javadoc) */
private boolean isValidPort(int port) {
return (port >= 0 && port <= 65535);
return port >= 0 && port <= 65535;
}
/**
@@ -144,7 +144,7 @@ public class ConnectionEndpoint implements Cloneable, Comparable<ConnectionEndpo
* @return a String value indicating the hostname or IP address in this ConnectionEndpoint.
*/
public String getHost() {
return host;
return this.host;
}
/**
@@ -153,7 +153,7 @@ public class ConnectionEndpoint implements Cloneable, Comparable<ConnectionEndpo
* @return an Integer value indicating the (service) port number in this ConnectionEndpoint.
*/
public int getPort() {
return port;
return this.port;
}
/**
@@ -168,25 +168,25 @@ public class ConnectionEndpoint implements Cloneable, Comparable<ConnectionEndpo
return new InetSocketAddress(getHost(), getPort());
}
/* (non-Javadoc) */
@Override
@SuppressWarnings("all")
protected Object clone() throws CloneNotSupportedException {
return new ConnectionEndpoint(this.getHost(), this.getPort());
}
/* (non-Javadoc) */
@Override
@SuppressWarnings("all")
public int compareTo(ConnectionEndpoint connectionEndpoint) {
int compareValue = getHost().compareTo(connectionEndpoint.getHost());
return (compareValue != 0 ? compareValue : getPort() - connectionEndpoint.getPort());
return compareValue != 0 ? compareValue : getPort() - connectionEndpoint.getPort();
}
/* (non-Javadoc) */
@Override
public boolean equals(final Object obj) {
if (obj == this) {
if (this == obj) {
return true;
}
@@ -200,16 +200,17 @@ public class ConnectionEndpoint implements Cloneable, Comparable<ConnectionEndpo
&& ObjectUtils.nullSafeEquals(this.getPort(), that.getPort());
}
/* (non-Javadoc) */
@Override
public int hashCode() {
int hashValue = 17;
hashValue = 37 * hashValue + ObjectUtils.nullSafeHashCode(getHost());
hashValue = 37 * hashValue + ObjectUtils.nullSafeHashCode(getPort());
return hashValue;
}
/* (non-Javadoc) */
@Override
public String toString() {
return String.format("%1$s[%2$d]", getHost(), getPort());

View File

@@ -1,271 +0,0 @@
/*
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.support;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.number.OrderingComparison.greaterThan;
import static org.hamcrest.number.OrderingComparison.lessThan;
import static org.junit.Assert.assertThat;
import java.net.InetSocketAddress;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* The ConnectionEndpointTest class is a test suite of test cases testing the contract and functionality
* of the ConnectionEndpoint class representing Pivotal GemFireSocket connection endpoints to Pivotal GemFire'services
* (such as Locators, etc).
*
* @author John Blum
* @see org.junit.Rule
* @see org.junit.Test
* @see org.junit.rules.ExpectedException
* @see org.springframework.data.gemfire.support.ConnectionEndpoint
* @since 1.6.3
*/
public class ConnectionEndpointTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void fromInetSocketAddress() {
InetSocketAddress socketAddress = new InetSocketAddress("localhost", 1234);
ConnectionEndpoint connectionEndpoint = ConnectionEndpoint.from(socketAddress);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo(socketAddress.getHostString())));
assertThat(connectionEndpoint.getPort(), is(equalTo(socketAddress.getPort())));
}
@Test
public void parseUsingDefaultHostAndDefaultPort() {
ConnectionEndpoint connectionEndpoint = ConnectionEndpoint.parse("[]");
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo(ConnectionEndpoint.DEFAULT_HOST)));
assertThat(connectionEndpoint.getPort(), is(equalTo(ConnectionEndpoint.DEFAULT_PORT)));
connectionEndpoint = ConnectionEndpoint.parse("[]", 1234);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo(ConnectionEndpoint.DEFAULT_HOST)));
assertThat(connectionEndpoint.getPort(), is(equalTo(1234)));
}
@Test
public void parseWithHostPort() {
ConnectionEndpoint connectionEndpoint = ConnectionEndpoint.parse("skullbox[12345]", 80);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo("skullbox")));
assertThat(connectionEndpoint.getPort(), is(equalTo(12345)));
connectionEndpoint = ConnectionEndpoint.parse("localhost[0]", 8080);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo("localhost")));
assertThat(connectionEndpoint.getPort(), is(equalTo(0)));
connectionEndpoint = ConnectionEndpoint.parse("jambox[1O1O1]", 443);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo("jambox")));
assertThat(connectionEndpoint.getPort(), is(equalTo(111)));
}
@Test
public void parseWithHostUsingDefaultPort() {
ConnectionEndpoint connectionEndpoint = ConnectionEndpoint.parse("mercury[oneTwoThreeFourFive]", 80);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo("mercury")));
assertThat(connectionEndpoint.getPort(), is(equalTo(80)));
connectionEndpoint = ConnectionEndpoint.parse("venus[OxCAFEBABE]", 443);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo("venus")));
assertThat(connectionEndpoint.getPort(), is(equalTo(443)));
connectionEndpoint = ConnectionEndpoint.parse("jupiter[#(^$)*!]", 21);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo("jupiter")));
assertThat(connectionEndpoint.getPort(), is(equalTo(21)));
connectionEndpoint = ConnectionEndpoint.parse("saturn[]", 22);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo("saturn")));
assertThat(connectionEndpoint.getPort(), is(equalTo(22)));
connectionEndpoint = ConnectionEndpoint.parse("uranis[", 23);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo("uranis")));
assertThat(connectionEndpoint.getPort(), is(equalTo(23)));
connectionEndpoint = ConnectionEndpoint.parse("neptune", 25);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo("neptune")));
assertThat(connectionEndpoint.getPort(), is(equalTo(25)));
connectionEndpoint = ConnectionEndpoint.parse("pluto");
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo("pluto")));
assertThat(connectionEndpoint.getPort(), is(equalTo(ConnectionEndpoint.DEFAULT_PORT)));
}
@Test
public void parseWithPortUsingDefaultHost() {
ConnectionEndpoint connectionEndpoint = ConnectionEndpoint.parse("[12345]", 80);
assertThat(connectionEndpoint, is(notNullValue()));
assertThat(connectionEndpoint.getHost(), is(equalTo(ConnectionEndpoint.DEFAULT_HOST)));
assertThat(connectionEndpoint.getPort(), is(equalTo(12345)));
}
@Test
public void parseWithBlankHost() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("'hostPort' must be specified");
ConnectionEndpoint.parse(" ", 12345);
}
@Test
public void parseWithEmptyHost() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("'hostPort' must be specified");
ConnectionEndpoint.parse("", 12345);
}
@Test
public void parseWithNullHost() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("'hostPort' must be specified");
ConnectionEndpoint.parse(null, 12345);
}
@Test
public void parseWithInvalidDefaultPort() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("port number [-1248] must be between 0 and 65535");
ConnectionEndpoint.parse("localhost", -1248);
}
@Test
public void parseDigits() {
assertThat(ConnectionEndpoint.parseDigits("123456789"), is(equalTo("123456789")));
assertThat(ConnectionEndpoint.parseDigits("3.14159"), is(equalTo("314159")));
assertThat(ConnectionEndpoint.parseDigits("-21.5"), is(equalTo("215")));
assertThat(ConnectionEndpoint.parseDigits("$156.78^#99!"), is(equalTo("1567899")));
assertThat(ConnectionEndpoint.parseDigits("oneTwoThree"), is(equalTo("")));
assertThat(ConnectionEndpoint.parseDigits(" "), is(equalTo("")));
assertThat(ConnectionEndpoint.parseDigits(""), is(equalTo("")));
assertThat(ConnectionEndpoint.parseDigits(null), is(equalTo("")));
}
@Test
public void parsePort() {
assertThat(ConnectionEndpoint.parsePort("12345", 80), is(equalTo(12345)));
assertThat(ConnectionEndpoint.parsePort("zero", 80), is(equalTo(80)));
}
@Test
public void constructConnectionEndpoint() {
ConnectionEndpoint connectionEndpoint = new ConnectionEndpoint("skullbox", 12345);
assertThat(connectionEndpoint.getHost(), is(equalTo("skullbox")));
assertThat(connectionEndpoint.getPort(), is(equalTo(12345)));
assertThat(connectionEndpoint.toString(), is(equalTo("skullbox[12345]")));
}
@Test
public void constructConnectionEndpointWithDefaultHost() {
ConnectionEndpoint connectionEndpoint = new ConnectionEndpoint(" ", 12345);
assertThat(connectionEndpoint.getHost(), is(equalTo(ConnectionEndpoint.DEFAULT_HOST)));
assertThat(connectionEndpoint.getPort(), is(equalTo(12345)));
}
@Test
public void constructConnectionEndpointWithInvalidPort() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("port number [-1] must be between 0 and 65535");
new ConnectionEndpoint("localhost", -1);
}
@Test
public void cloneConnectionEndpoint() throws CloneNotSupportedException {
ConnectionEndpoint originalConnectionEndpoint = new ConnectionEndpoint("skullbox", 12345);
assertThat(originalConnectionEndpoint.getHost(), is(equalTo("skullbox")));
assertThat(originalConnectionEndpoint.getPort(), is(equalTo(12345)));
ConnectionEndpoint clonedConnectionEndpoint = (ConnectionEndpoint) originalConnectionEndpoint.clone();
assertThat(clonedConnectionEndpoint, is(notNullValue()));
assertThat(clonedConnectionEndpoint, is(equalTo(originalConnectionEndpoint)));
}
@Test
public void compareConnectionEndpoints() {
ConnectionEndpoint connectionEndpointOne = new ConnectionEndpoint("localhost", 10334);
ConnectionEndpoint connectionEndpointTwo = new ConnectionEndpoint("localhost", 40404);
ConnectionEndpoint connectionEndpointThree = new ConnectionEndpoint("skullbox", 11235);
assertThat(connectionEndpointOne.compareTo(connectionEndpointOne), is(equalTo(0)));
assertThat(connectionEndpointOne.compareTo(connectionEndpointTwo), is(lessThan(0)));
assertThat(connectionEndpointOne.compareTo(connectionEndpointThree), is(lessThan(0)));
assertThat(connectionEndpointTwo.compareTo(connectionEndpointOne), is(greaterThan(0)));
assertThat(connectionEndpointTwo.compareTo(connectionEndpointTwo), is(equalTo(0)));
assertThat(connectionEndpointTwo.compareTo(connectionEndpointThree), is(lessThan(0)));
assertThat(connectionEndpointThree.compareTo(connectionEndpointOne), is(greaterThan(0)));
assertThat(connectionEndpointThree.compareTo(connectionEndpointTwo), is(greaterThan(0)));
assertThat(connectionEndpointThree.compareTo(connectionEndpointThree), is(equalTo(0)));
}
@Test
public void toInetSocketAddressEqualsHostPort() {
ConnectionEndpoint connectionEndpoint = new ConnectionEndpoint("localhost", 12345);
assertThat(connectionEndpoint.getHost(), is(equalTo("localhost")));
assertThat(connectionEndpoint.getPort(), is(equalTo(12345)));
InetSocketAddress socketAddress = connectionEndpoint.toInetSocketAddress();
assertThat(socketAddress, is(notNullValue()));
assertThat(socketAddress.getHostName(), is(equalTo(connectionEndpoint.getHost())));
assertThat(socketAddress.getPort(), is(equalTo(connectionEndpoint.getPort())));
}
}

View File

@@ -0,0 +1,313 @@
/*
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.support;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.InetSocketAddress;
import org.junit.Test;
/**
* Unit Tests for {@link ConnectionEndpoint}.
*
* @author John Blum
* @see java.net.InetSocketAddress
* @see org.junit.Test
* @see org.springframework.data.gemfire.support.ConnectionEndpoint
* @since 1.6.3
*/
public class ConnectionEndpointUnitTests {
@Test
public void fromInetSocketAddress() {
InetSocketAddress socketAddress = new InetSocketAddress("localhost", 1234);
ConnectionEndpoint connectionEndpoint = ConnectionEndpoint.from(socketAddress);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo(socketAddress.getHostString());
assertThat(connectionEndpoint.getPort()).isEqualTo(socketAddress.getPort());
}
@Test
public void parseUsingDefaultHostAndDefaultPort() {
ConnectionEndpoint connectionEndpoint = ConnectionEndpoint.parse("[]");
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo(ConnectionEndpoint.DEFAULT_HOST);
assertThat(connectionEndpoint.getPort()).isEqualTo(ConnectionEndpoint.DEFAULT_PORT);
connectionEndpoint = ConnectionEndpoint.parse("[]", 1234);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo(ConnectionEndpoint.DEFAULT_HOST);
assertThat(connectionEndpoint.getPort()).isEqualTo(1234);
}
@Test
public void parseWithHostPort() {
ConnectionEndpoint connectionEndpoint = ConnectionEndpoint.parse("skullbox[12345]", 80);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("skullbox");
assertThat(connectionEndpoint.getPort()).isEqualTo(12345);
connectionEndpoint = ConnectionEndpoint.parse("localhost[0]", 8080);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("localhost");
assertThat(connectionEndpoint.getPort()).isEqualTo(0);
connectionEndpoint = ConnectionEndpoint.parse("jambox[1O1O1]", 443);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("jambox");
assertThat(connectionEndpoint.getPort()).isEqualTo(111);
}
@Test
public void parseWithHostPortHavingSpacing() {
ConnectionEndpoint connectionEndpoint = ConnectionEndpoint.parse(" saturn [1 23 4 ]");
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("saturn");
assertThat(connectionEndpoint.getPort()).isEqualTo(1234);
}
@Test
public void parseWithHostUsingDefaultPort() {
ConnectionEndpoint connectionEndpoint =
ConnectionEndpoint.parse("mercury[oneTwoThreeFourFive]", 80);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("mercury");
assertThat(connectionEndpoint.getPort()).isEqualTo(80);
connectionEndpoint = ConnectionEndpoint.parse("venus[OxCAFEBABE]", 443);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("venus");
assertThat(connectionEndpoint.getPort()).isEqualTo(443);
connectionEndpoint = ConnectionEndpoint.parse("jupiter[#(^$)*!]", 21);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("jupiter");
assertThat(connectionEndpoint.getPort()).isEqualTo(21);
connectionEndpoint = ConnectionEndpoint.parse("saturn[]", 22);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("saturn");
assertThat(connectionEndpoint.getPort()).isEqualTo(22);
connectionEndpoint = ConnectionEndpoint.parse("uranis[", 23);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("uranis");
assertThat(connectionEndpoint.getPort()).isEqualTo(23);
connectionEndpoint = ConnectionEndpoint.parse("neptune", 25);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("neptune");
assertThat(connectionEndpoint.getPort()).isEqualTo(25);
connectionEndpoint = ConnectionEndpoint.parse("pluto");
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo("pluto");
assertThat(connectionEndpoint.getPort()).isEqualTo(ConnectionEndpoint.DEFAULT_PORT);
}
@Test
public void parseWithPortUsingDefaultHost() {
ConnectionEndpoint connectionEndpoint = ConnectionEndpoint.parse("[12345]", 80);
assertThat(connectionEndpoint).isNotNull();
assertThat(connectionEndpoint.getHost()).isEqualTo(ConnectionEndpoint.DEFAULT_HOST);
assertThat(connectionEndpoint.getPort()).isEqualTo(12345);
}
@Test(expected = IllegalArgumentException.class)
public void parseWithBlankHost() {
try {
ConnectionEndpoint.parse(" ", 12345);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("'hostPort' must be specified");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalArgumentException.class)
public void parseWithEmptyHost() {
try {
ConnectionEndpoint.parse("", 12345);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("'hostPort' must be specified");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalArgumentException.class)
public void parseWithNullHost() {
try {
ConnectionEndpoint.parse(null, 12345);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("'hostPort' must be specified");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalArgumentException.class)
public void parseWithInvalidDefaultPort() {
try {
ConnectionEndpoint.parse("localhost", -1248);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("port number [-1248] must be between 0 and 65535");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void parseDigits() {
assertThat(ConnectionEndpoint.parseDigits("123456789")).isEqualTo("123456789");
assertThat(ConnectionEndpoint.parseDigits("3.14159")).isEqualTo("314159");
assertThat(ConnectionEndpoint.parseDigits("-21.5")).isEqualTo("215");
assertThat(ConnectionEndpoint.parseDigits("$156.78^#99!")).isEqualTo("1567899");
assertThat(ConnectionEndpoint.parseDigits("oneTwoThree")).isEqualTo("");
assertThat(ConnectionEndpoint.parseDigits(" ")).isEqualTo("");
assertThat(ConnectionEndpoint.parseDigits("")).isEqualTo("");
assertThat(ConnectionEndpoint.parseDigits(null)).isEqualTo("");
}
@Test
public void parsePort() {
assertThat(ConnectionEndpoint.parsePort("12345", 80)).isEqualTo(12345);
assertThat(ConnectionEndpoint.parsePort("zero", 80)).isEqualTo(80);
}
@Test
public void constructConnectionEndpoint() {
ConnectionEndpoint connectionEndpoint = new ConnectionEndpoint("skullbox", 12345);
assertThat(connectionEndpoint.getHost()).isEqualTo("skullbox");
assertThat(connectionEndpoint.getPort()).isEqualTo(12345);
assertThat(connectionEndpoint.toString()).isEqualTo("skullbox[12345]");
}
@Test
public void constructConnectionEndpointWithDefaultHost() {
ConnectionEndpoint connectionEndpoint = new ConnectionEndpoint(" ", 12345);
assertThat(connectionEndpoint.getHost()).isEqualTo(ConnectionEndpoint.DEFAULT_HOST);
assertThat(connectionEndpoint.getPort()).isEqualTo(12345);
}
@Test(expected = IllegalArgumentException.class)
public void constructConnectionEndpointWithInvalidPort() {
try {
new ConnectionEndpoint("localhost", -1);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("port number [-1] must be between 0 and 65535");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void cloneConnectionEndpoint() throws CloneNotSupportedException {
ConnectionEndpoint originalConnectionEndpoint = new ConnectionEndpoint("skullbox", 12345);
assertThat(originalConnectionEndpoint.getHost()).isEqualTo("skullbox");
assertThat(originalConnectionEndpoint.getPort()).isEqualTo(12345);
ConnectionEndpoint clonedConnectionEndpoint = (ConnectionEndpoint) originalConnectionEndpoint.clone();
assertThat(clonedConnectionEndpoint).isNotNull();
assertThat(clonedConnectionEndpoint).isEqualTo(originalConnectionEndpoint);
}
@Test
@SuppressWarnings("all")
public void compareConnectionEndpoints() {
ConnectionEndpoint connectionEndpointOne = new ConnectionEndpoint("localhost", 10334);
ConnectionEndpoint connectionEndpointTwo = new ConnectionEndpoint("localhost", 40404);
ConnectionEndpoint connectionEndpointThree = new ConnectionEndpoint("skullbox", 11235);
assertThat(connectionEndpointOne.compareTo(connectionEndpointOne)).isEqualTo(0);
assertThat(connectionEndpointOne.compareTo(connectionEndpointTwo)).isLessThan(0);
assertThat(connectionEndpointOne.compareTo(connectionEndpointThree)).isLessThan(0);
assertThat(connectionEndpointTwo.compareTo(connectionEndpointOne)).isGreaterThan(0);
assertThat(connectionEndpointTwo.compareTo(connectionEndpointTwo)).isEqualTo(0);
assertThat(connectionEndpointTwo.compareTo(connectionEndpointThree)).isLessThan(0);
assertThat(connectionEndpointThree.compareTo(connectionEndpointOne)).isGreaterThan(0);
assertThat(connectionEndpointThree.compareTo(connectionEndpointTwo)).isGreaterThan(0);
assertThat(connectionEndpointThree.compareTo(connectionEndpointThree)).isEqualTo(0);
}
@Test
public void toInetSocketAddressEqualsHostPort() {
ConnectionEndpoint connectionEndpoint = new ConnectionEndpoint("localhost", 12345);
assertThat(connectionEndpoint.getHost()).isEqualTo("localhost");
assertThat(connectionEndpoint.getPort()).isEqualTo(12345);
InetSocketAddress socketAddress = connectionEndpoint.toInetSocketAddress();
assertThat(socketAddress).isNotNull();
assertThat(socketAddress.getHostName()).isEqualTo(connectionEndpoint.getHost());
assertThat(socketAddress.getPort()).isEqualTo(connectionEndpoint.getPort());
}
}