From: Stephen Kitt Date: Thu, 6 Oct 2016 08:13:58 +0000 (+0200) Subject: Support testing null against null in equalsHelper() X-Git-Tag: release/carbon~333 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F99%2F46599%2F4;hp=5703e5181092b44e8565fa318d1318c3d6a9295f;p=yangtools.git Support testing null against null in equalsHelper() In its intended context, equalsHelper() should never be called with two null parameters, but it's easy to support the general case — and because this isn't SQL, null equals itself. Change-Id: I826435e81a4dd59aaac72ad9ab07a80069f7a17f Signed-off-by: Stephen Kitt --- diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/EvenMoreObjects.java b/common/util/src/main/java/org/opendaylight/yangtools/util/EvenMoreObjects.java index c7988c1bf8..137ff43e78 100644 --- a/common/util/src/main/java/org/opendaylight/yangtools/util/EvenMoreObjects.java +++ b/common/util/src/main/java/org/opendaylight/yangtools/util/EvenMoreObjects.java @@ -34,12 +34,12 @@ public final class EvenMoreObjects { @SuppressWarnings("unchecked") public static boolean equalsHelper(T self, Object other, BooleanEqualsFunction equals) { - if (other == null) { - return false; - } if (other == self) { return true; } + if (other == null) { + return false; + } if (self.getClass() != other.getClass()) { return false; } diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/EvenMoreObjectsTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/EvenMoreObjectsTest.java index 644f6e32df..59bcd85217 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/EvenMoreObjectsTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/EvenMoreObjectsTest.java @@ -7,6 +7,8 @@ */ package org.opendaylight.yangtools.util; +import static org.junit.Assert.assertTrue; + import com.google.common.base.MoreObjects; import com.google.common.testing.EqualsTester; import java.util.Objects; @@ -15,7 +17,7 @@ import org.junit.Test; public class EvenMoreObjectsTest { @Test - public void test() { + public void thingPassesEqualsTester() { new EqualsTester() .addEqualityGroup(new Thing("hello", 123), new Thing("hello", 123)) .addEqualityGroup(new Thing("hoi", 123), new Thing("hoi", 123)) @@ -24,7 +26,12 @@ public class EvenMoreObjectsTest { .testEquals(); } - static class Thing { + @Test + public void nullEqualsNull() { + assertTrue(EvenMoreObjects.equalsHelper(null, null, (one, another) -> true)); + } + + private static class Thing { String name; Integer age;