From c6c46de559211c83d7e8bdf450d17628ae534af4 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Wed, 6 Nov 2024 14:44:57 +0000 Subject: [PATCH] JAL-4485 Tests for FileUtils.getMatchingVersionedFilesTest() --- src/jalview/util/FileUtils.java | 2 - test/jalview/util/FileUtilsTest.java | 182 ++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+), 2 deletions(-) diff --git a/src/jalview/util/FileUtils.java b/src/jalview/util/FileUtils.java index 2c761b3..1170f9c 100644 --- a/src/jalview/util/FileUtils.java +++ b/src/jalview/util/FileUtils.java @@ -403,8 +403,6 @@ public class FileUtils return files; } - Console.debug("Looking for all versions with a blacklist of size " - + versionBlacklist.length); for (String template : templates) { Console.debug("Using template '" + template + "'"); diff --git a/test/jalview/util/FileUtilsTest.java b/test/jalview/util/FileUtilsTest.java index 62dc73e..3631c16 100644 --- a/test/jalview/util/FileUtilsTest.java +++ b/test/jalview/util/FileUtilsTest.java @@ -21,7 +21,10 @@ package jalview.util; import java.io.File; +import java.io.IOException; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.testng.Assert; import org.testng.annotations.DataProvider; @@ -212,4 +215,183 @@ public class FileUtilsTest // }; } + + public static final String root0 = "test/files/tempTestDir"; + + @Test( + groups = "Functional", + dataProvider = "getMatchingVersionedFilesData") + public void getMatchingVersionedFilesTest(String[] filesToMake, + String[] templates, String[] roots, String[] versionWhitelist, + String[] versionBlacklist, String separator, boolean exists, + String[] expectedFilePaths) + { + Set createdFiles = new HashSet<>(); + // create the files + for (String n : filesToMake) + { + if (n != null) + { + File f = new File(root0 + File.separator + n); + f.getParentFile().mkdirs(); + try + { + f.createNewFile(); + createdFiles.add(f); + } catch (IOException e) + { + Assert.fail("Couldn't create file '" + f.getPath() + "'", e); + } + } + } + + Set matchedFiles = new HashSet<>(); + matchedFiles.addAll(FileUtils.getMatchingVersionedFiles(templates, + roots, versionWhitelist, versionBlacklist, separator, exists)); + + Set matchedFilenames = new HashSet<>(); + StringBuilder mSB = new StringBuilder(); + for (File f : matchedFiles) + { + matchedFilenames.add(f.getPath()); + if (mSB.length() > 0) + { + mSB.append(", "); + } + mSB.append(f.getPath()); + } + + StringBuilder eSB = new StringBuilder(); + Set expectedFilenames = new HashSet<>(); + for (int i = 0; i < expectedFilePaths.length; i++) + { + String path = root0 + File.separator + expectedFilePaths[i]; + expectedFilenames.add(path); + if (eSB.length() > 0) + { + eSB.append(", "); + } + eSB.append(path); + } + + Assert.assertEquals(matchedFilenames, expectedFilenames, + "Did not find the right set of filenames." + + "\nMatched files : " + mSB.toString() + " ." + + "\nExpected files: " + eSB.toString() + " .\n"); + + // delete created files + for (File f : createdFiles) + { + f.delete(); + } + rmEmptyDirs(root0); + } + + private static final boolean rmEmptyDirs(String path) + { + if (path.startsWith("/") || path.contains("..")) + { + System.err.println("Not running rmDirs on '" + path + + "'. Considered a bit dangerous."); + return false; + } + File f = new File(path); + if (!f.isDirectory()) + { + return false; + } + for (File c : f.listFiles()) + { + if (!rmEmptyDirs(c.getPath())) + { + return false; + } + } + f.delete(); + return true; + } + + @DataProvider(name = "getMatchingVersionedFilesData") + public Object[][] getMatchingVersionedFilesData() + { + String file1 = "dir/Applications/File-1/bin/file.exe"; + String file2 = "dir/Applications/File-2/bin/file.exe"; + String file3 = "user/Applications/File-3/bin/file.exe"; + String file4a = "dir/Applications/File-4/bin/file.exe"; + String file4b = "user/Applications/File-4/bin/file.exe"; + String filea = "dir/Applications/File/bin/file.exe"; + String fileb = "user/Applications/File/bin/file.exe"; + return new Object[][] { + /* + * String[] filesToMake + * String[] templates + * String[] roots + * String[] versionWhitelist + * String[] versionBlacklist + * String separator + * boolean exists + * String[] expectedFilePaths + */ + /* + */ + // empty whitelist and blacklist + { new String[] + { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" }, + new String[] + { root0 + "/dir/Applications", root0 + "/user/Applications" }, + new String[] {}, new String[] {}, "-", true, new String[] + { file1, file2, file3 } }, + // no whitelist or blacklist + { new String[] + { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" }, + new String[] + { root0 + "/dir/Applications", root0 + "/user/Applications" }, + null, null, "-", true, new String[] {} }, + // no blacklist + { new String[] + { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" }, + new String[] + { root0 + "/dir/Applications", root0 + "/user/Applications" }, + new String[] + { "1", "3" }, null, "-", true, new String[] { file1, file3 } }, + // no whitelist + { new String[] + { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" }, + new String[] + { root0 + "/dir/Applications", root0 + "/user/Applications" }, + null, new String[] + { "1", "3" }, "-", true, new String[] { file2 } }, + // whitelist (but only if exists) and blacklist + { new String[] + { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" }, + new String[] + { root0 + "/dir/Applications", root0 + "/user/Applications" }, + new String[] + { "4" }, new String[] { "1", "3" }, "-", true, + new String[] + { file2 } }, + // whitelist (if exists or not) and blacklist + { new String[] + { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" }, + new String[] + { root0 + "/dir/Applications", root0 + "/user/Applications" }, + new String[] + { "4" }, new String[] { "1", "3" }, "-", false, + new String[] + { file2, file4a, file4b, filea, fileb } }, + // whitelist (if exists or not) and blacklist but no separator so no + // unversioned paths + { new String[] + { file1, file2, file3 }, new String[] { "%s/File-%s/bin/file.exe" }, + new String[] + { root0 + "/dir/Applications", root0 + "/user/Applications" }, + new String[] + { "4" }, new String[] { "1", "3" }, null, false, + new String[] + { file2, file4a, file4b } }, + /* + */ + // + }; + } } -- 1.7.10.2