Search in sources :

Example 26 with FileText

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

the class MainFrameModel method parseFile.

/**
     * Parse a file and return the parse tree.
     * @param file the file to parse.
     * @return the root node of the parse tree.
     * @throws IOException if the file could not be read.
     * @throws ANTLRException if the file is not a Java source.
     */
public DetailAST parseFile(File file) throws IOException, ANTLRException {
    final FileText fileText = getFileText(file);
    final FileContents contents = new FileContents(fileText);
    return TreeWalker.parse(contents);
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) FileText(com.puppycrawl.tools.checkstyle.api.FileText)

Example 27 with FileText

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

the class OrderedPropertiesCheckTest method testKeepForLoopIntact.

/**
 * This test validates the PIT mutation of getIndex().
 * Here the for statement for
 * (int index = startLineNo; index < fileText.size(); index++)
 * will change to
 * for (int index = startLineNo; true; index++)
 * By creating a FileText having no lines it makes sure that
 * fileText.size() returning zero size.
 * This will keep the for loop intact.
 */
@Test
public void testKeepForLoopIntact() throws Exception {
    final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
    final OrderedPropertiesCheck check = new OrderedPropertiesCheck();
    check.configure(checkConfig);
    final String fileName = getPath("InputOrderedProperties2EmptyValue.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);
}
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 28 with FileText

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

the class UniquePropertiesCheckTest method testIoException.

/**
 * Tests IO exception, that can occur during reading of properties file.
 */
@Test
public void testIoException() throws Exception {
    final DefaultConfiguration checkConfig = createModuleConfig(UniquePropertiesCheck.class);
    final UniquePropertiesCheck check = new UniquePropertiesCheck();
    check.configure(checkConfig);
    final String fileName = getPath("InputUniquePropertiesCheckNotExisting.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 messages count: " + violations.size()).that(violations).hasSize(1);
    final Violation violation = violations.iterator().next();
    final String retrievedMessage = violations.iterator().next().getKey();
    assertWithMessage("violation key '" + retrievedMessage + "' is not valid").that(retrievedMessage).isEqualTo("unable.open.cause");
    assertWithMessage("violation '" + violation.getViolation() + "' 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 29 with FileText

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

the class RegexpMultilineCheckTest method testStateIsBeingReset.

/**
 * Done as a UT cause new instance of Detector is created each time 'verify' executed.
 *
 * @throws Exception some Exception
 */
@Test
public void testStateIsBeingReset() throws Exception {
    final TestLoggingReporter reporter = new TestLoggingReporter();
    final DetectorOptions detectorOptions = DetectorOptions.newBuilder().reporter(reporter).format("\\r").maximum(1).build();
    final MultilineDetector detector = new MultilineDetector(detectorOptions);
    final File file = File.createTempFile("junit", null, temporaryFolder);
    Files.write(file.toPath(), "first line \r\n second line \n\r third line".getBytes(StandardCharsets.UTF_8));
    detector.processLines(new FileText(file, StandardCharsets.UTF_8.name()));
    detector.processLines(new FileText(file, StandardCharsets.UTF_8.name()));
    assertWithMessage("Logged unexpected amount of issues").that(reporter.getLogCount()).isEqualTo(2);
}
Also used : TestLoggingReporter(com.puppycrawl.tools.checkstyle.internal.testmodules.TestLoggingReporter) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 30 with FileText

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

the class RegexpSinglelineCheckTest method testStateIsBeingReset.

/**
 * Done as a UT cause new instance of Detector is created each time 'verify' executed.
 *
 * @throws Exception some Exception
 */
@Test
public void testStateIsBeingReset() throws Exception {
    final String illegal = "System\\.(out)|(err)\\.print(ln)?\\(";
    final TestLoggingReporter reporter = new TestLoggingReporter();
    final DetectorOptions detectorOptions = DetectorOptions.newBuilder().reporter(reporter).format(illegal).maximum(1).build();
    final SinglelineDetector detector = new SinglelineDetector(detectorOptions);
    final File file = new File(getPath("InputRegexpSinglelineSemantic8.java"));
    detector.processLines(new FileText(file, StandardCharsets.UTF_8.name()));
    detector.processLines(new FileText(file, StandardCharsets.UTF_8.name()));
    assertWithMessage("Logged unexpected amount of issues").that(reporter.getLogCount()).isEqualTo(0);
}
Also used : TestLoggingReporter(com.puppycrawl.tools.checkstyle.internal.testmodules.TestLoggingReporter) 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