Skip to content

Commit 08f6d26

Browse files
Fix issue, improve coverage
1 parent b4302a7 commit 08f6d26

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

java-frontend/src/test/java/org/sonar/java/ast/JavaAstScannerTest.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ public void scanFile(JavaFileScannerContext context) {
480480
}
481481

482482
@Test
483-
void test_should_fail_on_stackoverflow() {
483+
void test_should_fail_on_stackoverflow_by_default() {
484484
InputFile trivialCompilationUnit = TestUtils.inputFile("src/test/resources/AstScannerNoParseError.txt");
485485
var files = List.of(trivialCompilationUnit);
486486
var problematicVisitor = new JavaFileScanner(){
@@ -492,8 +492,22 @@ public void scanFile(JavaFileScannerContext context) {
492492
List<JavaFileScanner> visitors = List.of(problematicVisitor);
493493

494494
// Assert that by default, StackOverflowError is propagated
495+
var emptySettings = new MapSettings();
495496
assertThrows(StackOverflowError.class, () ->
496-
scanFilesWithVisitorsAndContext(files, visitors, new MapSettings(), JavaVersionImpl.MAX_SUPPORTED));
497+
scanFilesWithVisitorsAndContext(files, visitors, emptySettings, JavaVersionImpl.MAX_SUPPORTED));
498+
}
499+
500+
@Test
501+
void test_should_not_fail_on_stackoverflow_when_set() {
502+
InputFile trivialCompilationUnit = TestUtils.inputFile("src/test/resources/AstScannerNoParseError.txt");
503+
var files = List.of(trivialCompilationUnit);
504+
var problematicVisitor = new JavaFileScanner(){
505+
@Override
506+
public void scanFile(JavaFileScannerContext context) {
507+
throw new StackOverflowError();
508+
}
509+
};
510+
List<JavaFileScanner> visitors = List.of(problematicVisitor);
497511

498512
// Assert that when configured to not fail on exception, StackOverflowError is swallowed, but logged
499513
MapSettings settings = new MapSettings().setProperty(SonarComponents.SONAR_FAIL_ON_STACKOVERFLOW, false);
@@ -502,6 +516,23 @@ public void scanFile(JavaFileScannerContext context) {
502516
.containsExactly("A stack overflow error occurred while analyzing file: 'src/test/resources/AstScannerNoParseError.txt'");
503517
}
504518

519+
@Test
520+
void test_should_fail_on_stackoverflow_when_sonar_components_null() {
521+
InputFile trivialCompilationUnit = TestUtils.inputFile("src/test/resources/AstScannerNoParseError.txt");
522+
var files = List.of(trivialCompilationUnit);
523+
var problematicVisitor = new JavaFileScanner(){
524+
@Override
525+
public void scanFile(JavaFileScannerContext context) {
526+
throw new StackOverflowError();
527+
}
528+
};
529+
List<JavaFileScanner> visitors = List.of(problematicVisitor);
530+
JavaAstScanner scanner = new JavaAstScanner(null, new NoOpTelemetry(), TelemetryKey.JAVA_ANALYSIS_MAIN);
531+
VisitorsBridge visitorBridge = new VisitorsBridge(visitors, new ArrayList<>(), null, new JavaVersionImpl(JavaVersionImpl.MAX_SUPPORTED));
532+
scanner.setVisitorBridge(visitorBridge);
533+
assertThrows(StackOverflowError.class, () -> scanner.scan(files));
534+
}
535+
505536
private void scanSingleFile(InputFile file, boolean failOnException) {
506537
scanFilesWithVisitors(Collections.singletonList(file), Collections.emptyList(), -1, failOnException, false);
507538
}

0 commit comments

Comments
 (0)