Search in sources :

Example 1 with PropertiesExpander

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);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) Checker(com.puppycrawl.tools.checkstyle.Checker) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) StringReader(java.io.StringReader) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Properties(java.util.Properties) File(java.io.File)

Example 2 with PropertiesExpander

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);
        }
    }
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) InputSource(org.xml.sax.InputSource) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger) File(java.io.File)

Example 3 with PropertiesExpander

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;
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Properties(java.util.Properties) ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) ThreadModeSettings(com.puppycrawl.tools.checkstyle.ThreadModeSettings) RootModule(com.puppycrawl.tools.checkstyle.api.RootModule) BuildException(org.apache.tools.ant.BuildException) PackageObjectFactory(com.puppycrawl.tools.checkstyle.PackageObjectFactory) ConfigurationLoader(com.puppycrawl.tools.checkstyle.ConfigurationLoader)

Example 4 with PropertiesExpander

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");
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) Properties(java.util.Properties) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 5 with PropertiesExpander

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");
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) Properties(java.util.Properties) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)10 Properties (java.util.Properties)10 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)8 Test (org.junit.jupiter.api.Test)5 Checker (com.puppycrawl.tools.checkstyle.Checker)4 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)4 File (java.io.File)4 URI (java.net.URI)4 InputSource (org.xml.sax.InputSource)3 StringReader (java.io.StringReader)2 ConfigurationLoader (com.puppycrawl.tools.checkstyle.ConfigurationLoader)1 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)1 DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)1 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)1 PackageObjectFactory (com.puppycrawl.tools.checkstyle.PackageObjectFactory)1 ThreadModeSettings (com.puppycrawl.tools.checkstyle.ThreadModeSettings)1 RootModule (com.puppycrawl.tools.checkstyle.api.RootModule)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Method (java.lang.reflect.Method)1 URLClassLoader (java.net.URLClassLoader)1