changeset 2011:53db26fe0673

Fix equals method in HeapInfo. Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-August/020588.html
author Jie Kang <jkang@redhat.com>
date Fri, 19 Aug 2016 10:41:56 -0400
parents c28824dce18c
children 4851a6db01a9
files vm-heap-analysis/common/src/main/java/com/redhat/thermostat/vm/heap/analysis/common/model/HeapInfo.java vm-heap-analysis/common/src/test/java/com/redhat/thermostat/vm/heap/analysis/common/model/HeapInfoTest.java
diffstat 2 files changed, 38 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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;
     }
 
--- 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