Search in sources :

Example 61 with DefaultInputFile

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);
}
Also used : PageLexer(org.sonar.plugins.web.lex.PageLexer) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) FileReader(java.io.FileReader) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) HtmlAstScanner(org.sonar.plugins.web.visitor.HtmlAstScanner) ExpectedException(org.junit.rules.ExpectedException)

Example 62 with DefaultInputFile

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);
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) FileReader(java.io.FileReader) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 63 with DefaultInputFile

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());
}
Also used : PageLexer(org.sonar.plugins.web.lex.PageLexer) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Node(org.sonar.plugins.web.node.Node) StringReader(java.io.StringReader) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 64 with DefaultInputFile

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);
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Node(org.sonar.plugins.web.node.Node) WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) FileReader(java.io.FileReader) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Aggregations

DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)64 Test (org.junit.Test)34 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)22 File (java.io.File)19 InputFile (org.sonar.api.batch.fs.InputFile)17 TaskResult (org.sonar.scanner.mediumtest.TaskResult)9 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)8 FileReader (java.io.FileReader)6 Path (java.nio.file.Path)6 InputModuleHierarchy (org.sonar.api.batch.fs.internal.InputModuleHierarchy)6 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)5 InputDir (org.sonar.api.batch.fs.InputDir)5 WebSourceCode (org.sonar.plugins.web.visitor.WebSourceCode)5 ScannerReportReader (org.sonar.scanner.protocol.output.ScannerReportReader)5 DefaultInputDir (org.sonar.api.batch.fs.internal.DefaultInputDir)4 Block (org.sonar.duplications.block.Block)4 Node (org.sonar.plugins.web.node.Node)4 CheckForNull (javax.annotation.CheckForNull)3 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)3 Issue (org.sonar.api.batch.sensor.issue.Issue)3