protected static DefaultConfiguration createModuleConfig(Class<?> clazz) { return new DefaultConfiguration(clazz.getName()); }
protected static DefaultConfiguration createModuleConfig(Class<?> clazz) { return new DefaultConfiguration(clazz.getName()); }
/** * 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; }
@Before public void setUp() throws Exception { final DefaultConfiguration checkConfig = new DefaultConfiguration( JavaDocCapture.class.getName()); checker = createChecker(checkConfig); }
@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 testCacheDirectoryDoesNotExistAndShouldBeCreated() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String filePath = String.format(Locale.getDefault(), "%s%2$stemp%2$scache.temp", temporaryFolder.getRoot(), File.separator); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); // no exception expected, cache directory should be created cache.persist(); assertTrue("cache exists in directory", new File(filePath).exists()); }
@Test public void testSetupChildListener() throws Exception { final Checker checker = new Checker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); checker.setModuleFactory(factory); final Configuration config = new DefaultConfiguration( DebugAuditAdapter.class.getCanonicalName()); checker.setupChild(config); final List<AuditListener> listeners = Whitebox.getInternalState(checker, "listeners"); assertTrue("Invalid child listener class", listeners.get(listeners.size() - 1) instanceof DebugAuditAdapter); }
@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()); }
@Test public void testConfigure() throws CheckstyleException { filter.configure(new DefaultConfiguration("test")); assertNotNull("object exists", filter); }
@Test public void testGetName() { final DefaultConfiguration config = new DefaultConfiguration("MyConfig"); assertEquals("Invalid configuration name", "MyConfig", config.getName()); }
@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 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); }
@Test public void testMultiThreadConfiguration() { final String name = "MyConfig"; final ThreadModeSettings multiThreadMode = new ThreadModeSettings(4, 2); final DefaultConfiguration config = new DefaultConfiguration(name, multiThreadMode); assertEquals("Invalid thread mode", multiThreadMode, config.getThreadModeSettings()); }
@Test public void testProcessNonJavaFilesWithoutException() throws Exception { final TreeWalker treeWalker = new TreeWalker(); treeWalker.setTabWidth(1); treeWalker.configure(new DefaultConfiguration("default config")); final File file = new File(getPath("InputTreeWalkerNotJava.xml")); final FileText fileText = new FileText(file, StandardCharsets.ISO_8859_1.name()); treeWalker.processFiltered(file, fileText); final Collection<Checks> checks = Whitebox.getInternalState(treeWalker, "ordinaryChecks"); assertTrue("No checks -> No parsing", checks.isEmpty()); }
@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); }
@Test public void testResetIfFileDoesNotExist() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final PropertyCacheFile cache = new PropertyCacheFile(config, "fileDoesNotExist.txt"); cache.load(); assertNotNull("Config hash key should not be null", cache.get(PropertyCacheFile.CONFIG_HASH_KEY)); }
@Test public void testConfigHashOnReset() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); cache.load(); final String hash = cache.get(PropertyCacheFile.CONFIG_HASH_KEY); assertNotNull("Config hash key should not be null", hash); cache.reset(); assertEquals("Invalid config hash key", hash, cache.get(PropertyCacheFile.CONFIG_HASH_KEY)); }
@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 testSettersForParameters() throws Exception { final TreeWalker treeWalker = new TreeWalker(); final DefaultConfiguration config = new DefaultConfiguration("default config"); treeWalker.setTabWidth(1); treeWalker.configure(config); treeWalker.setCacheFile(temporaryFolder.newFile().getPath()); assertEquals("Invalid setter result", 1, (int) Whitebox.getInternalState(treeWalker, "tabWidth")); assertEquals("Invalid configuration", config, Whitebox.getInternalState(treeWalker, "configuration")); }
@Test public void testInCache() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); cache.put("myFile", 1); assertTrue("Should return true when file is in cache", cache.isInCache("myFile", 1)); assertFalse("Should return false when file is not in cache", cache.isInCache("myFile", 2)); assertFalse("Should return false when file is not in cache", cache.isInCache("myFile1", 1)); }