HerdFileUtils extends FileUtils { public static void verifyFileExistsAndReadable(File file) { if (!file.exists()) { throw new IllegalArgumentException("File \"" + file.getName() + "\" doesn't exist."); } if (!file.isFile()) { throw new IllegalArgumentException("File \"" + file.getName() + "\" is not a valid file that can be read as a manifest. Is it a directory?"); } if (!file.canRead()) { throw new IllegalArgumentException("Unable to read file \"" + file.getName() + "\". Check permissions."); } }  static void verifyFileExistsAndReadable(File file); static void cleanDirectoryIgnoreException(File directory); static void deleteDirectoryIgnoreException(File directory); static void deleteFileIgnoreException(File file); static final long BYTES_PER_GB; }
@Test public void testVerifyFileExistsAndReadable() throws IOException { File testFile = createLocalFile(localTempPath.toString(), "SOME_FILE", FILE_SIZE_1_KB); HerdFileUtils.verifyFileExistsAndReadable(testFile); }
@Test public void testVerifyFileExistsAndReadableFileNoExists() throws IOException { File testFile = new File("I_DO_NOT_EXIST"); try { HerdFileUtils.verifyFileExistsAndReadable(testFile); fail("Should throw an IllegalArgumentException when file does not exist."); } catch (IllegalArgumentException e) { assertEquals(String.format("File \"%s\" doesn't exist.", testFile.getName()), e.getMessage()); } }
@Test public void testVerifyFileExistsAndReadableFileIsDirectory() throws IOException { File testDirectory = localTempPath.toFile(); try { HerdFileUtils.verifyFileExistsAndReadable(testDirectory); fail("Should throw an IllegalArgumentException when argument is a directory."); } catch (IllegalArgumentException e) { assertEquals(String.format("File \"%s\" is not a valid file that can be read as a manifest. Is it a directory?", testDirectory.getName()), e.getMessage()); } }
@Test public void testVerifyFileExistsAndReadableFileNotReadable() throws IOException { File testFile = createLocalFile(localTempPath.toString(), "SOME_FILE", FILE_SIZE_1_KB); if (testFile.setReadable(false)) { try { HerdFileUtils.verifyFileExistsAndReadable(testFile); fail("Should throw an IllegalArgumentException when file is not readable."); } catch (IllegalArgumentException e) { assertEquals(String.format("Unable to read file \"%s\". Check permissions.", testFile.getName()), e.getMessage()); } } }