PathUtil { static String getRelativeFileInternal(File canonicalBaseFile, File canonicalFileToRelativize) { List<String> basePath = getPathComponents(canonicalBaseFile); List<String> pathToRelativize = getPathComponents(canonicalFileToRelativize); if (!basePath.get(0).equals(pathToRelativize.get(0))) { return canonicalFileToRelativize.getPath(); } int commonDirs; StringBuilder sb = new StringBuilder(); for (commonDirs=1; commonDirs<basePath.size() && commonDirs<pathToRelativize.size(); commonDirs++) { if (!basePath.get(commonDirs).equals(pathToRelativize.get(commonDirs))) { break; } } boolean first = true; for (int i=commonDirs; i<basePath.size(); i++) { if (!first) { sb.append(File.separatorChar); } else { first = false; } sb.append(".."); } first = true; for (int i=commonDirs; i<pathToRelativize.size(); i++) { if (first) { if (sb.length() != 0) { sb.append(File.separatorChar); } first = false; } else { sb.append(File.separatorChar); } sb.append(pathToRelativize.get(i)); } if (sb.length() == 0) { return "."; } return sb.toString(); } private  PathUtil(); static File getRelativeFile(File baseFile, File fileToRelativize);  }
@Test public void pathUtilTest1() { File[] roots = File.listRoots(); if (roots.length > 1) { File basePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "test.txt"); File relativePath = new File(roots[1] + "some" + File.separatorChar + "dir" + File.separatorChar + "test.txt"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, relativePath.getPath()); } }
@Test public void pathUtilTest2() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "test.txt"); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "test.txt"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "."); }
@Test public void pathUtilTest3() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "."); }
@Test public void pathUtilTest4() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar + "dir"); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "."); }
@Test public void pathUtilTest5() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar + "dir"); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "."); }
@Test public void pathUtilTest6() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "."); }
@Test public void pathUtilTest7() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some"); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "dir"); }
@Test public void pathUtilTest8() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "dir"); }
@Test public void pathUtilTest9() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some"); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "dir"); }
@Test public void pathUtilTest10() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "dir"); }
@Test public void pathUtilTest11() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some"); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "dir2"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "dir" + File.separatorChar + "dir2"); }
@Test public void pathUtilTest12() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "dir2" + File.separatorChar); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "dir" + File.separatorChar + "dir2"); }
@Test public void pathUtilTest13() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some"); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "dir2" + File.separatorChar); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "dir" + File.separatorChar + "dir2"); }
@Test public void pathUtilTest14() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "dir2"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "dir" + File.separatorChar + "dir2"); }
@Test public void pathUtilTest15() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar + "dir3"); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "dir2"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, ".." + File.separatorChar + "dir" + File.separatorChar + "dir2"); }
@Test public void pathUtilTest16() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some2" + File.separatorChar + "dir3"); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "dir2"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, ".." + File.separatorChar + ".." + File.separatorChar + "some" + File.separatorChar + "dir" + File.separatorChar + "dir2"); }
@Test public void pathUtilTest17() { File[] roots = File.listRoots(); File basePath = new File(roots[0].getPath()); File relativePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "dir2"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, "some" + File.separatorChar + "dir" + File.separatorChar + "dir2"); }
@Test public void pathUtilTest18() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar + "dir"); File relativePath = new File(roots[0] + "some"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, ".."); }
@Test public void pathUtilTest19() { File[] roots = File.listRoots(); File basePath = new File(roots[0] + "some" + File.separatorChar + "dir" + File.separatorChar + "dir2"); File relativePath = new File(roots[0] + "some"); String path = PathUtil.getRelativeFileInternal(basePath, relativePath); Assert.assertEquals(path, ".." + File.separatorChar + ".."); }