Simplify resolve(String) a bit (#8741)
This commit is contained in:
parent
259deea2f7
commit
5b91e70c1b
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue