Os { public static File newTempDir(String prefix) { String sanitizedPrefix = (prefix==null ? "" : Strings.makeValidFilename(prefix) + "-"); String tmpParent = tmp(); for (int i = 0; i < TEMP_DIR_ATTEMPTS; i++) { String baseName = sanitizedPrefix + Identifiers.makeRandomId(4); File tempDir = new File(tmpParent, baseName); if (!tempDir.exists()) { if (tempDir.mkdir()) { Os.deleteOnExitRecursively(tempDir); return tempDir; } else { log.warn("Attempt to create temp dir failed " + tempDir + ". Either an IO error (disk full, no rights) or someone else created the folder after the !exists() check."); } } else { log.debug("Attempt to create temp dir failed, already exists " + tempDir + ". With ID of length 4 it is not unusual (15% chance) to have duplicate names at the 2000 samples mark."); } } throw new IllegalStateException("cannot create temporary folders in parent " + tmpParent + " after " + TEMP_DIR_ATTEMPTS + " attempts."); }  static String tmp(); static String user(); static String home(); static String mergePathsUnix(String ...items); static String mergePaths(String ...items); @Beta static DeletionResult deleteRecursively(File dir); @Beta static DeletionResult deleteRecursively(File dir, boolean skipSafetyChecks); @Beta static DeletionResult deleteRecursively(String dir); static void deleteOnExit(File directoryToDeleteIfEmptyOrFile); static void deleteOnExitRecursively(File directoryToCleanOrFile); static int deleteOnExitEmptyParentsUpTo(File subsubDirOrFile, File dir); static void deleteOnExitRecursivelyAndEmptyParentsUpTo(File directoryToCleanOrFile, File highestAncestorToDelete); static File mkdirs(File dir); static File writeToTempFile(InputStream is, String prefix, String suffix); static File writeToTempFile(InputStream is, File tempDir, String prefix, String suffix); static File writePropertiesToTempFile(Properties props, String prefix, String suffix); static File writePropertiesToTempFile(Properties props, File tempDir, String prefix, String suffix); static String tidyPath(String path); static boolean isAbsolutish(String path); static String fromHome(String path); static String nativePath(String path); static boolean isMicrosoftWindows(); static File newTempFile(String prefix, String ext); static File newTempFile(Class<?> clazz, String ext); static File newTempDir(String prefix); static File newTempDir(Class<?> clazz); static final String LINE_SEPARATOR; }
@Test(groups="Integration") public void testNewTempDir() { int CREATE_CNT = 5000; Collection<File> folders = new ArrayList<File>(CREATE_CNT); try { for (int i = 0; i < CREATE_CNT; i++) { try { folders.add(Os.newTempDir(OsTest.class)); } catch (IllegalStateException e) { log.warn("testNewTempDir failed at " + i + " iteration."); Exceptions.propagate(e); } } } finally { for (File folder : folders) { folder.delete(); } } }