Applying Thomas Neidhart's patch for LANG-905; fixing a bug in which EqualsBuilder considers two arrays of the same type to be equal, without considering the contents

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1535653 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2013-10-25 08:19:29 +00:00
parent 4fc5c6b35c
commit 70b3504b9f
3 changed files with 33 additions and 3 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.2" date="TBA" description="Next release">
<action issue="LANG-905" type="fix">EqualsBuilder returns true when comparing arrays, even when the elements are different</action>
<action issue="LANG-774" type="add" due-to="Erhan Bagdemir">Added isStarted, isSuspended and isStopped to StopWatch</action>
<action issue="LANG-917" type="fix" due-to="Arne Burmeister">Fixed exception when combining custom and choice format in ExtendedMessageFormat</action>
<action issue="LANG-848" type="add" due-to="Alexander Muthmann">Added StringUtils.isBlank/isEmpty CharSequence... methods</action>

View File

@ -24,6 +24,7 @@
import java.util.Set;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.tuple.Pair;
/**
@ -354,10 +355,14 @@ public static boolean reflectionEquals(final Object lhs, final Object rhs, final
}
final EqualsBuilder equalsBuilder = new EqualsBuilder();
try {
reflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients, excludeFields);
while (testClass.getSuperclass() != null && testClass != reflectUpToClass) {
testClass = testClass.getSuperclass();
if (testClass.isArray()) {
equalsBuilder.append(lhs, rhs);
} else {
reflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients, excludeFields);
while (testClass.getSuperclass() != null && testClass != reflectUpToClass) {
testClass = testClass.getSuperclass();
reflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients, excludeFields);
}
}
} catch (final IllegalArgumentException e) {
// In this case, we tried to test a subclass vs. a superclass and

View File

@ -1126,5 +1126,29 @@ public boolean equals(final Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
}
@Test
public void testReflectionArrays() throws Exception {
final TestObject one = new TestObject(1);
final TestObject two = new TestObject(2);
Object[] o1 = new Object[] { one };
Object[] o2 = new Object[] { two };
Object[] o3 = new Object[] { one };
assertTrue(!EqualsBuilder.reflectionEquals(o1, o2));
assertTrue(EqualsBuilder.reflectionEquals(o1, o1));
assertTrue(EqualsBuilder.reflectionEquals(o1, o3));
double[] d1 = { 0, 1 };
double[] d2 = { 2, 3 };
double[] d3 = { 0, 1 };
assertTrue(!EqualsBuilder.reflectionEquals(d1, d2));
assertTrue(EqualsBuilder.reflectionEquals(d1, d1));
assertTrue(EqualsBuilder.reflectionEquals(d1, d3));
}
}