use of org.sonar.java.SonarComponents 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.java.SonarComponents in project sonar-java by SonarSource.
the class JavaAstScannerTest method should_swallow_log_and_report_checks_exceptions_for_symbolic_execution.
@Test
public void should_swallow_log_and_report_checks_exceptions_for_symbolic_execution() {
JavaAstScanner scanner = new JavaAstScanner(JavaParser.createParser(), null);
logTester.clear();
SonarComponents sonarComponent = new SonarComponents(null, context.fileSystem(), null, null, null, null);
context.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
sonarComponent.setSensorContext(context);
scanner.setVisitorBridge(new VisitorsBridge(Collections.singletonList(new SECheck() {
@Override
public void init(MethodTree methodTree, CFG cfg) {
throw new NullPointerException("nobody expect the spanish inquisition !");
}
}), new ArrayList<>(), sonarComponent, SymbolicExecutionMode.ENABLED_WITHOUT_X_FILE));
scanner.scan(ImmutableList.of(new File("src/test/resources/se/MethodBehavior.java")));
assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1);
assertThat(logTester.logs(LoggerLevel.ERROR).get(0)).startsWith("Unable to run check class org.sonar.java.se.SymbolicExecutionVisitor");
assertThat(sonarComponent.analysisErrors).hasSize(1);
assertThat(sonarComponent.analysisErrors.get(0).getKind()).isSameAs(AnalysisError.Kind.SE_ERROR);
}
use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.
the class JavaAstScannerTest method should_report_analysis_error_in_sonarLint_context_withSQ_6_0.
@Test
public void should_report_analysis_error_in_sonarLint_context_withSQ_6_0() {
JavaAstScanner scanner = defaultJavaAstScanner();
FakeAuditListener listener = spy(new FakeAuditListener());
SonarComponents sonarComponents = mock(SonarComponents.class);
when(sonarComponents.reportAnalysisError(any(RecognitionException.class), any(File.class))).thenReturn(true);
scanner.setVisitorBridge(new VisitorsBridge(Lists.newArrayList(listener), Lists.newArrayList(), sonarComponents));
scanner.scan(ImmutableList.of(new File("src/test/resources/AstScannerParseError.txt")));
verify(sonarComponents).reportAnalysisError(any(RecognitionException.class), any(File.class));
verifyZeroInteractions(listener);
}
use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.
the class JavaSquidSensorTest method createSonarComponentsMock.
private static SonarComponents createSonarComponentsMock(SensorContextTester contextTester) {
Configuration settings = new MapSettings().asConfig();
DefaultFileSystem fs = contextTester.fileSystem();
JavaTestClasspath javaTestClasspath = new JavaTestClasspath(settings, fs);
JavaClasspath javaClasspath = new JavaClasspath(settings, fs);
FileLinesContext fileLinesContext = mock(FileLinesContext.class);
FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
SonarComponents sonarComponents = spy(new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, checkFactory));
sonarComponents.setSensorContext(contextTester);
BadMethodNameCheck check = new BadMethodNameCheck();
when(sonarComponents.checkClasses()).thenReturn(new JavaCheck[] { check });
return sonarComponents;
}
use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.
the class JavaSquidSensorTest method executeJavaSquidSensor.
private void executeJavaSquidSensor(SensorContextTester context) {
context.setRuntime(SonarRuntimeImpl.forSonarQube(Version.create(6, 7), SonarQubeSide.SCANNER));
// Mock visitor for metrics.
FileLinesContext fileLinesContext = mock(FileLinesContext.class);
FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
DefaultFileSystem fs = context.fileSystem().setWorkDir(tmp.getRoot().toPath());
JavaClasspath javaClasspath = mock(JavaClasspath.class);
JavaTestClasspath javaTestClasspath = mock(JavaTestClasspath.class);
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, checkFactory);
DefaultJavaResourceLocator javaResourceLocator = mock(DefaultJavaResourceLocator.class);
NoSonarFilter noSonarFilter = mock(NoSonarFilter.class);
PostAnalysisIssueFilter postAnalysisIssueFilter = new PostAnalysisIssueFilter(fs);
JavaSquidSensor jss = new JavaSquidSensor(sonarComponents, fs, javaResourceLocator, new MapSettings().asConfig(), noSonarFilter, postAnalysisIssueFilter);
jss.execute(context);
}
Aggregations