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