Skip assertEquals/assertNotEquals conversion when equals() argument is null - #1
Open
mattdepaula wants to merge 3 commits into
Open
Skip assertEquals/assertNotEquals conversion when equals() argument is null#1mattdepaula wants to merge 3 commits into
mattdepaula wants to merge 3 commits into
Conversation
…s null AssertFalseEqualsToAssertNotEquals and AssertTrueEqualsToAssertEquals were converting assertFalse(a.equals(null)) and assertTrue(a.equals(null)) to assertNotEquals(a, null) / assertEquals(a, null). AssertionsArgumentOrder then hoisted null to the expected position, producing assertNotEquals(null, a). Assertions.objectsAreEqual short-circuits on a null first argument, returning a == null without ever invoking a.equals(null). This silently disabled the null-contract test for custom equals() implementations. Fix: return false from isEquals() when the argument to .equals() is a null literal, leaving the original assertion unchanged. Fixes openrewrite#1071
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AssertFalseEqualsToAssertNotEqualsandAssertTrueEqualsToAssertEqualsboth rewriteassertFalse(a.equals(b))/assertTrue(a.equals(b))toassertNotEquals(a, b)/assertEquals(a, b). When the argument to.equals()is anullliteral this rewrite silently changes semantics:assertFalse(a.equals(null))— callsa.equals(null), exercising the null-contract ofequals()assertNotEquals(a, null)— JUnit's internalobjectsAreEqual(a, null)short-circuits toa == nullvia reference equality, never callinga.equals(null)The fix adds an early-exit guard in
isEquals()using the idiomaticJ.Literal.isLiteralValue(arg, null)check (consistent withAssertFalseNullToAssertNotNull,AssertTrueNullToAssertNull, and others in the same package).Upstream issue
openrewrite#1072
Changes
AssertFalseEqualsToAssertNotEquals:isEquals()returnsfalsewhen the argument to.equals()is a null literalAssertTrueEqualsToAssertEquals: same fixdoNotConvertWhenArgumentToEqualsIsNulltest asserting no change forassertFalse(a.equals(null))/assertTrue(a.equals(null))Test plan
AssertFalseEqualToAssertNotEqualsTest#doNotConvertWhenArgumentToEqualsIsNull— new, asserts no rewriteAssertTrueEqualsToAssertEqualsTest#doNotConvertWhenArgumentToEqualsIsNull— new, asserts no rewrite