[Misc] Document why SonarQube java:S5785 must not be applied to the equals() contract tests - #390
Merged
Merged
Conversation
…quals() contract tests * Add @SuppressWarnings("java:S5785") plus an explanatory comment on the 6 test methods that verify the equals()/hashCode() contract, following the SonarQube section of the Java Code Style, so the reason lives next to the code instead of only in SonarCloud * No assertion is changed Co-Authored-By: Vincent Massol <vincent@massol.net>
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Follow-up to #389, which took the opposite approach and was rightly closed after @michitux's review.
The 30
java:S5785issues in xwiki-rendering all sit in hand-writtenequals()/hashCode()contracttests. Converting
assertTrue(a.equals(b))toassertEquals(a, b)there is a bad idea, as the reviewpointed out: the boolean form is what makes it visible at the call site which object is the receiver and
what argument it gets. So instead of changing the assertions — and instead of just marking the issues
"accepted" in SonarCloud, where the reasoning is invisible to anyone reading the test — this PR records
the reason in the code, per the
SonarQube section of the Java Code Style.
Change
@SuppressWarnings("java:S5785"), preceded by an explanatory comment, on the 6 concerned test methods:xwiki-rendering-apiResourceReferenceTest#testEqualsxwiki-rendering-apiSyntaxTest#nonEqualityxwiki-rendering-apiSyntaxTypeTest#equalityAndInequalityxwiki-rendering-transformation-linkcheckerLinkStateTest#testEqualsxwiki-rendering-transformation-macroMacroIdTest#testEqualityxwiki-rendering-transformation-macroMacroContentSourceReferenceTest#equalsNo assertion is modified — the test code is byte-for-byte what is on
mastertoday, plus the commentand the annotation.
The explanation also names the specific trap, so the next person has the full picture: JUnit's
assertEquals/assertNotEqualsgo throughAssertionUtils.objectsAreEqual(a, b)(
a == null ? b == null : a.equals(b)), so a receiver-first conversion would in fact still call theoriginal
equals— but nothing at the call site says so, and SonarQube'sjava:S3415("swap these 2arguments") would later push someone to reverse the order, at which point the contract silently stops
being tested.
The annotations are scoped to the method rather than the class so the rest of each test class stays
covered by the rule.
There is precedent for exactly this pattern in xwiki-platform:
RegexEntityReferenceTestcarries@SuppressWarnings("java:S3415")with a comment explaining the same asymmetric-equalshazard.Notes
mvn clean install -Plegacy,quality(Checkstyle, Revapi, JaCoCo and the unit tests) onthe 3 affected modules.
will close on the next analysis once this lands, with the reason now living in the code.
Related
Other repos from the same sweep, unaffected by this discussion:
Generated by Claude Code