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();
}
}
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;
}
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();
}
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();
}
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);
}
Aggregations