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());
}
}
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;
}
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);
}
}
Aggregations