-
Notifications
You must be signed in to change notification settings - Fork 727
SONARJAVA-6405 #5636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
SONARJAVA-6405 #5636
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package checks.tests; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| class OneBeforeEachAfterEachCheckSample { // Noncompliant {{Only one method should be annotated @Before(Each).}} | ||
|
|
||
| @BeforeEach | ||
| void setUp1() { | ||
| // pass | ||
| } | ||
|
|
||
| @BeforeEach | ||
| void setUp2() { | ||
| // pass | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void decoy() { | ||
| fail("not important"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks.tests; | ||
|
|
||
| import org.sonar.check.Rule; | ||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
| import org.sonar.plugins.java.api.tree.ClassTree; | ||
| import org.sonar.plugins.java.api.tree.IdentifierTree; | ||
| import org.sonar.plugins.java.api.tree.MethodTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| // FIXME: get a rule id | ||
| @Rule(key = "S3360") | ||
| public class OneBeforeEachAfterEachCheck extends IssuableSubscriptionVisitor { | ||
|
|
||
| private static final String BEFORE_EACH = "org.junit.jupiter.api.BeforeEach"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| private static final String BEFORE = "org.junit.Before"; | ||
|
|
||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return List.of(Tree.Kind.CLASS); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| ClassTree classTree = (ClassTree) tree; | ||
| IdentifierTree className = classTree.simpleName(); | ||
| if (className == null) { | ||
| return; | ||
| } | ||
| int beforeCount = countAnnotations(BEFORE, classTree); | ||
| int beforeEachCount = countAnnotations(BEFORE_EACH, classTree); | ||
| if (beforeCount > 1 || beforeEachCount > 1) { | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
| reportIssue(className, "Only one method should be annotated @Before(Each)."); | ||
| } | ||
| } | ||
|
|
||
| private int countAnnotations(String annotation, ClassTree classTree) { | ||
| return (int) classTree.members().stream() | ||
| .filter(member -> member.is(Tree.Kind.METHOD)) | ||
| .map(MethodTree.class::cast) | ||
| .filter(method -> method.symbol().metadata().isAnnotatedWith(annotation)) | ||
| .count(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks.tests; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
|
||
| import static org.sonar.java.checks.verifier.TestUtils.testCodeSourcesPath; | ||
|
|
||
| class OneBeforeEachAfterEachCheckTest { | ||
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(testCodeSourcesPath("checks/tests/OneBeforeEachAfterEachCheckSample.java")) | ||
| .withCheck(new OneBeforeEachAfterEachCheck()) | ||
| .verifyIssues(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <h2>Why is this an issue?</h2> | ||
| <p>By default, the Maven Surefire plugin only executes test classes with names that end in "Test" or "TestCase". Name your class "TestClassX.java", | ||
| for instance, and it will be skipped.</p> | ||
| <p>This rule raises an issue for each test class with a name not ending in "Test" or "TestCase".</p> | ||
| <h3>Noncompliant code example</h3> | ||
| <pre> | ||
| public class TestClassX { // Noncompliant | ||
| @Test | ||
| public void testDoTheThing() { | ||
| //... | ||
| </pre> | ||
| <h3>Compliant solution</h3> | ||
| <pre> | ||
| public class ClassXTest { | ||
| @Test | ||
| public void testDoTheThing() { | ||
| //... | ||
| </pre> | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "title": "Test class names should end with \"Test\" or \"TestCase\"", | ||
|
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| "type": "CODE_SMELL", | ||
| "code": { | ||
| "impacts": { | ||
| "MAINTAINABILITY": "BLOCKER" | ||
| }, | ||
| "attribute": "IDENTIFIABLE" | ||
| }, | ||
| "status": "ready", | ||
| "remediation": { | ||
| "func": "Constant\/Issue", | ||
| "constantCost": "2min" | ||
| }, | ||
| "tags": [ | ||
| "tests" | ||
| ], | ||
| "defaultSeverity": "Blocker", | ||
| "ruleSpecification": "RSPEC-3360", | ||
| "sqKey": "S3360", | ||
| "scope": "Main", | ||
| "quickfix": "unknown" | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Quality: Test sample only covers @beforeeach, not @before or @after*
The test sample
OneBeforeEachAfterEachCheckSample.javaonly tests the case of multiple@BeforeEachmethods. It doesn't test@Before,@AfterEach,@After, compliant cases (single annotation), or mixed annotation scenarios. This leaves most of the rule's intended behavior unverified.Was this helpful? React with 👍 / 👎