Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 16:11:35 GMT
  2. Package jalview.util

File FileUtilsTest.java

 

Code metrics

24
96
14
1
465
353
28
0.29
6.86
14
2

Classes

Class Line # Actions
FileUtilsTest 37 96 28
0.708955270.9%
 

Contributing tests

This file is covered by 45 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.util;
22   
23    import java.io.File;
24    import java.io.IOException;
25    import java.nio.file.Paths;
26    import java.util.HashSet;
27    import java.util.List;
28    import java.util.Set;
29   
30    import org.testng.Assert;
31    import org.testng.annotations.DataProvider;
32    import org.testng.annotations.Test;
33    import static org.testng.Assert.assertFalse;
34    import static org.testng.Assert.assertTrue;
35   
36    @Test
 
37    public class FileUtilsTest
38    {
 
39  11 toggle @Test(groups = "Functional", dataProvider = "patternsAndMinNumFiles")
40    public void testJavaFileGlob(String pattern, int atLeast, int atMost)
41    {
42  11 List<File> files = FileUtils.getFilesFromGlob(pattern);
43  11 if (atLeast != -1)
44    {
45  11 Assert.assertTrue(files.size() > atLeast,
46    "Did not find more than " + atLeast + " files with " + pattern
47    + " (found " + files.size() + ")");
48    }
49  11 if (atLeast != -1)
50    {
51  11 Assert.assertTrue(files.size() > atLeast,
52    "Did not find more than " + atLeast + " files with " + pattern
53    + " (found " + files.size() + ")");
54    }
55  11 if (atMost != -1)
56    {
57  11 Assert.assertTrue(files.size() < atMost,
58    "Did not find fewer than " + atMost + " files with " + pattern
59    + " (found " + files.size() + ")");
60    }
61    }
62   
 
63  4 toggle @Test(groups = "Functional", dataProvider = "dirnamesAndBasenames")
64    public void testDirnamesAndBasenames(String filename, String dirname,
65    String endsWith, String basename, String notInDirname)
66    {
67  4 File file = new File(filename);
68  4 String d = FileUtils.getDirname(file);
69  4 String b = FileUtils.getBasename(file);
70  4 Assert.assertEquals(b, basename);
71  4 Assert.assertTrue(d.startsWith(dirname), "getDirname(" + file.getPath()
72    + ")=" + d + " didn't start with '" + dirname + "'");
73  4 Assert.assertTrue(d.endsWith(endsWith), "getDirname(" + file.getPath()
74    + ")=" + d + " didn't end with '" + endsWith + "'");
75   
76    // ensure dirname doesn't end with basename (which means you can't use same
77    // filename as dir in tests!)
78  4 Assert.assertFalse(d.endsWith(b), "Processed dirname '" + d
79    + "' ends with '" + b + "' when it shouldn't");
80   
81  4 if (notInDirname != null)
82  4 Assert.assertFalse(d.contains(notInDirname), "Processed directory '"
83    + d + "' contains '" + notInDirname + "' when it shouldn't");
84    }
85   
86    /**
87    * these need to be maintained as jalview's source base grows
88    *
89    * @return
90    */
 
91  1 toggle @DataProvider(name = "patternsAndMinNumFiles")
92    public Object[][] patternsAndMinNumFiles()
93    {
94  1 return new Object[][] { { "src/**/*.java", 900, 100000 },
95    { "src/**.java", 900, 100000 },
96    { "test/**/*.java", 250, 2500 },
97    { "test/**.java", 250, 2500 },
98    { "help/**/*.html", 100, 1000 },
99    { "test/**/F*.java", 15, 150 },
100    { "test/jalview/*/F*.java", 10, 15 }, // 12 at time of writing
101    { "test/jalview/**/F*.java", 18, 30 }, // 20 at time of writing
102    { "test/jalview/util/F**.java", 1, 5 }, // 2 at time of writing
103    { "src/jalview/b*/*.java", 18, 22 }, // 19 at time of writing
104    { "src/jalview/b**/*.java", 23, 50 }, // 22 at time of writing, now 27
105    };
106    }
107   
 
108  1 toggle @DataProvider(name = "dirnamesAndBasenames")
109    public Object[][] dirnamesAndBasenames()
110    {
111  1 String homeDir = new File(System.getProperty("user.home")).getPath();
112  1 return new Object[][] { // -1=startsWith, 0=equals, 1=endsWith
113    { "~/hello/sailor", homeDir, "/hello", "sailor", "~" }, //
114    { "./examples/uniref50.fa", "./", "examples", "uniref50", "Users" }, //
115    { "./examples/uniref50.1.fa", "./", "examples", "uniref50.1",
116    "Users" }, //
117    { "examples/uniref50.fa", "examples", "examples", "uniref50",
118    ".fa" }, //
119    };
120    }
121   
 
122  16 toggle @Test(groups = "Functional", dataProvider = "convertWildcardsToPathData")
123    public void convertWildcardsToPathTest(String value, String wildcard,
124    String dirname, String basename, String path)
125    {
126   
127  16 Assert.assertEquals(
128    FileUtils.convertWildcardsToPath(value, wildcard, dirname,
129    basename),
130    path, "Conversion of wildcard output path is not right.");
131   
132    }
133   
 
134  1 toggle @DataProvider(name = "convertWildcardsToPathData")
135    public Object[][] convertWildcardsToPathData()
136    {
137  1 return new Object[][] {
138    /*
139    * cmdline args
140    * Arg (null if only testing headless)
141    * String value if there is one (null otherwise)
142    * boolean value if String value is null
143    * expected value of isHeadless()
144    */
145    /*
146    */
147    { "*/*", "*", "{dirname}", "{basename}", "{dirname}/{basename}" },
148    { "*/", "*", "{dirname}", "{basename}", "{dirname}/" },
149    { "/*", "*", "{dirname}", "{basename}", "/{basename}" },
150    { "*", "*", "{dirname}", "{basename}", "{basename}" },
151    { "tmp/output/*/file-*.stk", "*", "{dirname}", "{basename}",
152    "tmp/output/{dirname}/file-{basename}.stk" },
153    { "/*.file", "*", "{dirname}", "{basename}", "/{basename}.file" },
154    { "*/file.stk", "*", "{dirname}", "{basename}",
155    "{dirname}/file.stk" },
156    { "tmp/abc*def/file.stk", "*", "{dirname}", "{basename}",
157    "tmp/abc{dirname}def/file.stk" },
158    { "a*b/c*d", "*", "{dirname}", "{basename}",
159    "a{dirname}b/c{basename}d" },
160    { "a*b/c", "*", "{dirname}", "{basename}", "a{dirname}b/c" },
161    { "a/b*c", "*", "{dirname}", "{basename}", "a/b{basename}c" },
162    { "a*b", "*", "{dirname}", "{basename}", "a{basename}b" },
163    { "aSTARb/cSTARd", "STAR", "BEFORE", "AFTER", "aBEFOREb/cAFTERd" },
164    { "aSTARb/c", "STAR", "BEFORE", "AFTER", "aBEFOREb/c" },
165    { "a/bSTARc", "STAR", "BEFORE", "AFTER", "a/bAFTERc" },
166    { "aSTARb", "STAR", "BEFORE", "AFTER", "aAFTERb" },
167    //
168    };
169    }
170   
 
171  15 toggle @Test(
172    groups = "Functional",
173    dataProvider = "stringFilenamesBaseAndExtensionsData")
174    public void stringGetBaseAndExtensionTest(String filename,
175    String extension, String base)
176    {
177  15 String thisBase = FileUtils.getBase(filename);
178  15 Assert.assertEquals(thisBase, base,
179    "base part of path and filename not as expected");
180  15 String thisExtension = FileUtils.getExtension(filename);
181  15 Assert.assertEquals(thisExtension, extension,
182    "extension part of filename not as expected");
183    }
184   
 
185  1 toggle @DataProvider(name = "stringFilenamesBaseAndExtensionsData")
186    public Object[][] stringFilenamesBaseAndExtensionsData()
187    {
188  1 return new Object[][] {
189    /*
190    * String full URL or path
191    * String base the above but without the extension if there is one
192    * String extension the filename extension if there is one
193    */
194    /*
195    */
196    { "/examples/uniref50.fa", "fa", "/examples/uniref50." },
197    { "/examples/uniref50", null, "/examples/uniref50" },
198    { "/examples/.uniref50", null, "/examples/.uniref50" },
199    { "/exampl.es/uniref50", null, "/exampl.es/uniref50" },
200    { "/examples/uniref50.", "", "/examples/uniref50." },
201    { "examples/uniref50.fa", "fa", "examples/uniref50." },
202    { "examples/uniref50", null, "examples/uniref50" },
203    { "examples/.uniref50", null, "examples/.uniref50" },
204    { "exampl.es/uniref50", null, "exampl.es/uniref50" },
205    { "examples/uniref50.", "", "examples/uniref50." },
206    { "https://www.jalview.org:443/examples/uniref50.fa", "fa",
207    "https://www.jalview.org:443/examples/uniref50." },
208    { "https://www.jalview.org:443/examples/uniref50", null,
209    "https://www.jalview.org:443/examples/uniref50" },
210    { "https://www.jalview.org:443/examples/.uniref50", null,
211    "https://www.jalview.org:443/examples/.uniref50" },
212    { "https://www.jalview.org:443/exampl.es/uniref50", null,
213    "https://www.jalview.org:443/exampl.es/uniref50" },
214    { "https://www.jalview.org:443/examples/uniref50.", "",
215    "https://www.jalview.org:443/examples/uniref50." },
216    /*
217    */
218    //
219    };
220    }
221   
222    public static final String root0 = "test/files/tempTestDir";
223   
 
224  7 toggle @Test(
225    groups = "Functional",
226    dataProvider = "getMatchingVersionedFilesData")
227    public void getMatchingVersionedFilesTest(String[] filesToMake,
228    String[] templates, String[] roots, String[] versionWhitelist,
229    String[] versionBlacklist, String separator, boolean exists,
230    String[] expectedFilePaths)
231    {
232  7 Set<File> createdFiles = new HashSet<>();
233    // create the files
234  7 for (String n : filesToMake)
235    {
236  21 if (n != null)
237    {
238  21 File f = new File(root0 + File.separator + n);
239  21 f.getParentFile().mkdirs();
240  21 try
241    {
242  21 f.createNewFile();
243  21 createdFiles.add(f);
244    } catch (IOException e)
245    {
246  0 Assert.fail("Couldn't create file '" + f.getPath() + "'", e);
247    }
248    }
249    }
250   
251  7 Set<File> matchedFiles = new HashSet<>();
252  7 matchedFiles.addAll(FileUtils.getMatchingVersionedFiles(templates,
253    roots, versionWhitelist, versionBlacklist, separator, exists));
254   
255  7 Set<String> matchedFilenames = new HashSet<>();
256  7 StringBuilder mSB = new StringBuilder();
257  7 for (File f : matchedFiles)
258    {
259  15 matchedFilenames.add(f.getPath());
260  15 if (mSB.length() > 0)
261    {
262  9 mSB.append(", ");
263    }
264  15 mSB.append(f.getPath());
265    }
266   
267  7 StringBuilder eSB = new StringBuilder();
268  7 Set<String> expectedFilenames = new HashSet<>();
269  22 for (int i = 0; i < expectedFilePaths.length; i++)
270    {
271  15 String path = root0 + File.separator + expectedFilePaths[i];
272  15 expectedFilenames.add(path);
273  15 if (eSB.length() > 0)
274    {
275  9 eSB.append(", ");
276    }
277  15 eSB.append(path);
278    }
279   
280  7 Assert.assertEquals(matchedFilenames, expectedFilenames,
281    "Did not find the right set of filenames."
282    + "\nMatched files : " + mSB.toString() + " ."
283    + "\nExpected files: " + eSB.toString() + " .\n");
284   
285    // delete created files
286  7 for (File f : createdFiles)
287    {
288  21 f.delete();
289    }
290  7 rmEmptyDirs(root0);
291    }
292   
 
293  77 toggle private static final boolean rmEmptyDirs(String path)
294    {
295  77 if (path.startsWith("/") || path.contains(".."))
296    {
297  0 System.err.println("Not running rmDirs on '" + path
298    + "'. Considered a bit dangerous.");
299  0 return false;
300    }
301  77 File f = new File(path);
302  77 if (!f.isDirectory())
303    {
304  0 return false;
305    }
306  77 for (File c : f.listFiles())
307    {
308  70 if (!rmEmptyDirs(c.getPath()))
309    {
310  0 return false;
311    }
312    }
313  77 f.delete();
314  77 return true;
315    }
316   
 
317  1 toggle @DataProvider(name = "getMatchingVersionedFilesData")
318    public Object[][] getMatchingVersionedFilesData()
319    {
320  1 String file1 = "dir/Applications/File-1/bin/file.exe";
321  1 String file2 = "dir/Applications/File-2/bin/file.exe";
322  1 String file3 = "user/Applications/File-3/bin/file.exe";
323  1 String file4a = "dir/Applications/File-4/bin/file.exe";
324  1 String file4b = "user/Applications/File-4/bin/file.exe";
325  1 String filea = "dir/Applications/File/bin/file.exe";
326  1 String fileb = "user/Applications/File/bin/file.exe";
327  1 return new Object[][] {
328    /*
329    * String[] filesToMake
330    * String[] templates
331    * String[] roots
332    * String[] versionWhitelist
333    * String[] versionBlacklist
334    * String separator
335    * boolean exists
336    * String[] expectedFilePaths
337    */
338    /*
339    */
340    // empty whitelist and blacklist
341    { new String[]
342    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
343    new String[]
344    { root0 + "/dir/Applications", root0 + "/user/Applications" },
345    new String[] {}, new String[] {}, "-", true, new String[]
346    { file1, file2, file3 } },
347    // no whitelist or blacklist
348    { new String[]
349    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
350    new String[]
351    { root0 + "/dir/Applications", root0 + "/user/Applications" },
352    null, null, "-", true, new String[] {} },
353    // no blacklist
354    { new String[]
355    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
356    new String[]
357    { root0 + "/dir/Applications", root0 + "/user/Applications" },
358    new String[]
359    { "1", "3" }, null, "-", true, new String[] { file1, file3 } },
360    // no whitelist
361    { new String[]
362    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
363    new String[]
364    { root0 + "/dir/Applications", root0 + "/user/Applications" },
365    null, new String[]
366    { "1", "3" }, "-", true, new String[] { file2 } },
367    // whitelist (but only if exists) and blacklist
368    { new String[]
369    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
370    new String[]
371    { root0 + "/dir/Applications", root0 + "/user/Applications" },
372    new String[]
373    { "4" }, new String[] { "1", "3" }, "-", true,
374    new String[]
375    { file2 } },
376    // whitelist (if exists or not) and blacklist
377    { new String[]
378    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
379    new String[]
380    { root0 + "/dir/Applications", root0 + "/user/Applications" },
381    new String[]
382    { "4" }, new String[] { "1", "3" }, "-", false,
383    new String[]
384    { file2, file4a, file4b, filea, fileb } },
385    // whitelist (if exists or not) and blacklist but no separator so no
386    // unversioned paths
387    { new String[]
388    { file1, file2, file3 }, new String[] { "%s/File-%s/bin/file.exe" },
389    new String[]
390    { root0 + "/dir/Applications", root0 + "/user/Applications" },
391    new String[]
392    { "4" }, new String[] { "1", "3" }, null, false,
393    new String[]
394    { file2, file4a, file4b } },
395    /*
396    */
397    //
398    };
399    }
400    /*
401    * FIXME fails on bamboo since it the working directory named according to the build number
402    */
 
403  0 toggle @Test(groups = {"Functional","Not-bamboo"})
404    public void testFindMatchingPaths() throws IOException
405    {
406  0 String expect1 = Paths.get("..", "jalview", "examples", "plantfdx.fa")
407    .toString();
408  0 String expect2 = Paths.get("../jalview/examples/plantfdx.features")
409    .toString();
410  0 String expect3 = Paths
411    .get("../jalview/examples/testdata/plantfdx.features")
412    .toString();
413   
414  0 List<String> matches = FileUtils
415    .findMatchingPaths(Paths.get(".."),
416    ".*[\\\\/]plant.*\\.f.*");
417  0 System.out.println(matches);
418  0 assertTrue(matches.contains(expect1));
419  0 assertTrue(matches.contains(expect2));
420  0 assertTrue(matches.contains(expect3));
421    }
422   
 
423  0 toggle @Test(groups = "External")
424    public void testWindowsPath() throws IOException
425    {
426  0 if (System.getProperty("os.name").startsWith("Windows"))
427    {
428    /*
429    * should pass provided Eclipse is installed
430    */
431  0 List<String> matches = FileUtils.findMatches("C:\\",
432    "Program Files*/eclips*/eclips?.exe");
433  0 assertFalse(matches.isEmpty());
434   
435    /*
436    * should pass provided Chimera is installed
437    */
438  0 matches = FileUtils.findMatches("C:\\",
439    "Program Files*/Chimera*/bin/{chimera,chimera.exe}");
440  0 assertFalse(matches.isEmpty());
441    }
442    }
443   
444    /*
445    * FIXME fails on bamboo since it the working directory named according to the build number
446    */
 
447  0 toggle @Test(groups = {"Functional","Not-bamboo"})
448    public void testFindMatches() throws IOException
449    {
450  0 String expect1 = Paths.get("..", "jalview", "examples", "plantfdx.fa")
451    .toString();
452  0 String expect2 = Paths.get("../jalview/examples/plantfdx.features")
453    .toString();
454  0 String expect3 = Paths
455    .get("../jalview/examples/testdata/plantfdx.features")
456    .toString();
457   
458  0 List<String> matches = FileUtils
459    .findMatches("..", "jalview/ex*/plant*.f*");
460  0 System.out.println(matches);
461  0 assertTrue(matches.contains(expect1));
462  0 assertTrue(matches.contains(expect2));
463  0 assertFalse(matches.contains(expect3));
464    }
465    }