@Test public void testWithCacheWithNoViolation() throws Exception { final Checker checker = new Checker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); checker.setModuleFactory(factory); checker.configure(createModuleConfig(TranslationCheck.class)); final File cacheFile = temporaryFolder.newFile(); checker.setCacheFile(cacheFile.getPath()); checker.setupChild(createModuleConfig(TranslationCheck.class)); final File tmpFile = temporaryFolder.newFile("file.java"); final List<File> files = new ArrayList<>(1); files.add(tmpFile); checker.process(files); // invoke destroy to persist cache checker.destroy(); final Properties cache = new Properties(); cache.load(Files.newBufferedReader(cacheFile.toPath())); // There should 2 objects in cache: processed file (file.java) and checker configuration. final int expectedNumberOfObjectsInCache = 2; assertEquals("Cache has unexpected size", expectedNumberOfObjectsInCache, cache.size()); final String expectedConfigHash = "B8535A811CA90BE8B7A14D40BCA62B4FC2447B46"; assertEquals("Cache has unexpected hash", expectedConfigHash, cache.getProperty(PropertyCacheFile.CONFIG_HASH_KEY)); assertNotNull("Cache file has null path", cache.getProperty(tmpFile.getPath())); }
@Test public void testDestroyCheckerWithWrongCacheFileNameLength() throws Exception { final Checker checker = new Checker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); checker.setModuleFactory(factory); checker.configure(new DefaultConfiguration("default config")); // We set wrong file name length in order to reproduce IOException on OS Linux, OS Windows. // The maximum file name length which is allowed in most UNIX, Windows file systems is 255. // See https://en.wikipedia.org/wiki/Filename; checker.setCacheFile(String.format(Locale.ENGLISH, "%0300d", 0)); try { checker.destroy(); fail("Exception did not happen"); } catch (IllegalStateException ex) { assertTrue("Cause of exception differs from IOException", ex.getCause() instanceof IOException); } }
final String[] fileExtensions = {"java", "xml", "properties"}; checker.setFileExtensions(fileExtensions); checker.setCacheFile(temporaryFolder.newFile().getPath()); final int counter = checker.process(files);
@Test public void testIgnoredFileExtensions() throws Exception { final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration"); checkerConfig.addAttribute("charset", StandardCharsets.UTF_8.name()); checkerConfig.addAttribute("cacheFile", temporaryFolder.newFile().getPath()); final Checker checker = new Checker(); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(checkerConfig); final DebugAuditAdapter auditAdapter = new DebugAuditAdapter(); checker.addListener(auditAdapter); final List<File> allIgnoredFiles = new ArrayList<>(); final File ignoredFile = new File("file.pdf"); allIgnoredFiles.add(ignoredFile); final String[] fileExtensions = {"java", "xml", "properties"}; checker.setFileExtensions(fileExtensions); checker.setCacheFile(temporaryFolder.newFile().getPath()); final int counter = checker.process(allIgnoredFiles); // comparing to 0 as there is no legal file in input final int numLegalFiles = 0; assertEquals("There were more legal files than expected", numLegalFiles, counter); assertEquals("Audit was started on larger amount of files than expected", numLegalFiles, auditAdapter.getNumFilesStarted()); assertEquals("Audit was finished on larger amount of files than expected", numLegalFiles, auditAdapter.getNumFilesFinished()); }