use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonar-web by SonarSource.
the class HeaderCheckTest method scanWithWrongInputFile.
public static void scanWithWrongInputFile(File file, DefaultNodeVisitor visitor) {
HtmlAstScanner walker = new HtmlAstScanner(Collections.emptyList());
walker.addVisitor(visitor);
FileReader reader;
try {
reader = new FileReader(file);
} catch (Exception e) {
throw new IllegalArgumentException("unable to read file");
}
WebSourceCode result = new WebSourceCode(new DefaultInputFile("key", /* wrong path */
".").setLanguage(WebConstants.LANGUAGE_KEY).setType(InputFile.Type.MAIN).setModuleBaseDir(new File(".").toPath()));
walker.scan(new PageLexer().parse(reader), // won't be able to resolve the file
result, StandardCharsets.UTF_8);
}
use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonar-web by SonarSource.
the class WebSensorTest method testSensor.
/**
* Unit test which is more kind of an integration test. The purpose of this test is to get early feedback on changes in
* the number of issues.
*/
@Test
public void testSensor() throws Exception {
DefaultInputFile inputFile = new DefaultInputFile("key", "user-properties.jsp").setLanguage(WebConstants.LANGUAGE_KEY).setType(InputFile.Type.MAIN).initMetadata(new FileMetadata().readMetadata(new FileReader(new File(TEST_DIR, "user-properties.jsp"))));
tester.fileSystem().add(inputFile);
sensor.execute(tester);
String componentKey = inputFile.key();
assertThat(tester.measure(componentKey, CoreMetrics.NCLOC).value()).isEqualTo(227);
assertThat(tester.measure(componentKey, CoreMetrics.COMMENT_LINES).value()).isEqualTo(14);
assertThat(tester.measure(componentKey, CoreMetrics.COMPLEXITY).value()).isEqualTo(1);
assertThat(tester.cpdTokens(componentKey)).hasSize(224);
assertThat(tester.highlightingTypeAt(componentKey, 1, 0)).containsOnly(TypeOfText.COMMENT);
assertThat(tester.highlightingTypeAt(componentKey, 18, 0)).containsOnly(TypeOfText.COMMENT);
assertThat(tester.highlightingTypeAt(componentKey, 19, 0)).containsOnly(TypeOfText.ANNOTATION);
assertThat(tester.highlightingTypeAt(componentKey, 29, 17)).containsOnly(TypeOfText.STRING);
assertThat(tester.highlightingTypeAt(componentKey, 29, 0)).containsOnly(TypeOfText.KEYWORD);
assertThat(tester.allIssues()).hasSize(84);
}
use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonar-web by SonarSource.
the class NoSonarScannerTest method scanNoSonar.
@Test
public void scanNoSonar() {
List<Node> nodeList = new PageLexer().parse(new StringReader("<table>\n<!-- //NOSONAR --><td>\n</table>"));
WebSourceCode webSourceCode = new WebSourceCode(new DefaultInputFile("key", "dummy.jsp"));
NoSonarFilter noSonarFilter = spy(new NoSonarFilter());
HtmlAstScanner pageScanner = new HtmlAstScanner(Collections.emptyList());
pageScanner.addVisitor(new NoSonarScanner(noSonarFilter));
pageScanner.scan(nodeList, webSourceCode, Charsets.UTF_8);
verify(noSonarFilter, times(1)).noSonarInFile(any(InputFile.class), isOnlyIgnoringLine2());
}
use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonar-web by SonarSource.
the class PageCountLinesTest method testCountLinesJspFile.
@Test
public void testCountLinesJspFile() throws FileNotFoundException {
List<Node> nodeList = lexer.parse(new FileReader(TestUtils.getResource("checks/AvoidHtmlCommentCheck/document.jsp")));
String relativePath = "testdocument.jsp";
WebSourceCode webSourceCode = new WebSourceCode(new DefaultInputFile("key", relativePath).setModuleBaseDir(new File(".").toPath()));
scanner.scan(nodeList, webSourceCode, Charsets.UTF_8);
assertThat(webSourceCode.getMeasure(CoreMetrics.NCLOC)).isEqualTo(2);
assertThat(webSourceCode.getDetailedLinesOfCode()).containsOnly(1, 3);
assertThat(webSourceCode.getMeasure(CoreMetrics.COMMENT_LINES)).isEqualTo(6);
assertThat(webSourceCode.getDetailedLinesOfComments()).containsOnly(2, 4, 6, 7, 8, 10);
}
Aggregations