Search in sources :

Example 1 with FileText

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

the class IllegalInstantiationCheckTest method testNullClassLoader.

@Test
public void testNullClassLoader() throws Exception {
    final DetailAST exprAst = new DetailAST();
    exprAst.setType(TokenTypes.EXPR);
    final DetailAST newAst = new DetailAST();
    newAst.setType(TokenTypes.LITERAL_NEW);
    newAst.setLineNo(1);
    newAst.setColumnNo(1);
    final DetailAST identAst = new DetailAST();
    identAst.setType(TokenTypes.IDENT);
    identAst.setText("Boolean");
    final DetailAST lparenAst = new DetailAST();
    lparenAst.setType(TokenTypes.LPAREN);
    final DetailAST elistAst = new DetailAST();
    elistAst.setType(TokenTypes.ELIST);
    final DetailAST rparenAst = new DetailAST();
    rparenAst.setType(TokenTypes.RPAREN);
    exprAst.addChild(newAst);
    newAst.addChild(identAst);
    identAst.setNextSibling(lparenAst);
    lparenAst.setNextSibling(elistAst);
    elistAst.setNextSibling(rparenAst);
    final IllegalInstantiationCheck check = new IllegalInstantiationCheck();
    final File inputFile = new File(getNonCompilablePath("InputIllegalInstantiationLang.java"));
    check.setFileContents(new FileContents(new FileText(inputFile, "UTF-8")));
    check.configure(createCheckConfig(IllegalInstantiationCheck.class));
    check.setMessages(new LocalizedMessages());
    check.setClasses("java.lang.Boolean");
    check.visitToken(newAst);
    check.finishTree(newAst);
}
Also used : LocalizedMessages(com.puppycrawl.tools.checkstyle.api.LocalizedMessages) 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) Test(org.junit.Test)

Example 2 with FileText

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

the class AstRegressionTest method verifyAstRaw.

private static void verifyAstRaw(String expectedTextPrintFileName, String actualJava, boolean withComments) throws Exception {
    final File expectedFile = new File(expectedTextPrintFileName);
    final String expectedContents = new FileText(expectedFile, System.getProperty("file.encoding", "UTF-8")).getFullText().toString().replace("\r", "");
    final FileText actualFileContents = FileText.fromLines(new File(""), Arrays.asList(actualJava.split("\\n|\\r\\n?")));
    final String actualContents = AstTreeStringPrinter.printAst(actualFileContents, withComments);
    assertEquals("Generated AST from Java code should match pre-defined AST", expectedContents, actualContents);
}
Also used : FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File)

Example 3 with FileText

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

the class OrderedPropertiesCheckTest method testIoException.

/**
 * Tests IO exception, that can occur during reading of properties file.
 */
@Test
public void testIoException() throws Exception {
    final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
    final OrderedPropertiesCheck check = new OrderedPropertiesCheck();
    check.configure(checkConfig);
    final String fileName = getPath("InputOrderedPropertiesCheckNotExisting.properties");
    final File file = new File(fileName);
    final FileText fileText = new FileText(file, Collections.emptyList());
    final SortedSet<Violation> violations = check.process(file, fileText);
    assertWithMessage("Wrong violations count").that(violations).hasSize(1);
    final Violation violation = violations.iterator().next();
    final String retrievedMessage = violations.iterator().next().getKey();
    assertWithMessage("violation key is not valid").that(retrievedMessage).isEqualTo("unable.open.cause");
    assertWithMessage("violation is not valid").that(getCheckMessage(MSG_IO_EXCEPTION_KEY, fileName, getFileNotFoundDetail(file))).isEqualTo(violation.getViolation());
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 4 with FileText

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

the class TreeWalkerTest method testProcessNonJavaFiles.

@Test
public void testProcessNonJavaFiles() throws Exception {
    final TreeWalker treeWalker = new TreeWalker();
    final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
    treeWalker.setModuleFactory(factory);
    treeWalker.configure(new DefaultConfiguration("default config"));
    final DefaultConfiguration childConfig = createModuleConfig(JavadocParagraphCheck.class);
    treeWalker.setupChild(childConfig);
    final File file = new File("input.java");
    final List<String> lines = new ArrayList<>(Arrays.asList("package com.puppycrawl.tools.checkstyle;", "", "error public class InputTreeWalkerFileWithViolation {}"));
    final FileText fileText = new FileText(file, lines);
    treeWalker.setFileContents(new FileContents(fileText));
    try {
        treeWalker.processFiltered(file, fileText);
        assertWithMessage("Exception expected").fail();
    } catch (CheckstyleException ex) {
        assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("IllegalStateException occurred while parsing file input.java.");
    }
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) ArrayList(java.util.ArrayList) 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 5 with FileText

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

the class TreeWalkerTest method testBehaviourWithZeroChecks.

@Test
public void testBehaviourWithZeroChecks() throws Exception {
    final TreeWalker treeWalker = new TreeWalker();
    final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
    treeWalker.setModuleFactory(factory);
    // create file that should throw exception
    final File file = new File(temporaryFolder, "file.java");
    final FileText fileText = new FileText(file, new ArrayList<>());
    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)

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