PathUtils { public static String resolveRelativePath(String relativePath, String anchorPath, char expectedSeparator) { if (relativePath == null || relativePath.isEmpty()) { return null; } relativePath = sanitize(relativePath, expectedSeparator); anchorPath = sanitize(anchorPath, expectedSeparator); if (relativePath.startsWith(".." + expectedSeparator) || relativePath .startsWith("." + expectedSeparator)) { return resolvePathInner(relativePath, anchorPath, expectedSeparator); } return null; } private  PathUtils(); static String resolveRelativePath(String relativePath, String anchorPath,
      char expectedSeparator); static final Pattern PATH_SEPARATOR_REGEX; }
@Test void testRelativePathResolutionUnixStyle() { assertEquals("test/test/sample.peb", PathUtils.resolveRelativePath("./sample.peb", "test/test/", FORWARD_SLASH)); assertEquals("test/test/sample.peb", PathUtils.resolveRelativePath("./sample.peb", "test/test/other.peb", FORWARD_SLASH)); assertEquals("test/sample.peb", PathUtils.resolveRelativePath("../sample.peb", "test/test/", FORWARD_SLASH)); assertEquals("test/sample.peb", PathUtils.resolveRelativePath("../sample.peb", "test/test/other.peb", FORWARD_SLASH)); assertNull( PathUtils.resolveRelativePath("test/sample.peb", "test/test/other.peb", FORWARD_SLASH)); }
@Test void testRelativePathResolutionWindowsStyle() { assertEquals("test\\test\\sample.peb", PathUtils.resolveRelativePath(".\\sample.peb", "test\\test\\", BACKWARD_SLASH)); assertEquals("test\\test\\sample.peb", PathUtils.resolveRelativePath(".\\sample.peb", "test\\test\\other.peb", BACKWARD_SLASH)); assertEquals("test\\sample.peb", PathUtils.resolveRelativePath("..\\sample.peb", "test\\test\\", BACKWARD_SLASH)); assertEquals("test\\sample.peb", PathUtils.resolveRelativePath("..\\sample.peb", "test\\test\\other.peb", BACKWARD_SLASH)); assertEquals(null, PathUtils.resolveRelativePath("test\\sample.peb", "test\\test\\other.peb", BACKWARD_SLASH)); }
@Test void testRelativePathResolutionMixedStyle1() { assertEquals("test/test/sample.peb", PathUtils.resolveRelativePath(".\\sample.peb", "test/test/", FORWARD_SLASH)); assertEquals("test/test/sample.peb", PathUtils.resolveRelativePath(".\\sample.peb", "test/test/other.peb", FORWARD_SLASH)); assertEquals("test/sample.peb", PathUtils.resolveRelativePath("..\\sample.peb", "test/test/", FORWARD_SLASH)); assertEquals("test/sample.peb", PathUtils.resolveRelativePath("..\\sample.peb", "test/test/other.peb", FORWARD_SLASH)); assertEquals(null, PathUtils.resolveRelativePath("test\\sample.peb", "test/test/other.peb", FORWARD_SLASH)); }
@Test void testRelativePathResolutionMixedStyle2() { assertEquals("test\\test\\sample.peb", PathUtils.resolveRelativePath("./sample.peb", "test\\test\\", BACKWARD_SLASH)); assertEquals("test\\test\\sample.peb", PathUtils.resolveRelativePath("./sample.peb", "test\\test\\other.peb", BACKWARD_SLASH)); assertEquals("test\\sample.peb", PathUtils.resolveRelativePath("../sample.peb", "test\\test\\", BACKWARD_SLASH)); assertEquals("test\\sample.peb", PathUtils.resolveRelativePath("../sample.peb", "test\\test\\other.peb", BACKWARD_SLASH)); assertEquals(null, PathUtils.resolveRelativePath("test/sample.peb", "test\\test\\other.peb", BACKWARD_SLASH)); }