java - Bug in Path's .relativize() documentation or am I doing something blatantly wrong here? -
ok well, here fun. note "elements" here instance of pathelements
, , these .resolve()
, .relativize()
fully tested , known work... links relevant method implementations path.resolve()
, path.relativize()
extract of test method fails:
/* * test part of path's .relativize() method: * * <p> 2 {@link #normalize normalized} paths <i>p</i> , * <i>q</i>, <i>q</i> not have root component, * <blockquote> * <i>p</i><tt>.relativize(</tt><i>p</i><tt>.resolve(</tt><i>q</i><tt>)) * .equals(</tt><i>q</i><tt>)</tt> * </blockquote> * * unfortunately, turns out not true! whether p absolute or * relative, indeed case path elements (root, names) * same filesystem differs. * * path's .equals() requires 2 filesystems equal in * order 2 paths equals, contract can not obeyed; or * doing wrong. */ @test(enabled = false) public void relativizeresolveroundrobinworks() { /* * in order set environment define mock * filesystemprovider both our mock filesystems return when * .provider() called. * * suppose same pathelementsfactory used; while * code not written yet, there should 1 such factory per * filesystemprovider anyway (which fed generated filesystem * instances -- @ least that's plan). * * note test method assumes .equals() , .hashcode() * not implemented on genericpath. such check filesystem * same (this required path's equals()) , path * elements same (this package's requirements). */ final filesystemprovider fsprovider = mock(filesystemprovider.class); final pathelementsfactory elementsfactory = new unixpathelementsfactory(); final filesystem fsforp = mock(filesystem.class); final filesystem fsforq = mock(filesystem.class); when(fsforp.provider()).thenreturn(fsprovider); when(fsforq.provider()).thenreturn(fsprovider); /* * path operated. contract says, has no root * component. */ final genericpath q = new genericpath(fsforq, elementsfactory, new pathelements(null, new string[] { "q1", "q2" })); /* * path against both resolution , relativization * performed. take 2 versions of it: non absolute 1 , * absolute one. * * note since use unixpathelementsfactory, equate * absolute path (or not) path has root component (or not). */ genericpath p; // "rr" in "resolved, relativized" genericpath rr; final customsoftassertions soft = customsoftassertions.create(); /* * try absolute version first... */ p = new genericpath(fsforp, elementsfactory, new pathelements("/", new string[] { "p1", "p2" })); rr = (genericpath) p.relativize(p.resolve(q)); soft.assertthat(rr.getfilesystem()) .as("rr , q filesystems should same (p absolute)") .issameas(q.getfilesystem()); soft.assertthat(rr.elements).hassamecontentsas(q.elements); /* * non absolute version */ p = new genericpath(fsforp, elementsfactory, new pathelements(null, new string[] { "p1", "p2" })); rr = (genericpath) p.relativize(p.resolve(q)); soft.assertthat(rr.getfilesystem()) .as("rr , q filesystems should same (p not absolute)") .issameas(q.getfilesystem()); soft.assertthat(rr.elements).hassamecontentsas(q.elements); soft.assertall(); }
this test fails with:
org.assertj.core.api.softassertionerror: following 2 assertions failed: 1) [rr , q filesystems should same (p absolute)] expecting: <mock filesystem, hashcode: 1125642929> , actual: <mock filesystem, hashcode: 1497261280> refer same object 2) [rr , q filesystems should same (p not absolute)] expecting: <mock filesystem, hashcode: 1125642929> , actual: <mock filesystem, hashcode: 1497261280> refer same object
and so!
- r = p.resolve(q) give path shares same filesystem p, not q;
- therefore, p.relativize(r) give path same filesystem p;
- but path contract requires in order 2 paths equal, share same filesystem.
and always false in scenario.
so, blatant bug in documentation or overlooking something?
a: documentation not mention paths other filesystem.
the documentation resolve , relativize not clear here. in case argument path different filesystem resolve , relativize must throw providermismatchexception. test should throw earlier.
the constraints on path can quite different filesystem filesystem. consider relativize case. should path can walk 1 filesystem disjoint filesystem ?
note: tested several filesystem implementations , throw in case.
Comments
Post a Comment