From 40d8c7796748e87a278cfe3a14ffe077658b724f Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Thu, 10 Jul 2014 17:21:58 -0400 Subject: [PATCH] tmf: Make TmfEventField's equals() also check the sub-fields 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 Reviewed-on: https://git.eclipse.org/r/30001 Tested-by: Hudson CI Reviewed-by: Matthew Khouzam --- .../core/tests/event/TmfEventFieldTest.java | 52 +++++++++++++++++++ .../tmf/core/event/TmfEventField.java | 12 +++++ 2 files changed, 64 insertions(+) diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfEventFieldTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfEventFieldTest.java index 1148d3b5ea..e69f31e3c1 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfEventFieldTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfEventFieldTest.java @@ -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 // ------------------------------------------------------------------------ diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventField.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventField.java index 81c242f951..f98221b8c7 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventField.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventField.java @@ -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; } -- 2.34.1