/** * Deletes all files associated with given {@link StorageData}, but only of a types supplied * with a fileTypes parameter. * * @param storageData * Storage to delete data for. * @param fileTypes * File types to delete. * @throws IOException * If {@link IOException} happens. */ protected void deleteStorageDataFromDisk(IStorageData storageData, StorageFileType... fileTypes) throws IOException { Path storageDir = getStoragePath(storageData); if (log.isDebugEnabled()) { log.info("Deleting the storage data from disk. Path: " + storageDir + ". File types to delete: " + ArrayUtils.toString(fileTypes)); } if (Files.exists(storageDir)) { Files.walkFileTree(storageDir, new StorageDeleteFileVisitor(fileTypes, false)); } }
/** * Test that the deleting of the storage files works with visitor. */ @Test public void storageDeleteFileVisitor() throws IOException { StorageFileType storageFileType = StorageFileType.DATA_FILE; // we create additional dir and file in that dir Path dir = testPath.resolve("dir"); Files.createDirectories(dir); Path file = dir.resolve(storageFileType.getExtension()); Files.createFile(file); // first with no dir deletion Files.walkFileTree(dir, new StorageDeleteFileVisitor(new StorageFileType[] { storageFileType }, false)); assertThat(Files.exists(file), is(false)); assertThat(Files.exists(dir), is(true)); assertThat(Files.exists(testPath), is(true)); // then with dir deletion Files.walkFileTree(dir, new StorageDeleteFileVisitor(new StorageFileType[] { storageFileType }, true)); assertThat(Files.exists(dir), is(false)); assertThat(Files.exists(testPath), is(true)); }