/** * Returns {@code TreeWalker} module configuration. * @param config The configuration object. * @return The {@code TreeWalker} module configuration. */ private static Configuration getTreeWalkerConfig(Configuration config) { Configuration result = null; final Configuration[] children = config.getChildren(); for (Configuration child : children) { if ("TreeWalker".equals(child.getName())) { result = child; break; } } return result; }
public static Set<Configuration> getModules(Configuration config) { final Set<Configuration> result = new HashSet<>(); for (Configuration child : config.getChildren()) { if ("TreeWalker".equals(child.getName())) { result.addAll(getModules(child)); } else { result.add(child); } } return result; }
public static Set<Configuration> getChecks(Configuration config) { final Set<Configuration> result = new HashSet<>(); for (Configuration child : config.getChildren()) { if ("TreeWalker".equals(child.getName())) { result.addAll(getModules(child)); } } return result; }
@Test public void testConfigWithIgnoreUsingInputSource() throws Exception { final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration(new InputSource( new File(getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml")) .toURI().toString()), new PropertiesExpander(new Properties()), true); final Configuration[] children = config.getChildren(); assertEquals("Invalid children count", 0, children[0].getChildren().length); }
@Test public void testLoadConfigurationDeprecated() throws Exception { final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( Files.newInputStream(Paths.get( getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml"))), new PropertiesExpander(new Properties()), true); final Configuration[] children = config.getChildren(); assertEquals("Invalid children count", 0, children[0].getChildren().length); }
@Test public void testCustomMessages() throws Exception { final Properties props = new Properties(); props.setProperty("checkstyle.basedir", "basedir"); final DefaultConfiguration config = (DefaultConfiguration) loadConfiguration( "InputConfigurationLoaderCustomMessages.xml", props); final Configuration[] children = config.getChildren(); final Configuration[] grandchildren = children[0].getChildren(); final String expectedKey = "name.invalidPattern"; assertTrue("Messages should contain key: " + expectedKey, grandchildren[0].getMessages() .containsKey(expectedKey)); }
@Test public void testConfigWithIgnore() throws Exception { final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml"), new PropertiesExpander(new Properties()), true); final Configuration[] children = config.getChildren(); assertEquals("Invalid children count", 0, children[0].getChildren().length); }
@Test public void testLoadConfigurationFromClassPath() throws Exception { final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml"), new PropertiesExpander(new Properties()), true); final Configuration[] children = config.getChildren(); assertEquals("Invalid children count", 0, children[0].getChildren().length); }
/** * Implements the Configurable interface using bean introspection. * * <p>Subclasses are allowed to add behaviour. After the bean * based setup has completed first the method * {@link #finishLocalSetup finishLocalSetup} * is called to allow completion of the bean's local setup, * after that the method {@link #setupChild setupChild} * is called for each {@link Configuration#getChildren child Configuration} * of {@code configuration}. * * @see Configurable */ @Override public final void configure(Configuration config) throws CheckstyleException { configuration = config; final String[] attributes = config.getAttributeNames(); for (final String key : attributes) { final String value = config.getAttribute(key); tryCopyProperty(config.getName(), key, value, true); } finishLocalSetup(); final Configuration[] childConfigs = config.getChildren(); for (final Configuration childConfig : childConfigs) { setupChild(childConfig); } }
@Test public void testConstructors() throws Exception { final Properties props = new Properties(); props.setProperty("checkstyle.basedir", "basedir"); final String fName = getPath("InputConfigurationLoaderChecks.xml"); final Configuration configuration = ConfigurationLoader.loadConfiguration(fName, new PropertiesExpander(props), ConfigurationLoader.IgnoredModulesOptions.OMIT); assertEquals("Name is not expected", "Checker", configuration.getName()); final DefaultConfiguration configuration1 = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( new InputSource(Files.newInputStream(Paths.get( getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml")))), new PropertiesExpander(new Properties()), ConfigurationLoader.IgnoredModulesOptions.EXECUTE); final Configuration[] children = configuration1.getChildren(); assertEquals("Unexpected children size", 1, children[0].getChildren().length); }
final Configuration[] grandchildren = children[0].getChildren(); atts.clear(); verifyConfigNode(
@Test public void testLoadConfigurationWrongUrl() { try { final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( ";InputConfigurationLoaderModuleIgnoreSeverity.xml", new PropertiesExpander(new Properties()), true); final Configuration[] children = config.getChildren(); assertEquals("Invalid children count", 0, children[0].getChildren().length); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Invalid exception message", "Unable to find: ;InputConfigurationLoaderModuleIgnoreSeverity.xml", ex.getMessage()); } }
@Test public void testModuleNameInSingleThreadMode() throws Exception { TestRootModuleChecker.reset(); exit.checkAssertionAfterwards(() -> { assertEquals("Unexpected output log", "", systemOut.getLog()); assertEquals("Unexpected system error log", "", systemErr.getLog()); assertTrue("Invalid checker state", TestRootModuleChecker.isProcessed()); final DefaultConfiguration config = (DefaultConfiguration) TestRootModuleChecker.getConfig(); final ThreadModeSettings multiThreadModeSettings = config.getThreadModeSettings(); assertEquals("Invalid checker thread number", 1, multiThreadModeSettings.getCheckerThreadsNumber()); assertEquals("Invalid checker thread number", 1, multiThreadModeSettings.getTreeWalkerThreadsNumber()); final Configuration checkerConfiguration = config .getChildren()[0]; assertEquals("Invalid checker name", "Checker", checkerConfiguration.getName()); final Configuration treeWalkerConfig = checkerConfiguration.getChildren()[0]; assertEquals("Invalid checker children name", "TreeWalker", treeWalkerConfig.getName()); }); Main.main("-C", "1", "-W", "1", "-c", getPath("InputMainConfig-multi-thread-mode.xml"), getPath("InputMain.java")); }
public Collection<FileSetCheck> load(PropertyResolver propertyResolver) { Configuration config = loadConfiguration( getClass().getResourceAsStream("spring-checkstyle.xml"), propertyResolver); return Arrays.stream(config.getChildren()).map(this::load) .collect(Collectors.toList()); }
/** * Returns {@code TreeWalker} module configuration. * @param config The configuration object. * @return The {@code TreeWalker} module configuration. */ private static Configuration getTreeWalkerConfig(Configuration config) { Configuration result = null; final Configuration[] children = config.getChildren(); for (Configuration child : children) { if ("TreeWalker".equals(child.getName())) { result = child; break; } } return result; }
private void defineCharset(com.puppycrawl.tools.checkstyle.api.Configuration configuration) { com.puppycrawl.tools.checkstyle.api.Configuration[] modules = configuration.getChildren(); for (com.puppycrawl.tools.checkstyle.api.Configuration module : modules) { if (("Checker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(module.getName())) && (module instanceof DefaultConfiguration)) { Charset charset = getCharset(); LOG.info("Checkstyle charset: " + charset.name()); ((DefaultConfiguration) module).addAttribute("charset", charset.name()); } } }
private void defineCharset(com.puppycrawl.tools.checkstyle.api.Configuration configuration) { com.puppycrawl.tools.checkstyle.api.Configuration[] modules = configuration.getChildren(); for (com.puppycrawl.tools.checkstyle.api.Configuration module : modules) { if (("Checker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(module.getName())) && module instanceof DefaultConfiguration) { Charset charset = getCharset(); LOG.info("Checkstyle charset: " + charset.name()); ((DefaultConfiguration) module).addAttribute("charset", charset.name()); } } }
private void defineCharset(Configuration configuration) { defineModuleCharset(configuration); for (Configuration module : configuration.getChildren()) { defineModuleCharset(module); } }
private void sortConfiguration( List<ConfReference> result, Configuration config, ChainedItem<Configuration> parent, CheckstyleResults results ) for ( Configuration childConfig : config.getChildren() )
/** * Implements the Configurable interface using bean introspection. * * <p>Subclasses are allowed to add behaviour. After the bean * based setup has completed first the method * {@link #finishLocalSetup finishLocalSetup} * is called to allow completion of the bean's local setup, * after that the method {@link #setupChild setupChild} * is called for each {@link Configuration#getChildren child Configuration} * of {@code configuration}. * * @see Configurable */ @Override public final void configure(Configuration config) throws CheckstyleException { configuration = config; final String[] attributes = config.getAttributeNames(); for (final String key : attributes) { final String value = config.getAttribute(key); tryCopyProperty(config.getName(), key, value, true); } finishLocalSetup(); final Configuration[] childConfigs = config.getChildren(); for (final Configuration childConfig : childConfigs) { setupChild(childConfig); } }