Search in sources :

Example 31 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.

the class TreeWalkerTest method testProcessWithParserThrowable.

@Test
public void testProcessWithParserThrowable() throws Exception {
    final TreeWalker treeWalker = new TreeWalker();
    treeWalker.configure(createModuleConfig(TypeNameCheck.class));
    final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
    treeWalker.setModuleFactory(factory);
    treeWalker.setupChild(createModuleConfig(TypeNameCheck.class));
    final File file = new File(temporaryFolder, "file.java");
    final List<String> lines = new ArrayList<>();
    lines.add(" classD a {} ");
    final FileText fileText = new FileText(file, lines);
    treeWalker.setFileContents(new FileContents(fileText));
    try {
        treeWalker.processFiltered(file, fileText);
        assertWithMessage("Exception is expected").fail();
    } catch (CheckstyleException exception) {
        assertWithMessage("Error message is unexpected").that(exception.getMessage().contains("occurred while parsing file")).isTrue();
    }
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) ArrayList(java.util.ArrayList) TypeNameCheck(com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 32 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.

the class TreeWalkerTest method testProcessNonJavaFilesWithoutException.

@Test
public void testProcessNonJavaFilesWithoutException() throws Exception {
    final TreeWalker treeWalker = new TreeWalker();
    treeWalker.setTabWidth(1);
    treeWalker.configure(new DefaultConfiguration("default config"));
    final File file = new File(getPath("InputTreeWalkerNotJava.xml"));
    final FileText fileText = new FileText(file, StandardCharsets.ISO_8859_1.name());
    treeWalker.processFiltered(file, fileText);
    final Collection<Checks> checks = TestUtil.getInternalState(treeWalker, "ordinaryChecks");
    assertWithMessage("No checks -> No parsing").that(checks).isEmpty();
}
Also used : Checks(org.mockito.internal.util.Checks) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 33 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.

the class TreeWalkerTest method testBehaviourWithOrdinaryAndCommentChecks.

@Test
public void testBehaviourWithOrdinaryAndCommentChecks() throws Exception {
    final TreeWalker treeWalker = new TreeWalker();
    treeWalker.configure(createModuleConfig(TypeNameCheck.class));
    treeWalker.configure(createModuleConfig(CommentsIndentationCheck.class));
    final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
    treeWalker.setModuleFactory(factory);
    treeWalker.setupChild(createModuleConfig(TypeNameCheck.class));
    treeWalker.setupChild(createModuleConfig(CommentsIndentationCheck.class));
    final File file = new File(temporaryFolder, "file.java");
    final List<String> lines = new ArrayList<>();
    lines.add(" class a%$# {} ");
    final FileText fileText = new FileText(file, lines);
    treeWalker.setFileContents(new FileContents(fileText));
    try {
        treeWalker.processFiltered(file, fileText);
        assertWithMessage("file is not compilable, exception is expected").fail();
    } catch (CheckstyleException exception) {
        final String message = "IllegalStateException occurred while parsing file";
        assertWithMessage("Error message is unexpected").that(exception.getMessage().contains(message)).isTrue();
    }
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) ArrayList(java.util.ArrayList) TypeNameCheck(com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck) CommentsIndentationCheck(com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 34 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.

the class XpathFileGeneratorAstFilterTest method createTreeWalkerAuditEvent.

private static TreeWalkerAuditEvent createTreeWalkerAuditEvent(String fileName, Violation violation) throws Exception {
    final File file = new File(getPath(fileName));
    final FileText fileText = new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
    final FileContents fileContents = new FileContents(fileText);
    final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
    return new TreeWalkerAuditEvent(fileContents, fileName, violation, rootAst);
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File)

Example 35 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.

the class NewlineAtEndOfFileCheckTest method testWrongFile.

@Test
public void testWrongFile() throws Exception {
    final DefaultConfiguration checkConfig = createModuleConfig(NewlineAtEndOfFileCheck.class);
    final NewlineAtEndOfFileCheck check = new NewlineAtEndOfFileCheck();
    check.configure(checkConfig);
    final List<String> lines = new ArrayList<>(1);
    lines.add("txt");
    final File impossibleFile = new File("");
    final FileText fileText = new FileText(impossibleFile, lines);
    final Set<Violation> violations = check.process(impossibleFile, fileText);
    assertWithMessage("Amount of violations is unexpected").that(violations).hasSize(1);
    final Iterator<Violation> iterator = violations.iterator();
    assertWithMessage("Violation message differs from expected").that(iterator.next().getViolation()).isEqualTo(getCheckMessage(MSG_KEY_UNABLE_OPEN, ""));
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) ArrayList(java.util.ArrayList) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) FileText(com.puppycrawl.tools.checkstyle.api.FileText) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

FileText (com.puppycrawl.tools.checkstyle.api.FileText)49 File (java.io.File)41 Test (org.junit.jupiter.api.Test)29 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)18 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)15 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)7 Violation (com.puppycrawl.tools.checkstyle.api.Violation)7 ArrayList (java.util.ArrayList)6 TreeWalkerAuditEvent (com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent)5 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)4 TypeNameCheck (com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 TestLoggingReporter (com.puppycrawl.tools.checkstyle.internal.testmodules.TestLoggingReporter)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 RecognitionException (antlr.RecognitionException)1 TokenStreamException (antlr.TokenStreamException)1 TokenStreamRecognitionException (antlr.TokenStreamRecognitionException)1 ClassPath (com.google.common.reflect.ClassPath)1 LineColumn (com.puppycrawl.tools.checkstyle.api.LineColumn)1