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 ImportControlCheckTest method testBroken.
@Test
public void testBroken() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
checkConfig.addAttribute("file", getPath("import-control_broken.xml"));
try {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputImportControl.java"), expected);
fail("Test should fail if exception was not thrown");
} catch (CheckstyleException ex) {
final String message = getCheckstyleExceptionMessage(ex);
assertTrue(message.startsWith("Unable to load "));
}
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class FileLengthCheckTest method testArgs.
@Test
public void testArgs() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(FileLengthCheck.class);
try {
checkConfig.addAttribute("max", "abc");
createChecker(checkConfig);
fail("Should indicate illegal args");
} catch (CheckstyleException ex) {
// Expected Exception because of illegal argument for "max"
assertEquals("cannot initialize module" + " com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck" + " - illegal value 'abc' for property 'max' of module" + " com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck", ex.getMessage());
}
}
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;
}
Aggregations