FileOpUtils { public static void retainTempDirs(Set<File> retain, String base, FileOp mFop) { for (int i = 0; i < 100; i++) { File dir = getTempDir(base, i); if (mFop.exists(dir) && !retain.contains(dir)) { mFop.deleteFileOrFolder(dir); } } } private  FileOpUtils(); @NonNull static FileOp create(); static void recursiveCopy(@NonNull File src, @NonNull File dest, @NonNull FileOp fop,
            @NonNull ProgressIndicator progress); static void safeRecursiveOverwrite(@NonNull File src, @NonNull File dest,
            @NonNull FileOp fop, @NonNull ProgressIndicator progress); @Nullable static File getNewTempDir(@NonNull String base, @NonNull FileOp fileOp); @NonNull @VisibleForTesting static File getTempDir(@NonNull String base, int i); @NonNull static File append(@NonNull File base, @NonNull String... segments); @NonNull static File append(@NonNull String base, @NonNull String... segments); @NonNull static String makeRelative(@NonNull File baseDir, @NonNull File toBeRelative, FileOp fop); static void retainTempDirs(Set<File> retain, String base, FileOp mFop);  }
@Test public void retainTempDirs() throws Exception { MockFileOp fop = new MockFileOp(); Set<File> retain = new HashSet<>(); for (int i = 0; i < 10; i++) { File f = FileOpUtils.getNewTempDir("Test", fop); if (i % 2 == 0) { retain.add(f); } } Set<String> desiredResult = retain.stream() .map(File::getPath) .collect(Collectors.toSet()); for (int i = 0; i < 10; i++) { desiredResult.add(FileOpUtils.getNewTempDir("OtherTest", fop).getPath()); } File root = new File(System.getProperty("java.io.tmpdir")); while (root != null) { desiredResult.add(root.getPath()); root = root.getParentFile(); } FileOpUtils.retainTempDirs(retain, "Test", fop); desiredResult = desiredResult.stream().map(fop::getAgnosticAbsPath).collect(Collectors.toSet()); assertEquals(desiredResult, Sets.newHashSet(fop.getExistingFolders())); }