@Override public int process(List<File> files) throws CheckstyleException { if (cacheFile != null) { cacheFile.putExternalResources(getExternalResourceLocations()); } // Prepare to start fireAuditStarted(); for (final FileSetCheck fsc : fileSetChecks) { fsc.beginProcessing(charset); } final List<File> targetFiles = files.stream() .filter(file -> CommonUtil.matchesFileExtension(file, fileExtensions)) .collect(Collectors.toList()); processFiles(targetFiles); // Finish up // It may also log!!! fileSetChecks.forEach(FileSetCheck::finishProcessing); // It may also log!!! fileSetChecks.forEach(FileSetCheck::destroy); final int errorCount = counter.getCount(); fireAuditFinished(); return errorCount; }
@Test public void testExternalResourceIsSavedInCache() throws Exception { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); cache.load(); final Set<String> resources = new HashSet<>(); final String pathToResource = getPath("InputPropertyCacheFileExternal.properties"); resources.add(pathToResource); cache.putExternalResources(resources); final MessageDigest digest = MessageDigest.getInstance("SHA-1"); final URI uri = CommonUtil.getUriByFilename(pathToResource); final byte[] input = ByteStreams.toByteArray(new BufferedInputStream(uri.toURL().openStream())); final ByteArrayOutputStream out = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(out)) { oos.writeObject(input); } digest.update(out.toByteArray()); final String expected = BaseEncoding.base16().upperCase().encode(digest.digest()); assertEquals("Hashes are not equal", expected, cache.get("module-resource*?:" + pathToResource)); }
@Test public void testCacheRemainsWhenExternalResourceTheSame() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String externalResourcePath = temporaryFolder.newFile().getPath(); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); // pre-populate with cache with resources cache.load(); final Set<String> resources = new HashSet<>(); resources.add(externalResourcePath); cache.putExternalResources(resources); cache.persist(); // test cache with same resources and new file cache.load(); cache.put("myFile", 1); cache.putExternalResources(resources); assertTrue("Should return true in file is in cache", cache.isInCache("myFile", 1)); }
final String externalResourceFileName = "non_existent_file.xml"; nonExistentExternalResources.add(externalResourceFileName); cache.putExternalResources(nonExistentExternalResources);
@Test public void testConfigHashRemainsOnResetExternalResources() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); // create cache with one file cache.load(); cache.put("myFile", 1); final String hash = cache.get(PropertyCacheFile.CONFIG_HASH_KEY); assertNotNull("Config hash key should not be null", hash); // apply new external resource to clear cache final Set<String> resources = new HashSet<>(); resources.add("dummy"); cache.putExternalResources(resources); assertEquals("Invalid config hash key", hash, cache.get(PropertyCacheFile.CONFIG_HASH_KEY)); assertFalse("Should return false in file is not in cache", cache.isInCache("myFile", 1)); }
final String externalResourceFileName = "non_existent_file.xml"; nonExistentExternalResources.add(externalResourceFileName); cache.putExternalResources(nonExistentExternalResources);
/** * This SuppressWarning("unchecked") required to suppress * "Unchecked generics array creation for varargs parameter" during mock. * @throws IOException when smth wrong with file creation or cache.load */ @Test public void testNonExistentResource() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); // create cache with one file cache.load(); final String myFile = "myFile"; cache.put(myFile, 1); final String hash = cache.get(PropertyCacheFile.CONFIG_HASH_KEY); assertNotNull("Config hash key should not be null", hash); mockStatic(ByteStreams.class); when(ByteStreams.toByteArray(any(BufferedInputStream.class))) .thenThrow(IOException.class); // apply new external resource to clear cache final Set<String> resources = new HashSet<>(); final String resource = getPath("InputPropertyCacheFile.header"); resources.add(resource); cache.putExternalResources(resources); assertFalse("Should return false in file is not in cache", cache.isInCache(myFile, 1)); assertFalse("Should return false in file is not in cache", cache.isInCache(resource, 1)); }
@Override public int process(List<File> files) throws CheckstyleException { if (cacheFile != null) { cacheFile.putExternalResources(getExternalResourceLocations()); } // Prepare to start fireAuditStarted(); for (final FileSetCheck fsc : fileSetChecks) { fsc.beginProcessing(charset); } final List<File> targetFiles = files.stream() .filter(file -> CommonUtil.matchesFileExtension(file, fileExtensions)) .collect(Collectors.toList()); processFiles(targetFiles); // Finish up // It may also log!!! fileSetChecks.forEach(FileSetCheck::finishProcessing); // It may also log!!! fileSetChecks.forEach(FileSetCheck::destroy); final int errorCount = counter.getCount(); fireAuditFinished(); return errorCount; }