@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 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 testEmptyConfiguration() throws Exception { final DefaultConfiguration config = (DefaultConfiguration) loadConfiguration("InputConfigurationLoaderEmpty.xml"); verifyConfigNode(config, "Checker", 0, new Properties()); }
private Configuration loadConfiguration( String name, Properties props) throws Exception { final String fName = getPath(name); return ConfigurationLoader.loadConfiguration(fName, new PropertiesExpander(props)); }
private Configuration loadConfiguration(String name) throws Exception { return loadConfiguration(name, new Properties()); }
@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); }
@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 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 testEmptyModuleResolver() throws Exception { final DefaultConfiguration config = (DefaultConfiguration) loadConfiguration( "InputConfigurationLoaderEmpty.xml", new Properties()); verifyConfigNode(config, "Checker", 0, new Properties()); }
@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 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 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 testNonExistentPropertyName() throws Exception { try { loadConfiguration("InputConfigurationLoaderNonexistentProperty.xml"); fail("exception in expected"); } catch (CheckstyleException ex) { assertEquals("Invalid exception message", "unable to parse configuration stream", ex.getMessage()); assertSame("Expected cause of type SAXException", SAXException.class, ex.getCause().getClass()); assertSame("Expected cause of type CheckstyleException", CheckstyleException.class, ex.getCause().getCause().getClass()); assertEquals("Invalid exception cause message", "Property ${nonexistent} has not been set", ex.getCause().getCause().getMessage()); } }
@Test public void testExternalEntity() throws Exception { final Properties props = new Properties(); props.setProperty("checkstyle.basedir", "basedir"); final DefaultConfiguration config = (DefaultConfiguration) loadConfiguration( "InputConfigurationLoaderExternalEntity.xml", props); final Properties atts = new Properties(); atts.setProperty("tabWidth", "4"); atts.setProperty("basedir", "basedir"); verifyConfigNode(config, "Checker", 2, atts); }
@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 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 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 testMissingPropertyName() throws Exception { try { loadConfiguration("InputConfigurationLoaderMissingPropertyName.xml"); 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 testExternalEntitySubdirectory() throws Exception { final Properties props = new Properties(); props.setProperty("checkstyle.basedir", "basedir"); final DefaultConfiguration config = (DefaultConfiguration) loadConfiguration( "subdir/InputConfigurationLoaderExternalEntitySubDir.xml", props); final Properties attributes = new Properties(); attributes.setProperty("tabWidth", "4"); attributes.setProperty("basedir", "basedir"); verifyConfigNode(config, "Checker", 2, attributes); }
@Test public void testReplacePropertiesReplace() throws Exception { final String[][] testValues = { {"${a}", "A"}, {"x${a}", "xA"}, {"${a}x", "Ax"}, {"${a}${b}", "AB"}, {"x${a}${b}", "xAB"}, {"${a}x${b}", "AxB"}, {"${a}${b}x", "ABx"}, {"x${a}y${b}", "xAyB"}, {"${a}x${b}y", "AxBy"}, {"x${a}${b}y", "xABy"}, {"x${a}y${b}z", "xAyBz"}, {"$$", "$"}, }; final Properties props = initProperties(); for (String[] testValue : testValues) { final String value = (String) getReplacePropertiesMethod().invoke( null, testValue[0], new PropertiesExpander(props), null); assertEquals("\"" + testValue[0] + "\"", testValue[1], value); } }