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