Search in sources :

Example 6 with FileText

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

the class TreeWalkerTest method testProcessWithRecognitionException.

@Test
public void testProcessWithRecognitionException() 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(" class 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("IllegalStateException 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 7 with FileText

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

the class TranslationCheckTest method testStateIsCleared.

/**
 * Even when we pass several files to AbstractModuleTestSupport#verify,
 * the check processes it during one run, so we cannot reproduce situation
 * when TranslationCheck#beginProcessing called several times during single run.
 * So, we have to use reflection to check this particular case.
 *
 * @throws Exception when code tested throws exception
 */
@Test
public void testStateIsCleared() throws Exception {
    final File fileToProcess = new File(getPath("InputTranslationCheckFireErrors_de.properties"));
    final String charset = StandardCharsets.UTF_8.name();
    final TranslationCheck check = new TranslationCheck();
    check.beginProcessing(charset);
    check.processFiltered(fileToProcess, new FileText(fileToProcess, charset));
    check.beginProcessing(charset);
    final Field field = check.getClass().getDeclaredField("filesToProcess");
    field.setAccessible(true);
    assertWithMessage("Stateful field is not cleared on beginProcessing").that((Iterable<?>) field.get(check)).isEmpty();
}
Also used : Field(java.lang.reflect.Field) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 8 with FileText

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

the class UniquePropertiesCheckTest method testNotFoundKey.

/**
 * Tests the {@link UniquePropertiesCheck#getLineNumber(FileText, String)}
 * method return value.
 *
 * @noinspection JavadocReference Test javadocs should explain all.
 */
@Test
public void testNotFoundKey() throws Exception {
    final List<String> testStrings = new ArrayList<>(3);
    final Method getLineNumber = UniquePropertiesCheck.class.getDeclaredMethod("getLineNumber", FileText.class, String.class);
    assertWithMessage("Get line number method should be present").that(getLineNumber).isNotNull();
    getLineNumber.setAccessible(true);
    testStrings.add("");
    testStrings.add("0 = 0");
    testStrings.add("445");
    final FileText fileText = new FileText(new File("some.properties"), testStrings);
    final Object lineNumber = getLineNumber.invoke(UniquePropertiesCheck.class, fileText, "some key");
    assertWithMessage("Line number should not be null").that(lineNumber).isNotNull();
    assertWithMessage("Invalid line number").that(lineNumber).isEqualTo(1);
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 9 with FileText

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

the class JavadocPackageCheckTest method testCheckstyleExceptionIfFailedToGetCanonicalPathToFile.

/**
 * Using direct call to check here because there is no other way
 * to reproduce exception with invalid canonical path.
 */
@Test
public void testCheckstyleExceptionIfFailedToGetCanonicalPathToFile() {
    final JavadocPackageCheck check = new JavadocPackageCheck();
    final File fileWithInvalidPath = new File("\u0000\u0000\u0000");
    final FileText mockFileText = new FileText(fileWithInvalidPath, Collections.emptyList());
    final String expectedExceptionMessage = "Exception while getting canonical path to file " + fileWithInvalidPath.getPath();
    try {
        check.processFiltered(fileWithInvalidPath, mockFileText);
        assertWithMessage("CheckstyleException expected to be thrown").fail();
    } catch (CheckstyleException ex) {
        assertWithMessage("Invalid exception message. Expected: " + expectedExceptionMessage).that(ex.getMessage()).isEqualTo(expectedExceptionMessage);
    }
}
Also used : 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 10 with FileText

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

the class AbstractXpathTestSupport method generateXpathQueries.

/**
 * Returns a list of XPath queries to locate the violation nodes in a Java file.
 *
 * @param fileToProcess the Java file to be processed. {@link File} type object.
 * @param position the position of violation in the file. {@link ViolationPosition} object.
 * @return a list of strings containing XPath queries for locating violation nodes.
 * @throws Exception can throw exceptions while accessing file contents.
 */
private static List<String> generateXpathQueries(File fileToProcess, ViolationPosition position) throws Exception {
    final FileText fileText = new FileText(fileToProcess, StandardCharsets.UTF_8.name());
    final DetailAST rootAst = JavaParser.parseFile(fileToProcess, JavaParser.Options.WITH_COMMENTS);
    final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(rootAst, position.violationLineNumber, position.violationColumnNumber, fileText, DEFAULT_TAB_WIDTH);
    return queryGenerator.generate();
}
Also used : XpathQueryGenerator(com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText)

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