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);
}
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);
}
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());
}
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);
}
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);
}
Aggregations