use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project checkstyle by checkstyle.
the class XdocsPagesTest method validateCheckstyleXml.
private static void validateCheckstyleXml(String fileName, String code, String unserializedSource) throws IOException, CheckstyleException {
// can't process non-existent examples, or out of context snippets
if (!code.contains("com.mycompany") && !code.contains("checkstyle-packages") && !code.contains("MethodLimit") && !code.contains("<suppress ") && !code.contains("<import-control ") && !unserializedSource.startsWith("<property ") && !unserializedSource.startsWith("<taskdef ")) {
// validate checkstyle structure and contents
try {
final Properties properties = new Properties();
properties.setProperty("checkstyle.header.file", new File("config/java.header").getCanonicalPath());
final PropertiesExpander expander = new PropertiesExpander(properties);
final Configuration config = ConfigurationLoader.loadConfiguration(new InputSource(new StringReader(code)), expander, false);
final Checker checker = new Checker();
try {
final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
checker.setModuleClassLoader(moduleClassLoader);
checker.configure(config);
} finally {
checker.destroy();
}
} catch (CheckstyleException ex) {
throw new CheckstyleException(fileName + " has invalid Checkstyle xml (" + ex.getMessage() + "): " + unserializedSource, ex);
}
}
}
use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project mapstruct by mapstruct.
the class CompilingStatement method assertCheckstyleRules.
private void assertCheckstyleRules() throws Exception {
if (sourceOutputDir != null) {
Properties properties = new Properties();
properties.put("checkstyle.cache.file", classOutputDir + "/checkstyle.cache");
final Checker checker = new Checker();
checker.setModuleClassLoader(Checker.class.getClassLoader());
checker.configure(ConfigurationLoader.loadConfiguration(new InputSource(getClass().getClassLoader().getResourceAsStream("checkstyle-for-generated-sources.xml")), new PropertiesExpander(properties), true));
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
checker.addListener(new DefaultLogger(ByteStreams.nullOutputStream(), true, errorStream, true));
int errors = checker.process(findGeneratedFiles(new File(sourceOutputDir)));
if (errors > 0) {
String errorLog = errorStream.toString("UTF-8");
assertThat(true).describedAs("Expected checkstyle compliant output, but got errors:\n" + errorLog).isEqualTo(false);
}
}
}
use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project checkstyle by checkstyle.
the class CheckstyleAntTask method createRootModule.
/**
* Creates new instance of the root module.
*
* @return new instance of the root module
* @throws BuildException if the root module could not be created.
*/
private RootModule createRootModule() {
final RootModule rootModule;
try {
final Properties props = createOverridingProperties();
final ThreadModeSettings threadModeSettings = ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE;
final ConfigurationLoader.IgnoredModulesOptions ignoredModulesOptions;
if (executeIgnoredModules) {
ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.EXECUTE;
} else {
ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.OMIT;
}
final Configuration configuration = ConfigurationLoader.loadConfiguration(config, new PropertiesExpander(props), ignoredModulesOptions, threadModeSettings);
final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
final ModuleFactory factory = new PackageObjectFactory(Checker.class.getPackage().getName() + ".", moduleClassLoader);
rootModule = (RootModule) factory.createModule(configuration.getName());
rootModule.setModuleClassLoader(moduleClassLoader);
rootModule.configure(configuration);
} catch (final CheckstyleException ex) {
throw new BuildException(String.format(Locale.ROOT, "Unable to create Root Module: " + "config {%s}, classpath {%s}.", config, classpath), ex);
}
return rootModule;
}
use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project checkstyle by checkstyle.
the class CommonUtilTest method testGetUriByFilenameFindsRelativeResourceOnClasspath.
@Test
public void testGetUriByFilenameFindsRelativeResourceOnClasspath() throws Exception {
final String filename = getPackageLocation() + "/InputCommonUtilTest_empty_checks.xml";
final URI uri = CommonUtil.getUriByFilename(filename);
final Properties properties = System.getProperties();
final Configuration config = ConfigurationLoader.loadConfiguration(uri.toString(), new PropertiesExpander(properties));
assertWithMessage("Unexpected config name!").that(config.getName()).isEqualTo("Checker");
}
use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project checkstyle by checkstyle.
the class CommonUtilTest method testGetUriByFilenameClasspathPrefixLoadConfig.
@Test
public void testGetUriByFilenameClasspathPrefixLoadConfig() throws Exception {
final String filename = CommonUtil.CLASSPATH_URL_PROTOCOL + getPackageLocation() + "/InputCommonUtilTestWithChecks.xml";
final URI uri = CommonUtil.getUriByFilename(filename);
final Properties properties = System.getProperties();
final Configuration config = ConfigurationLoader.loadConfiguration(uri.toString(), new PropertiesExpander(properties));
assertWithMessage("Unexpected config name!").that(config.getName()).isEqualTo("Checker");
}
Aggregations