LocalResourceId implements ResourceId { @Override public LocalResourceId resolve(String other, ResolveOptions resolveOptions) { checkState(isDirectory(), "Expected the path is a directory, but had [%s].", pathString); checkArgument( resolveOptions.equals(StandardResolveOptions.RESOLVE_FILE) || resolveOptions.equals(StandardResolveOptions.RESOLVE_DIRECTORY), "ResolveOptions: [%s] is not supported.", resolveOptions); checkArgument( !(resolveOptions.equals(StandardResolveOptions.RESOLVE_FILE) && other.endsWith("/")), "The resolved file: [%s] should not end with '/'.", other); if (SystemUtils.IS_OS_WINDOWS) { return resolveLocalPathWindowsOS(other, resolveOptions); } else { return resolveLocalPath(other, resolveOptions); } } private  LocalResourceId(Path path, boolean isDirectory); @Override LocalResourceId resolve(String other, ResolveOptions resolveOptions); @Override LocalResourceId getCurrentDirectory(); @Override @Nullable String getFilename(); @Override String getScheme(); @Override boolean isDirectory(); @Override String toString(); @Override boolean equals(@Nullable Object obj); @Override int hashCode();  }
@Test public void testResolveInUnix() { if (SystemUtils.IS_OS_WINDOWS) { return; } assertEquals( toResourceIdentifier("/root/tmp/aa"), toResourceIdentifier("/root/tmp/").resolve("aa", StandardResolveOptions.RESOLVE_FILE)); assertEquals( toResourceIdentifier("/root/tmp/aa/bb/cc/"), toResourceIdentifier("/root/tmp/") .resolve("aa", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("bb", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("cc", StandardResolveOptions.RESOLVE_DIRECTORY)); assertEquals( toResourceIdentifier("/root/tmp/aa"), toResourceIdentifier("/root/tmp/bb/") .resolve("/root/tmp/aa", StandardResolveOptions.RESOLVE_FILE)); assertEquals( toResourceIdentifier("file:/aa"), toResourceIdentifier("file: }
@Test public void testResolveNormalizationInUnix() { if (SystemUtils.IS_OS_WINDOWS) { return; } assertEquals( toResourceIdentifier("file: toResourceIdentifier("file: .resolve("aa", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("..", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("bb", StandardResolveOptions.RESOLVE_FILE)); assertEquals( toResourceIdentifier("file: toResourceIdentifier("file: .resolve("aa", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve(".", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("bb", StandardResolveOptions.RESOLVE_FILE)); assertEquals( toResourceIdentifier("aa/bb"), toResourceIdentifier("a/../") .resolve("aa", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve(".", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("bb", StandardResolveOptions.RESOLVE_FILE)); assertEquals( toResourceIdentifier("/aa/bb"), toResourceIdentifier("/a/../") .resolve("aa", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve(".", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("bb", StandardResolveOptions.RESOLVE_FILE)); assertEquals( toResourceIdentifier("aa/bb"), toResourceIdentifier("./") .resolve("aa", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve(".", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("bb", StandardResolveOptions.RESOLVE_FILE)); assertEquals( toResourceIdentifier("../aa/bb"), toResourceIdentifier("../") .resolve("aa", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve(".", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("bb", StandardResolveOptions.RESOLVE_FILE)); assertEquals( toResourceIdentifier("~/aa/bb/"), toResourceIdentifier("~/") .resolve("aa", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve(".", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("bb", StandardResolveOptions.RESOLVE_DIRECTORY)); }
@Test public void testResolveHandleBadInputsInUnix() { if (SystemUtils.IS_OS_WINDOWS) { return; } assertEquals( toResourceIdentifier("/root/tmp/"), toResourceIdentifier("/root/").resolve("tmp/", StandardResolveOptions.RESOLVE_DIRECTORY)); }
@Test public void testResolveInvalidInputs() { assumeFalse(SystemUtils.IS_OS_WINDOWS); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("The resolved file: [tmp/] should not end with '/'."); toResourceIdentifier("/root/").resolve("tmp/", StandardResolveOptions.RESOLVE_FILE); }
@Test public void testResolveInvalidNotDirectory() { assumeFalse(SystemUtils.IS_OS_WINDOWS); ResourceId tmp = toResourceIdentifier("/root/").resolve("tmp", StandardResolveOptions.RESOLVE_FILE); thrown.expect(IllegalStateException.class); thrown.expectMessage("Expected the path is a directory, but had [/root/tmp]."); tmp.resolve("aa", StandardResolveOptions.RESOLVE_FILE); }
@Test public void testResolveInWindowsOS() { assumeFalse(SystemUtils.IS_OS_WINDOWS); if (!SystemUtils.IS_OS_WINDOWS) { return; } assertEquals( toResourceIdentifier("C:\\my home\\out put"), toResourceIdentifier("C:\\my home\\") .resolve("out put", StandardResolveOptions.RESOLVE_FILE)); assertEquals( toResourceIdentifier("C:\\out put"), toResourceIdentifier("C:\\my home\\") .resolve("..", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve(".", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("out put", StandardResolveOptions.RESOLVE_FILE)); assertEquals( toResourceIdentifier("C:\\my home\\**\\*"), toResourceIdentifier("C:\\my home\\") .resolve("**", StandardResolveOptions.RESOLVE_DIRECTORY) .resolve("*", StandardResolveOptions.RESOLVE_FILE)); }