void checkWritableDir(String tempPath) { try { File tempFile = File.createTempFile("check", "tmp", new File(tempPath)); deleteQuietly(tempFile); } catch (IOException e) { throw new IllegalStateException(format("Temp directory is not writable: %s", tempPath), e); } }
static Props loadPropsFromCommandLineArgs(String[] args) { if (args.length != 1) { throw new IllegalArgumentException("Only a single command-line argument is accepted " + "(absolute path to configuration file)"); } File propertyFile = new File(args[0]); Properties properties = new Properties(); Reader reader = null; try { reader = new InputStreamReader(new FileInputStream(propertyFile), StandardCharsets.UTF_8); properties.load(reader); } catch (Exception e) { throw new IllegalStateException("Could not read properties from file: " + args[0], e); } finally { IOUtils.closeQuietly(reader); deleteQuietly(propertyFile); } return new Props(properties); } }
@Test public void deleteQuietly_does_not_fail_if_argument_is_null() { FileUtils2.deleteQuietly(null); }
@Test public void deleteQuietly_does_not_fail_if_file_does_not_exist() throws IOException { File file = new File(temporaryFolder.newFolder(), "blablabl"); assertThat(file).doesNotExist(); FileUtils2.deleteQuietly(file); }
@Test public void deleteQuietly_deletes_symbolicLink() throws IOException { assumeTrue(SystemUtils.IS_OS_UNIX); Path folder = temporaryFolder.newFolder().toPath(); Path file1 = Files.createFile(folder.resolve("file1.txt")); Path symLink = Files.createSymbolicLink(folder.resolve("link1"), file1); assertThat(file1).isRegularFile(); assertThat(symLink).isSymbolicLink(); FileUtils2.deleteQuietly(symLink.toFile()); assertThat(symLink).doesNotExist(); assertThat(file1).isRegularFile(); }
@Test public void deleteDirectory_deletes_directory_and_content() throws IOException { Path target = temporaryFolder.newFolder().toPath(); Path childFile1 = Files.createFile(target.resolve("file1.txt")); Path childDir1 = Files.createDirectory(target.resolve("subDir1")); Path childFile2 = Files.createFile(childDir1.resolve("file2.txt")); Path childDir2 = Files.createDirectory(childDir1.resolve("subDir2")); assertThat(target).isDirectory(); assertThat(childFile1).isRegularFile(); assertThat(childDir1).isDirectory(); assertThat(childFile2).isRegularFile(); assertThat(childDir2).isDirectory(); FileUtils2.deleteQuietly(target.toFile()); assertThat(target).doesNotExist(); assertThat(childFile1).doesNotExist(); assertThat(childDir1).doesNotExist(); assertThat(childFile2).doesNotExist(); assertThat(childDir2).doesNotExist(); }
@Test public void deleteQuietly_deletes_directory_and_content() throws IOException { Path target = temporaryFolder.newFolder().toPath(); Path childFile1 = Files.createFile(target.resolve("file1.txt")); Path childDir1 = Files.createDirectory(target.resolve("subDir1")); Path childFile2 = Files.createFile(childDir1.resolve("file2.txt")); Path childDir2 = Files.createDirectory(childDir1.resolve("subDir2")); assertThat(target).isDirectory(); assertThat(childFile1).isRegularFile(); assertThat(childDir1).isDirectory(); assertThat(childFile2).isRegularFile(); assertThat(childDir2).isDirectory(); FileUtils2.deleteQuietly(target.toFile()); assertThat(target).doesNotExist(); assertThat(childFile1).doesNotExist(); assertThat(childDir1).doesNotExist(); assertThat(childFile2).doesNotExist(); assertThat(childDir2).doesNotExist(); }