From bf9a0b39f68278e7fb244ec3b07a9e91b6e82bf3 Mon Sep 17 00:00:00 2001 From: tomasz-tylenda-sonarsource Date: Thu, 12 Dec 2024 11:17:41 +0100 Subject: [PATCH] SONARJAVA-5241 Support .withoutSemantic() mode in FilterVerifier (#4958) Co-authored-by: Dorian Burihabwa --- .../filters/LombokFilterWithoutSemantic.java | 9 ++++++ .../java/filters/EclipseI18NFilterTest.java | 3 +- .../sonar/java/filters/FilterVerifier.java | 28 +++++++++++++++++-- .../java/filters/FilterVerifierTest.java | 15 +++++++++- .../java/filters/GeneratedCodeFilterTest.java | 2 +- .../java/filters/GoogleAutoFilterTest.java | 3 +- .../sonar/java/filters/LombokFilterTest.java | 10 ++++++- .../sonar/java/filters/SpringFilterTest.java | 6 ++-- .../filters/SuppressWarningFilterTest.java | 8 +++--- 9 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 java-checks/src/test/files/filters/LombokFilterWithoutSemantic.java diff --git a/java-checks/src/test/files/filters/LombokFilterWithoutSemantic.java b/java-checks/src/test/files/filters/LombokFilterWithoutSemantic.java new file mode 100644 index 00000000000..526b96b6a65 --- /dev/null +++ b/java-checks/src/test/files/filters/LombokFilterWithoutSemantic.java @@ -0,0 +1,9 @@ +import lombok.experimental.UtilityClass; + +// FP happening without semantics. +@UtilityClass +public class Utility { // WithIssue + public static int triple(int in) { + return in * 3; + } +} diff --git a/java-checks/src/test/java/org/sonar/java/filters/EclipseI18NFilterTest.java b/java-checks/src/test/java/org/sonar/java/filters/EclipseI18NFilterTest.java index 05a819da45c..8c59ed32a06 100644 --- a/java-checks/src/test/java/org/sonar/java/filters/EclipseI18NFilterTest.java +++ b/java-checks/src/test/java/org/sonar/java/filters/EclipseI18NFilterTest.java @@ -22,7 +22,8 @@ class EclipseI18NFilterTest { @Test void test() { - FilterVerifier.verify("src/test/files/filters/EclipseI18NFilter.java", new EclipseI18NFilter()); + FilterVerifier.newInstance() + .verify("src/test/files/filters/EclipseI18NFilter.java", new EclipseI18NFilter()); } } diff --git a/java-checks/src/test/java/org/sonar/java/filters/FilterVerifier.java b/java-checks/src/test/java/org/sonar/java/filters/FilterVerifier.java index 17697fcab50..599da598d41 100644 --- a/java-checks/src/test/java/org/sonar/java/filters/FilterVerifier.java +++ b/java-checks/src/test/java/org/sonar/java/filters/FilterVerifier.java @@ -40,6 +40,7 @@ import org.sonar.java.SonarComponents; import org.sonar.java.ast.JavaAstScanner; import org.sonar.java.ast.visitors.SubscriptionVisitor; +import org.sonar.java.checks.verifier.CheckVerifier; import org.sonar.java.checks.verifier.FilesUtils; import org.sonar.java.checks.verifier.TestUtils; import org.sonar.java.model.JavaVersionImpl; @@ -58,7 +59,16 @@ public class FilterVerifier { - public static void verify(String filename, JavaIssueFilter filter, JavaCheck... extraJavaChecks) { + private FilterVerifier() { + } + + public static FilterVerifier newInstance() { + return new FilterVerifier(); + } + + private boolean withoutSemantic = false; + + public void verify(String filename, JavaIssueFilter filter, JavaCheck... extraJavaChecks) { IssueCollector issueCollector = new IssueCollector(); List visitors = new ArrayList<>(); visitors.add(filter); @@ -74,7 +84,12 @@ public static void verify(String filename, JavaIssueFilter filter, JavaCheck... projectClasspath.add(new File("target/test-classes")); InputFile inputFile = TestUtils.inputFile(filename); - VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(visitors, projectClasspath, sonarComponents(inputFile), new JavaVersionImpl()); + VisitorsBridgeForTests visitorsBridge; + if (this.withoutSemantic) { + visitorsBridge = new VisitorsBridgeForTests(visitors, sonarComponents(inputFile), new JavaVersionImpl()); + } else { + visitorsBridge = new VisitorsBridgeForTests(visitors, projectClasspath, sonarComponents(inputFile), new JavaVersionImpl()); + } JavaAstScanner.scanSingleFileForTests(inputFile, visitorsBridge); JavaFileScannerContextForTests testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext(); @@ -114,6 +129,15 @@ public static void verify(String filename, JavaIssueFilter filter, JavaCheck... } } + /** + * Tells the verifier that no bytecode will be provided. + * See {@link CheckVerifier#withoutSemantic()} for more. + */ + public FilterVerifier withoutSemantic() { + this.withoutSemantic = true; + return this; + } + private static Set instantiateRules(Set> filteredRules) { Set rules = new HashSet<>(); for (Class rule : filteredRules) { diff --git a/java-checks/src/test/java/org/sonar/java/filters/FilterVerifierTest.java b/java-checks/src/test/java/org/sonar/java/filters/FilterVerifierTest.java index 4232db9586e..81b39d178ba 100644 --- a/java-checks/src/test/java/org/sonar/java/filters/FilterVerifierTest.java +++ b/java-checks/src/test/java/org/sonar/java/filters/FilterVerifierTest.java @@ -29,7 +29,20 @@ class FilterVerifierTest { void filterVerifierShouldFailInCaseOfInvalidFile() { String parseErrorFile = "src/test/files/filters/FilterParseError.java"; JavaIssueFilter testIssueFilter = new TestIssueFilter(); - assertThrows(AssertionError.class, () -> FilterVerifier.verify(parseErrorFile, testIssueFilter)); + FilterVerifier filterVerifier = FilterVerifier.newInstance(); + assertThrows( + AssertionError.class, + () -> filterVerifier.verify(parseErrorFile, testIssueFilter)); + } + + @Test + void filterVerifierShouldFailInCaseOfInvalidFileWithoutSemantic() { + String parseErrorFile = "src/test/files/filters/FilterParseError.java"; + JavaIssueFilter testIssueFilter = new TestIssueFilter(); + FilterVerifier filterWithoutSemantic = FilterVerifier.newInstance().withoutSemantic(); + assertThrows( + AssertionError.class, + () -> filterWithoutSemantic.verify(parseErrorFile, testIssueFilter)); } /** diff --git a/java-checks/src/test/java/org/sonar/java/filters/GeneratedCodeFilterTest.java b/java-checks/src/test/java/org/sonar/java/filters/GeneratedCodeFilterTest.java index a0f2ee65165..4db73ed4e93 100644 --- a/java-checks/src/test/java/org/sonar/java/filters/GeneratedCodeFilterTest.java +++ b/java-checks/src/test/java/org/sonar/java/filters/GeneratedCodeFilterTest.java @@ -28,7 +28,7 @@ class GeneratedCodeFilterTest { void test() { CommentRegularExpressionCheck commentRegularExpressionCheck = new CommentRegularExpressionCheck(); commentRegularExpressionCheck.regularExpression = ".*alpha.*"; - FilterVerifier.verify("src/test/files/filters/GeneratedCodeFilter.java", new GeneratedCodeFilter(), + FilterVerifier.newInstance().verify("src/test/files/filters/GeneratedCodeFilter.java", new GeneratedCodeFilter(), // activated rules commentRegularExpressionCheck, new BadClassNameCheck(), diff --git a/java-checks/src/test/java/org/sonar/java/filters/GoogleAutoFilterTest.java b/java-checks/src/test/java/org/sonar/java/filters/GoogleAutoFilterTest.java index 3a31513eb82..1f24b37ea2b 100644 --- a/java-checks/src/test/java/org/sonar/java/filters/GoogleAutoFilterTest.java +++ b/java-checks/src/test/java/org/sonar/java/filters/GoogleAutoFilterTest.java @@ -22,7 +22,8 @@ class GoogleAutoFilterTest { @Test void test() { - FilterVerifier.verify("src/test/files/filters/GoogleAutoFilter.java", new GoogleAutoFilter()); + FilterVerifier.newInstance() + .verify("src/test/files/filters/GoogleAutoFilter.java", new GoogleAutoFilter()); } } diff --git a/java-checks/src/test/java/org/sonar/java/filters/LombokFilterTest.java b/java-checks/src/test/java/org/sonar/java/filters/LombokFilterTest.java index ee8cda50d4c..f91149f1050 100644 --- a/java-checks/src/test/java/org/sonar/java/filters/LombokFilterTest.java +++ b/java-checks/src/test/java/org/sonar/java/filters/LombokFilterTest.java @@ -22,6 +22,14 @@ class LombokFilterTest { @Test void test() { - FilterVerifier.verify("src/test/files/filters/LombokFilter.java", new LombokFilter()); + FilterVerifier.newInstance() + .verify("src/test/files/filters/LombokFilter.java", new LombokFilter()); + } + + @Test + void testWithoutSemantic() { + FilterVerifier.newInstance() + .withoutSemantic() + .verify("src/test/files/filters/LombokFilterWithoutSemantic.java", new LombokFilter()); } } diff --git a/java-checks/src/test/java/org/sonar/java/filters/SpringFilterTest.java b/java-checks/src/test/java/org/sonar/java/filters/SpringFilterTest.java index cee9f60b309..ee0a471d295 100644 --- a/java-checks/src/test/java/org/sonar/java/filters/SpringFilterTest.java +++ b/java-checks/src/test/java/org/sonar/java/filters/SpringFilterTest.java @@ -24,11 +24,13 @@ class SpringFilterTest { @Test void test() { - FilterVerifier.verify(mainCodeSourcesPath("filters/SpringFilter.java"), new SpringFilter()); + FilterVerifier.newInstance() + .verify(mainCodeSourcesPath("filters/SpringFilter.java"), new SpringFilter()); } @Test void test_without_semantic() { - FilterVerifier.verify(nonCompilingTestSourcesPath("filters/SpringFilter.java"), new SpringFilter()); + FilterVerifier.newInstance() + .verify(nonCompilingTestSourcesPath("filters/SpringFilter.java"), new SpringFilter()); } } diff --git a/java-checks/src/test/java/org/sonar/java/filters/SuppressWarningFilterTest.java b/java-checks/src/test/java/org/sonar/java/filters/SuppressWarningFilterTest.java index e7a12a72daa..e9e21941a87 100644 --- a/java-checks/src/test/java/org/sonar/java/filters/SuppressWarningFilterTest.java +++ b/java-checks/src/test/java/org/sonar/java/filters/SuppressWarningFilterTest.java @@ -56,7 +56,7 @@ class SuppressWarningFilterTest { public static final String CONSTANT_RULE_KEY = "java:S115"; @Test void verify() { - FilterVerifier.verify("src/test/files/filters/SuppressWarningFilter.java", new SuppressWarningFilter(), + FilterVerifier.newInstance().verify("src/test/files/filters/SuppressWarningFilter.java", new SuppressWarningFilter(), // activated rules new UnusedPrivateFieldCheck(), new BadConstantNameCheck(), @@ -80,7 +80,7 @@ void verify() { @Test void verify_2() { - FilterVerifier.verify("src/test/files/filters/SuppressWarningFilter_2.java", new SuppressWarningFilter(), + FilterVerifier.newInstance().verify("src/test/files/filters/SuppressWarningFilter_2.java", new SuppressWarningFilter(), // activated rules new BoxedBooleanExpressionsCheck(), new ImmediateReverseBoxingCheck(), @@ -95,7 +95,7 @@ void verify_2() { @Test void verify_unused() { - FilterVerifier.verify("src/test/files/filters/SuppressWarningFilter_unused.java", new SuppressWarningFilter(), + FilterVerifier.newInstance().verify("src/test/files/filters/SuppressWarningFilter_unused.java", new SuppressWarningFilter(), // activated rules new UnusedLocalVariableCheck(), new UnusedLabelCheck(), @@ -109,7 +109,7 @@ void verify_unused() { @Test void verify_javadoc() { - FilterVerifier.verify("src/test/files/filters/SuppressWarningFilter_javadoc.java", new SuppressWarningFilter(), + FilterVerifier.newInstance().verify("src/test/files/filters/SuppressWarningFilter_javadoc.java", new SuppressWarningFilter(), // activated rules new UndocumentedApiCheck() );