diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1476d9b93..efb78ebee 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + DiffBuilder.append(String, Object left, Object right) does not do a left.equals(right) check StrSubstitutor.replaceSystemProperties does not work consistently Add option to disable the "objectsTriviallyEqual" test in DiffBuilder Add (T) casts to get unit tests to pass in old JDK diff --git a/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java b/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java index 9f21e42dd..bf3461941 100644 --- a/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java @@ -882,6 +882,10 @@ public class DiffBuilder implements Builder { } // Not array type + if (lhs != null ? lhs.equals(rhs) : rhs.equals(lhs)) { + return this; + } + diffs.add(new Diff(fieldName) { private static final long serialVersionUID = 1L; diff --git a/src/test/java/org/apache/commons/lang3/builder/DiffBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/DiffBuilderTest.java index ae835fef8..9d7c3971d 100644 --- a/src/test/java/org/apache/commons/lang3/builder/DiffBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/DiffBuilderTest.java @@ -19,7 +19,9 @@ package org.apache.commons.lang3.builder; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import org.apache.commons.lang3.ArrayUtils; import org.hamcrest.Matcher; @@ -321,18 +323,56 @@ public class DiffBuilderTest { assertEquals(class1.objectField, diff.getLeft()); assertEquals(class2.objectField, diff.getRight()); } - + + /** + * Test that "left" and "right" are the same instance and are equal. + */ @Test - public void testObjectsEqual() throws Exception { - final TypeTestClass class1 = new TypeTestClass(); - final TypeTestClass class2 = new TypeTestClass(); - class1.objectField = "Some string"; - class2.objectField = "Some string"; - final DiffResult list = class1.diff(class2); + public void testObjectsSameAndEqual() throws Exception { + final Integer sameObject = 1; + final TypeTestClass left = new TypeTestClass(); + left.objectField = sameObject; + final TypeTestClass right = new TypeTestClass(); + right.objectField = sameObject; + assertTrue(left.objectField == right.objectField); + assertTrue(left.objectField.equals(right.objectField)); + + final DiffResult list = left.diff(right); assertEquals(0, list.getNumberOfDiffs()); } - - + + /** + * Test that "left" and "right" are the same instance but are equal. + */ + @Test + public void testObjectsNotSameButEqual() throws Exception { + final TypeTestClass left = new TypeTestClass(); + left.objectField = new Integer(1); + final TypeTestClass right = new TypeTestClass(); + right.objectField = new Integer(1); + assertFalse(left.objectField == right.objectField); + assertTrue(left.objectField.equals(right.objectField)); + + final DiffResult list = left.diff(right); + assertEquals(0, list.getNumberOfDiffs()); + } + + /** + * Test that "left" and "right" are not the same instance and are not equal. + */ + @Test + public void testObjectsNotSameNorEqual() throws Exception { + final TypeTestClass left = new TypeTestClass(); + left.objectField = 4; + final TypeTestClass right = new TypeTestClass(); + right.objectField = 100; + assertFalse(left.objectField == right.objectField); + assertFalse(left.objectField.equals(right.objectField)); + + final DiffResult list = left.diff(right); + assertEquals(1, list.getNumberOfDiffs()); + } + @Test public void testObjectArray() throws Exception { final TypeTestClass class1 = new TypeTestClass();