[testng] 46/355: symmetric equals assertion

Eugene Zhukov eugene-guest at moszumanska.debian.org
Tue Aug 18 10:19:46 UTC 2015


This is an automated email from the git hooks/post-receive script.

eugene-guest pushed a commit to annotated tag OpenBSD
in repository testng.

commit 18788dcf8b825f4e62e3a25bd9530786172a16ec
Author: Dennis Byrne <dennis.byrne at gmail.com>
Date:   Fri Feb 21 09:52:00 2014 -0800

    symmetric equals assertion
---
 src/main/java/org/testng/Assert.java     | 16 ++++----
 src/test/java/org/testng/AssertTest.java | 68 ++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/testng/Assert.java b/src/main/java/org/testng/Assert.java
index c945696..d57dceb 100644
--- a/src/main/java/org/testng/Assert.java
+++ b/src/main/java/org/testng/Assert.java
@@ -112,13 +112,15 @@ public class Assert {
     if((expected == null) && (actual == null)) {
       return;
     }
-    if(expected != null) {
-      if (expected.getClass().isArray()) {
-        assertArrayEquals(actual, expected, message);
-        return;
-      } else if (expected.equals(actual)) {
-        return;
-      }
+    if(expected == null ^ actual == null) {
+      failNotEquals(actual, expected, message);
+    }
+    if (expected.getClass().isArray()) {
+       assertArrayEquals(actual, expected, message);
+       return;
+    }
+    if (expected.equals(actual) && actual.equals(expected)) {
+       return;
     }
     failNotEquals(actual, expected, message);
   }
diff --git a/src/test/java/org/testng/AssertTest.java b/src/test/java/org/testng/AssertTest.java
index 10e3af8..d8b839b 100644
--- a/src/test/java/org/testng/AssertTest.java
+++ b/src/test/java/org/testng/AssertTest.java
@@ -115,4 +115,72 @@ public class AssertTest {
 
     Assert.assertEquals(mapActual, mapExpected);
   }
+
+  @Test(expectedExceptions = AssertionError.class)
+  public void assertEqualsSymmetricScalar() {
+    Assert.assertEquals(new Asymmetric(42, 'd'), new Contrived(42));
+  }
+
+  @Test(expectedExceptions = AssertionError.class)
+  public void assertEqualsSymmetricArrays() {
+    Object[] actual = {new Integer(1), new Asymmetric(42, 'd'), "inDay"};
+    Object[] expected = {new Integer(1), new Contrived(42), "inDay"};
+    Assert.assertEquals(actual, expected);
+  }
+
+  class Contrived {
+
+    int integer;
+
+    Contrived(int integer){
+      this.integer = integer;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+      if (this == o) return true;
+      if (!(o instanceof Contrived)) return false;
+
+      Contrived contrived = (Contrived) o;
+
+      if (integer != contrived.integer) return false;
+
+      return true;
+    }
+
+    @Override
+    public int hashCode() {
+      return integer;
+    }
+  }
+
+  class Asymmetric extends Contrived {
+
+      char character;
+
+      Asymmetric(int integer, char character) {
+          super(integer);
+          this.character = character;
+      }
+
+      @Override
+      public boolean equals(Object o) {
+          if (this == o) return true;
+          if (!(o instanceof Asymmetric)) return false;
+          if (!super.equals(o)) return false;
+
+          Asymmetric that = (Asymmetric) o;
+
+          if (character != that.character) return false;
+
+          return true;
+      }
+
+      @Override
+      public int hashCode() {
+          int result = super.hashCode();
+          result = 31 * result + (int) character;
+          return result;
+      }
+  }
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/testng.git



More information about the pkg-java-commits mailing list