tmf: Make TmfEventField's equals() also check the sub-fields
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Thu, 10 Jul 2014 21:21:58 +0000 (17:21 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Thu, 17 Jul 2014 17:10:32 +0000 (13:10 -0400)
If two TmfEventField objects have the same name/value but different
sub-fields, they were considered equals. With this patch, they also
need the same sub-fields.

Change-Id: I7e07a5624348b812878e1934425a29fba4737e4c
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/30001
Tested-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfEventFieldTest.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventField.java

index 1148d3b5ea318e8a09d0ad2193fbdacd45210f54..e69f31e3c1a19054c86fda1d6f94edeac8f89acc 100644 (file)
@@ -17,6 +17,7 @@ package org.eclipse.linuxtools.tmf.core.tests.event;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
@@ -25,6 +26,7 @@ import static org.junit.Assert.fail;
 import java.util.Collection;
 
 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
+import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
 import org.junit.Test;
 
@@ -279,6 +281,56 @@ public class TmfEventFieldTest {
         assertFalse("equals", fField1.equals(fStructTerminalField1));
     }
 
+    /**
+     * Test with same fields, but different values (should not be equal)
+     */
+    @Test
+    public void testNonEqualsValue() {
+        final String fieldName = "myfield";
+        final Object value1 = new String("test-string");
+        final Object value2 = new TmfEvent();
+        final TmfEventField[] fields = { fField1, fField2 };
+
+        final TmfEventField field1 = new TmfEventField(fieldName, value1, fields);
+        final TmfEventField field2 = new TmfEventField(fieldName, value2, fields);
+
+        assertNotEquals(field1, field2);
+        assertNotEquals(field2, field1);
+    }
+
+    /**
+     * Test with same value, but different fields (should not be equal)
+     */
+    @Test
+    public void testNonEqualsFields() {
+        final String fieldName = "myfield";
+        final Object value = new String("test-string");
+        final TmfEventField[] fields1 = { fField1, fField2 };
+        final TmfEventField[] fields2 = { fField2, fField3 };
+
+        final TmfEventField field1 = new TmfEventField(fieldName, value, fields1);
+        final TmfEventField field2 = new TmfEventField(fieldName, value, fields2);
+
+        assertNotEquals(field1, field2);
+        assertNotEquals(field2, field1);
+    }
+
+    /**
+     * Test with same field and values (should be equals)
+     */
+    @Test
+    public void testEqualsEverything() {
+        final String fieldName = "myfield";
+        final Object value = new String("test-string");
+        final TmfEventField[] fields = { fField1, fField2 };
+
+        final TmfEventField field1 = new TmfEventField(fieldName, value, fields);
+        final TmfEventField field2 = new TmfEventField(fieldName, value, fields);
+
+        assertEquals(field1, field2);
+        assertEquals(field2, field1);
+    }
+
     // ------------------------------------------------------------------------
     // toString
     // ------------------------------------------------------------------------
index 81c242f95191c3728d21b5b6da12b25be308619e..f98221b8c7820d82761ce61c5276825afab83849 100644 (file)
@@ -175,6 +175,7 @@ public class TmfEventField implements ITmfEventField {
         int result = 1;
         result = prime * result + fName.hashCode();
         result = prime * result + ((value == null) ? 0 : value.hashCode());
+        result = prime * result + fFields.hashCode();
         return result;
     }
 
@@ -189,10 +190,15 @@ public class TmfEventField implements ITmfEventField {
         if (!(obj instanceof TmfEventField)) {
             return false;
         }
+
         final TmfEventField other = (TmfEventField) obj;
+
+        /* Check that 'fName' is the same */
         if (!fName.equals(other.fName)) {
             return false;
         }
+
+        /* Check that 'fValue' is the same */
         Object value = this.fValue;
         if (value == null) {
             if (other.fValue != null) {
@@ -201,6 +207,12 @@ public class TmfEventField implements ITmfEventField {
         } else if (!value.equals(other.fValue)) {
             return false;
         }
+
+        /* Check that 'fFields' are the same */
+        if (!fFields.equals(other.fFields)) {
+            return false;
+        }
+
         return true;
     }
 
This page took 0.028224 seconds and 5 git commands to generate.