Simplify resolve(String) a bit (#8741)

This commit is contained in:
Joakim Erdfelt 2022-10-20 06:22:21 -05:00 committed by GitHub
parent 259deea2f7
commit 5b91e70c1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 11 deletions

View File

@ -285,17 +285,6 @@ public class PathResource extends Resource
if (URIUtil.SLASH.equals(subUriPath))
return this;
// Sub-paths are always resolved under the given URI,
// we compensate for input sub-paths like "/subdir"
// where default resolve behavior would be to treat
// that like an absolute path.
while (subUriPath.startsWith(URIUtil.SLASH))
{
// TODO XXX this appears entirely unnecessary and inefficient. We already have utilities
// to handle appending path strings with/without slashes.
subUriPath = subUriPath.substring(1);
}
URI uri = getURI();
URI resolvedUri = URIUtil.addPath(uri, subUriPath);
Path path = Paths.get(resolvedUri);

View File

@ -378,6 +378,19 @@ public class ResourceTest
assertThrows(IllegalArgumentException.class, () -> resource.resolve("./../bar"));
}
@Test
public void testResolveStartsWithSlash(WorkDir workDir)
{
Path testdir = workDir.getEmptyPathDir();
Path foo = testdir.resolve("foo");
FS.ensureDirExists(foo);
Resource resourceBase = resourceFactory.newResource(testdir);
// test that a resolve starting with `/` works.
Resource resourceDir = resourceBase.resolve("/foo");
assertTrue(Resources.exists(resourceDir));
assertThat(resourceDir.getURI(), is(foo.toUri()));
}
@Test
public void testNewResourcePathDoesNotExist(WorkDir workDir)
{