Search in sources :

Example 11 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class JavaSquidSensorTest method testIssueCreation.

private void testIssueCreation(InputFile.Type onType, int expectedIssues) throws IOException {
    MapSettings settings = new MapSettings();
    NoSonarFilter noSonarFilter = mock(NoSonarFilter.class);
    SensorContextTester context = createContext(onType).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
    DefaultFileSystem fs = context.fileSystem();
    SonarComponents sonarComponents = createSonarComponentsMock(context);
    DefaultJavaResourceLocator javaResourceLocator = new DefaultJavaResourceLocator(fs, new JavaClasspath(settings.asConfig(), fs));
    PostAnalysisIssueFilter postAnalysisIssueFilter = new PostAnalysisIssueFilter(fs);
    JavaSquidSensor jss = new JavaSquidSensor(sonarComponents, fs, javaResourceLocator, settings.asConfig(), noSonarFilter, postAnalysisIssueFilter);
    jss.execute(context);
    verify(noSonarFilter, times(1)).noSonarInFile(fs.inputFiles().iterator().next(), Sets.newHashSet(96));
    verify(sonarComponents, times(expectedIssues)).reportIssue(any(AnalyzerMessage.class));
    settings.setProperty(Java.SOURCE_VERSION, "wrongFormat");
    jss.execute(context);
    settings.setProperty(Java.SOURCE_VERSION, "1.7");
    jss.execute(context);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaClasspath(org.sonar.java.JavaClasspath) MapSettings(org.sonar.api.config.internal.MapSettings) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) PostAnalysisIssueFilter(org.sonar.java.filters.PostAnalysisIssueFilter) DefaultJavaResourceLocator(org.sonar.java.DefaultJavaResourceLocator) AnalyzerMessage(org.sonar.java.AnalyzerMessage) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem)

Example 12 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class XmlFileSensorTest method test_no_issues_but_xml_file_still_published.

@Test
public void test_no_issues_but_xml_file_still_published() throws Exception {
    SensorContextTester context = SensorContextTester.create(new File("src/test/files/maven2/").getAbsoluteFile());
    DefaultFileSystem fs = context.fileSystem();
    final File file = new File("src/test/files/maven2/pom.xml");
    DefaultInputFile inputFile = new TestInputFileBuilder("", "pom.xml").setModuleBaseDir(fs.baseDirPath()).setPublish(false).build();
    fs.add(inputFile);
    SonarComponents sonarComponents = createSonarComponentsMock(fs, file);
    XmlFileSensor sensor = new XmlFileSensor(sonarComponents, fs);
    assertThat(inputFile.isPublished()).isFalse();
    sensor.execute(context);
    assertThat(inputFile.isPublished()).isTrue();
    verify(sonarComponents, never()).reportIssue(Mockito.argThat(argument -> file.getAbsolutePath().equals(argument.getFile().getAbsolutePath())));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Lists(com.google.common.collect.Lists) Files(com.google.common.io.Files) JavaCheck(org.sonar.plugins.java.api.JavaCheck) SonarComponents(org.sonar.java.SonarComponents) Checks(org.sonar.api.batch.rule.Checks) Nullable(javax.annotation.Nullable) Before(org.junit.Before) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) RuleAnnotationUtils(org.sonar.api.rules.RuleAnnotationUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) PomElementOrderCheck(org.sonar.java.checks.xml.maven.PomElementOrderCheck) RuleKey(org.sonar.api.rule.RuleKey) Mockito.mock(org.mockito.Mockito.mock) SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 13 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class FileLinesVisitorTest method checkLines.

private void checkLines(String filename, FileLinesContext context) {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    when(sonarComponents.fileLength(Mockito.any(File.class))).thenAnswer(invocation -> {
        File arg = (File) invocation.getArguments()[0];
        return Files.readLines(arg, StandardCharsets.UTF_8).size();
    });
    when(sonarComponents.fileLinesContextFor(Mockito.any(File.class))).thenReturn(context);
    JavaSquid squid = new JavaSquid(new JavaVersionImpl(), null, null, null, null, new FileLinesVisitor(sonarComponents));
    squid.scan(Lists.newArrayList(new File(baseDir, filename)), Collections.emptyList());
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) JavaSquid(org.sonar.java.JavaSquid) File(java.io.File)

Example 14 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class TestDefaultJavaFileScannerContextWithSensorContextTester method setup.

@Before
public void setup() throws IOException {
    sensorContext = SensorContextTester.create(Paths.get(""));
    sensorContext.fileSystem().add(new TestInputFileBuilder("myProjectKey", JAVA_FILE.getPath()).setLanguage("java").initMetadata(new String(Files.readAllBytes(JAVA_FILE.toPath()), StandardCharsets.UTF_8)).build());
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, sensorContext.fileSystem(), javaClasspath, javaTestClasspath, checkFactory);
    sonarComponents.setSensorContext(sensorContext);
    // spy getRuleKey call, to avoid mocking CheckFactory and Checks
    sonarComponents = spy(sonarComponents);
    when(sonarComponents.getRuleKey(any())).thenReturn(RuleKey.of("repository", "rule"));
    CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(JAVA_FILE);
    tree = cut.types().get(0);
    scannerContext = new DefaultJavaFileScannerContext(cut, JAVA_FILE, null, sonarComponents, null, true);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SonarComponents(org.sonar.java.SonarComponents) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Before(org.junit.Before)

Example 15 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.

@Test
public void should_swallow_log_and_report_checks_exceptions() {
    JavaAstScanner scanner = defaultJavaAstScanner();
    SonarComponents sonarComponent = new SonarComponents(null, context.fileSystem(), null, null, null, null);
    sonarComponent.setSensorContext(context);
    scanner.setVisitorBridge(new VisitorsBridge(Collections.singleton(new CheckThrowingException(new NullPointerException("foo"))), new ArrayList<>(), sonarComponent));
    File scannedFile = new File("src/test/resources/AstScannerNoParseError.txt");
    scanner.scan(ImmutableList.of(scannedFile));
    assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1).contains("Unable to run check class org.sonar.java.ast.JavaAstScannerTest$CheckThrowingException -  on file " + scannedFile.getPath() + ", To help improve SonarJava, please report this problem to SonarSource : see https://www.sonarqube.org/community/");
    assertThat(sonarComponent.analysisErrors).hasSize(1);
    assertThat(sonarComponent.analysisErrors.get(0).getKind()).isSameAs(AnalysisError.Kind.CHECK_ERROR);
    logTester.clear();
    scanner.setVisitorBridge(new VisitorsBridge(new AnnotatedCheck(new NullPointerException("foo"))));
    scannedFile = new File("src/test/resources/AstScannerParseError.txt");
    scanner.scan(ImmutableList.of(scannedFile));
    assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(3).contains("Unable to run check class org.sonar.java.ast.JavaAstScannerTest$AnnotatedCheck - AnnotatedCheck on file " + scannedFile.getPath() + ", To help improve SonarJava, please report this problem to SonarSource : see https://www.sonarqube.org/community/");
}
Also used : SonarComponents(org.sonar.java.SonarComponents) 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)

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