Search in sources :

Example 46 with WebSourceCode

use of org.sonar.plugins.web.visitor.WebSourceCode in project sonar-web by SonarSource.

the class TestHelper method scan.

public static WebSourceCode scan(File file, DefaultNodeVisitor visitor) {
    FileReader fileReader;
    try {
        fileReader = new FileReader(file);
    } catch (FileNotFoundException e) {
        throw Throwables.propagate(e);
    }
    WebSourceCode result = new WebSourceCode(new DefaultInputFile("key", file.getPath()).setLanguage(WebConstants.LANGUAGE_KEY).setType(InputFile.Type.MAIN).setModuleBaseDir(new File(".").toPath()));
    HtmlAstScanner walker = new HtmlAstScanner(ImmutableList.of(new PageCountLines(), new ComplexityVisitor()));
    walker.addVisitor(visitor);
    walker.scan(new PageLexer().parse(fileReader), result, Charsets.UTF_8);
    return result;
}
Also used : PageLexer(org.sonar.plugins.web.lex.PageLexer) ComplexityVisitor(org.sonar.plugins.web.analyzers.ComplexityVisitor) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileNotFoundException(java.io.FileNotFoundException) WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) PageCountLines(org.sonar.plugins.web.analyzers.PageCountLines) 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)

Example 47 with WebSourceCode

use of org.sonar.plugins.web.visitor.WebSourceCode in project sonar-web by SonarSource.

the class IllegalAttributeCheckTest method custom.

@Test
public void custom() {
    IllegalAttributeCheck check = new IllegalAttributeCheck();
    check.attributes = "foo.a,b";
    WebSourceCode sourceCode = TestHelper.scan(new File("src/test/resources/checks/IllegalAttributeCheck.html"), check);
    checkMessagesVerifier.verify(sourceCode.getIssues()).next().atLine(1).withMessage("Remove the \"a\" attribute from the \"foo\" tag").next().atLine(3).withMessage("Remove the \"b\" attribute from the \"baz\" tag");
}
Also used : WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) File(java.io.File) Test(org.junit.Test)

Example 48 with WebSourceCode

use of org.sonar.plugins.web.visitor.WebSourceCode in project sonar-web by SonarSource.

the class RequiredAttributeCheckTest method custom.

@Test
public void custom() {
    RequiredAttributeCheck check = new RequiredAttributeCheck();
    check.attributes = "img.alt,script.type";
    WebSourceCode sourceCode = TestHelper.scan(new File("src/test/resources/checks/RequiredAttributeCheck.html"), check);
    checkMessagesVerifier.verify(sourceCode.getIssues()).next().atLine(2).withMessage("Add the missing \"alt\" to element \"img\".").next().atLine(7).withMessage("Add the missing \"type\" to element \"script\".");
}
Also used : WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) File(java.io.File) Test(org.junit.Test)

Example 49 with WebSourceCode

use of org.sonar.plugins.web.visitor.WebSourceCode in project sonar-web by SonarSource.

the class DoubleQuotesCheckTest method test.

@Test
public void test() throws Exception {
    WebSourceCode sourceCode = TestHelper.scan(new File("src/test/resources/checks/doubleQuotesCheck.html"), new DoubleQuotesCheck());
    checkMessagesVerifier.verify(sourceCode.getIssues()).next().atLine(1).withMessage("Use double quotes instead of single ones.");
}
Also used : WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) File(java.io.File) Test(org.junit.Test)

Example 50 with WebSourceCode

use of org.sonar.plugins.web.visitor.WebSourceCode in project sonar-web by SonarSource.

the class FileLengthCheckTest method custom.

@Test
public void custom() {
    FileLengthCheck check = new FileLengthCheck();
    check.maxLength = 3;
    WebSourceCode sourceCode = TestHelper.scan(new File("src/test/resources/checks/FileLengthCheck.html"), check);
    checkMessagesVerifier.verify(sourceCode.getIssues()).next().atLine(null).withMessage("Current file has 5 lines, which is greater than 3 authorized. Split it into smaller files.");
}
Also used : WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) File(java.io.File) Test(org.junit.Test)

Aggregations

WebSourceCode (org.sonar.plugins.web.visitor.WebSourceCode)93 File (java.io.File)91 Test (org.junit.Test)89 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)10 InputFile (org.sonar.api.batch.fs.InputFile)8 FileReader (java.io.FileReader)6 PageLexer (org.sonar.plugins.web.lex.PageLexer)3 Node (org.sonar.plugins.web.node.Node)3 HtmlAstScanner (org.sonar.plugins.web.visitor.HtmlAstScanner)3 FileNotFoundException (java.io.FileNotFoundException)1 ExpectedException (org.junit.rules.ExpectedException)1 FilePredicates (org.sonar.api.batch.fs.FilePredicates)1 FileSystem (org.sonar.api.batch.fs.FileSystem)1 ComplexityVisitor (org.sonar.plugins.web.analyzers.ComplexityVisitor)1 PageCountLines (org.sonar.plugins.web.analyzers.PageCountLines)1