Path { public static Path parse(String path) { Path root = new Path("/"); if ("/".equals(path)) return root; Path current = root; int length = path.length(); int from = 1; for (int i = 1; i < length; i++) { char ch = path.charAt(i); if (ch == '/') { current.next = new Path(path.substring(from, i)); current = current.next; from = i + 1; } } String lastPath = from == length ? "/" : path.substring(from); current.next = new Path(lastPath); return root; } private  Path(String value); static Path parse(String path); final String value; public Path next; }
@Test void parseRootURL() { Path path = Path.parse("/"); assertThat(path.value).isEqualTo("/"); assertThat(path.next).isNull(); }
@Test void parseOneLevelURL() { Path path = Path.parse("/path1"); assertEquals("/", path.value); assertEquals("path1", path.next.value); assertNull(path.next.next); }
@Test void parseOneLevelURLWithTrailingSlash() { Path path = Path.parse("/path1/"); assertEquals("/", path.value); assertEquals("path1", path.next.value); assertEquals("/", path.next.next.value); assertNull(path.next.next.next); }
@Test void parseURL() { Path path = Path.parse("/path1/path2"); assertEquals("/", path.value); assertEquals("path1", path.next.value); assertEquals("path2", path.next.next.value); assertNull(path.next.next.next); }
@Test void parseURLWithTrailingSlash() { Path path = Path.parse("/path1/path2/"); assertEquals("/", path.value); assertEquals("path1", path.next.value); assertEquals("path2", path.next.next.value); assertEquals("/", path.next.next.next.value); assertNull(path.next.next.next.next); }