use of org.sonar.plugins.java.api.JavaFileScanner in project sonar-java by SonarSource.
the class VisitorsBridgeTest method log_only_50_elements.
@Test
public void log_only_50_elements() throws Exception {
DecimalFormat formatter = new DecimalFormat("00");
IntFunction<String> classNotFoundName = i -> "NotFound" + formatter.format(i);
VisitorsBridge visitorsBridge = new VisitorsBridge(Collections.singletonList((JavaFileScanner) context -> {
assertThat(context.getSemanticModel()).isNotNull();
((SemanticModel) context.getSemanticModel()).classesNotFound().addAll(IntStream.range(0, 60).mapToObj(classNotFoundName).collect(Collectors.toList()));
}), Lists.newArrayList(), null);
checkFile("Foo.java", "class Foo {}", visitorsBridge);
visitorsBridge.endOfAnalysis();
assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Classes not found during the analysis : [" + IntStream.range(0, 50).mapToObj(classNotFoundName).sorted().collect(Collectors.joining(", ")) + ", ...]");
}
use of org.sonar.plugins.java.api.JavaFileScanner in project sonar-java by SonarSource.
the class VisitorsBridgeTest method rethrow_exception_when_hidden_property_set_to_true.
@Test
public void rethrow_exception_when_hidden_property_set_to_true() {
NullPointerException npe = new NullPointerException("BimBadaboum");
JavaFileScanner visitor = c -> {
throw npe;
};
File currentFile = new File("");
SensorContextTester sensorContextTester = SensorContextTester.create(currentFile);
SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null, null);
sonarComponents.setSensorContext(sensorContextTester);
VisitorsBridge visitorsBridge = new VisitorsBridge(Collections.singleton(visitor), new ArrayList<>(), sonarComponents);
visitorsBridge.setCurrentFile(currentFile);
try {
visitorsBridge.visitFile(null);
assertThat(sonarComponents.analysisErrors).hasSize(1);
} catch (Exception e) {
e.printStackTrace();
Fail.fail("Exception should be swallowed when property is not set");
}
sensorContextTester.settings().appendProperty(SonarComponents.FAIL_ON_EXCEPTION_KEY, "true");
try {
visitorsBridge.visitFile(null);
Fail.fail("scanning of file should have raise an exception");
} catch (Exception e) {
assertThat(e).isSameAs(npe);
}
}
use of org.sonar.plugins.java.api.JavaFileScanner in project sonar-java by SonarSource.
the class JavaAstScannerTest method should_handle_analysis_cancellation.
@Test
public void should_handle_analysis_cancellation() throws Exception {
JavaFileScanner visitor = spy(new JavaFileScanner() {
@Override
public void scanFile(JavaFileScannerContext context) {
// do nothing
}
});
SonarComponents sonarComponents = mock(SonarComponents.class);
when(sonarComponents.analysisCancelled()).thenReturn(true);
JavaAstScanner scanner = new JavaAstScanner(JavaParser.createParser(), sonarComponents);
scanner.setVisitorBridge(new VisitorsBridge(Lists.newArrayList(visitor), Lists.newArrayList(), sonarComponents));
scanner.scan(ImmutableList.of(new File("src/test/files/metrics/NoSonar.java")));
verifyZeroInteractions(visitor);
}
use of org.sonar.plugins.java.api.JavaFileScanner in project sonar-java by SonarSource.
the class JavaCheckVerifier method scanFile.
private static void scanFile(String filename, JavaFileScanner check, JavaCheckVerifier javaCheckVerifier, Collection<File> classpath, boolean withSemantic) {
JavaFileScanner expectedIssueCollector = new ExpectedIssueCollector(javaCheckVerifier);
VisitorsBridgeForTests visitorsBridge;
File file = new File(filename);
SonarComponents sonarComponents = CheckVerifier.sonarComponents(file);
if (withSemantic) {
visitorsBridge = new VisitorsBridgeForTests(Lists.newArrayList(check, expectedIssueCollector), Lists.newArrayList(classpath), sonarComponents);
} else {
visitorsBridge = new VisitorsBridgeForTests(Lists.newArrayList(check, expectedIssueCollector), sonarComponents);
}
JavaAstScanner.scanSingleFileForTests(file, visitorsBridge, javaCheckVerifier.javaVersion);
VisitorsBridgeForTests.TestJavaFileScannerContext testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext();
if (testJavaFileScannerContext == null) {
Fail.fail("Semantic was required but it was not possible to create it. Please checks the logs to find out the reason.");
}
javaCheckVerifier.checkIssues(testJavaFileScannerContext.getIssues(), javaCheckVerifier.providedJavaVersion);
}
use of org.sonar.plugins.java.api.JavaFileScanner in project sonar-java by SonarSource.
the class JavaCheckVerifier method scanFile.
private void scanFile(String filename, JavaFileScanner[] checks, Collection<File> classpath) {
List<JavaFileScanner> visitors = new ArrayList<>(Arrays.asList(checks));
visitors.add(expectations.parser());
VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(visitors, Lists.newArrayList(classpath), null);
JavaAstScanner.scanSingleFileForTests(new File(filename), visitorsBridge);
VisitorsBridgeForTests.TestJavaFileScannerContext testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext();
checkIssues(testJavaFileScannerContext.getIssues());
}
Aggregations