Search in sources :

Example 26 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonarqube by SonarSource.

the class OneQuickFixPerLineSensorTest method testRule.

@Test
public void testRule() throws IOException {
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo").setLanguage(Xoo.KEY).initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n").build();
    SensorContextTester context = SensorContextTester.create(temp.newFolder());
    context.fileSystem().add(inputFile);
    sensor.execute(context);
    // One issue per line
    assertThat(context.allIssues()).hasSize(10);
    for (Issue issue : context.allIssues()) {
        assertThat(issue.isQuickFixAvailable()).isTrue();
    }
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Issue(org.sonar.api.batch.sensor.issue.Issue) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 27 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JavaSquidTest method collectAnalysisErrors.

private SonarComponents collectAnalysisErrors(String code) throws IOException {
    SensorContextTester context = SensorContextTester.create(temp.getRoot().getAbsoluteFile());
    File file = temp.newFile().getAbsoluteFile();
    Files.write(code, file, StandardCharsets.UTF_8);
    DefaultInputFile defaultFile = new TestInputFileBuilder(temp.getRoot().getAbsolutePath(), file.getName()).setLanguage("java").initMetadata(new String(java.nio.file.Files.readAllBytes(file.getAbsoluteFile().toPath()), StandardCharsets.UTF_8)).setCharset(StandardCharsets.UTF_8).build();
    context.fileSystem().add(defaultFile);
    context.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
    // Mock visitor for metrics.
    FileLinesContext fileLinesContext = mock(FileLinesContext.class);
    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
    Server server = mock(Server.class);
    when(server.getPublicRootUrl()).thenReturn("https://sonarcloud.io");
    FileSystem fs = context.fileSystem();
    JavaClasspath javaClasspath = mock(JavaClasspath.class);
    JavaTestClasspath javaTestClasspath = mock(JavaTestClasspath.class);
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, mock(CheckFactory.class));
    sonarComponents.setSensorContext(context);
    JavaSquid javaSquid = new JavaSquid(new JavaVersionImpl(), sonarComponents, new Measurer(fs, context, mock(NoSonarFilter.class)), mock(JavaResourceLocator.class), null);
    javaSquid.scan(Collections.singletonList(file), Collections.emptyList());
    return sonarComponents;
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Server(org.sonar.api.platform.Server) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileSystem(org.sonar.api.batch.fs.FileSystem) CheckFactory(org.sonar.api.batch.rule.CheckFactory) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) FileLinesContext(org.sonar.api.measures.FileLinesContext)

Example 28 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JavaSquidTest method number_of_visitors_in_sonarLint_context_LTS.

@Test
public void number_of_visitors_in_sonarLint_context_LTS() throws Exception {
    SensorContextTester context = SensorContextTester.create(temp.getRoot().getAbsoluteFile());
    // set up a file to analyze
    File file = temp.newFile().getAbsoluteFile();
    Files.write("/***/\nclass A {\n String foo() {\n  return foo();\n }\n}", file, StandardCharsets.UTF_8);
    DefaultInputFile defaultFile = new TestInputFileBuilder(temp.getRoot().getAbsolutePath(), file.getName()).setLanguage("java").initMetadata(new String(java.nio.file.Files.readAllBytes(file.getAbsoluteFile().toPath()), StandardCharsets.UTF_8)).setCharset(StandardCharsets.UTF_8).build();
    context.fileSystem().add(defaultFile);
    // Set sonarLint runtime
    context.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
    // Mock visitor for metrics.
    FileLinesContext fileLinesContext = mock(FileLinesContext.class);
    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
    FileSystem fs = context.fileSystem();
    JavaClasspath javaClasspath = mock(JavaClasspath.class);
    JavaTestClasspath javaTestClasspath = mock(JavaTestClasspath.class);
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, mock(CheckFactory.class));
    sonarComponents.setSensorContext(context);
    JavaSquid javaSquid = new JavaSquid(new JavaVersionImpl(), sonarComponents, new Measurer(fs, context, mock(NoSonarFilter.class)), mock(JavaResourceLocator.class), null);
    javaSquid.scan(Collections.singletonList(file), Collections.emptyList());
    // No symbol table : check reference to foo is empty.
    assertThat(context.referencesForSymbolAt(defaultFile.key(), 3, 8)).isNull();
    // No metrics on lines
    verify(fileLinesContext, never()).save();
    // No highlighting
    assertThat(context.highlightingTypeAt(defaultFile.key(), 1, 0)).isEmpty();
    // No measures
    assertThat(context.measures(defaultFile.key())).isEmpty();
    verify(javaClasspath, times(1)).getElements();
    verify(javaTestClasspath, times(1)).getElements();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileSystem(org.sonar.api.batch.fs.FileSystem) CheckFactory(org.sonar.api.batch.rule.CheckFactory) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) FileLinesContext(org.sonar.api.measures.FileLinesContext) Test(org.junit.Test)

Example 29 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class SonarComponentsTest method cancellation.

@Test
public void cancellation() {
    SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null, null);
    SensorContextTester context = SensorContextTester.create(new File(""));
    sonarComponents.setSensorContext(context);
    context.setRuntime(SonarRuntimeImpl.forSonarLint(V6_7));
    assertThat(sonarComponents.analysisCancelled()).isFalse();
    // cancellation only handled from SQ 6.0
    context.setCancelled(true);
    assertThat(sonarComponents.analysisCancelled()).isTrue();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 30 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class SonarComponentsTest method readFileContentFromInputFile.

@Test
public void readFileContentFromInputFile() throws Exception {
    // read a file containing kanji set with correct encoding and expecting proper length of read input.
    File file = new File("src/test/files/Kanji.java");
    DefaultInputFile inputFile = spy(new TestInputFileBuilder("", file.getPath()).initMetadata(new String(java.nio.file.Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)).setCharset(StandardCharsets.UTF_8).build());
    SensorContextTester context = SensorContextTester.create(new File(""));
    DefaultFileSystem fileSystem = context.fileSystem();
    fileSystem.add(inputFile);
    fileSystem.setEncoding(StandardCharsets.ISO_8859_1);
    SonarComponents sonarComponents = new SonarComponents(null, fileSystem, null, null, null, null);
    context.setRuntime(SonarRuntimeImpl.forSonarLint(V6_7));
    sonarComponents.setSensorContext(context);
    String fileContent = sonarComponents.fileContent(file);
    assertThat(fileContent).hasSize(59);
    List<String> fileLines = sonarComponents.fileLines(file);
    assertThat(fileLines).hasSize(5);
    assertThat(fileLines.get(0)).hasSize(11);
    verify(inputFile, times(1)).contents();
    reset(inputFile);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Aggregations

SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)56 Test (org.junit.Test)48 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)35 File (java.io.File)34 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)23 InputFile (org.sonar.api.batch.fs.InputFile)22 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)10 MapSettings (org.sonar.api.config.internal.MapSettings)8 SonarComponents (org.sonar.java.SonarComponents)7 Issue (org.sonar.api.batch.sensor.issue.Issue)5 RecognitionException (com.sonar.sslr.api.RecognitionException)4 Path (java.nio.file.Path)4 JavaCheck (org.sonar.plugins.java.api.JavaCheck)4 Lists (com.google.common.collect.Lists)3 Nullable (javax.annotation.Nullable)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Test (org.junit.jupiter.api.Test)3 RuleKey (org.sonar.api.rule.RuleKey)3 JavaResourceLocator (org.sonar.plugins.java.api.JavaResourceLocator)3