Skip to content

Commit

Permalink
Fixes #5521 ResourceCollection NPE
Browse files Browse the repository at this point in the history
Fix constructor and addPath so that all resources in a RC must exist when created.
  • Loading branch information
gregw committed Oct 28, 2020
1 parent c793b54 commit a1e8375
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
Expand Up @@ -62,8 +62,19 @@ public ResourceCollection()
* @param resources the resources to be added to collection
*/
public ResourceCollection(Resource... resources)
{
this(Arrays.asList(resources));
}

/**
* Instantiates a new resource collection.
*
* @param resources the resources to be added to collection
*/
public ResourceCollection(Collection<Resource> resources)
{
_resources = new ArrayList<>();

for (Resource r : resources)
{
if (r == null)
Expand All @@ -82,17 +93,6 @@ public ResourceCollection(Resource... resources)
}
}

/**
* Instantiates a new resource collection.
*
* @param resources the resources to be added to collection
*/
public ResourceCollection(Collection<Resource> resources)
{
_resources = new ArrayList<>();
_resources.addAll(resources);
}

/**
* Instantiates a new resource collection.
*
Expand Down Expand Up @@ -247,27 +247,28 @@ public Resource addPath(String path) throws IOException
ArrayList<Resource> resources = null;

// Attempt a simple (single) Resource lookup that exists
Resource addedResource = null;
for (Resource res : _resources)
{
Resource r = res.addPath(path);
if (!r.isDirectory() && r.exists())
{
// Return simple (non-directory) Resource
return r;
}

addedResource = res.addPath(path);
if (!addedResource.exists())
continue;
if (!addedResource.isDirectory())
return addedResource; // Return simple (non-directory) Resource
if (resources == null)
{
resources = new ArrayList<>();
}
resources.add(addedResource);
}

resources.add(r);
if (resources == null)
{
if (addedResource != null)
return addedResource; // This will not exist
return EmptyResource.INSTANCE;
}

if (resources.size() == 1)
{
return resources.get(0);
}

return new ResourceCollection(resources);
}
Expand Down
Expand Up @@ -35,7 +35,6 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -189,7 +188,7 @@ public void testList() throws Exception

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(), emptyArray());
assertThat(rc1.addPath("unknown").list(), nullValue());
}

@Test
Expand Down

0 comments on commit a1e8375

Please sign in to comment.