public static Configuration loadConfiguration(String path, Properties props) throws CheckstyleException { return ConfigurationLoader.loadConfiguration(path, new PropertiesExpander(props)); }
@Test public void testExternalEntityFromUri() throws Exception { final Properties props = new Properties(); props.setProperty("checkstyle.basedir", "basedir"); final File file = new File( getPath("subdir/InputConfigurationLoaderExternalEntitySubDir.xml")); final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( file.toURI().toString(), new PropertiesExpander(props)); final Properties atts = new Properties(); atts.setProperty("tabWidth", "4"); atts.setProperty("basedir", "basedir"); verifyConfigNode(config, "Checker", 2, atts); }
@Test public void testReplacePropertiesDefault() throws Exception { final Properties props = new Properties(); final String defaultValue = "defaultValue"; final String value = (String) getReplacePropertiesMethod().invoke( null, "${checkstyle.basedir}", new PropertiesExpander(props), defaultValue); assertEquals("Invalid property value", defaultValue, value); }
private Configuration loadConfiguration( String name, Properties props) throws Exception { final String fName = getPath(name); return ConfigurationLoader.loadConfiguration(fName, new PropertiesExpander(props)); }
@Test public void testResourceLoadConfiguration() throws Exception { final Properties props = new Properties(); props.setProperty("checkstyle.basedir", "basedir"); // load config that's only found in the classpath final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( getPath("InputConfigurationLoaderChecks.xml"), new PropertiesExpander(props)); //verify the root, and property substitution final Properties attributes = new Properties(); attributes.setProperty("tabWidth", "4"); attributes.setProperty("basedir", "basedir"); verifyConfigNode(config, "Checker", 3, attributes); }
@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 testResourceLoadConfigurationWithSingleThreadConfiguration() throws Exception { final Properties props = new Properties(); props.setProperty("checkstyle.basedir", "basedir"); final PropertiesExpander propertiesExpander = new PropertiesExpander(props); final String configPath = getPath("InputConfigurationLoaderChecks.xml"); final ThreadModeSettings singleThreadModeSettings = ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE; final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( configPath, propertiesExpander, singleThreadModeSettings); final Properties attributes = new Properties(); attributes.setProperty("tabWidth", "4"); attributes.setProperty("basedir", "basedir"); verifyConfigNode(config, "Checker", 3, attributes); }
@Test public void testCtorException() { try { final Object test = new PropertiesExpander(null); Assert.fail("exception expected but got " + test); } catch (IllegalArgumentException ex) { Assert.assertEquals("Invalid exception message", "cannot pass null", ex.getMessage()); } }
@Test public void testDefaultProperties() { final Properties properties = new Properties(System.getProperties()); properties.setProperty("test", "checkstyle"); Assert.assertEquals("Invalid user.home property", System.getProperty("user.home"), properties.getProperty("user.home")); Assert.assertEquals("Invalid checkstyle property", "checkstyle", properties.getProperty("test")); final PropertiesExpander expander = new PropertiesExpander(properties); Assert.assertEquals("Invalid user.home property", System.getProperty("user.home"), expander.resolve("user.home")); Assert.assertEquals("Invalid checkstyle property", "checkstyle", expander.resolve("test")); }
@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 testReplacePropertiesSyntaxError() throws Exception { final Properties props = initProperties(); try { final String value = (String) getReplacePropertiesMethod().invoke( null, "${a", new PropertiesExpander(props), null); fail("expected to fail, instead got: " + value); } catch (InvocationTargetException ex) { assertEquals("Invalid exception cause message", "Syntax error in property: ${a", ex.getCause().getMessage()); } }
@Test public void testReplacePropertiesMissingProperty() throws Exception { final Properties props = initProperties(); try { final String value = (String) getReplacePropertiesMethod().invoke( null, "${c}", new PropertiesExpander(props), null); fail("expected to fail, instead got: " + value); } catch (InvocationTargetException ex) { assertEquals("Invalid exception cause message", "Property ${c} has not been set", ex.getCause().getMessage()); } }
@Test public void testReplacePropertiesNoReplace() throws Exception { final String[] testValues = {null, "", "a", "$a", "{a", "{a}", "a}", "$a}", "$", "a$b", }; final Properties props = initProperties(); for (String testValue : testValues) { final String value = (String) getReplacePropertiesMethod().invoke( null, testValue, new PropertiesExpander(props), null); assertEquals("\"" + testValue + "\"", value, testValue); } }
@Test public void testConfigCheckerWithIgnore() throws Exception { final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( getPath("InputConfigurationLoaderCheckerIgnoreSeverity.xml"), new PropertiesExpander(new Properties()), true); final Configuration[] children = config.getChildren(); assertEquals("Invalid children count", 0, children.length); }
@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); }
@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); }
@Test public void testResourceLoadConfigurationWithMultiThreadConfiguration() throws Exception { final Properties props = new Properties(); props.setProperty("checkstyle.basedir", "basedir"); final PropertiesExpander propertiesExpander = new PropertiesExpander(props); final String configPath = getPath("InputConfigurationLoaderChecks.xml"); final ThreadModeSettings multiThreadModeSettings = new ThreadModeSettings(4, 2); try { ConfigurationLoader.loadConfiguration( configPath, propertiesExpander, multiThreadModeSettings); fail("An exception is expected"); } catch (IllegalArgumentException ex) { assertEquals("Invalid exception message", "Multi thread mode for Checker module is not implemented", ex.getMessage()); } }
@Test public void testMissingPropertyNameInMethodWithBooleanParameter() throws Exception { try { final String fName = getPath("InputConfigurationLoaderMissingPropertyName.xml"); ConfigurationLoader.loadConfiguration(fName, new PropertiesExpander(new Properties()), false); fail("missing property name"); } catch (CheckstyleException ex) { assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().contains("\"name\"")); assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().contains("\"property\"")); assertTrue("Invalid exception message: " + ex.getMessage(), ex.getMessage().endsWith(":8:41")); } }
@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()); } }