# HG changeset patch # User Jie Kang # Date 1471617716 14400 # Node ID 53db26fe067360ead904aef6dd9af70d44c77eff # Parent c28824dce18c54cd217df177163bb81383351b9b Fix equals method in HeapInfo. Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-August/020588.html diff -r c28824dce18c -r 53db26fe0673 vm-heap-analysis/common/src/main/java/com/redhat/thermostat/vm/heap/analysis/common/model/HeapInfo.java --- a/vm-heap-analysis/common/src/main/java/com/redhat/thermostat/vm/heap/analysis/common/model/HeapInfo.java Fri Jul 08 19:31:34 2016 +0200 +++ b/vm-heap-analysis/common/src/main/java/com/redhat/thermostat/vm/heap/analysis/common/model/HeapInfo.java Fri Aug 19 10:41:56 2016 -0400 @@ -109,7 +109,7 @@ return false; } HeapInfo other = (HeapInfo) o; - return vmId == other.vmId && Objects.equals(heapDumpId, other.heapDumpId) + return Objects.equals(vmId, other.vmId) && Objects.equals(heapDumpId, other.heapDumpId) && Objects.equals(histogramId, other.histogramId) && timeStamp == other.timeStamp; } diff -r c28824dce18c -r 53db26fe0673 vm-heap-analysis/common/src/test/java/com/redhat/thermostat/vm/heap/analysis/common/model/HeapInfoTest.java --- a/vm-heap-analysis/common/src/test/java/com/redhat/thermostat/vm/heap/analysis/common/model/HeapInfoTest.java Fri Jul 08 19:31:34 2016 +0200 +++ b/vm-heap-analysis/common/src/test/java/com/redhat/thermostat/vm/heap/analysis/common/model/HeapInfoTest.java Fri Aug 19 10:41:56 2016 -0400 @@ -36,8 +36,12 @@ package com.redhat.thermostat.vm.heap.analysis.common.model; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import org.junit.Before; @@ -75,6 +79,39 @@ } @Test + public void testEquals() { + /** + * Use new String for distinct objects to catch + * equals issues + * "1" == "1" is true + * new String("1") == new String("1") is false + * new String("1").equals(new String "1") is true + */ + HeapInfo a = new HeapInfo(new String("1"), new String("1"), 0l); + assertThat(a, is(equalTo(a))); + + HeapInfo b = new HeapInfo(new String("1"), new String("1"), 0l); + assertThat(a, is(equalTo(b))); + } + + @Test + public void testNotEquals() { + HeapInfo a = new HeapInfo(new String("1"), new String("1"), 0l); + HeapInfo b = new HeapInfo(new String("1"), new String("2"), 0l); + + assertThat(a, is(not(equalTo(b)))); + } + + @Test + public void testHashEquals() { + HeapInfo a = new HeapInfo(new String("1"), new String("1"), 0l); + assertThat(a.hashCode(), is(equalTo(a.hashCode()))); + + HeapInfo b = new HeapInfo(new String("1"), new String("1"), 0l); + assertThat(a.hashCode(), is(equalTo(b.hashCode()))); + } + + @Test public void testBasicInstantiation() { try { // pojo converters use this