Search in sources :

Example 6 with PropertiesExpander

use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project checkstyle by checkstyle.

the class ChainedPropertyUtilTest method testPropertyChaining.

@Test
public void testPropertyChaining() throws Exception {
    final File propertiesFile = new File(getPath("InputChainedPropertyUtil.properties"));
    final Properties properties = loadProperties(propertiesFile);
    final Properties resolvedProperties = ChainedPropertyUtil.getResolvedProperties(properties);
    final PropertiesExpander expander = new PropertiesExpander(resolvedProperties);
    final String message = "Unexpected property resolution.";
    assertWithMessage(message).that(expander.resolve("basedir")).isEqualTo("/home");
    assertWithMessage(message).that(expander.resolve("checkstyle.dir")).isEqualTo("/home/checkstyle");
    assertWithMessage(message).that(expander.resolve("config.dir")).isEqualTo("/home/checkstyle/configs");
    assertWithMessage(message).that(expander.resolve("checkstyle.suppressions.file")).isEqualTo("/home/checkstyle/configs/suppressions.xml");
    assertWithMessage(message).that(expander.resolve("checkstyle.dir")).isEqualTo("/home/checkstyle");
    assertWithMessage(message).that(expander.resolve("str")).isEqualTo("value");
}
Also used : PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) Properties(java.util.Properties) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 7 with PropertiesExpander

use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project mapstruct by mapstruct.

the class CompilingExtension 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), ConfigurationLoader.IgnoredModulesOptions.OMIT));
        ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
        checker.addListener(new DefaultLogger(NullOutputStream.NULL_OUTPUT_STREAM, AutomaticBean.OutputStreamOptions.CLOSE, errorStream, AutomaticBean.OutputStreamOptions.CLOSE));
        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 8 with PropertiesExpander

use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project checkstyle by checkstyle.

the class XdocsPagesTest method isValidCheckstyleXml.

private static boolean isValidCheckstyleXml(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("<suppress-xpath ") && !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, IgnoredModulesOptions.EXECUTE);
            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);
        }
    }
    return true;
}
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 9 with PropertiesExpander

use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project checkstyle by checkstyle.

the class CommonUtilTest method testGetUriByFilenameFindsRelativeResourceOnClasspathPrefix.

@Test
public void testGetUriByFilenameFindsRelativeResourceOnClasspathPrefix() throws Exception {
    final String filename = CommonUtil.CLASSPATH_URL_PROTOCOL + 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 10 with PropertiesExpander

use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project checkstyle by checkstyle.

the class CommonUtilTest method testGetUriByFilenameFindsAbsoluteResourceOnClasspath.

@Test
public void testGetUriByFilenameFindsAbsoluteResourceOnClasspath() 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)

Aggregations

PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)11 Properties (java.util.Properties)11 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)8 Checker (com.puppycrawl.tools.checkstyle.Checker)5 File (java.io.File)5 Test (org.junit.jupiter.api.Test)5 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)4 URI (java.net.URI)4 InputSource (org.xml.sax.InputSource)4 DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 StringReader (java.io.StringReader)2 ConfigurationLoader (com.puppycrawl.tools.checkstyle.ConfigurationLoader)1 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)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 Method (java.lang.reflect.Method)1 URLClassLoader (java.net.URLClassLoader)1