OperationsOnFiles { public static void moveWithPruneEmptydirectories(String source, String target) { Path sourcePath; Path targetPath; try { sourcePath = FileSystems.getDefault() .getPath(source); targetPath = FileSystems.getDefault() .getPath(target); createDirectoryIfNotExists(targetPath); Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); removeFileAndParentsIfEmpty(sourcePath); } catch (IOException | NullPointerException e) { BFLogger.logError("Unable to move file from: [" + source + "] to: [" + target + "]"); } } private  OperationsOnFiles(); static void moveWithPruneEmptydirectories(String source, String target); static void removeFileAndParentsIfEmpty(Path path);  }
@Test public void movingFileFromExistingToExistingPath() { OperationsOnFiles.moveWithPruneEmptydirectories(goodSourceFilePath, goodTargetFilePath); Assert.assertTrue("File was not moved properly into the target directory.", goodTargetFile.exists()); Assert.assertFalse("File still exist in source path after moving.", goodSourceFile.exists()); }
@Test public void movingFileFromExisitingToNotExitsingPath() { OperationsOnFiles.moveWithPruneEmptydirectories(goodSourceFilePath, badTargetFilePath); Assert.assertTrue("File was not moved properly into the target directory.", badTargetfile.exists()); Assert.assertFalse("File still exist in source path after moving.", goodSourceFile.exists()); }
@Test public void movingFileFromNotExisitingToExitsingPath() { OperationsOnFiles.moveWithPruneEmptydirectories(badSourceFilePath, goodTargetFilePath); Assert.assertFalse("File was moved properly into the target directory.", badTargetfile.exists()); Assert.assertFalse("File still exist in source path after moving.", badSourceFile.exists()); }
@Test public void movingFileFromNotExisitingToNotExitsingPath() { OperationsOnFiles.moveWithPruneEmptydirectories(badSourceFilePath, badTargetFilePath); Assert.assertFalse("File was moved properly into the target directory.", badTargetfile.exists()); Assert.assertFalse("File still exist in source path after moving.", badSourceFile.exists()); }
@Test public void movingFileFromNullToNotExitsingPath() { OperationsOnFiles.moveWithPruneEmptydirectories(null, badTargetFilePath); Assert.assertFalse("File was moved properly into the target directory.", badTargetfile.exists()); Assert.assertFalse("File still exist in source path after moving.", badSourceFile.exists()); }