@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); }
private Configuration loadConfiguration( String name, Properties props) throws Exception { final String fName = getPath(name); return ConfigurationLoader.loadConfiguration(fName, new PropertiesExpander(props)); }
@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 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 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 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 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 testConfigWithIgnoreExceptionalAttributes() throws Exception { // emulate exception from unrelated code, but that is same try-catch final DefaultConfiguration tested = PowerMockito.mock(DefaultConfiguration.class); when(tested.getAttributeNames()).thenReturn(new String[] {"severity"}); when(tested.getName()).thenReturn("MemberName"); when(tested.getAttribute("severity")).thenThrow(CheckstyleException.class); // to void creation of 2 other mocks for now reason, only one moc is used for all cases PowerMockito.whenNew(DefaultConfiguration.class) .withArguments("MemberName", ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE) .thenReturn(tested); PowerMockito.whenNew(DefaultConfiguration.class) .withArguments("Checker", ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE) .thenReturn(tested); PowerMockito.whenNew(DefaultConfiguration.class) .withArguments("TreeWalker", ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE) .thenReturn(tested); try { ConfigurationLoader.loadConfiguration( getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml"), new PropertiesExpander(new Properties()), true); fail("Exception is expected"); } catch (CheckstyleException expected) { assertEquals("Invalid exception cause message", "Problem during accessing 'severity' attribute for MemberName", expected.getCause().getMessage()); } }