use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.
the class XmlAnalyzerTest method should_not_run_pom_check_when_no_pom_file_provided.
@Test
public void should_not_run_pom_check_when_no_pom_file_provided() {
DefaultFileSystem fs = new DefaultFileSystem(new File(""));
File xmlFile = new File("src/test/files/xml/parsing.xml");
fs.add(new TestInputFileBuilder("", xmlFile.getAbsolutePath()).setLanguage("xml").build());
SonarComponents sonarComponents = createSonarComponentsMock(fs, XML_CHECK, POM_CHECK);
XmlAnalyzer analyzer = new XmlAnalyzer(sonarComponents, XML_CHECK, POM_CHECK);
analyzer.scan(Lists.newArrayList(xmlFile));
verify(sonarComponents, never()).addIssue(any(File.class), eq(POM_CHECK), any(Integer.class), anyString(), isNull());
verify(sonarComponents, times(1)).addIssue(any(File.class), eq(XML_CHECK), any(Integer.class), anyString(), isNull());
verify(sonarComponents, never()).reportIssue(any(AnalyzerMessage.class));
}
use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.
the class PomCheckContextImplTest method createSonarComponentsMock.
private static SonarComponents createSonarComponentsMock() {
SonarComponents sonarComponents = mock(SonarComponents.class);
Mockito.when(sonarComponents.fileLines(any(File.class))).thenReturn(Arrays.asList("line 1", "line 2", "line 3 is longer"));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
reportedMessage = "onLine:" + invocation.getArguments()[3];
return null;
}
}).when(sonarComponents).addIssue(any(File.class), eq(CHECK), eq(LINE), anyString(), eq(null));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
reportedMessage = "onFile:" + invocation.getArguments()[3];
return null;
}
}).when(sonarComponents).addIssue(any(File.class), eq(CHECK), eq(-1), anyString(), eq(null));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
AnalyzerMessage analyzerMessage = (AnalyzerMessage) invocation.getArguments()[0];
reportedMessage = "analyzerMessage:" + analyzerMessage.getMessage();
for (AnalyzerMessage secondary : analyzerMessage.flows.stream().map(l -> l.get(0)).collect(Collectors.toList())) {
TextSpan location = secondary.primaryLocation();
reportedMessage += ";" + secondary.getMessage() + "[" + location.startLine + ";" + location.startCharacter + "/" + location.endLine + ";" + location.endCharacter + "]";
}
return null;
}
}).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
return sonarComponents;
}
use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.
the class CheckVerifier method sonarComponents.
static SonarComponents sonarComponents(File file) {
SensorContextTester context = SensorContextTester.create(new File("")).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
context.setSettings(new MapSettings().setProperty("sonar.java.failOnException", true));
SonarComponents sonarComponents = new SonarComponents(null, context.fileSystem(), null, null, null, null) {
@Override
public boolean reportAnalysisError(RecognitionException re, File file) {
return false;
}
};
sonarComponents.setSensorContext(context);
context.fileSystem().add(new TestInputFileBuilder("", file.getPath()).setCharset(StandardCharsets.UTF_8).build());
return sonarComponents;
}
use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.
the class JavaCheckVerifier method scanFile.
private static void scanFile(String filename, JavaFileScanner check, JavaCheckVerifier javaCheckVerifier, Collection<File> classpath, boolean withSemantic) {
JavaFileScanner expectedIssueCollector = new ExpectedIssueCollector(javaCheckVerifier);
VisitorsBridgeForTests visitorsBridge;
File file = new File(filename);
SonarComponents sonarComponents = CheckVerifier.sonarComponents(file);
if (withSemantic) {
visitorsBridge = new VisitorsBridgeForTests(Lists.newArrayList(check, expectedIssueCollector), Lists.newArrayList(classpath), sonarComponents);
} else {
visitorsBridge = new VisitorsBridgeForTests(Lists.newArrayList(check, expectedIssueCollector), sonarComponents);
}
JavaAstScanner.scanSingleFileForTests(file, visitorsBridge, javaCheckVerifier.javaVersion);
VisitorsBridgeForTests.TestJavaFileScannerContext testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext();
if (testJavaFileScannerContext == null) {
Fail.fail("Semantic was required but it was not possible to create it. Please checks the logs to find out the reason.");
}
javaCheckVerifier.checkIssues(testJavaFileScannerContext.getIssues(), javaCheckVerifier.providedJavaVersion);
}
use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.
the class SyntaxHighlighterVisitorTest method setUp.
@Before
public void setUp() throws Exception {
context = SensorContextTester.create(temp.getRoot());
fs = context.fileSystem();
sonarComponents = new SonarComponents(mock(FileLinesContextFactory.class), fs, mock(JavaClasspath.class), mock(JavaTestClasspath.class), mock(CheckFactory.class));
sonarComponents.setSensorContext(context);
syntaxHighlighterVisitor = new SyntaxHighlighterVisitor(sonarComponents);
}
Aggregations