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