@Test public void testNoGapBetweenStaticImports() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(ImportOrderCheck.class); checkConfig.addAttribute("groups", "java, javax, org"); checkConfig.addAttribute("separated", "true"); checkConfig.addAttribute("ordered", "true"); checkConfig.addAttribute("option", "bottom"); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkConfig, getPath("InputImportOrderNoGapBetweenStaticImports.java"), expected); }
/** * Creates {@link DefaultConfiguration} for the {@link TreeWalker} * based on the given {@link Configuration} instance. * @param config {@link Configuration} instance. * @return {@link DefaultConfiguration} for the {@link TreeWalker} * based on the given {@link Configuration} instance. */ protected static DefaultConfiguration createTreeWalkerConfig(Configuration config) { final DefaultConfiguration dc = new DefaultConfiguration("configuration"); final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class); // make sure that the tests always run with this charset dc.addAttribute("charset", StandardCharsets.UTF_8.name()); dc.addChild(twConf); twConf.addChild(config); return dc; }
final String name = threadModeSettings.resolveName(originalName); final DefaultConfiguration conf = new DefaultConfiguration(name, threadModeSettings); final DefaultConfiguration top = configStack.peek(); top.addChild(conf); top.addAttribute(name, value); top.addMessage(key, value);
/** * Creates {@link DefaultConfiguration} for the given {@link Configuration} instance. * @param config {@link Configuration} instance. * @return {@link DefaultConfiguration} for the given {@link Configuration} instance. */ protected static DefaultConfiguration createRootConfig(Configuration config) { final DefaultConfiguration dc = new DefaultConfiguration(ROOT_MODULE_NAME); if (config != null) { dc.addChild(config); } return dc; }
@Test public void testRemoveChild() { final DefaultConfiguration config = new DefaultConfiguration("MyConfig"); final DefaultConfiguration configChild = new DefaultConfiguration("childConfig"); assertEquals("Invalid children count", 0, config.getChildren().length); config.addChild(configChild); assertEquals("Invalid children count", 1, config.getChildren().length); config.removeChild(configChild); assertEquals("Invalid children count", 0, config.getChildren().length); }
private void verifySuppressed(Configuration moduleConfig, String fileName, String[] expectedViolations, String... suppressedViolations) throws Exception { final DefaultConfiguration memberNameCheckConfig = createModuleConfig(MemberNameCheck.class); memberNameCheckConfig.addAttribute("id", "ignore"); final DefaultConfiguration constantNameCheckConfig = createModuleConfig(ConstantNameCheck.class); constantNameCheckConfig.addAttribute("id", null); final DefaultConfiguration treewalkerConfig = createModuleConfig(TreeWalker.class); treewalkerConfig.addChild(memberNameCheckConfig); treewalkerConfig.addChild(constantNameCheckConfig); treewalkerConfig.addChild(createModuleConfig(IllegalCatchCheck.class)); if (moduleConfig != null) { treewalkerConfig.addChild(moduleConfig); } final DefaultConfiguration checkerConfig = createRootConfig(treewalkerConfig); verify(checkerConfig, fileName, removeSuppressed(expectedViolations, suppressedViolations)); }
@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 testGetAttributeNames() { final DefaultConfiguration config = new DefaultConfiguration("MyConfig"); config.addAttribute("attribute", "value"); final String[] actual = config.getAttributeNames(); final String[] expected = {"attribute"}; assertArrayEquals("Invalid attribute names", expected, actual); }
final DefaultConfiguration checkConfig = createModuleConfig(HiddenFieldCheck.class); checkConfig.addAttribute("ignoreFormat", "^i.*$"); Assert.assertNotNull("Ignore format should not be null", checkConfig.getAttribute("ignoreFormat")); final String[] expected = { "18:13: " + getCheckMessage(MSG_KEY, "hidden"),
@Test public void shouldLogActualLineLength() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(LineLengthCheck.class); checkConfig.addAttribute("max", "80"); checkConfig.addAttribute("ignorePattern", "^.*is OK.*regexp.*$"); checkConfig.addMessage("maxLineLen", "{0},{1}"); final String[] expected = { "18: 80,81", "145: 80,83", }; verify(checkConfig, getPath("InputLineLengthSimple.java"), expected); }
@Test public void testAddMessageAndGetMessages() { final DefaultConfiguration config = new DefaultConfiguration("MyConfig"); config.addMessage("key", "value"); final Map<String, String> expected = new TreeMap<>(); expected.put("key", "value"); assertEquals("Invalid message map", expected, config.getMessages()); }
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 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()); } }
@Test public void testPathToCacheContainsOnlyFileName() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String filePath = "temp.cache"; final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); // no exception expected cache.persist(); assertTrue("Cache file does not exist", Files.exists(Paths.get(filePath))); Files.delete(Paths.get(filePath)); }
@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 testCustomMessageWithParametersNegative() throws Exception { final DefaultConfiguration config = createModuleConfig(emptyCheck.getClass()); config.addMessage("msgKey", "This is a custom message {0."); emptyCheck.configure(config); try { emptyCheck.log(1, "msgKey", "TestParam"); fail("exception expected"); } catch (IllegalArgumentException ex) { assertEquals("Error message is unexpected", "Unmatched braces in the pattern.", ex.getMessage()); } }
@Test public void testDefaultMultiThreadConfiguration() { final String name = "MyConfig"; final DefaultConfiguration config = new DefaultConfiguration(name); final ThreadModeSettings singleThreadMode = ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE; assertEquals("Invalid thread mode", singleThreadMode, config.getThreadModeSettings()); }
@Test public void testWithMultipleChecks() throws Exception { final DefaultConfiguration checksConfig = createModuleConfig(TreeWalker.class); checksConfig.addChild(createModuleConfig(AtclauseOrderCheck.class)); checksConfig.addChild(createModuleConfig(JavadocParagraphCheck.class)); final DefaultConfiguration checkerConfig = createRootConfig(checksConfig); verify(checkerConfig, getPath("InputAbstractJavadocCorrectParagraph.java")); }
final DefaultConfiguration parentModule = configStack.peek(); parentModule.removeChild(recentModule);
private void verifySuppressed(Configuration moduleConfig, String fileName, String[] expectedViolations, String... suppressedViolations) throws Exception { final DefaultConfiguration memberNameCheckConfig = createModuleConfig(MemberNameCheck.class); memberNameCheckConfig.addAttribute("id", "ignore"); final DefaultConfiguration constantNameCheckConfig = createModuleConfig(ConstantNameCheck.class); constantNameCheckConfig.addAttribute("id", null); final DefaultConfiguration treewalkerConfig = createModuleConfig(TreeWalker.class); treewalkerConfig.addChild(memberNameCheckConfig); treewalkerConfig.addChild(constantNameCheckConfig); treewalkerConfig.addChild(createModuleConfig(IllegalCatchCheck.class)); if (moduleConfig != null) { treewalkerConfig.addChild(moduleConfig); } final DefaultConfiguration checkerConfig = createRootConfig(treewalkerConfig); verify(checkerConfig, fileName, removeSuppressed(expectedViolations, suppressedViolations)); }