ImmutableListPathCore implements PathCore<ImmutableListPathCore> { @Override public ImmutableListPathCore relativize(ImmutableListPathCore other) { if (this.equals(other)) { return emptyInstance; } else if (isAbsolute() && !other.isAbsolute()) { throw new IllegalArgumentException( "cannot relativize from absolute path to relative path"); } else if (!isAbsolute() && other.isAbsolute()) { throw new IllegalArgumentException( "cannot relativize from relative path to absolute path"); } else if (isAbsolute() && !getRootString().equals(other.getRootString())) { throw new IllegalArgumentException( "cannot relativize between paths with different roots"); } else { return relativizeNormalizedPaths(normalize(), other.normalize()); } } ImmutableListPathCore(@Nullable String root, Iterable<String> path); static Parser parser(String separator); static Parser namedRootParser(String separator, NamedRootFormat format, String rootId); @Override boolean isAbsolute(); @Override ImmutableListPathCore getRoot(); @Override ImmutableListPathCore getFileName(); @Override ImmutableListPathCore getParent(); @Override int getNameCount(); @Override ImmutableListPathCore getName(int index); @Override ImmutableListPathCore subpath(int beginIndex, int endIndex); @Override boolean startsWith(ImmutableListPathCore other); @Override boolean endsWith(ImmutableListPathCore other); @Override ImmutableListPathCore normalize(); @Override ImmutableListPathCore resolve(ImmutableListPathCore other); @Override ImmutableListPathCore resolveSibling(ImmutableListPathCore other); @Override ImmutableListPathCore relativize(ImmutableListPathCore other); @Override String toPathString(String separator); @Override String toString(); @Override int compareTo(ImmutableListPathCore other); @Override boolean equals(Object obj); @Override int hashCode(); @Nullable String getRootString(); ImmutableList<String> getPathSegments();  }
@Test public void relativizeMixedPathsThrowsException() { try { absolute.relativize(relative); fail("relativized between absolute and relative allowed"); } catch (IllegalArgumentException expected) { } try { relative.relativize(absolute); fail("relativized between relative and absolute allowed"); } catch (IllegalArgumentException expected) { } }
@Test(expected = IllegalArgumentException.class) public void relativizeAbsolutePathsWithDifferentRootsThrowsException() { ImmutableListPathCore target = core(ROOT_NAME, "path", "to", "directory"); absolute.relativize(target); }
@Test public void relativizeSelfIsEmpty() { ImmutableListPathCore relativized = relative.relativize(relative); assertEquals("relativized path is not empty", empty, relativized); }
@Test public void relativizeSelfAfterNormalizationIsEmpty() { ImmutableListPathCore target = core(null, "path", "to", ".", "file"); ImmutableListPathCore relativized = relative.relativize(target); assertEquals("relativized path is not empty", empty, relativized); }