Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Restrict TestResourceLoader from search of non-classpath resources in subfolders #2421

Merged
merged 3 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,9 +54,19 @@ public Resource[] getResources(String resourceLocation, String resourcePattern)
try
{
String normalizedResourceLocation = StringUtils.appendIfMissing(resourceLocation, SEPARATOR);
String locationPattern = resourceLocation.startsWith(FILE_URL_PREFIX)
? normalizedResourceLocation : CLASSPATH_ALL_URL_PREFIX + normalizedResourceLocation;
Resource[] allResources = resourcePatternResolver.getResources(locationPattern + "**/" + resourcePattern);
String locationPattern;
String fullLocationPattern;
if (resourceLocation.startsWith(FILE_URL_PREFIX))
{
locationPattern = normalizedResourceLocation;
fullLocationPattern = locationPattern + resourcePattern;
}
else
{
locationPattern = CLASSPATH_ALL_URL_PREFIX + normalizedResourceLocation;
fullLocationPattern = locationPattern + "**/" + resourcePattern;
}
Resource[] allResources = resourcePatternResolver.getResources(fullLocationPattern);
List<URL> resourceUrls = new ArrayList<>(allResources.length);
for (Resource resource : allResources)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -65,7 +65,7 @@ private void initLoader(Map<String, String> props)
testResourceLoader = new TestResourceLoader(propertyParser, resourcePatternResolver);
}

private Resource[] mockGetAllResources(String prefix, String... urls) throws IOException
private Resource[] mockResources(String... urls) throws IOException
{
Resource[] allResources = new Resource[urls.length];
for (int i = 0; i < urls.length; i++)
Expand All @@ -74,18 +74,26 @@ private Resource[] mockGetAllResources(String prefix, String... urls) throws IOE
when(resource.getURL()).thenReturn(new URL(urls[i]));
allResources[i] = resource;
}
return allResources;
}

private Resource[] mockGetAllResources(String prefix, String... urls) throws IOException
{
Resource[] allResources = mockResources(urls);
when(resourcePatternResolver.getResources(startsWith(prefix))).thenReturn(allResources);
return allResources;
}

@Test
void testGetFileResources() throws IOException
{
var path = "file:///C:/Users/test/";
var fileName = "temp.table";
initLoader(Map.of(BRAND_KEY, BRAND_VALUE));
var allResources = mockGetAllResources("file://",
"file:///C:/Users/test/vividus/ca/temp.table", "file:///C:/Users/test/vividus/temp.table");
Resource[] allResources = mockResources("file:///C:/Users/test/vividus/ca/temp.table");
when(resourcePatternResolver.getResources(path + fileName)).thenReturn(allResources);

var actualResources = testResourceLoader.getResources("file:///C:/Users/test/", "temp.table");
var actualResources = testResourceLoader.getResources(path, fileName);
assertArrayEquals(allResources, actualResources);
}

Expand Down