FileHelper { public static Collection<File> wipeOldFiles(File directory, Long cutoffTime, boolean deleteDirectory) { if (directory == null || !directory.exists()) { return new ArrayList<File>(); } Collection<File> result = new ArrayList<File>(); Collection<File> oldFiles = FileHelper.getFilesOlderThan(directory, cutoffTime, Boolean.TRUE); for (File file : oldFiles) { String logString = "Deleting File: \"" + file.toString() + "\" ... "; if (file.canWrite() && file.delete()) { logString += "done. "; result.add(file); if (file.getParentFile().isDirectory()) { if (file.getParentFile() != directory && file.getParentFile().delete()) { log.info("Deleting Directory: \"" + file.getParent() + "\" ... done"); } else if (file.getParentFile() == directory && deleteDirectory) { log.info("Deleting Directory: \"" + file.getParent() + "\" ... done"); } } } else { logString += "FAILED!"; } log.info(logString); } return result; }  static byte[] base64Encode(final File input); static byte[] base64Encode(final byte[] input); static byte[] getByteArrayFromFile(File file); static boolean renameFile(final File fromFile, final String toFileName); static boolean copyFileToPath(final File inFile, final String outFilePath); static boolean copyFileToPath(final File inFile, final String outPath, boolean deleteOriginalFile); static Collection<File> wipeOldFiles(File directory, Long cutoffTime, boolean deleteDirectory); static boolean createDir(File directory); static boolean createDir(String directory); static boolean deleteDirRecursively(File directory); static boolean deleteDirRecursively(String directory); static boolean deleteFileQuietly(String filePath); static boolean deleteFileQuietly(File file); static boolean deleteFile(String filePath); static boolean deleteFile(File file); static boolean doesDirectoryOrFileExist(String filePath); static File findFile(String file, String rootPath); static List<String> getFileList(String filePath, boolean recursive); static List<String> getFileList(String filePath, String[] extensions, boolean recursive); static Collection<File> getFileCollection(String filePath, boolean recursive); static Collection<?> getFileCollection(String filePath, String[] extensions, boolean recursive); static String getSystemTemp(); static boolean zipDirectory(String inputDirectory, String absoluteFileName); static boolean unzipFile(String outputDirectory, File zipFile); static String createUserDirectory(String applicationUserSpaceDir); static boolean updateTimestamp(final String path, final boolean recursive); static Boolean validateShapeFile(final File shapeFile); static Boolean validateShapeZIP(final File shapeZip);  }
@Test public void testWipeOldFilesWithNoOldFiles() { System.out.println("Test: " + "testWipeOldFilesWithNoOldFiles"); Collection<File> result = FileHelper.wipeOldFiles(new File(this.tempDir), 3600000l, true); assertTrue(result.isEmpty()); }
@Test @Ignore public void testWipeOldFilesWithAnAlreadyOpenFile() throws FileNotFoundException, IOException { Collection<File> result = new ArrayList<File>(); File openFile = new File(FileHelperTest.sampleDir); InputStream is = null; try { is = new FileInputStream(openFile); result = FileHelper.wipeOldFiles(openFile, 3600000l, false); } finally { if (is != null) { is.close(); } } assertTrue(result.isEmpty()); }
@Test public void testWipeOldFilesWithFakeDirectory() { System.out.println("Test: " + "testWipeOldFilesWithFakeDirectory"); Collection<File> result = FileHelper.wipeOldFiles(new File("/not/real"), 3600000l, true); assertTrue(result.isEmpty()); }
@Test public void testWipeOldFilesWithNullArgument() { System.out.println("Test: " + "testWipeOldFilesWithNullArgument"); Collection<File> result = new ArrayList<File>(); result = FileHelper.wipeOldFiles(null, 3600000l, true); assertTrue(result.isEmpty()); }
@Test public void testWipeOldFilesWithOldFiles() { System.out.println("Test: " + "testWipeOldFilesWithOldFiles"); Collection<File> result = new ArrayList<File>(); result = FileHelper.wipeOldFiles(new File(this.tempDir), 1l, true); assertTrue(!result.isEmpty()); }
@Test public void testWipeOldFilesWithOldFilesWhileLockingDirectory() throws FileNotFoundException, IOException { System.out.println("Test: " + "testWipeOldFilesWithOldFilesWhileLockingDirectory"); Collection<File> result = new ArrayList<File>(); File directory = new File(this.tempDir); String singleFile = this.tempDir + System.getProperty("file.separator") + "Sample_Files" + System.getProperty("file.separator") + "Shapefiles" + System.getProperty("file.separator") + "demo_HUCs.dbf"; File file1 = new File(singleFile); file1.setReadOnly(); result = FileHelper.wipeOldFiles(directory, 1l, true); boolean containsFail = true; for (File file : result) { if (file.getPath().equals(singleFile)) { containsFail = false; } } assertTrue(containsFail); file1.setWritable(false); FileHelper.wipeOldFiles(directory, 1l, true); }