/** * Check whether the given path starts with the path prefix of the branch. * * @param branch * @param path * @return */ public static boolean startsWithPrefix(Branch branch, String path) { String prefix = branch.getPathPrefix(); prefix = sanitize(prefix); return path.startsWith(prefix); }
/** * Remove the configured path prefix of the branch from the given path. * * @param branch * Branch which contains the prefix * @param path * Path to be handled * @return stripped path */ public static String strip(Branch branch, String path) { String prefix = branch.getPathPrefix(); prefix = sanitize(prefix); return !startsWithPrefix(branch, path) ? path : path.substring(prefix.length()); }
@Test public void testStartsWithPrefix() { Branch branch = Mockito.mock(Branch.class); when(branch.getPathPrefix()).thenReturn("cba"); assertFalse(PathPrefixUtil.startsWithPrefix(branch, "/abc")); when(branch.getPathPrefix()).thenReturn("abc"); assertTrue(PathPrefixUtil.startsWithPrefix(branch, "/abc")); } }
@Test public void testStrip() { Branch branch = Mockito.mock(Branch.class); when(branch.getPathPrefix()).thenReturn(""); assertEquals("", PathPrefixUtil.strip(branch, "")); when(branch.getPathPrefix()).thenReturn("abc"); assertEquals("", PathPrefixUtil.strip(branch, "")); assertEquals("", PathPrefixUtil.strip(branch, "/abc")); }