Search in sources :

Example 96 with CheckstyleException

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));
    }
}
Also used : CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException)

Example 97 with CheckstyleException

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 ''");
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.jupiter.api.Test)

Example 98 with CheckstyleException

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.]");
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.jupiter.api.Test)

Example 99 with CheckstyleException

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'");
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.jupiter.api.Test)

Example 100 with CheckstyleException

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'");
    }
}
Also used : DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.jupiter.api.Test)

Aggregations

CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)146 Test (org.junit.jupiter.api.Test)58 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)56 File (java.io.File)32 IOException (java.io.IOException)27 Test (org.junit.Test)27 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)18 Properties (java.util.Properties)15 ArrayList (java.util.ArrayList)14 URL (java.net.URL)11 Violation (com.puppycrawl.tools.checkstyle.api.Violation)10 FileText (com.puppycrawl.tools.checkstyle.api.FileText)8 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)7 InputStream (java.io.InputStream)7 SAXException (org.xml.sax.SAXException)7 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)6 URI (java.net.URI)6 Set (java.util.Set)6 Checker (com.puppycrawl.tools.checkstyle.Checker)5 MalformedURLException (java.net.MalformedURLException)5