### Question:
Path implements Iterable<String>, Serializable { public Path toRelative(Path ancestor) { if (isRoot()) { throw new IllegalStateException("Cannot make root path relative"); } if (!isDescendantOf(ancestor)) { throw new IllegalArgumentException("Cannot create relative path because this path: " + this + " is not descendant of ancestor argument: " + ancestor); } Path fragment = new Path(path.substring(ancestor.path.length()), false); if (fragment.isAbsolute()) { fragment = fragment.toRelative(new Path("/")); } return fragment; } Path(String path);  Path(String path, boolean canonize); Path canonical(); boolean isCanonical(); boolean isRoot(); boolean isAbsolute(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); Iterator<String> iterator(); String getName(); boolean isAncestorOf(Path other); boolean isDescendantOf(Path other); boolean isParentOf(Path other); boolean isChildOf(Path other); int size(); Path parent(); String part(int index); Path subpath(int idx); Path toAbsolute(); Path append(Path relative); Path toRelative(Path ancestor); static final Path ROOT; static final Comparator<Path> SMALLEST_FIRST_COMPARATOR; static final Comparator<Path> LARGEST_FIRST_COMPARATOR; }### Answer:
@Test public void toRelative() { try { new Path("/").toRelative(new Path("/")); fail(); } catch (IllegalStateException e) { } try { new Path("/foo/bar").toRelative(new Path("/bar")); fail(); } catch (IllegalArgumentException e) { } assertEquals(new Path("foo"), new Path("/foo").toRelative(new Path("/"))); assertEquals(new Path("bar"), new Path("/foo/bar").toRelative(new Path("/foo"))); assertEquals(new Path("bar"), new Path("foo/bar").toRelative(new Path("foo"))); }