简体   繁体   English

Path的.relativize()文档中的错误,还是我在这里公然做错了什么?

[英]Bug in Path's .relativize() documentation or am I doing something blatantly wrong here?

OK well, here is something fun. 好吧,这很有趣。 Note that "elements" here is an instance of PathElements , and for these .resolve() and .relativize() are fully tested and known to work ... Links to the relevant method implementations Path.resolve() , Path.relativize() 请注意,“元素”这里是一个实例PathElements ,并为这些.resolve().relativize()经过全面测试已知可以使用 ...链接到相关的方法实现Path.resolve() Path.relativize()

Extract of the test method that fails: 失败的测试方法的摘录:

/*
 * This test this part of the Path's .relativize() method:
 *
 * <p> For any two {@link #normalize normalized} paths <i>p</i> and
 * <i>q</i>, where <i>q</i> does not have a 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, that turns out NOT TO BE TRUE! Whether p is absolute or
 * relative, it is indeed the case that the path elements (root, names) are
 * the same but the filesystem DIFFERS.
 *
 * An as Path's .equals() requires that the two filesystems be equal in
 * order for two Paths to be equals, this contract can not be obeyed; or I
 * am doing something VERY wrong.
 */
@Test(enabled = false)
public void relativizeResolveRoundRobinWorks()
{
    /*
     * In order to set up the environment we define a mock
     * FileSystemProvider which both our mock filesystems will return when
     * .provider() is called.
     *
     * We also suppose that the same PathElementsFactory is used; while this
     * code is not written yet, there should be only one such factory per
     * FileSystemProvider anyway (which is fed into all generated FileSystem
     * instances -- at least that's the plan).
     *
     * Note that this test method assumes that .equals() and .hashCode() are
     * not implemented on GenericPath. As such we check that the FileSystem
     * is the same (this is required by Path's equals()) and that the path
     * elements are the same (this is 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);

    /*
     * The path to be operated. As the contract says, it has no root
     * component.
     */
    final GenericPath q = new GenericPath(fsForQ, elementsFactory,
        new PathElements(null, new String[] { "q1", "q2" }));

    /*
     * The path against which both resolution and relativization are
     * performed. We take two versions of it: a non absolute one and an
     * absolute one.
     *
     * Note that since we use a UnixPathElementsFactory, we equate an
     * absolute path (or not) to a path which has a root component (or not).
     */
    GenericPath p;
    // "rr" as in "resolved, relativized"
    GenericPath rr;

    final CustomSoftAssertions soft = CustomSoftAssertions.create();

    /*
     * Try with the 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 and q filesystems should be the same (p absolute)")
        .isSameAs(q.getFileSystem());
    soft.assertThat(rr.elements).hasSameContentsAs(q.elements);

    /*
     * Now with the 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 and q filesystems should be the 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: 
The following 2 assertions failed:
1) [rr and q filesystems should be the same (p absolute)] 
Expecting:
 <Mock for FileSystem, hashCode: 1125642929>
and actual:
 <Mock for FileSystem, hashCode: 1497261280>
to refer to the same object
2) [rr and q filesystems should be the same (p not absolute)] 
Expecting:
 <Mock for FileSystem, hashCode: 1125642929>
and actual:
 <Mock for FileSystem, hashCode: 1497261280>
to refer to the same object

And obviously so! 显然是这样!

  • r = p.resolve(q) will give a Path which shares the same filesystem as p, not q; r = p.resolve(q)将给出一个与p共享同一文件系统的路径,而不是q;
  • therefore, p.relativize(r) will also give a Path with the same filesystem as p; 因此,p.relativize(r)还将提供与p具有相同文件系统的Path;
  • but the Path contract requires that in order for two paths to be equal, they share the same filesystem. 但Path合同要求,为了使两个路径相等,它们必须共享同一文件系统。

And this is always false in this scenario. 在这种情况下,这始终是错误的。

So, is this a blatant bug in the documentation or am I overlooking something? 那么,这是文档中的一个公然错误还是我忽略了什么?

A: The documentation does not mention Paths from other filesystem. 答:该文档没有提及其他文件系统的路径。

The documentation for resolve and relativize is not perfectly clear here. 解决和相对化的文档在这里不是很清楚。 But in case were the argument path is from a different filesystem resolve and relativize must throw ProviderMismatchException. 但是,如果参数路径来自其他文件系统,则解析和相对化必须抛出ProviderMismatchException。 Your test should throw earlier. 您的测试应该提早进行。

The constraints on a path can be quite different from filesystem to filesystem. 路径的约束在文件系统之间可能有很大的不同。 Consider the relativize case. 考虑相对论的情况。 What should a path look like that can walk from one filesystem to another disjoint filesystem ? 从一个文件系统到另一个不相交的文件系统应该走什么样的路径?

Note: I tested several FileSystem implementations and all throw in that case. 注意:我测试了几种FileSystem实现,在这种情况下全部抛出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编译器中的错误或我做错了什么? - Bug in compiler or am I doing something wrong? 杰克逊(Jackson)的mixin类无法解决问题:错误或我做错了什么? - Jackson's mixin class doesn't do the trick: bug or am I doing something wrong? 这是 Jackson JsonParser 中的错误,还是我做错了什么? - Is this a bug in the Jackson JsonParser, or am I doing something wrong? getSource()-我在做什么错呢? - getSource() - What am I doing wrong here? Cassandra:将列设置为null后的写入会随机丢失。 这是一个错误,还是我做错了什么? - Cassandra: Writes after setting a column to null are lost randomly. Is this a bug, or I am doing something wrong? Tomcat maxthreads,我做错了什么吗? - Tomcat maxthreads, am I doing something wrong? 我正在向某人的现有Swing代码添加一个组合框。 我在这里做错了什么? - I'm adding a combobox to someone's existing Swing code. What am I doing wrong here? 我在做什么错(Java)十进制转换为二进制 - What am I Doing Wrong Here(Java) Decimal to Binary 我在这里做错了什么? 方法不起作用 - What am I doing wrong here? Method Does not work Java读/写:我在这里做错了什么? - Java read/write: what am I doing wrong here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM