FileUtil { public static String getRelativePath(File root, File file) { return new RelativePathBase(root).getRelativePathFor(file); } private  FileUtil(); static File getUserDir(); static List<File> toList(File... files); static String getRelativePath(File root, File file); static File toAbsoluteFile(File currentDirectory, File file); static List<String> getPathElements(File file); static List<File> expandFiles(String... paths); static List<File> expandFiles(File currentDirectory, String... paths); static FilenameFilter getFileNameFilter(String name); static boolean isWildcardFileName(String name); static File getOutputDirectoryGivenClass(final Class<?> clazz); static File getDirectoryOfClassFile(Class<?> clazz); static File getDirectoryOfClassFile(final String className); static final String ROOT_UNIX; static final String ROOT_WINDOWS; static final String ROOT_WINDOWS_NETWORK; static final String ROOT; }
@Test public void testRelativePath_3_indirectParent() { final String actual = FileUtil.getRelativePath(new File("/home/john"), new File("/home/john/documents/important")); Assert.assertEquals("./documents/important", actual); }
@Test public void testRelativePath_4_commonAncestor() { final String actual1 = FileUtil.getRelativePath(new File("/home/john"), new File("/home/smith/public/holidays.pdf")); Assert.assertEquals("../smith/public/holidays.pdf", actual1); final String actual2 = FileUtil.getRelativePath(new File("/home/john/documents"), new File("/home/smith/public/holidays.pdf")); Assert.assertEquals("../../smith/public/holidays.pdf", actual2); }
@Test public void testRelativePath_5_noCommonAncestor() { final String actual = FileUtil.getRelativePath(new File(FileUtil.ROOT + "home/john"), new File(FileUtil.ROOT + "var/tmp/test.out")); Assert.assertEquals(FileUtil.ROOT.replace('\\', '/') + "var/tmp/test.out", actual); }
@Test public void testRelativePath_1_equal() { final String actual = FileUtil.getRelativePath(new File("/home/john"), new File("/home/john")); Assert.assertEquals(".", actual); }
@Test public void testRelativePath_2_directParent() { final String actual = FileUtil.getRelativePath(new File("/home/john"), new File("/home/john/notes.txt")); Assert.assertEquals("notes.txt", actual); }