HerdFileUtils.verifyFileExistsAndReadable(realFile);
@Override public void execute() throws Exception { HerdFileUtils.cleanDirectoryIgnoreException(testFile); } });
@Override public void execute() throws Exception { HerdFileUtils.deleteDirectoryIgnoreException(testFile); } });
/** * Logs all files found in the specified local directory. * * @param directory the target local directory */ private void logLocalDirectoryContents(File directory) { Collection<File> files = HerdFileUtils.listFiles(directory, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); LOGGER.info(String.format("Found %d files in \"%s\" target local directory:", files.size(), directory.getPath())); for (File file : files) { LOGGER.info(String.format(" %s", file.getPath())); } }
/** * Deletes a local test file. * * @throws IOException if fails to create a local test file */ @Test public void testDeleteFileIgnoreException() throws IOException { File testFile = createLocalFile(localTempPath.toString(), "SOME_FILE", FILE_SIZE_1_KB); HerdFileUtils.deleteFileIgnoreException(testFile); assertFalse(testFile.exists()); assertTrue(localTempPath.toFile().exists()); }
/** * Reads a JSON manifest file into a JSON manifest object. * * @param jsonManifestFile the JSON manifest file. * * @return the manifest object. * @throws java.io.IOException if any errors were encountered reading the JSON file. * @throws IllegalArgumentException if the manifest file has validation errors. */ public M readJsonManifest(File jsonManifestFile) throws IOException, IllegalArgumentException { // Verify that the file exists and can be read. HerdFileUtils.verifyFileExistsAndReadable(jsonManifestFile); // Deserialize the JSON manifest. BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(jsonManifestFile)); BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, Charsets.UTF_8)); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE); M manifest = getManifestFromReader(reader, objectMapper); // Validate the manifest and return it. validateManifest(manifest); return manifest; }
/** * Cleans up a local test directory by deleting a test file. * * @throws IOException if fails to create a local test file */ @Test public void testCleanDirectoryIgnoreException() throws IOException { File testFile = createLocalFile(localTempPath.toString(), "SOME_FILE", FILE_SIZE_1_KB); HerdFileUtils.cleanDirectoryIgnoreException(localTempPath.toFile()); assertFalse(testFile.exists()); assertTrue(localTempPath.toFile().exists()); }
@Override public void execute() throws Exception { HerdFileUtils.deleteDirectoryIgnoreException(new File("I_DO_NOT_EXIST")); } });
/** * Reads a JSON manifest file into a JSON manifest object. * * @param jsonManifestFile the JSON manifest file. * * @return the manifest object. * @throws java.io.IOException if any errors were encountered reading the JSON file. * @throws IllegalArgumentException if the manifest file has validation errors. */ public M readJsonManifest(File jsonManifestFile) throws IOException, IllegalArgumentException { // Verify that the file exists and can be read. HerdFileUtils.verifyFileExistsAndReadable(jsonManifestFile); // Deserialize the JSON manifest. BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(jsonManifestFile)); BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, Charsets.UTF_8)); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE); M manifest = getManifestFromReader(reader, objectMapper); // Validate the manifest and return it. validateManifest(manifest); return manifest; }
HerdFileUtils.cleanDirectoryIgnoreException(targetLocalDirectory);
/** * Deletes a local test directory with a test file. * * @throws IOException if fails to create a local test file */ @Test public void testDeleteDirectoryIgnoreException() throws IOException { File testFile = createLocalFile(localTempPath.toString(), "SOME_FILE", FILE_SIZE_1_KB); HerdFileUtils.deleteDirectoryIgnoreException(localTempPath.toFile()); assertFalse(testFile.exists()); assertFalse(localTempPath.toFile().exists()); }
@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()); } } }