Repository: twill Updated Branches: refs/heads/master b038401c9 -> a45c0042c
(TWILL-264) Fix Discoverable.hashCode implementation This closes #80 on Github Signed-off-by: Terence Yim <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/twill/repo Commit: http://git-wip-us.apache.org/repos/asf/twill/commit/a45c0042 Tree: http://git-wip-us.apache.org/repos/asf/twill/tree/a45c0042 Diff: http://git-wip-us.apache.org/repos/asf/twill/diff/a45c0042 Branch: refs/heads/master Commit: a45c0042c7bc26acc8a9a2d3aafc95cf4acc61c9 Parents: b038401 Author: Terence Yim <[email protected]> Authored: Fri Jan 25 10:00:00 2019 -0800 Committer: Terence Yim <[email protected]> Committed: Fri Jan 25 14:37:32 2019 -0800 ---------------------------------------------------------------------- twill-discovery-api/pom.xml | 4 ++ .../apache/twill/discovery/Discoverable.java | 11 ++-- .../twill/discovery/DiscoverableTest.java | 53 ++++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/twill/blob/a45c0042/twill-discovery-api/pom.xml ---------------------------------------------------------------------- diff --git a/twill-discovery-api/pom.xml b/twill-discovery-api/pom.xml index 496f160..928a11a 100644 --- a/twill-discovery-api/pom.xml +++ b/twill-discovery-api/pom.xml @@ -35,5 +35,9 @@ <artifactId>twill-common</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/twill/blob/a45c0042/twill-discovery-api/src/main/java/org/apache/twill/discovery/Discoverable.java ---------------------------------------------------------------------- diff --git a/twill-discovery-api/src/main/java/org/apache/twill/discovery/Discoverable.java b/twill-discovery-api/src/main/java/org/apache/twill/discovery/Discoverable.java index f36a3db..da39397 100644 --- a/twill-discovery-api/src/main/java/org/apache/twill/discovery/Discoverable.java +++ b/twill-discovery-api/src/main/java/org/apache/twill/discovery/Discoverable.java @@ -63,7 +63,7 @@ public class Discoverable { @Override public String toString() { - return "{name=" + name + ", address=" + address + ", payload=" + payload + "}"; + return "{name=" + name + ", address=" + address + ", payload=" + Arrays.toString(payload) + "}"; } @Override @@ -77,12 +77,15 @@ public class Discoverable { Discoverable other = (Discoverable) o; - return name.equals(other.getName()) && address.equals(other.getSocketAddress()) && - Arrays.equals(payload, other.getPayload()); + return Objects.equals(name, other.name) && + Objects.equals(address, other.address) && + Arrays.equals(payload, other.payload); } @Override public int hashCode() { - return Objects.hash(name, address, payload); + int result = Objects.hash(name, address); + result = 31 * result + Arrays.hashCode(payload); + return result; } } http://git-wip-us.apache.org/repos/asf/twill/blob/a45c0042/twill-discovery-api/src/test/java/org/apache/twill/discovery/DiscoverableTest.java ---------------------------------------------------------------------- diff --git a/twill-discovery-api/src/test/java/org/apache/twill/discovery/DiscoverableTest.java b/twill-discovery-api/src/test/java/org/apache/twill/discovery/DiscoverableTest.java new file mode 100644 index 0000000..dbac8c2 --- /dev/null +++ b/twill-discovery-api/src/test/java/org/apache/twill/discovery/DiscoverableTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.apache.twill.discovery; + +import org.junit.Assert; +import org.junit.Test; + +import java.net.InetSocketAddress; + +/** + * Test for the {@link Discoverable} object. + */ +public class DiscoverableTest { + + @Test + public void testEqualsAndHashCode() { + Discoverable d1 = new Discoverable("service1", InetSocketAddress.createUnresolved("host1", 12345), + new byte[] {1, 2, 3, 4}); + Discoverable d2 = new Discoverable("service1", InetSocketAddress.createUnresolved("host1", 12345), + new byte[] {1, 2, 3, 4}); + Assert.assertEquals(d1, d2); + Assert.assertEquals(d1.hashCode(), d2.hashCode()); + + Discoverable d3 = new Discoverable("service2", InetSocketAddress.createUnresolved("host1", 12345), + new byte[] {1, 2, 3, 4}); + Assert.assertNotEquals(d1, d3); + Assert.assertNotEquals(d1.hashCode(), d3.hashCode()); + + Discoverable d4 = new Discoverable("service1", InetSocketAddress.createUnresolved("host2", 12345), + new byte[] {1, 2, 3, 4}); + Assert.assertNotEquals(d1, d4); + Assert.assertNotEquals(d1.hashCode(), d4.hashCode()); + + Discoverable d5 = new Discoverable("service1", InetSocketAddress.createUnresolved("host1", 12345)); + Assert.assertNotEquals(d1, d5); + Assert.assertNotEquals(d1.hashCode(), d5.hashCode()); + } +}
