Clover icon

Coverage Report

  1. Project Clover database Wed Nov 13 2024 16:21:17 GMT
  2. Package jalview.util

File FileUtilsTest.java

 

Code metrics

22
75
11
1
397
302
24
0.32
6.82
11
2.18

Classes

Class Line # Actions
FileUtilsTest 34 75 24
0.879629688%
 

Contributing tests

This file is covered by 38 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.util.HashSet;
26    import java.util.List;
27    import java.util.Set;
28   
29    import org.testng.Assert;
30    import org.testng.annotations.DataProvider;
31    import org.testng.annotations.Test;
32   
33    @Test
 
34    public class FileUtilsTest
35    {
 
36  11 toggle @Test(groups = "Functional", dataProvider = "patternsAndMinNumFiles")
37    public void testJavaFileGlob(String pattern, int atLeast, int atMost)
38    {
39  11 List<File> files = FileUtils.getFilesFromGlob(pattern);
40  11 if (atLeast != -1)
41    {
42  11 Assert.assertTrue(files.size() > atLeast,
43    "Did not find more than " + atLeast + " files with " + pattern
44    + " (found " + files.size() + ")");
45    }
46  11 if (atLeast != -1)
47    {
48  11 Assert.assertTrue(files.size() > atLeast,
49    "Did not find more than " + atLeast + " files with " + pattern
50    + " (found " + files.size() + ")");
51    }
52  11 if (atMost != -1)
53    {
54  11 Assert.assertTrue(files.size() < atMost,
55    "Did not find fewer than " + atMost + " files with " + pattern
56    + " (found " + files.size() + ")");
57    }
58    }
59   
 
60  4 toggle @Test(groups = "Functional", dataProvider = "dirnamesAndBasenames")
61    public void testDirnamesAndBasenames(String filename, String dirname,
62    String endsWith, String basename, String notInDirname)
63    {
64  4 File file = new File(filename);
65  4 String d = FileUtils.getDirname(file);
66  4 String b = FileUtils.getBasename(file);
67  4 Assert.assertEquals(b, basename);
68  4 Assert.assertTrue(d.startsWith(dirname), "getDirname(" + file.getPath()
69    + ")=" + d + " didn't start with '" + dirname + "'");
70  4 Assert.assertTrue(d.endsWith(endsWith), "getDirname(" + file.getPath()
71    + ")=" + d + " didn't end with '" + endsWith + "'");
72   
73    // ensure dirname doesn't end with basename (which means you can't use same
74    // filename as dir in tests!)
75  4 Assert.assertFalse(d.endsWith(b), "Processed dirname '" + d
76    + "' ends with '" + b + "' when it shouldn't");
77   
78  4 if (notInDirname != null)
79  4 Assert.assertFalse(d.contains(notInDirname), "Processed directory '"
80    + d + "' contains '" + notInDirname + "' when it shouldn't");
81    }
82   
83    /**
84    * these need to be maintained as jalview's source base grows
85    *
86    * @return
87    */
 
88  1 toggle @DataProvider(name = "patternsAndMinNumFiles")
89    public Object[][] patternsAndMinNumFiles()
90    {
91  1 return new Object[][] { { "src/**/*.java", 900, 100000 },
92    { "src/**.java", 900, 100000 },
93    { "test/**/*.java", 250, 2500 },
94    { "test/**.java", 250, 2500 },
95    { "help/**/*.html", 100, 1000 },
96    { "test/**/F*.java", 15, 150 },
97    { "test/jalview/*/F*.java", 10, 15 }, // 12 at time of writing
98    { "test/jalview/**/F*.java", 18, 30 }, // 20 at time of writing
99    { "test/jalview/util/F**.java", 1, 5 }, // 2 at time of writing
100    { "src/jalview/b*/*.java", 14, 19 }, // 15 at time of writing
101    { "src/jalview/b**/*.java", 23, 26 }, // 22 at time of writing
102    };
103    }
104   
 
105  1 toggle @DataProvider(name = "dirnamesAndBasenames")
106    public Object[][] dirnamesAndBasenames()
107    {
108  1 String homeDir = new File(System.getProperty("user.home")).getPath();
109  1 return new Object[][] { // -1=startsWith, 0=equals, 1=endsWith
110    { "~/hello/sailor", homeDir, "/hello", "sailor", "~" }, //
111    { "./examples/uniref50.fa", "./", "examples", "uniref50", "Users" }, //
112    { "./examples/uniref50.1.fa", "./", "examples", "uniref50.1",
113    "Users" }, //
114    { "examples/uniref50.fa", "examples", "examples", "uniref50",
115    ".fa" }, //
116    };
117    }
118   
 
119  16 toggle @Test(groups = "Functional", dataProvider = "convertWildcardsToPathData")
120    public void convertWildcardsToPathTest(String value, String wildcard,
121    String dirname, String basename, String path)
122    {
123   
124  16 Assert.assertEquals(
125    FileUtils.convertWildcardsToPath(value, wildcard, dirname,
126    basename),
127    path, "Conversion of wildcard output path is not right.");
128   
129    }
130   
 
131  1 toggle @DataProvider(name = "convertWildcardsToPathData")
132    public Object[][] convertWildcardsToPathData()
133    {
134  1 return new Object[][] {
135    /*
136    * cmdline args
137    * Arg (null if only testing headless)
138    * String value if there is one (null otherwise)
139    * boolean value if String value is null
140    * expected value of isHeadless()
141    */
142    /*
143    */
144    { "*/*", "*", "{dirname}", "{basename}", "{dirname}/{basename}" },
145    { "*/", "*", "{dirname}", "{basename}", "{dirname}/" },
146    { "/*", "*", "{dirname}", "{basename}", "/{basename}" },
147    { "*", "*", "{dirname}", "{basename}", "{basename}" },
148    { "tmp/output/*/file-*.stk", "*", "{dirname}", "{basename}",
149    "tmp/output/{dirname}/file-{basename}.stk" },
150    { "/*.file", "*", "{dirname}", "{basename}", "/{basename}.file" },
151    { "*/file.stk", "*", "{dirname}", "{basename}",
152    "{dirname}/file.stk" },
153    { "tmp/abc*def/file.stk", "*", "{dirname}", "{basename}",
154    "tmp/abc{dirname}def/file.stk" },
155    { "a*b/c*d", "*", "{dirname}", "{basename}",
156    "a{dirname}b/c{basename}d" },
157    { "a*b/c", "*", "{dirname}", "{basename}", "a{dirname}b/c" },
158    { "a/b*c", "*", "{dirname}", "{basename}", "a/b{basename}c" },
159    { "a*b", "*", "{dirname}", "{basename}", "a{basename}b" },
160    { "aSTARb/cSTARd", "STAR", "BEFORE", "AFTER", "aBEFOREb/cAFTERd" },
161    { "aSTARb/c", "STAR", "BEFORE", "AFTER", "aBEFOREb/c" },
162    { "a/bSTARc", "STAR", "BEFORE", "AFTER", "a/bAFTERc" },
163    { "aSTARb", "STAR", "BEFORE", "AFTER", "aAFTERb" },
164    //
165    };
166    }
167   
 
168  15 toggle @Test(
169    groups = "Functional",
170    dataProvider = "stringFilenamesBaseAndExtensionsData")
171    public void stringGetBaseAndExtensionTest(String filename,
172    String extension, String base)
173    {
174  15 String thisBase = FileUtils.getBase(filename);
175  15 Assert.assertEquals(thisBase, base,
176    "base part of path and filename not as expected");
177  15 String thisExtension = FileUtils.getExtension(filename);
178  15 Assert.assertEquals(thisExtension, extension,
179    "extension part of filename not as expected");
180    }
181   
 
182  1 toggle @DataProvider(name = "stringFilenamesBaseAndExtensionsData")
183    public Object[][] stringFilenamesBaseAndExtensionsData()
184    {
185  1 return new Object[][] {
186    /*
187    * String full URL or path
188    * String base the above but without the extension if there is one
189    * String extension the filename extension if there is one
190    */
191    /*
192    */
193    { "/examples/uniref50.fa", "fa", "/examples/uniref50." },
194    { "/examples/uniref50", null, "/examples/uniref50" },
195    { "/examples/.uniref50", null, "/examples/.uniref50" },
196    { "/exampl.es/uniref50", null, "/exampl.es/uniref50" },
197    { "/examples/uniref50.", "", "/examples/uniref50." },
198    { "examples/uniref50.fa", "fa", "examples/uniref50." },
199    { "examples/uniref50", null, "examples/uniref50" },
200    { "examples/.uniref50", null, "examples/.uniref50" },
201    { "exampl.es/uniref50", null, "exampl.es/uniref50" },
202    { "examples/uniref50.", "", "examples/uniref50." },
203    { "https://www.jalview.org:443/examples/uniref50.fa", "fa",
204    "https://www.jalview.org:443/examples/uniref50." },
205    { "https://www.jalview.org:443/examples/uniref50", null,
206    "https://www.jalview.org:443/examples/uniref50" },
207    { "https://www.jalview.org:443/examples/.uniref50", null,
208    "https://www.jalview.org:443/examples/.uniref50" },
209    { "https://www.jalview.org:443/exampl.es/uniref50", null,
210    "https://www.jalview.org:443/exampl.es/uniref50" },
211    { "https://www.jalview.org:443/examples/uniref50.", "",
212    "https://www.jalview.org:443/examples/uniref50." },
213    /*
214    */
215    //
216    };
217    }
218   
219    public static final String root0 = "test/files/tempTestDir";
220   
 
221  7 toggle @Test(
222    groups = "Functional",
223    dataProvider = "getMatchingVersionedFilesData")
224    public void getMatchingVersionedFilesTest(String[] filesToMake,
225    String[] templates, String[] roots, String[] versionWhitelist,
226    String[] versionBlacklist, String separator, boolean exists,
227    String[] expectedFilePaths)
228    {
229  7 Set<File> createdFiles = new HashSet<>();
230    // create the files
231  7 for (String n : filesToMake)
232    {
233  21 if (n != null)
234    {
235  21 File f = new File(root0 + File.separator + n);
236  21 f.getParentFile().mkdirs();
237  21 try
238    {
239  21 f.createNewFile();
240  21 createdFiles.add(f);
241    } catch (IOException e)
242    {
243  0 Assert.fail("Couldn't create file '" + f.getPath() + "'", e);
244    }
245    }
246    }
247   
248  7 Set<File> matchedFiles = new HashSet<>();
249  7 matchedFiles.addAll(FileUtils.getMatchingVersionedFiles(templates,
250    roots, versionWhitelist, versionBlacklist, separator, exists));
251   
252  7 Set<String> matchedFilenames = new HashSet<>();
253  7 StringBuilder mSB = new StringBuilder();
254  7 for (File f : matchedFiles)
255    {
256  15 matchedFilenames.add(f.getPath());
257  15 if (mSB.length() > 0)
258    {
259  9 mSB.append(", ");
260    }
261  15 mSB.append(f.getPath());
262    }
263   
264  7 StringBuilder eSB = new StringBuilder();
265  7 Set<String> expectedFilenames = new HashSet<>();
266  22 for (int i = 0; i < expectedFilePaths.length; i++)
267    {
268  15 String path = root0 + File.separator + expectedFilePaths[i];
269  15 expectedFilenames.add(path);
270  15 if (eSB.length() > 0)
271    {
272  9 eSB.append(", ");
273    }
274  15 eSB.append(path);
275    }
276   
277  7 Assert.assertEquals(matchedFilenames, expectedFilenames,
278    "Did not find the right set of filenames."
279    + "\nMatched files : " + mSB.toString() + " ."
280    + "\nExpected files: " + eSB.toString() + " .\n");
281   
282    // delete created files
283  7 for (File f : createdFiles)
284    {
285  21 f.delete();
286    }
287  7 rmEmptyDirs(root0);
288    }
289   
 
290  77 toggle private static final boolean rmEmptyDirs(String path)
291    {
292  77 if (path.startsWith("/") || path.contains(".."))
293    {
294  0 System.err.println("Not running rmDirs on '" + path
295    + "'. Considered a bit dangerous.");
296  0 return false;
297    }
298  77 File f = new File(path);
299  77 if (!f.isDirectory())
300    {
301  0 return false;
302    }
303  77 for (File c : f.listFiles())
304    {
305  70 if (!rmEmptyDirs(c.getPath()))
306    {
307  0 return false;
308    }
309    }
310  77 f.delete();
311  77 return true;
312    }
313   
 
314  1 toggle @DataProvider(name = "getMatchingVersionedFilesData")
315    public Object[][] getMatchingVersionedFilesData()
316    {
317  1 String file1 = "dir/Applications/File-1/bin/file.exe";
318  1 String file2 = "dir/Applications/File-2/bin/file.exe";
319  1 String file3 = "user/Applications/File-3/bin/file.exe";
320  1 String file4a = "dir/Applications/File-4/bin/file.exe";
321  1 String file4b = "user/Applications/File-4/bin/file.exe";
322  1 String filea = "dir/Applications/File/bin/file.exe";
323  1 String fileb = "user/Applications/File/bin/file.exe";
324  1 return new Object[][] {
325    /*
326    * String[] filesToMake
327    * String[] templates
328    * String[] roots
329    * String[] versionWhitelist
330    * String[] versionBlacklist
331    * String separator
332    * boolean exists
333    * String[] expectedFilePaths
334    */
335    /*
336    */
337    // empty whitelist and blacklist
338    { new String[]
339    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
340    new String[]
341    { root0 + "/dir/Applications", root0 + "/user/Applications" },
342    new String[] {}, new String[] {}, "-", true, new String[]
343    { file1, file2, file3 } },
344    // no whitelist or blacklist
345    { new String[]
346    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
347    new String[]
348    { root0 + "/dir/Applications", root0 + "/user/Applications" },
349    null, null, "-", true, new String[] {} },
350    // no blacklist
351    { new String[]
352    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
353    new String[]
354    { root0 + "/dir/Applications", root0 + "/user/Applications" },
355    new String[]
356    { "1", "3" }, null, "-", true, new String[] { file1, file3 } },
357    // no whitelist
358    { new String[]
359    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
360    new String[]
361    { root0 + "/dir/Applications", root0 + "/user/Applications" },
362    null, new String[]
363    { "1", "3" }, "-", true, new String[] { file2 } },
364    // whitelist (but only if exists) and blacklist
365    { new String[]
366    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
367    new String[]
368    { root0 + "/dir/Applications", root0 + "/user/Applications" },
369    new String[]
370    { "4" }, new String[] { "1", "3" }, "-", true,
371    new String[]
372    { file2 } },
373    // whitelist (if exists or not) and blacklist
374    { new String[]
375    { file1, file2, file3 }, new String[] { "%s/File%s/bin/file.exe" },
376    new String[]
377    { root0 + "/dir/Applications", root0 + "/user/Applications" },
378    new String[]
379    { "4" }, new String[] { "1", "3" }, "-", false,
380    new String[]
381    { file2, file4a, file4b, filea, fileb } },
382    // whitelist (if exists or not) and blacklist but no separator so no
383    // unversioned paths
384    { new String[]
385    { file1, file2, file3 }, new String[] { "%s/File-%s/bin/file.exe" },
386    new String[]
387    { root0 + "/dir/Applications", root0 + "/user/Applications" },
388    new String[]
389    { "4" }, new String[] { "1", "3" }, null, false,
390    new String[]
391    { file2, file4a, file4b } },
392    /*
393    */
394    //
395    };
396    }
397    }