Search in sources :

Example 16 with SonarComponents

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);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) VisitorsBridge(org.sonar.java.model.VisitorsBridge) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 17 with SonarComponents

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);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) SECheck(org.sonar.java.se.checks.SECheck) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) CFG(org.sonar.java.cfg.CFG) ArrayList(java.util.ArrayList) VisitorsBridge(org.sonar.java.model.VisitorsBridge) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 18 with SonarComponents

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);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) VisitorsBridge(org.sonar.java.model.VisitorsBridge) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.Test)

Example 19 with SonarComponents

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;
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaClasspath(org.sonar.java.JavaClasspath) BadMethodNameCheck(org.sonar.java.checks.naming.BadMethodNameCheck) Configuration(org.sonar.api.config.Configuration) MapSettings(org.sonar.api.config.internal.MapSettings) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) JavaTestClasspath(org.sonar.java.JavaTestClasspath) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) FileLinesContext(org.sonar.api.measures.FileLinesContext) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 20 with 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);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaClasspath(org.sonar.java.JavaClasspath) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) MapSettings(org.sonar.api.config.internal.MapSettings) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) PostAnalysisIssueFilter(org.sonar.java.filters.PostAnalysisIssueFilter) JavaTestClasspath(org.sonar.java.JavaTestClasspath) DefaultJavaResourceLocator(org.sonar.java.DefaultJavaResourceLocator) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) FileLinesContext(org.sonar.api.measures.FileLinesContext) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Aggregations

SonarComponents (org.sonar.java.SonarComponents)36 File (java.io.File)27 Test (org.junit.Test)21 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)17 AnalyzerMessage (org.sonar.java.AnalyzerMessage)14 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)13 JavaCheck (org.sonar.plugins.java.api.JavaCheck)11 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)10 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)7 Before (org.junit.Before)6 Lists (com.google.common.collect.Lists)3 RecognitionException (com.sonar.sslr.api.RecognitionException)3 ArrayList (java.util.ArrayList)3 Nullable (javax.annotation.Nullable)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 MapSettings (org.sonar.api.config.internal.MapSettings)3 VisitorsBridge (org.sonar.java.model.VisitorsBridge)3 Files (com.google.common.io.Files)2 IOException (java.io.IOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2