Skip to content

Commit

Permalink
SONARJAVA-5241 Support .withoutSemantic() mode in FilterVerifier (#4958)
Browse files Browse the repository at this point in the history
Co-authored-by: Dorian Burihabwa <[email protected]>
  • Loading branch information
1 parent 2d6bb09 commit bf9a0b3
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<JavaCheck> visitors = new ArrayList<>();
visitors.add(filter);
Expand All @@ -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();

Expand Down Expand Up @@ -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<JavaCheck> instantiateRules(Set<Class<? extends JavaCheck>> filteredRules) {
Set<JavaCheck> rules = new HashSet<>();
for (Class<? extends JavaCheck> rule : filteredRules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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()
);
Expand Down

0 comments on commit bf9a0b3

Please sign in to comment.