Skip to content

SONARJAVA-5041 S5786 should raise an issue on JUnit5 annotated lifecycle methods with a public modifier#5004

Merged
dorian-burihabwa-sonarsource merged 2 commits intomasterfrom
alban/SONARJAVA-5041
Feb 13, 2025
Merged

SONARJAVA-5041 S5786 should raise an issue on JUnit5 annotated lifecycle methods with a public modifier#5004
dorian-burihabwa-sonarsource merged 2 commits intomasterfrom
alban/SONARJAVA-5041

Conversation

@alban-auzeill
Copy link
Copy Markdown
Member

@alban-auzeill alban-auzeill commented Feb 6, 2025

@sonarqube-next
Copy link
Copy Markdown

sonarqube-next Bot commented Feb 6, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good overall but it looks like we don't have examples of issues raised on classes by S5786 with the new logic. It probably would help if we could get some documentation on the modifier scope enums to understand their usage

522
],
"org.eclipse.jetty:jetty-project:jetty-io/src/test/java/org/eclipse/jetty/io/SslEngineBehaviorTest.java": [
39,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 39 matches the class declaration. Are changes to the rules supposed to change our behavior on classes in addition to methods?

634
],
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/ErrorHandlerTest.java": [
54,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was fun to review 😅


raiseIssueOnMethods(junit5ClassMethods, ModifierScope.CLASS_METHOD);
raiseIssueOnMethods(junit5InstanceMethods, ModifierScope.INSTANCE_METHOD);
if (!junit5InstanceMethods.isEmpty()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract this for readability?

Suggested change
if (!junit5InstanceMethods.isEmpty()) {
boolean classHasJunit5InstanceMethods = !junit5InstanceMethods.isEmpty();
if (classHasJunit5InstanceMethods) {

if (!testMethods.isEmpty()) {
raiseIssueOnNotCompliantModifiers(classTree.modifiers(), false);
// Can we change the visibility of the class?
if (!hasPublicStaticMethods && !hasPublicStaticFields) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I like seeing doubling down on negatives here

Comment on lines +39 to +46
public enum ModifierScope {
CLASS,
CLASS_METHOD,
INSTANCE_METHOD
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be very helpful to document how these enums represent and how they can be used

@sonarqube-next
Copy link
Copy Markdown

@dorian-burihabwa-sonarsource dorian-burihabwa-sonarsource deleted the alban/SONARJAVA-5041 branch February 13, 2025 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants