Search in sources :

Example 76 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class ConfigurationLoaderTest method testLoadConfigurationWrongUrl.

@Test
public void testLoadConfigurationWrongUrl() {
    try {
        final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration(";config_with_ignore.xml", new PropertiesExpander(new Properties()), true);
        final Configuration[] children = config.getChildren();
        assertEquals(0, children[0].getChildren().length);
        fail("Exception is expected");
    } catch (CheckstyleException ex) {
        assertEquals("Unable to find: ;config_with_ignore.xml", ex.getMessage());
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 77 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class BaseCheckTestSupport method getCheckConfig.

/**
     * Returns {@link Configuration} instance for the given check name.
     * This implementation uses {@link BaseCheckTestSupport#getConfiguration()} method inside.
     * @param checkName check name.
     * @return {@link Configuration} instance for the given check name.
     * @throws CheckstyleException if exception occurs during configuration loading.
     */
protected static Configuration getCheckConfig(String checkName, String checkId) throws CheckstyleException {
    final Configuration result;
    final List<Configuration> configs = getCheckConfigs(checkName);
    if (configs.size() == 1) {
        result = configs.get(0);
    } else {
        result = configs.stream().filter(conf -> {
            try {
                return conf.getAttribute("id").equals(checkId);
            } catch (CheckstyleException ex) {
                throw new IllegalStateException("problem to get ID attribute from " + conf, ex);
            }
        }).findFirst().orElseGet(null);
    }
    return result;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) CommonUtils(com.puppycrawl.tools.checkstyle.utils.CommonUtils) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) AbstractViolationReporter(com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter) ByteArrayInputStream(java.io.ByteArrayInputStream) BriefUtLogger(com.puppycrawl.tools.checkstyle.BriefUtLogger) Locale(java.util.Locale) Map(java.util.Map) ConfigurationLoader(com.puppycrawl.tools.checkstyle.ConfigurationLoader) Properties(java.util.Properties) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) TreeWalker(com.puppycrawl.tools.checkstyle.TreeWalker) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException)

Example 78 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project maven-plugins by apache.

the class DefaultCheckstyleExecutor method getConfiguration.

public Configuration getConfiguration(CheckstyleExecutorRequest request) throws CheckstyleExecutorException {
    try {
        // Checkstyle will always use the context classloader in order
        // to load resources (dtds),
        // so we have to fix it
        ClassLoader checkstyleClassLoader = PackageNamesLoader.class.getClassLoader();
        Thread.currentThread().setContextClassLoader(checkstyleClassLoader);
        String configFile = getConfigFile(request);
        Properties overridingProperties = getOverridingProperties(request);
        Configuration config = ConfigurationLoader.loadConfiguration(configFile, new PropertiesExpander(overridingProperties), request.isOmitIgnoredModules());
        String effectiveEncoding = StringUtils.isNotEmpty(request.getEncoding()) ? request.getEncoding() : System.getProperty("file.encoding", "UTF-8");
        if (StringUtils.isEmpty(request.getEncoding())) {
            getLogger().warn("File encoding has not been set, using platform encoding " + effectiveEncoding + ", i.e. build is platform dependent!");
        }
        // MCHECKSTYLE-332 Checkstyle 6.16+ (#569): the cache is moved to the Checker module instead of TreeWalker
        boolean cacheInChecker = false;
        for (Method method : Checker.class.getMethods()) {
            if ("setCacheFile".equals(method.getName()) && Arrays.equals(method.getParameterTypes(), new Class<?>[] { String.class })) {
                cacheInChecker = true;
                break;
            }
        }
        if ("Checker".equals(config.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(config.getName())) {
            if (config instanceof DefaultConfiguration) {
                // MCHECKSTYLE-173 Only add the "charset" attribute if it has not been set
                addAttributeIfNotExists((DefaultConfiguration) config, "charset", effectiveEncoding);
                if (cacheInChecker) {
                    addAttributeIfNotExists((DefaultConfiguration) config, "cacheFile", request.getCacheFile());
                }
            } else {
                getLogger().warn("Failed to configure file encoding on module " + config);
            }
        }
        Configuration[] modules = config.getChildren();
        for (Configuration module : modules) {
            if ("TreeWalker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.TreeWalker".equals(module.getName())) {
                if (module instanceof DefaultConfiguration) {
                    if (!cacheInChecker) {
                        addAttributeIfNotExists((DefaultConfiguration) module, "cacheFile", request.getCacheFile());
                    }
                } else {
                    getLogger().warn("Failed to configure cache file on module " + module);
                }
            }
        }
        return config;
    } catch (CheckstyleException e) {
        throw new CheckstyleExecutorException("Failed during checkstyle configuration", e);
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) URLClassLoader(java.net.URLClassLoader) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Method(java.lang.reflect.Method) Properties(java.util.Properties)

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