Fixes #5521 ResourceCollection list NPE (#5523)

Fixes #5521 ResourceCollection list NPE
This commit is contained in:
Greg Wilkins 2020-10-28 15:23:29 +01:00 committed by GitHub
parent 47885f7ecb
commit 6698a314ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 7 deletions

View File

@ -571,11 +571,7 @@ public class PathResource extends Resource
int size = entries.size();
return entries.toArray(new String[size]);
}
catch (DirectoryIteratorException e)
{
LOG.debug(e);
}
catch (IOException e)
catch (DirectoryIteratorException | IOException e)
{
LOG.debug(e);
}

View File

@ -434,11 +434,12 @@ public class ResourceCollection extends Resource
public String[] list()
{
assertResourcesSet();
HashSet<String> set = new HashSet<>();
for (Resource r : _resources)
{
Collections.addAll(set, r.list());
String[] list = r.list();
if (list != null)
Collections.addAll(set, list);
}
String[] result = set.toArray(new String[0]);
Arrays.sort(result);

View File

@ -22,6 +22,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.Arrays;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
@ -32,7 +33,9 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -174,6 +177,20 @@ public class ResourceCollectionTest
});
}
@Test
public void testList() throws Exception
{
ResourceCollection rc1 = new ResourceCollection(
Resource.newResource("src/test/resources/org/eclipse/jetty/util/resource/one/"),
Resource.newResource("src/test/resources/org/eclipse/jetty/util/resource/two/"),
Resource.newResource("src/test/resources/org/eclipse/jetty/util/resource/three/"));
assertThat(Arrays.asList(rc1.list()), contains("1.txt", "2.txt", "3.txt", "dir/"));
assertThat(Arrays.asList(rc1.addPath("dir").list()), contains("1.txt", "2.txt", "3.txt"));
assertThat(rc1.addPath("unknown").list(), nullValue());
// TODO for jetty-10 assertThat(rc1.addPath("unknown").list(), nullValue());
}
@Test
public void testMultipleSources1() throws Exception
{