Skip to content

Commit

Permalink
Issue #10084 - Updating URLResource.getFileName() support
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Jul 8, 2023
1 parent 7875230 commit 10fbabd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,35 @@ public static String getFileName(URI uri)
*/
public static String getFileName(String path)
{
if (path == null || "/".equals(path))
if (path == null)
return "";
if (path.equals("/"))
return "/";
int idx = path.lastIndexOf('/');
if (idx >= 0)
{
if (idx == path.length() - 1)
{
// we found the trailing slash
// eg: file:/path/to/dir/
// we want to return the "dir/" here
int previousSlash = path.lastIndexOf('/', idx - 1);
if (previousSlash >= 0)
{
// we have a previous slash
// so return the segment and trailing slash
return path.substring(previousSlash + 1, idx + 1);
}
else
{
// we have no previous slash
// this input string is something like "foo/"
// so return it all
return path;
}
}
return path.substring(idx + 1);
}
return path;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,14 @@ public static Stream<Arguments> fileNameSource()
{
return Stream.of(
Arguments.of(null, ""),
Arguments.of(URI.create("file:/"), ""),
Arguments.of(URI.create("file:///"), ""),
Arguments.of(URI.create("file:zed/"), ""),
Arguments.of(URI.create("foo:bar"), "bar"),
Arguments.of(URI.create("file:/"), "/"),
Arguments.of(URI.create("file:///"), "/"),
Arguments.of(URI.create("file:zed/"), "zed/"),
Arguments.of(URI.create("file:///path/to/test.txt"), "test.txt"),
Arguments.of(URI.create("file:///path/to/dir/"), ""),
Arguments.of(URI.create("file:///path/to/dir/"), "dir/"),
Arguments.of(URI.create("jar:file:///home/user/libs/jetty-server-12.jar!/org/eclipse/jetty/server/jetty-dir.css"), "jetty-dir.css"),
Arguments.of(URI.create("http://eclipse.org/jetty/"), ""),
Arguments.of(URI.create("http://eclipse.org/jetty/"), "jetty/"),
Arguments.of(URI.create("http://eclipse.org/jetty/index.html"), "index.html"),
Arguments.of(URI.create("http://eclipse.org/jetty/docs.html?query=val#anchor"), "docs.html")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.URIUtil;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
Expand All @@ -44,6 +49,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(WorkDirExtension.class)
public class UrlResourceFactoryTest
{
@Test
Expand All @@ -69,15 +75,15 @@ public void testHttps() throws IOException
assertThat(resource.lastModified().toEpochMilli(), not(Instant.EPOCH));
assertThat(resource.length(), not(-1));
assertTrue(resource.isDirectory());
assertThat(resource.getFileName(), is(""));
assertThat(resource.getFileName(), is("/"));

Resource blogs = resource.resolve("blog/");
assertThat(blogs, notNullValue());
assertTrue(blogs.exists());
assertThat(blogs.lastModified().toEpochMilli(), not(Instant.EPOCH));
assertThat(blogs.length(), not(-1));
assertTrue(blogs.isDirectory());
assertThat(blogs.getFileName(), is(""));
assertThat(blogs.getFileName(), is("blog/"));

Resource favicon = resource.resolve("favicon.ico");
assertThat(favicon, notNullValue());
Expand All @@ -93,6 +99,27 @@ public void testHttps() throws IOException
}
}

@Test
public void testGetFileName(WorkDir workDir) throws IOException
{
Path tmpPath = workDir.getEmptyPathDir();
Path dir = tmpPath.resolve("foo-dir");
FS.ensureDirExists(dir);
Path file = dir.resolve("bar.txt");
Files.writeString(file, "This is bar.txt", StandardCharsets.UTF_8);

URLResourceFactory urlResourceFactory = new URLResourceFactory();

Resource baseResource = urlResourceFactory.newResource(tmpPath);
assertThat(baseResource.getFileName(), endsWith("/"));

Resource dirResource = baseResource.resolve("foo-dir/");
assertThat(dirResource.getFileName(), endsWith("foo-dir/"));

Resource fileResource = dirResource.resolve("bar.txt");
assertThat(fileResource.getFileName(), endsWith("bar.txt"));
}

@Test
public void testInputStreamCleanedUp() throws Exception
{
Expand Down

0 comments on commit 10fbabd

Please sign in to comment.