use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class VisitorsBridgeTest method rethrow_exception_when_hidden_property_set_to_true.
@Test
public void rethrow_exception_when_hidden_property_set_to_true() {
NullPointerException npe = new NullPointerException("BimBadaboum");
JavaFileScanner visitor = c -> {
throw npe;
};
File currentFile = new File("");
SensorContextTester sensorContextTester = SensorContextTester.create(currentFile);
SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null, null);
sonarComponents.setSensorContext(sensorContextTester);
VisitorsBridge visitorsBridge = new VisitorsBridge(Collections.singleton(visitor), new ArrayList<>(), sonarComponents);
visitorsBridge.setCurrentFile(currentFile);
try {
visitorsBridge.visitFile(null);
assertThat(sonarComponents.analysisErrors).hasSize(1);
} catch (Exception e) {
e.printStackTrace();
Fail.fail("Exception should be swallowed when property is not set");
}
sensorContextTester.settings().appendProperty(SonarComponents.FAIL_ON_EXCEPTION_KEY, "true");
try {
visitorsBridge.visitFile(null);
Fail.fail("scanning of file should have raise an exception");
} catch (Exception e) {
assertThat(e).isSameAs(npe);
}
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JavaSquidSensorTest method verify_analysis_errors_are_collected_on_parse_error.
@Test
public void verify_analysis_errors_are_collected_on_parse_error() throws Exception {
SensorContextTester context = createParseErrorContext();
context.settings().setProperty(SonarComponents.COLLECT_ANALYSIS_ERRORS_KEY, true);
executeJavaSquidSensor(context);
String feedback = context.<String>measure("projectKey", "sonarjava_feedback").value();
Collection<AnalysisError> analysisErrors = new Gson().fromJson(feedback, new TypeToken<Collection<AnalysisError>>() {
}.getType());
assertThat(analysisErrors).hasSize(1);
AnalysisError analysisError = analysisErrors.iterator().next();
assertThat(analysisError.getMessage()).startsWith("Parse error at line 6 column 1:");
assertThat(analysisError.getCause()).startsWith("com.sonar.sslr.api.RecognitionException: Parse error at line 6 column 1:");
assertThat(analysisError.getFilename()).endsWith("ParseError.java");
assertThat(analysisError.getKind()).isEqualTo(AnalysisError.Kind.PARSE_ERROR);
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JavaSquidSensorTest method createContext.
private static SensorContextTester createContext(InputFile.Type onType) throws IOException {
SensorContextTester context = SensorContextTester.create(new File("src/test/java/").getAbsoluteFile());
DefaultFileSystem fs = context.fileSystem();
String effectiveKey = "org/sonar/plugins/java/JavaSquidSensorTest.java";
File file = new File(fs.baseDir(), effectiveKey);
DefaultInputFile inputFile = new TestInputFileBuilder("", effectiveKey).setLanguage("java").setModuleBaseDir(fs.baseDirPath()).setType(onType).initMetadata(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)).setCharset(StandardCharsets.UTF_8).build();
fs.add(inputFile);
return context;
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester 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);
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JavaSquidSensorTest method createParseErrorContext.
private SensorContextTester createParseErrorContext() throws IOException {
File file = new File("src/test/files/ParseError.java");
SensorContextTester context = SensorContextTester.create(file.getParentFile().getAbsoluteFile());
DefaultInputFile defaultFile = new TestInputFileBuilder(file.getParentFile().getPath(), file.getName()).setLanguage("java").initMetadata(new String(Files.readAllBytes(file.getAbsoluteFile().toPath()), StandardCharsets.UTF_8)).setCharset(StandardCharsets.UTF_8).build();
context.fileSystem().add(defaultFile);
return context;
}
Aggregations