final Path toDirPath = toDir.toPath(); final Path fileToMovePath = fileToMove.toPath(); return pathToFileAfterMove( fromDirPath, toDirPath, fileToMovePath ).toFile();
/** * Create a consumer of FileHandle that uses fileHandle.rename to move file held by file handle to move from * directory to directory. * <p> * Possibly IOException will be wrapped in UncheckedIOException * * @param from Directory to move file from. * @param to Directory to move file to. * @return A new Consumer that moves the file wrapped by the file handle. */ static Consumer<FileHandle> handleRenameBetweenDirectories( File from, File to ) { return fileHandle -> { try { fileHandle.rename( FileUtils.pathToFileAfterMove( from, to, fileHandle.getFile() ) ); } catch ( IOException e ) { throw new UncheckedIOException( e ); } }; }
@Test public void pathToFileAfterMoveMustThrowIfFromDirIsCompletePathToFile() { File file = new File( "/a/b/f" ); File from = new File( "/a/b/f" ); File to = new File( "/a/c" ); expected.expect( IllegalArgumentException.class ); pathToFileAfterMove( from, to, file ); }
@Test public void pathToFileAfterMoveMustThrowIfFileNotSubPathToFromShorter() { File file = new File( "/a" ); File from = new File( "/a/b" ); File to = new File( "/a/c" ); expected.expect( IllegalArgumentException.class ); pathToFileAfterMove( from, to, file ); }
@Test public void pathToFileAfterMoveMustWorkIfNotMovingAtAllAndFileHasSubDir() { File file = new File( "/a/b/d/f" ); File from = new File( "/a/b" ); File to = new File( "/a/b" ); assertThat( pathToFileAfterMove( from, to, file ).getPath(), is( path( "/a/b/d/f" ) ) ); }
@Test public void pathToFileAfterMoveMustThrowIfFileNotSubPathToFromSameLength() { File file = new File( "/a/f" ); File from = new File( "/a/b" ); File to = new File( "/a/c" ); expected.expect( IllegalArgumentException.class ); pathToFileAfterMove( from, to, file ); }
@Test public void pathToFileAfterMoveMustThrowIfFileNotSubPathToFromLonger() { File file = new File( "/a/c/f" ); File from = new File( "/a/b" ); File to = new File( "/a/c" ); expected.expect( IllegalArgumentException.class ); pathToFileAfterMove( from, to, file ); }
@Test public void pathToFileAfterMoveMustWorkIfMovingToSibling() { File file = new File( "/a/b/f" ); File from = new File( "/a/b" ); File to = new File( "/a/c" ); assertThat( pathToFileAfterMove( from, to, file ).getPath(), is( path( "/a/c/f" ) ) ); }
@Test public void pathToFileAfterMoveMustWorkIfMovingOutOfDir() { File file = new File( "/a/b/f" ); File from = new File( "/a/b" ); File to = new File( "/c" ); assertThat( pathToFileAfterMove( from, to, file ).getPath(), is( path( "/c/f" ) ) ); }
@Test public void pathToFileAfterMoveMustWorkIfMovingOutOfDirAndFileHasSubDir() { File file = new File( "/a/b/d/f" ); File from = new File( "/a/b" ); File to = new File( "/c" ); assertThat( pathToFileAfterMove( from, to, file ).getPath(), is( path( "/c/d/f" ) ) ); }
@Test public void pathToFileAfterMoveMustWorkIfNotMovingAtAll() { File file = new File( "/a/b/f" ); File from = new File( "/a/b" ); File to = new File( "/a/b" ); assertThat( pathToFileAfterMove( from, to, file ).getPath(), is( path( "/a/b/f" ) ) ); }
@Test public void pathToFileAfterMoveMustWorkIfMovingToSiblingAndFileHasSubDir() { File file = new File( "/a/b/d/f" ); File from = new File( "/a/b" ); File to = new File( "/a/c" ); assertThat( pathToFileAfterMove( from, to, file ).getPath(), is( path( "/a/c/d/f" ) ) ); }
@Test public void pathToFileAfterMoveMustWorkIfMovingToSubDir() { File file = new File( "/a/b/f" ); File from = new File( "/a/b" ); File to = new File( "/a/b/c" ); assertThat( pathToFileAfterMove( from, to, file ).getPath(), is( path( "/a/b/c/f" ) ) ); }
@Test public void pathToFileAfterMoveMustWorkIfMovingToSubDirAndFileHasSubDir() { File file = new File( "/a/b/d/f" ); File from = new File( "/a/b" ); File to = new File( "/a/b/c" ); assertThat( pathToFileAfterMove( from, to, file ).getPath(), is( path( "/a/b/c/d/f" ) ) ); }
final Path toDirPath = toDir.toPath(); final Path fileToMovePath = fileToMove.toPath(); return pathToFileAfterMove( fromDirPath, toDirPath, fileToMovePath ).toFile();
/** * Create a consumer of FileHandle that uses fileHandle.rename to move file held by file handle to move from * directory to directory. * <p> * Possibly IOException will be wrapped in UncheckedIOException * * @param from Directory to move file from. * @param to Directory to move file to. * @return A new Consumer that moves the file wrapped by the file handle. */ static Consumer<FileHandle> handleRenameBetweenDirectories( File from, File to ) { return fileHandle -> { try { fileHandle.rename( FileUtils.pathToFileAfterMove( from, to, fileHandle.getFile() ) ); } catch ( IOException e ) { throw new UncheckedIOException( e ); } }; }