use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class ImportControlCheckTest method testUrlIncorrectUrl.
@Test
public void testUrlIncorrectUrl() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
checkConfig.addAttribute("url", "https://{WrongCharsInURL}");
try {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputImportControl.java"), expected);
fail("Test should fail if exception was not thrown");
} catch (final CheckstyleException ex) {
final String message = getCheckstyleExceptionMessage(ex);
assertTrue(message.startsWith("Unable to find: "));
}
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class ParenPadCheckTest method testInvalidOption.
@Test
public void testInvalidOption() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(ParenPadCheck.class);
checkConfig.addAttribute("option", "invalid_option");
try {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputParenPad.java"), expected);
fail("exception expected");
} catch (CheckstyleException ex) {
assertTrue(ex.getMessage().startsWith("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - " + "Cannot set property 'option' to 'invalid_option' in module"));
}
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class Main method validateCli.
/**
* Do validation of Command line options.
* @param cmdLine command line object
* @param filesToProcess List of files to process found from the command line.
* @return list of violations
*/
// -@cs[CyclomaticComplexity] Breaking apart will damage encapsulation
private static List<String> validateCli(CommandLine cmdLine, List<File> filesToProcess) {
final List<String> result = new ArrayList<>();
if (filesToProcess.isEmpty()) {
result.add("Files to process must be specified, found 0.");
} else // ensure there is no conflicting options
if (cmdLine.hasOption(OPTION_T_NAME) || cmdLine.hasOption(OPTION_CAPITAL_T_NAME) || cmdLine.hasOption(OPTION_J_NAME) || cmdLine.hasOption(OPTION_CAPITAL_J_NAME)) {
if (cmdLine.hasOption(OPTION_C_NAME) || cmdLine.hasOption(OPTION_P_NAME) || cmdLine.hasOption(OPTION_F_NAME) || cmdLine.hasOption(OPTION_O_NAME)) {
result.add("Option '-t' cannot be used with other options.");
} else if (filesToProcess.size() > 1) {
result.add("Printing AST is allowed for only one file.");
}
} else // ensure a configuration file is specified
if (cmdLine.hasOption(OPTION_C_NAME)) {
final String configLocation = cmdLine.getOptionValue(OPTION_C_NAME);
try {
// test location only
CommonUtils.getUriByFilename(configLocation);
} catch (CheckstyleException ignored) {
result.add(String.format("Could not find config XML file '%s'.", configLocation));
}
// validate optional parameters
if (cmdLine.hasOption(OPTION_F_NAME)) {
final String format = cmdLine.getOptionValue(OPTION_F_NAME);
if (!PLAIN_FORMAT_NAME.equals(format) && !XML_FORMAT_NAME.equals(format)) {
result.add(String.format("Invalid output format." + " Found '%s' but expected '%s' or '%s'.", format, PLAIN_FORMAT_NAME, XML_FORMAT_NAME));
}
}
if (cmdLine.hasOption(OPTION_P_NAME)) {
final String propertiesLocation = cmdLine.getOptionValue(OPTION_P_NAME);
final File file = new File(propertiesLocation);
if (!file.exists()) {
result.add(String.format("Could not find file '%s'.", propertiesLocation));
}
}
} else {
result.add("Must specify a config XML file.");
}
return result;
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class Main method loadProperties.
/**
* Loads properties from a File.
* @param file
* the properties file
* @return the properties in file
* @throws CheckstyleException
* when could not load properties file
*/
private static Properties loadProperties(File file) throws CheckstyleException {
final Properties properties = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
properties.load(fis);
} catch (final IOException ex) {
throw new CheckstyleException(String.format("Unable to load properties from file '%s'.", file.getAbsolutePath()), ex);
} finally {
Closeables.closeQuietly(fis);
}
return properties;
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class AstTreeStringPrinter method parseFileText.
/**
* Parse a text and return the parse tree.
* @param text the text to parse.
* @param withComments true to include comment nodes to the tree
* @return the root node of the parse tree.
* @throws CheckstyleException if the file is not a Java source.
*/
private static DetailAST parseFileText(FileText text, boolean withComments) throws CheckstyleException {
final FileContents contents = new FileContents(text);
final DetailAST result;
try {
if (withComments) {
result = TreeWalker.parseWithComments(contents);
} else {
result = TreeWalker.parse(contents);
}
} catch (RecognitionException | TokenStreamException ex) {
final String exceptionMsg = String.format(Locale.ROOT, "%s occurred during the analysis of file %s.", ex.getClass().getSimpleName(), text.getFile().getPath());
throw new CheckstyleException(exceptionMsg, ex);
}
return result;
}
Aggregations