Skip to content

Commit 6f7a81d

Browse files
Fix issue, improve coverage
1 parent b4302a7 commit 6f7a81d

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,9 @@ 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));
497498

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

506+
@Test
507+
void test_should_fail_on_stackoverflow_when_sonar_components_null() {
508+
InputFile trivialCompilationUnit = TestUtils.inputFile("src/test/resources/AstScannerNoParseError.txt");
509+
var files = List.of(trivialCompilationUnit);
510+
var problematicVisitor = new JavaFileScanner(){
511+
@Override
512+
public void scanFile(JavaFileScannerContext context) {
513+
throw new StackOverflowError();
514+
}
515+
};
516+
List<JavaFileScanner> visitors = List.of(problematicVisitor);
517+
JavaAstScanner scanner = new JavaAstScanner(null, new NoOpTelemetry(), TelemetryKey.JAVA_ANALYSIS_MAIN);
518+
VisitorsBridge visitorBridge = new VisitorsBridge(visitors, new ArrayList<>(), null, new JavaVersionImpl(JavaVersionImpl.MAX_SUPPORTED));
519+
scanner.setVisitorBridge(visitorBridge);
520+
assertThrows(StackOverflowError.class, () -> scanner.scan(files));
521+
}
522+
505523
private void scanSingleFile(InputFile file, boolean failOnException) {
506524
scanFilesWithVisitors(Collections.singletonList(file), Collections.emptyList(), -1, failOnException, false);
507525
}

0 commit comments

Comments
 (0)