/** * 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; }
/** * 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; }
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)); }
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)); }
treewalkerConfig.addChild(holderConfig); treewalkerConfig.addChild(memberNameCheckConfig); treewalkerConfig.addChild(constantNameCheckConfig); treewalkerConfig.addChild(createModuleConfig(ParameterNumberCheck.class)); treewalkerConfig.addChild(createModuleConfig(IllegalCatchCheck.class)); treewalkerConfig.addChild(uncommentedMainCheckConfig); treewalkerConfig.addChild(createModuleConfig(JavadocTypeCheck.class)); checkerConfig.addChild(moduleConfig);
@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")); }
@Test public void testClearLazyLoadCacheInDetailAST() throws Exception { final DefaultConfiguration checkConfig1 = createModuleConfig(CheckWhichDoesNotRequireCommentNodes.class); final DefaultConfiguration checkConfig2 = createModuleConfig(CheckWhichRequiresCommentNodes.class); final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class); treeWalkerConfig.addChild(checkConfig1); treeWalkerConfig.addChild(checkConfig2); final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig); final String filePath = getPath("InputCheckerClearDetailAstLazyLoadCache.java"); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkerConfig, filePath, expected); }
/** * It is OK to have long test method name here as it describes the test purpose. */ @Test public void testCacheAndCheckWhichDoesNotImplementExternalResourceHolderInterface() throws Exception { assertFalse("ExternalResourceHolder has changed his parent", ExternalResourceHolder.class.isAssignableFrom(HiddenFieldCheck.class)); final DefaultConfiguration checkConfig = createModuleConfig(HiddenFieldCheck.class); final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class); treeWalkerConfig.addChild(checkConfig); final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig); checkerConfig.addAttribute("charset", StandardCharsets.UTF_8.name()); final File cacheFile = temporaryFolder.newFile(); checkerConfig.addAttribute("cacheFile", cacheFile.getPath()); final File tmpFile = temporaryFolder.newFile("file.java"); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkerConfig, tmpFile.getPath(), expected); final Properties cacheAfterFirstRun = new Properties(); cacheAfterFirstRun.load(Files.newBufferedReader(cacheFile.toPath())); // one more time to reuse cache verify(checkerConfig, tmpFile.getPath(), expected); final Properties cacheAfterSecondRun = new Properties(); cacheAfterSecondRun.load(Files.newBufferedReader(cacheFile.toPath())); assertEquals("Cache from first run differs from second run cache", cacheAfterFirstRun, cacheAfterSecondRun); }
@Test public void testExternalResourceFiltersWithNoExternalResource() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(EmptyStatementCheck.class); final DefaultConfiguration filterConfig = createModuleConfig(SuppressWithNearbyCommentFilter.class); final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class); treeWalkerConfig.addChild(checkConfig); treeWalkerConfig.addChild(filterConfig); final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig); final File cacheFile = temporaryFolder.newFile(); checkerConfig.addAttribute("cacheFile", cacheFile.getPath()); final String filePath = temporaryFolder.newFile("file.java").getPath(); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkerConfig, filePath, expected); }
@Test public void testAll() throws Exception { final Configuration checkConfig = createModuleConfig(SuppressWarningsHolder.class); final DefaultConfiguration treeWalker = createModuleConfig(TreeWalker.class); final Configuration filter = createModuleConfig(SuppressWarningsFilter.class); final DefaultConfiguration violationCheck = createModuleConfig(TypecastParenPadCheck.class); violationCheck.addAttribute("option", "space"); treeWalker.addChild(checkConfig); treeWalker.addChild(violationCheck); final DefaultConfiguration root = createRootConfig(treeWalker); root.addChild(filter); final String[] expected = { "8:72: " + getCheckMessage(TypecastParenPadCheck.class, AbstractParenPadCheck.MSG_WS_NOT_PRECEDED, ")"), }; verify(root, getPath("InputSuppressWarningsHolder6.java"), expected); }
@Test public void testCacheOnViolationSuppression() throws Exception { final File cacheFile = temporaryFolder.newFile(); final DefaultConfiguration violationCheck = createModuleConfig(DummyFileSetViolationCheck.class); final DefaultConfiguration filterConfig = createModuleConfig(SuppressionFilter.class); filterConfig.addAttribute("file", getPath("InputCheckerSuppressAll.xml")); final DefaultConfiguration checkerConfig = createRootConfig(violationCheck); checkerConfig.addAttribute("cacheFile", cacheFile.getPath()); checkerConfig.addChild(filterConfig); final String fileViolationPath = temporaryFolder.newFile("ViolationFile.java").getPath(); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkerConfig, fileViolationPath, expected); try (InputStream input = Files.newInputStream(cacheFile.toPath())) { final Properties details = new Properties(); details.load(input); assertNotNull("suppressed violation file saved in cache", details.getProperty(fileViolationPath)); } }
@Test public void testHaltOnExceptionOff() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(CheckWhichThrowsError.class); final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class); treeWalkerConfig.addChild(checkConfig); final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig); checkerConfig.addChild(treeWalkerConfig); checkerConfig.addAttribute("haltOnException", "false"); final String filePath = getPath("InputChecker.java"); final String[] expected = { "1: " + getCheckMessage(EXCEPTION_MSG, "java.lang.IndexOutOfBoundsException: test"), }; verify(checkerConfig, filePath, expected); }
@Test public void testRejectBadFile() throws Exception { final DefaultConfiguration filterConfig = createModuleConfig(BeforeExecutionExclusionFileFilter.class); filterConfig.addAttribute("fileNamePattern", "IncorrectClass\\.java"); final DefaultConfiguration checkConfig = createModuleConfig(FinalLocalVariableCheck.class); final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class); treeWalkerConfig.addChild(checkConfig); final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig); checkerConfig.addChild(filterConfig); final String[] violations = CommonUtil.EMPTY_STRING_ARRAY; verify(checkerConfig, getNonCompilablePath("InputBeforeExecutionExclusionFileFilterIncorrectClass.java"), violations); }
@Test public void testCacheWhenFileExternalResourceContentDoesNotChange() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class); checkConfig.addAttribute("file", getPath("InputImportControlOneRegExp.xml")); final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class); treeWalkerConfig.addChild(checkConfig); final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig); final File cacheFile = temporaryFolder.newFile(); checkerConfig.addAttribute("cacheFile", cacheFile.getPath()); final String filePath = temporaryFolder.newFile("EmptyFile.java").getPath(); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkerConfig, filePath, expected); // One more time to use cache. verify(checkerConfig, filePath, expected); assertTrue("External resource is not present in cache", new String(Files.readAllBytes(cacheFile.toPath()), StandardCharsets.UTF_8).contains("InputImportControlOneRegExp.xml")); }
@Test public void testBehaviourWithChecksAndFilters() throws Exception { final DefaultConfiguration filterConfig = createModuleConfig(SuppressionCommentFilter.class); filterConfig.addAttribute("checkCPP", "false"); final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class); treeWalkerConfig.addChild(createModuleConfig(MemberNameCheck.class)); treeWalkerConfig.addChild(filterConfig); final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig); final File file = new File(getPath("InputTreeWalkerSuppressionCommentFilter.java")); final String[] expected = { "9:17: " + getCheckMessage(MemberNameCheck.class, "name.invalidPattern", "P", "^[a-z][a-zA-Z0-9]*$"), "4:17: " + getCheckMessage(MemberNameCheck.class, "name.invalidPattern", "I", "^[a-z][a-zA-Z0-9]*$"), }; verify(checkerConfig, file.getPath(), expected); }
@Test public void testCacheWhenFileExternalResourceContentDoesNotChange() throws Exception { final DefaultConfiguration filterConfig = createModuleConfig(SuppressionXpathFilter.class); filterConfig.addAttribute("file", getPath("InputTreeWalkerSuppressionXpathFilter.xml")); final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class); treeWalkerConfig.addChild(filterConfig); final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig); final File cacheFile = temporaryFolder.newFile(); checkerConfig.addAttribute("cacheFile", cacheFile.getPath()); final String filePath = temporaryFolder.newFile("file.java").getPath(); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkerConfig, filePath, expected); // One more time to use cache. verify(checkerConfig, filePath, expected); assertTrue("External resource is not present in cache", new String(Files.readAllBytes(cacheFile.toPath()), StandardCharsets.UTF_8).contains( "InputTreeWalkerSuppressionXpathFilter.xml")); }
@Test public void testClearCache() throws Exception { final DefaultConfiguration violationCheck = createModuleConfig(DummyFileSetViolationCheck.class); final DefaultConfiguration checkerConfig = new DefaultConfiguration("myConfig"); checkerConfig.addAttribute("charset", "UTF-8"); final File cacheFile = temporaryFolder.newFile(); checkerConfig.addAttribute("cacheFile", cacheFile.getPath()); checkerConfig.addChild(violationCheck); final Checker checker = new Checker(); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(checkerConfig); checker.addListener(getBriefUtLogger()); checker.process(Collections.singletonList(new File("dummy.java"))); checker.clearCache(); // invoke destroy to persist cache final PropertyCacheFile cache = Whitebox.getInternalState(checker, "cacheFile"); cache.persist(); final Properties cacheAfterClear = new Properties(); cacheAfterClear.load(Files.newBufferedReader(cacheFile.toPath())); assertEquals("Cache has unexpected size", 1, cacheAfterClear.size()); }
@Test public void testTreeWalkerFilterAbsolutePath() throws Exception { final DefaultConfiguration filterConfig = createModuleConfig(SuppressionXpathFilter.class); filterConfig.addAttribute("file", getPath("InputTreeWalkerSuppressionXpathFilterAbsolute.xml")); final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class); final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class); treeWalkerConfig.addChild(filterConfig); treeWalkerConfig.addChild(checkConfig); final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig); // test is only valid when relative paths are given final String filePath = "src/test/resources/" + getPackageLocation() + "/InputTreeWalkerSuppressionXpathFilterAbsolute.java"; final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkerConfig, filePath, expected); }
@Test public void testFileNameNoExtension() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class); checkConfig.addAttribute("file", getResourcePath("InputImportControlFileNameNoExtension.xml")); final DefaultConfiguration treewalkerConfig = createModuleConfig(TreeWalker.class); treewalkerConfig.addAttribute("fileExtensions", ""); treewalkerConfig.addChild(checkConfig); final String[] expected = { "3:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Image"), }; verify(treewalkerConfig, getPath("InputImportControlFileNameNoExtension"), expected); }
@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); }