Skip to content

Commit

Permalink
Fixing JT-173 (backport of initial PR made by phax)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent SCHOELENS committed Aug 7, 2023
1 parent 949454e commit 42b9fe7
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.Scanner;
Expand Down Expand Up @@ -95,11 +96,74 @@ public static List<File> scanDirectoryForFiles(BuildContext buildContext, final

scanner.scan();

final List<File> files = new ArrayList<File>();
final List<File> files = new ArrayList<>();
for (final String name : scanner.getIncludedFiles()) {
files.add(new File(directory, name).getCanonicalFile());
}

return files;
return reorderFiles(files, directory, includes);
}

private static boolean isWildcard (final String s) {
return s.indexOf ('*') >= 0 || s.indexOf ('?') >= 0;
}

/**
* Reorder the result of "scanner.getIncludedFile" so that the order of the
* source includes is maintained as good as possible. Source wildcard matches
* are postponed to the end.<br>
* Examples:<br>
* If the includes contain [a, b, c] and the resulting list should be in that
* order.<br>
* If the includes contain [a, b*, c] the resulting list should be [a, c,
* matches-of(b*)]
*
* @param resolvedFiles
* resolved files from scanner.getIncludedFiles. May not be
* <code>null</code>.
* @param includes
* The source includes in the correct order. May be <code>null</code>
* or empty.
* @return The ordered list of files, that tries to take the source order as
* good as possible
*/
protected static List <File> reorderFiles(final List<File> resolvedFiles, File directory, final String[] includes) {
if (includes == null || includes.length == 0) {
// return "as is"
return resolvedFiles;
}

final List<File> ret = new ArrayList<>(resolvedFiles.size());
// copy of the initial list in order to avoid changes in resolvedFiles var
List<File> resolvedFilesList = new ArrayList<>(resolvedFiles);
for (final String inc : includes) {
// Only deal with fixed files
if (!isWildcard(inc)) {
// Ensure to use the system path separator
final String sUnifiedInclude = inc.replace('\\', File.separatorChar).replace('/', File.separatorChar);
final File fileInclude = new File(directory, sUnifiedInclude);

// Find all matches in the resolved files list
final List <File> matches = new ArrayList<>();
for (final File resFile : resolvedFilesList) {
if (resFile.getAbsolutePath().equals(fileInclude.getAbsolutePath())) {
matches.add(resFile);
}
}

for (final File match : matches) {
// Add all matches to the result list
ret.add(match);

// Remove from the main list
resolvedFilesList.remove(match);
}
}
}

// Add all remaining resolved files in the order "as is"
ret.addAll(resolvedFilesList);

return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.jvnet.jaxb.maven.util;

import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class IOUtilsTests {

private File directory = new File("/");

@Test
public void reorderFilesIncludesNull() {
List<File> files = new ArrayList<>();
files.add(new File(directory, "a.xsd"));
files.add(new File(directory, "c.xsd"));
files.add(new File(directory, "b.xsd"));
List<File> orderedFiles = IOUtils.reorderFiles(files, directory, null);

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.size(), orderedFiles.size());
Assert.assertEquals(files.get(0), orderedFiles.get(0));
Assert.assertEquals(files.get(1), orderedFiles.get(1));
Assert.assertEquals(files.get(2), orderedFiles.get(2));
}

@Test
public void reorderFilesIncludesEmpty() {
List<File> files = new ArrayList<>();
files.add(new File(directory, "a.xsd"));
files.add(new File(directory, "c.xsd"));
files.add(new File(directory, "b.xsd"));
List<File> orderedFiles = IOUtils.reorderFiles(files, directory, new String[] {});

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.size(), orderedFiles.size());
Assert.assertEquals(files.get(0), orderedFiles.get(0));
Assert.assertEquals(files.get(1), orderedFiles.get(1));
Assert.assertEquals(files.get(2), orderedFiles.get(2));
}

@Test
public void reorderFilesIncludesNoWildcard() {
List<File> files = new ArrayList<>();
files.add(new File(directory, "a.xsd"));
files.add(new File(directory, "c.xsd"));
files.add(new File(directory, "b.xsd"));
List<File> orderedFiles = IOUtils.reorderFiles(files, directory, new String[] { "b.xsd", "c.xsd", "a.xsd" });

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.size(), orderedFiles.size());
Assert.assertEquals(files.get(2), orderedFiles.get(0));
Assert.assertEquals(files.get(1), orderedFiles.get(1));
Assert.assertEquals(files.get(0), orderedFiles.get(2));
}

@Test
public void reorderFilesIncludesNoWildcardWithCommonSuffix() {
List<File> files = new ArrayList<>();
files.add(new File(directory, "a.xsd"));
files.add(new File(directory, "b.xsd"));
files.add(new File(directory, "service-ab.xsd"));
List<File> orderedFiles = IOUtils.reorderFiles(files, directory, new String[] { "b.xsd", "a.xsd", "service-ab.xsd" });

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.size(), orderedFiles.size());
Assert.assertEquals(files.get(1), orderedFiles.get(0));
Assert.assertEquals(files.get(0), orderedFiles.get(1));
Assert.assertEquals(files.get(2), orderedFiles.get(2));
}

@Test
public void reorderFilesIncludesWithWildcard() {
List<File> files = new ArrayList<>();
files.add(new File(directory, "a.xsd"));
files.add(new File(directory, "common/c1.xsd"));
files.add(new File(directory, "b.xsd"));
files.add(new File(directory, "common/c2.xsd"));
files.add(new File(directory, "common/a.xsd"));
files.add(new File(directory, "common/b.xsd"));
List<File> orderedFiles = IOUtils.reorderFiles(files, directory, new String[] { "common/*.xsd", "a.xsd", "b.xsd" });

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.size(), orderedFiles.size());
// a.xsd is the first file
Assert.assertEquals(files.get(0), orderedFiles.get(0));
// b.xsd is the second file
Assert.assertEquals(files.get(2), orderedFiles.get(1));
// and then we have all common/*.xsd files in same order
Assert.assertEquals(files.get(1), orderedFiles.get(2));
Assert.assertEquals(files.get(3), orderedFiles.get(3));
Assert.assertEquals(files.get(4), orderedFiles.get(4));
Assert.assertEquals(files.get(5), orderedFiles.get(5));
}
}

0 comments on commit 42b9fe7

Please sign in to comment.