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;
//
};
}
+
+ 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<File> 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<File> matchedFiles = new HashSet<>();
+ matchedFiles.addAll(FileUtils.getMatchingVersionedFiles(templates,
+ roots, versionWhitelist, versionBlacklist, separator, exists));
+
+ Set<String> 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<String> 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 } },
+ /*
+ */
+ //
+ };
+ }
}