use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JavaSensorTest method filter_checks_when_autoscan_true.
@Test
void filter_checks_when_autoscan_true() throws IOException {
MapSettings settings = new MapSettings();
settings.setProperty("sonar.internal.analysis.autoscan", "true");
SensorContextTester context = analyzeTwoFilesWithIssues(settings);
assertThat(context.allIssues()).extracting(issue -> issue.ruleKey().toString()).contains(// main check in SonarWay
"java:S1220", // test check in SonarWay
"java:S2187").doesNotContain("CustomRepository:CustomMainCheck", "CustomRepository:CustomJspCheck", "CustomRepository:CustomTestCheck", // main check in SonarWay, not supported by autoscan (CombineCatchCheck)
"java:S2147", // not in SonarWay (FileHeaderCheck)
"java:S1451", // SE check (BooleanGratuitousExpressionsCheck)
"java:S2589");
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JavaSensorTest method should_not_invoke_jasper_jsp_compilation_in_autoscan_for_security_reasons.
@Test
void should_not_invoke_jasper_jsp_compilation_in_autoscan_for_security_reasons() throws Exception {
Path base = tmp.newFolder().toPath();
MapSettings settings = new MapSettings();
settings.setProperty("sonar.internal.analysis.autoscan", "true");
SensorContextTester context = SensorContextTester.create(base);
context.setSettings(settings);
context.fileSystem().setWorkDir(tmp.newFolder().toPath());
SonarComponents sonarComponents = createSonarComponentsMock(context);
JspCodeScanner jspCodeVisitor = mock(JspCodeScanner.class);
when(sonarComponents.mainChecks()).thenReturn(Collections.emptyList());
when(sonarComponents.testChecks()).thenReturn(Collections.emptyList());
when(sonarComponents.jspChecks()).thenReturn(Collections.singletonList(jspCodeVisitor));
Jasper jasper = mock(Jasper.class);
JavaSensor jss = new JavaSensor(sonarComponents, context.fileSystem(), mock(JavaResourceLocator.class), context.config(), mock(NoSonarFilter.class), null, jasper);
jss.execute(context);
verify(jasper, never()).generateFiles(any(), any());
verify(jspCodeVisitor, never()).scanFile(any());
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class CheckstyleSensorTest method executeSensorImporting.
private List<ExternalIssue> executeSensorImporting(@Nullable String fileName) throws IOException {
SensorContextTester context = ExternalReportTestUtils.createContext(PROJECT_DIR);
if (fileName != null) {
File reportFile = ExternalReportTestUtils.generateReport(PROJECT_DIR, tmp, fileName);
context.settings().setProperty("sonar.java.checkstyle.reportPaths", reportFile.getPath());
}
checkstyleSensor.execute(context);
return new ArrayList<>(context.allExternalIssues());
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class PmdSensorTest method createContext.
public static SensorContextTester createContext(Path projectDir) throws IOException {
SensorContextTester context = SensorContextTester.create(projectDir);
Files.list(projectDir).filter(Files::isRegularFile).forEach(file -> addFileToContext(context, projectDir, file));
return context;
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SonarComponentsTest method add_issue_or_parse_error.
@Test
void add_issue_or_parse_error() throws Exception {
JavaCheck expectedCheck = new CustomCheck();
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
SensorContextTester context = SensorContextTester.create(new File("."));
DefaultFileSystem fileSystem = context.fileSystem();
TestInputFileBuilder inputFileBuilder = new TestInputFileBuilder("", "file.java");
inputFileBuilder.setLines(45);
int[] lineStartOffsets = new int[45];
lineStartOffsets[35] = 12;
lineStartOffsets[42] = 1;
int lastValidOffset = 420;
inputFileBuilder.setOriginalLineStartOffsets(lineStartOffsets);
inputFileBuilder.setOriginalLineEndOffsets(computeLineEndOffsets(lineStartOffsets, lastValidOffset));
inputFileBuilder.setLastValidOffset(lastValidOffset);
InputFile inputFile = inputFileBuilder.build();
fileSystem.add(inputFile);
when(this.checks.ruleKey(any(JavaCheck.class))).thenReturn(mock(RuleKey.class));
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fileSystem, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
sonarComponents.setSensorContext(context);
sonarComponents.addIssue(inputFile, expectedCheck, -5, "message on wrong line", null);
sonarComponents.addIssue(inputFile, expectedCheck, 42, "message on line 42", 1);
sonarComponents.reportIssue(new AnalyzerMessage(expectedCheck, inputFile, 35, "other message", 0));
List<Issue> issues = new ArrayList<>(context.allIssues());
assertThat(issues).hasSize(3);
assertThat(issues.get(0).primaryLocation().message()).isEqualTo("message on wrong line");
assertThat(issues.get(1).primaryLocation().message()).isEqualTo("message on line 42");
assertThat(issues.get(2).primaryLocation().message()).isEqualTo("other message");
RecognitionException parseError = new RecognitionException(-1, "invalid code", new Exception("parse error"));
context.setRuntime(SonarRuntimeImpl.forSonarLint(V8_9));
assertThat(sonarComponents.reportAnalysisError(parseError, inputFile)).isTrue();
context.setRuntime(SonarRuntimeImpl.forSonarQube(V8_9, SonarQubeSide.SCANNER, SonarEdition.COMMUNITY));
assertThat(sonarComponents.reportAnalysisError(parseError, inputFile)).isFalse();
}
Aggregations