Skip to content

Commit

Permalink
Merge pull request #2914 from ahgittin/github-2913
Browse files Browse the repository at this point in the history
GitHub 2913
  • Loading branch information
juherr committed May 30, 2023
2 parents 2a4fb57 + 88eb73f commit 38279c6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Current

Fixed: GITHUB-2913: Maps containing nulls can be incorrectly considered equal (Alex Heneveld)

7.8.0
Fixed: GITHUB-2906: Generate testng-results.xml per test suite (Krishnan Mahadevan)
New: GITHUB-2897: Not exception but warning if some (not all) of the given test names are not found in suite files. (Bruce Wen)
Expand Down
7 changes: 5 additions & 2 deletions testng-asserts/src/main/java/org/testng/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -2109,11 +2109,14 @@ private static String getNotEqualReason(Map<?, ?> actual, Map<?, ?> expected) {
Object key = entry.getKey();
Object value = entry.getValue();
Object expectedValue = expected.get(key);
String assertMessage =
"Maps do not match for key:" + key + " actual:" + value + " expected:" + expectedValue;
if (!areEqualImpl(value, expectedValue)) {
String assertMessage =
"Maps do not match for key:" + key + " actual:" + value + " expected:" + expectedValue;
return assertMessage;
}
if (value == null && !expected.containsKey(key)) {
return "Maps do not match for key:" + key + " actual: null but not present in expected";
}
}
return null;
}
Expand Down
76 changes: 76 additions & 0 deletions testng-asserts/src/test/java/test/asserttests/AssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,80 @@ public void checkArrayNotEqualsFailsWhenDifferentOrder() {

assertNotEquals(array1, array2);
}

@Test
public void checkMapEquals() {
Map<String, Object> map1 = new LinkedHashMap<>();
map1.put("a", "1");
map1.put("b", 2);
Map<String, Object> map2 = new LinkedHashMap<>();
map2.put("b", 2);
map2.put("a", "1");

assertMapsEqual(map1, map2);
}

@Test
public void checkMapNotEquals() {
Map<String, Object> map1 = new LinkedHashMap<>();
map1.put("a", "1");
map1.put("b", "2");
Map<String, Object> map2 = new LinkedHashMap<>();
map2.put("a", "1");
map2.put("b", 2);

assertMapsNotEqual(map1, map2);
}

@Test(description = "GITHUB-2913")
public void checkMapNotEqualsWithNull() {
Map<String, Object> map1 = new LinkedHashMap<>();
map1.put("a", 1);
map1.put("b", null);
Map<String, Object> map2 = new LinkedHashMap<>();
map2.put("a", 1);
map2.put("c", null);

assertMapsNotEqual(map1, map2);
}

protected void assertMapsEqual(Map m1, Map m2) {
assertEquals((Object) m1, (Object) m2);
assertEquals(m1, m2);

boolean succeeded = false;
try {
assertNotEquals((Object) m1, (Object) m2);
succeeded = true;
} catch (AssertionError expected) {
}
assertFalse(succeeded, "Maps reported as not equal when equal: " + m1 + " / " + m2);

try {
assertNotEquals(m1, m2);
succeeded = true;
} catch (AssertionError expected) {
}
assertFalse(succeeded, "Maps reported as not equal when equal: " + m1 + " / " + m2);
}

protected void assertMapsNotEqual(Map m1, Map m2) {
boolean succeeded = false;
try {
assertEquals((Object) m1, (Object) m2);
succeeded = true;
} catch (AssertionError expected) {
}
assertFalse(succeeded, "Maps reported as equal when not equal: " + m1 + " / " + m2);

try {
assertEquals(m1, m2);
succeeded = true;
} catch (AssertionError expected) {
}
assertFalse(succeeded, "Maps reported as equal when not equal: " + m1 + " / " + m2);

assertNotEquals((Object) m1, (Object) m2);
assertNotEquals(m1, m2);
}
}

0 comments on commit 38279c6

Please sign in to comment.