use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class ConfigurationLoader method parsePropertyString.
/**
* Parses a string containing {@code ${xxx}} style property
* references into two lists. The first list is a collection
* of text fragments, while the other is a set of string property names.
* {@code null} entries in the first list indicate a property
* reference from the second list.
*
* <p>Code copied from ant -
* http://cvs.apache.org/viewcvs/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
*
* @param value Text to parse. Must not be {@code null}.
* @param fragments List to add text fragments to.
* Must not be {@code null}.
* @param propertyRefs List to add property names to.
* Must not be {@code null}.
*
* @throws CheckstyleException if the string contains an opening
* {@code ${} without a closing
* {@code }}
*/
private static void parsePropertyString(String value, List<String> fragments, List<String> propertyRefs) throws CheckstyleException {
int prev = 0;
// search for the next instance of $ from the 'prev' position
int pos = value.indexOf(DOLLAR_SIGN, prev);
while (pos >= 0) {
// if there was any text before this, add it as a fragment
if (pos > 0) {
fragments.add(value.substring(prev, pos));
}
// then move past it
if (pos == value.length() - 1) {
fragments.add(String.valueOf(DOLLAR_SIGN));
prev = pos + 1;
} else if (value.charAt(pos + 1) == '{') {
// property found, extract its name or bail on a typo
final int endName = value.indexOf('}', pos);
if (endName == -1) {
throw new CheckstyleException("Syntax error in property: " + value);
}
final String propertyName = value.substring(pos + 2, endName);
fragments.add(null);
propertyRefs.add(propertyName);
prev = endName + 1;
} else {
if (value.charAt(pos + 1) == DOLLAR_SIGN) {
// backwards compatibility two $ map to one mode
fragments.add(String.valueOf(DOLLAR_SIGN));
} else {
// new behaviour: $X maps to $X for all values of X!='$'
fragments.add(value.substring(pos, pos + 2));
}
prev = pos + 2;
}
// search for the next instance of $ from the 'prev' position
pos = value.indexOf(DOLLAR_SIGN, prev);
}
// if there is any tail to the file, append it
if (prev < value.length()) {
fragments.add(value.substring(prev));
}
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class RegexpHeaderCheckTest method testEmptyFilename.
@Test
public void testEmptyFilename() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
checkConfig.addProperty("headerFile", "");
try {
createChecker(checkConfig);
assertWithMessage("Checker creation should not succeed with invalid headerFile").fail();
} catch (CheckstyleException ex) {
assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("cannot initialize module" + " com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck" + " - Cannot set property 'headerFile' to ''");
}
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class ClassFanOutComplexityCheckTest method testExcludedPackagesCommonPackagesWithEndingDot.
@Test
public void testExcludedPackagesCommonPackagesWithEndingDot() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(ClassFanOutComplexityCheck.class);
checkConfig.addProperty("max", "0");
checkConfig.addProperty("excludedPackages", "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.");
try {
createChecker(checkConfig);
assertWithMessage("exception expected").fail();
} catch (CheckstyleException ex) {
assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - " + "cannot initialize module com.puppycrawl.tools.checkstyle.checks." + "metrics.ClassFanOutComplexityCheck - " + "Cannot set property 'excludedPackages' to " + "'com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.'");
assertWithMessage("Invalid exception message,").that(ex.getCause().getCause().getCause().getCause().getMessage()).isEqualTo("the following values are not valid identifiers: [" + "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.]");
}
}
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 = createModuleConfig(FileLengthCheck.class);
try {
checkConfig.addProperty("max", "abc");
createChecker(checkConfig);
assertWithMessage("Should indicate illegal args").fail();
} catch (CheckstyleException ex) {
assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.checks." + "sizes.FileLengthCheck - " + "illegal value 'abc' for property 'max'");
}
}
use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.
the class EmptyForIteratorPadCheckTest method testInvalidOption.
@Test
public void testInvalidOption() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(EmptyForIteratorPadCheck.class);
checkConfig.addProperty("option", "invalid_option");
try {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(createChecker(checkConfig), getPath("InputEmptyForIteratorPad2.java"), expected);
assertWithMessage("exception expected").fail();
} catch (CheckstyleException ex) {
assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - " + "cannot initialize module com.puppycrawl.tools.checkstyle.checks." + "whitespace.EmptyForIteratorPadCheck - " + "Cannot set property 'option' to 'invalid_option'");
}
}
Aggregations