Search in sources :

Example 81 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 82 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 83 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)

Example 84 with CheckstyleException

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

Example 85 with CheckstyleException

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

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