use of org.sonar.api.batch.sensor.highlighting.NewHighlighting in project sonar-java by SonarSource.
the class SonarComponentsTest method test_sonar_components.
@Test
public void test_sonar_components() {
SensorContextTester sensorContextTester = spy(SensorContextTester.create(new File("")));
DefaultFileSystem fs = sensorContextTester.fileSystem();
JavaTestClasspath javaTestClasspath = mock(JavaTestClasspath.class);
ImmutableList<File> javaTestClasspathList = ImmutableList.of();
when(javaTestClasspath.getElements()).thenReturn(javaTestClasspathList);
File file = new File("foo.java");
fs.add(new TestInputFileBuilder("", "foo.java").build());
FileLinesContext fileLinesContext = mock(FileLinesContext.class);
when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fs, null, javaTestClasspath, checkFactory);
sonarComponents.setSensorContext(sensorContextTester);
JavaCheck[] visitors = sonarComponents.checkClasses();
assertThat(visitors).hasSize(0);
Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
assertThat(testChecks).hasSize(0);
assertThat(sonarComponents.getFileSystem()).isEqualTo(fs);
assertThat(sonarComponents.getJavaClasspath()).isEmpty();
assertThat(sonarComponents.getJavaTestClasspath()).isEqualTo(javaTestClasspathList);
NewHighlighting newHighlighting = sonarComponents.highlightableFor(file);
assertThat(newHighlighting).isNotNull();
verify(sensorContextTester, times(1)).newHighlighting();
NewSymbolTable newSymbolTable = sonarComponents.symbolizableFor(file);
assertThat(newSymbolTable).isNotNull();
verify(sensorContextTester, times(1)).newSymbolTable();
assertThat(sonarComponents.fileLinesContextFor(file)).isEqualTo(fileLinesContext);
JavaClasspath javaClasspath = mock(JavaClasspath.class);
List<File> list = mock(List.class);
when(javaClasspath.getElements()).thenReturn(list);
sonarComponents = new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, checkFactory);
assertThat(sonarComponents.getJavaClasspath()).isEqualTo(list);
}
use of org.sonar.api.batch.sensor.highlighting.NewHighlighting in project sonarqube by SonarSource.
the class SyntaxHighlightingSensor method processFileHighlighting.
private void processFileHighlighting(InputFile inputFile, SensorContext context) {
File ioFile = inputFile.file();
File highlightingFile = new File(ioFile.getParentFile(), ioFile.getName() + HIGHLIGHTING_EXTENSION);
if (highlightingFile.exists()) {
LOG.debug("Processing " + highlightingFile.getAbsolutePath());
try {
List<String> lines = FileUtils.readLines(highlightingFile, context.fileSystem().encoding().name());
int lineNumber = 0;
NewHighlighting highlighting = context.newHighlighting().onFile(inputFile);
for (String line : lines) {
lineNumber++;
if (StringUtils.isBlank(line) || line.startsWith("#")) {
continue;
}
processLine(highlightingFile, lineNumber, highlighting, line);
}
highlighting.save();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
use of org.sonar.api.batch.sensor.highlighting.NewHighlighting in project sonar-web by SonarSource.
the class HtmlTokensVisitor method highlightAndDuplicate.
private void highlightAndDuplicate() {
if (!getHtmlSourceCode().shouldComputeMetric()) {
return;
}
NewHighlighting highlighting = context.newHighlighting();
InputFile inputFile = getHtmlSourceCode().inputFile();
highlighting.onFile(inputFile);
NewCpdTokens cpdTokens = context.newCpdTokens();
cpdTokens.onFile(inputFile);
String fileContent;
try {
fileContent = inputFile.contents();
} catch (IOException e) {
throw new IllegalStateException("Cannot read " + inputFile, e);
}
for (Token token : HtmlLexer.create(context.fileSystem().encoding()).lex(fileContent)) {
TokenType tokenType = token.getType();
if (!tokenType.equals(GenericTokenType.EOF)) {
TokenLocation tokenLocation = new TokenLocation(token);
cpdTokens.addToken(tokenLocation.startLine(), tokenLocation.startCharacter(), tokenLocation.endLine(), tokenLocation.endCharacter(), token.getValue());
}
if (tokenType.equals(HtmlTokenType.DOCTYPE)) {
highlight(highlighting, token, TypeOfText.STRUCTURED_COMMENT);
} else if (tokenType.equals(HtmlTokenType.EXPRESSION)) {
highlight(highlighting, token, TypeOfText.ANNOTATION);
} else if (tokenType.equals(HtmlTokenType.TAG)) {
highlight(highlighting, token, TypeOfText.KEYWORD);
} else if (tokenType.equals(HtmlTokenType.ATTRIBUTE)) {
TokenLocation tokenLocation = new TokenLocation(token);
highlighting.highlight(tokenLocation.startLine(), tokenLocation.startCharacter() + /* = */
1, tokenLocation.endLine(), tokenLocation.endCharacter(), TypeOfText.STRING);
}
for (Trivia trivia : token.getTrivia()) {
highlight(highlighting, trivia.getToken(), TypeOfText.COMMENT);
}
}
highlighting.save();
cpdTokens.save();
}
use of org.sonar.api.batch.sensor.highlighting.NewHighlighting in project sonar-web by SonarSource.
the class WebTokensVisitor method highlightAndDuplicate.
private void highlightAndDuplicate() {
NewHighlighting highlighting = context.newHighlighting();
InputFile inputFile = getWebSourceCode().inputFile();
highlighting.onFile(inputFile);
NewCpdTokens cpdTokens = context.newCpdTokens();
cpdTokens.onFile(inputFile);
for (Token token : WebLexer.create(context.fileSystem().encoding()).lex(inputFile.file())) {
TokenType tokenType = token.getType();
if (!tokenType.equals(GenericTokenType.EOF)) {
TokenLocation tokenLocation = new TokenLocation(token);
cpdTokens.addToken(tokenLocation.startLine(), tokenLocation.startCharacter(), tokenLocation.endLine(), tokenLocation.endCharacter(), token.getValue());
}
if (tokenType.equals(WebTokenType.DOCTYPE)) {
highlight(highlighting, token, TypeOfText.STRUCTURED_COMMENT);
} else if (tokenType.equals(WebTokenType.EXPRESSION)) {
highlight(highlighting, token, TypeOfText.ANNOTATION);
} else if (tokenType.equals(WebTokenType.TAG)) {
highlight(highlighting, token, TypeOfText.KEYWORD);
} else if (tokenType.equals(WebTokenType.ATTRIBUTE)) {
TokenLocation tokenLocation = new TokenLocation(token);
highlighting.highlight(tokenLocation.startLine(), tokenLocation.startCharacter() + /* = */
1, tokenLocation.endLine(), tokenLocation.endCharacter(), TypeOfText.STRING);
}
for (Trivia trivia : token.getTrivia()) {
highlight(highlighting, trivia.getToken(), TypeOfText.COMMENT);
}
}
highlighting.save();
cpdTokens.save();
}
Aggregations