FileSyncUtils { public static String getRelativePath(File target, File base) { return getRelativePath(target.getAbsolutePath(), base.getAbsolutePath()); } private  FileSyncUtils(); static String getRelativePath(File target, File base); static String getRelativePath(String targetPath, String basePath); static String getRelativePath(String targetPath, String basePath, String pathSeparator);  }
@Test public void testGetRelativePathsUnix() { assertEquals("stuff/xyz.dat", FileSyncUtils.getRelativePath("/var/data/stuff/xyz.dat", "/var/data/", "/")); assertEquals("../../b/c", FileSyncUtils.getRelativePath("/a/b/c", "/a/x/y/", "/")); assertEquals("../../b/c", FileSyncUtils.getRelativePath("/m/n/o/a/b/c", "/m/n/o/a/x/y/", "/")); }
@Test public void testGetRelativePathFileToFile() { String target = "C:\\Windows\\Boot\\Fonts\\chs_boot.ttf"; String base = "C:\\Windows\\Speech\\Common\\sapisvr.exe"; String relPath = FileSyncUtils.getRelativePath(target, base, "\\"); assertEquals("..\\..\\Boot\\Fonts\\chs_boot.ttf", relPath); }
@Test public void testGetRelativePathDirectoryToFile() { String target = "C:\\Windows\\Boot\\Fonts\\chs_boot.ttf"; String base = "C:\\Windows\\Speech\\Common\\"; String relPath = FileSyncUtils.getRelativePath(target, base, "\\"); assertEquals("..\\..\\Boot\\Fonts\\chs_boot.ttf", relPath); }
@Test public void testGetRelativePathFileToDirectory() { String target = "C:\\Windows\\Boot\\Fonts"; String base = "C:\\Windows\\Speech\\Common\\foo.txt"; String relPath = FileSyncUtils.getRelativePath(target, base, "\\"); assertEquals("..\\..\\Boot\\Fonts", relPath); }
@Test public void testGetRelativePathDirectoryToDirectory() { String target = "C:\\Windows\\Boot\\"; String base = "C:\\Windows\\Speech\\Common\\"; String expected = "..\\..\\Boot"; String relPath = FileSyncUtils.getRelativePath(target, base, "\\"); assertEquals(expected, relPath); }
@Test public void testGetRelativePathDifferentDriveLetters() { String target = "D:\\sources\\recovery\\RecEnv.exe"; String base = "C:\\Java\\workspace\\AcceptanceTests\\Standard test data\\geo\\"; try { FileSyncUtils.getRelativePath(target, base, "\\"); fail(); } catch (PathResolutionException ex) { } }