Search in sources :

Example 66 with CheckstyleException

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

Example 67 with CheckstyleException

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

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

Example 69 with CheckstyleException

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

Example 70 with CheckstyleException

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

Aggregations

CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)78 Test (org.junit.Test)46 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)33 File (java.io.File)15 IOException (java.io.IOException)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)9 Properties (java.util.Properties)8 URL (java.net.URL)6 SAXException (org.xml.sax.SAXException)5 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)4 URI (java.net.URI)4 Checker (com.puppycrawl.tools.checkstyle.Checker)3 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)3 BufferedInputStream (java.io.BufferedInputStream)3 FileInputStream (java.io.FileInputStream)3 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3