private static void verifyConfigNode( DefaultConfiguration config, String name, int childrenLength, Properties atts) throws Exception { assertEquals("name.", name, config.getName()); assertEquals( "children.length.", childrenLength, config.getChildren().length); final String[] attNames = config.getAttributeNames(); assertEquals("attributes.length", atts.size(), attNames.length); for (String attName : attNames) { assertEquals( "attribute[" + attName + "]", atts.getProperty(attName), config.getAttribute(attName)); } }
@Test public void testAddAttributeAndGetAttribute() throws CheckstyleException { final DefaultConfiguration config = new DefaultConfiguration("MyConfig"); config.addAttribute("attribute", "first"); assertEquals("Invalid attribute value", "first", config.getAttribute("attribute")); config.addAttribute("attribute", "second"); assertEquals("Invalid attribute value", "first,second", config.getAttribute("attribute")); }
@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()); } }
@Test public void testExceptionForNonExistentAttribute() { final String name = "MyConfig"; final DefaultConfiguration config = new DefaultConfiguration(name); final String attributeName = "NonExistent#$%"; try { config.getAttribute(attributeName); fail("Exception is expected"); } catch (CheckstyleException expected) { assertEquals("Invalid exception message", "missing key '" + attributeName + "' in " + name, expected.getMessage()); } }
checkConfig.addAttribute("ignoreFormat", "^i.*$"); Assert.assertNotNull("Ignore format should not be null", checkConfig.getAttribute("ignoreFormat")); final String[] expected = { "18:13: " + getCheckMessage(MSG_KEY, "hidden"),
private void addAttributeIfNotExists( DefaultConfiguration config, String name, String value ) { try { // MCHECKSTYLE-132 DefaultConfiguration addAttribute has changed in checkstyle 5.3 if ( config.getAttribute( name ) == null ) { config.addAttribute( name, value ); } } catch ( CheckstyleException ex ) { // MCHECKSTYLE-159 Checkstyle 5.4+ throws an exception when trying to access an attribute that doesn't exist config.addAttribute( name, value ); } }