Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

SPR-16838 PathMatchingResourcePatternResolver should enforce consistent alphabetical sorting of directory content #1832

Closed
wants to merge 1 commit into from
Closed
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
Expand Up @@ -790,7 +790,10 @@ protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> r
}
return;
}
Arrays.sort(dirContents);

// enforce consistent alphabetical sorting to avoid different order between OS
Arrays.sort(dirContents, (f1, f2) -> f1.getName().compareTo(f2.getName()));

for (File content : dirContents) {
String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/");
if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

package org.springframework.core.io.support;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

import org.junit.rules.TemporaryFolder;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -56,6 +59,8 @@ public class PathMatchingResourcePatternResolverTests {

private PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test(expected = FileNotFoundException.class)
public void invalidPrefixWithPatternElementInIt() throws IOException {
Expand Down Expand Up @@ -117,6 +122,27 @@ public void rootPatternRetrievalInJarFiles() throws IOException {
}
assertTrue("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar", found);
}


@Test
public void testConsistentFileOrder() throws IOException {

List<String> expectedFileNames = new ArrayList<>();

expectedFileNames.add(folder.newFile("A.txt").getName());
expectedFileNames.add(folder.newFile("B.txt").getName());
expectedFileNames.add(folder.newFile("P.txt").getName());
File newFolder = folder.newFolder("message");
expectedFileNames.add(File.createTempFile("Message", ".txt", newFolder).getName());

Set<File> matchingFiles = resolver.retrieveMatchingFiles(folder.getRoot(), "**/*.txt");
assertEquals(expectedFileNames.size(), matchingFiles.size());
int i = 0;
for (File file : matchingFiles) {
assertEquals(expectedFileNames.get(i), file.getName());
i++;
}
}


private void assertProtocolAndFilenames(Resource[] resources, String protocol, String... filenames)
Expand Down