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