Clover icon

Coverage Report

  1. Project Clover database Tue Oct 29 2024 21:36:55 GMT
  2. Package jalview.util

File FileUtilsTest.java

 

Code metrics

8
26
8
1
215
154
12
0.46
3.25
8
1.5

Classes

Class Line # Actions
FileUtilsTest 31 26 12
0.904761990.5%
 

Contributing tests

This file is covered by 29 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.util.List;
25   
26    import org.testng.Assert;
27    import org.testng.annotations.DataProvider;
28    import org.testng.annotations.Test;
29   
30    @Test
 
31    public class FileUtilsTest
32    {
 
33  11 toggle @Test(groups = "Functional", dataProvider = "patternsAndMinNumFiles")
34    public void testJavaFileGlob(String pattern, int atLeast, int atMost)
35    {
36  11 List<File> files = FileUtils.getFilesFromGlob(pattern);
37  11 if (atLeast != -1)
38    {
39  11 Assert.assertTrue(files.size() > atLeast,
40    "Did not find more than " + atLeast + " files with " + pattern
41    + " (found " + files.size() + ")");
42    }
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 (atMost != -1)
50    {
51  11 Assert.assertTrue(files.size() < atMost,
52    "Did not find fewer than " + atMost + " files with " + pattern
53    + " (found " + files.size() + ")");
54    }
55    }
56   
 
57  4 toggle @Test(groups = "Functional", dataProvider = "dirnamesAndBasenames")
58    public void testDirnamesAndBasenames(String filename, String dirname,
59    String endsWith, String basename, String notInDirname)
60    {
61  4 File file = new File(filename);
62  4 String d = FileUtils.getDirname(file);
63  4 String b = FileUtils.getBasename(file);
64  4 Assert.assertEquals(b, basename);
65  4 Assert.assertTrue(d.startsWith(dirname), "getDirname(" + file.getPath()
66    + ")=" + d + " didn't start with '" + dirname + "'");
67  4 Assert.assertTrue(d.endsWith(endsWith), "getDirname(" + file.getPath()
68    + ")=" + d + " didn't end with '" + endsWith + "'");
69   
70    // ensure dirname doesn't end with basename (which means you can't use same
71    // filename as dir in tests!)
72  4 Assert.assertFalse(d.endsWith(b), "Processed dirname '" + d
73    + "' ends with '" + b + "' when it shouldn't");
74   
75  4 if (notInDirname != null)
76  4 Assert.assertFalse(d.contains(notInDirname), "Processed directory '"
77    + d + "' contains '" + notInDirname + "' when it shouldn't");
78    }
79   
80    /**
81    * these need to be maintained as jalview's source base grows
82    *
83    * @return
84    */
 
85  1 toggle @DataProvider(name = "patternsAndMinNumFiles")
86    public Object[][] patternsAndMinNumFiles()
87    {
88  1 return new Object[][] { { "src/**/*.java", 900, 100000 },
89    { "src/**.java", 900, 100000 },
90    { "test/**/*.java", 250, 2500 },
91    { "test/**.java", 250, 2500 },
92    { "help/**/*.html", 100, 1000 },
93    { "test/**/F*.java", 15, 150 },
94    { "test/jalview/*/F*.java", 10, 15 }, // 12 at time of writing
95    { "test/jalview/**/F*.java", 18, 30 }, // 20 at time of writing
96    { "test/jalview/util/F**.java", 1, 5 }, // 2 at time of writing
97    { "src/jalview/b*/*.java", 14, 19 }, // 15 at time of writing
98    { "src/jalview/b**/*.java", 23, 26 }, // 22 at time of writing
99    };
100    }
101   
 
102  1 toggle @DataProvider(name = "dirnamesAndBasenames")
103    public Object[][] dirnamesAndBasenames()
104    {
105  1 String homeDir = new File(System.getProperty("user.home")).getPath();
106  1 return new Object[][] { // -1=startsWith, 0=equals, 1=endsWith
107    { "~/hello/sailor", homeDir, "/hello", "sailor", "~" }, //
108    { "./examples/uniref50.fa", "./", "examples", "uniref50", "Users" }, //
109    { "./examples/uniref50.1.fa", "./", "examples", "uniref50.1",
110    "Users" }, //
111    { "examples/uniref50.fa", "examples", "examples", "uniref50",
112    ".fa" }, //
113    };
114    }
115   
 
116  16 toggle @Test(groups = "Functional", dataProvider = "convertWildcardsToPathData")
117    public void convertWildcardsToPathTest(String value, String wildcard,
118    String dirname, String basename, String path)
119    {
120   
121  16 Assert.assertEquals(
122    FileUtils.convertWildcardsToPath(value, wildcard, dirname,
123    basename),
124    path, "Conversion of wildcard output path is not right.");
125   
126    }
127   
 
128  1 toggle @DataProvider(name = "convertWildcardsToPathData")
129    public Object[][] convertWildcardsToPathData()
130    {
131  1 return new Object[][] {
132    /*
133    * cmdline args
134    * Arg (null if only testing headless)
135    * String value if there is one (null otherwise)
136    * boolean value if String value is null
137    * expected value of isHeadless()
138    */
139    /*
140    */
141    { "*/*", "*", "{dirname}", "{basename}", "{dirname}/{basename}" },
142    { "*/", "*", "{dirname}", "{basename}", "{dirname}/" },
143    { "/*", "*", "{dirname}", "{basename}", "/{basename}" },
144    { "*", "*", "{dirname}", "{basename}", "{basename}" },
145    { "tmp/output/*/file-*.stk", "*", "{dirname}", "{basename}",
146    "tmp/output/{dirname}/file-{basename}.stk" },
147    { "/*.file", "*", "{dirname}", "{basename}", "/{basename}.file" },
148    { "*/file.stk", "*", "{dirname}", "{basename}",
149    "{dirname}/file.stk" },
150    { "tmp/abc*def/file.stk", "*", "{dirname}", "{basename}",
151    "tmp/abc{dirname}def/file.stk" },
152    { "a*b/c*d", "*", "{dirname}", "{basename}",
153    "a{dirname}b/c{basename}d" },
154    { "a*b/c", "*", "{dirname}", "{basename}", "a{dirname}b/c" },
155    { "a/b*c", "*", "{dirname}", "{basename}", "a/b{basename}c" },
156    { "a*b", "*", "{dirname}", "{basename}", "a{basename}b" },
157    { "aSTARb/cSTARd", "STAR", "BEFORE", "AFTER", "aBEFOREb/cAFTERd" },
158    { "aSTARb/c", "STAR", "BEFORE", "AFTER", "aBEFOREb/c" },
159    { "a/bSTARc", "STAR", "BEFORE", "AFTER", "a/bAFTERc" },
160    { "aSTARb", "STAR", "BEFORE", "AFTER", "aAFTERb" },
161    //
162    };
163    }
164   
 
165  15 toggle @Test(
166    groups = "Functional",
167    dataProvider = "stringFilenamesBaseAndExtensionsData")
168    public void stringGetBaseAndExtensionTest(String filename,
169    String extension, String base)
170    {
171  15 String thisBase = FileUtils.getBase(filename);
172  15 Assert.assertEquals(thisBase, base,
173    "base part of path and filename not as expected");
174  15 String thisExtension = FileUtils.getExtension(filename);
175  15 Assert.assertEquals(thisExtension, extension,
176    "extension part of filename not as expected");
177    }
178   
 
179  1 toggle @DataProvider(name = "stringFilenamesBaseAndExtensionsData")
180    public Object[][] stringFilenamesBaseAndExtensionsData()
181    {
182  1 return new Object[][] {
183    /*
184    * String full URL or path
185    * String base the above but without the extension if there is one
186    * String extension the filename extension if there is one
187    */
188    /*
189    */
190    { "/examples/uniref50.fa", "fa", "/examples/uniref50." },
191    { "/examples/uniref50", null, "/examples/uniref50" },
192    { "/examples/.uniref50", null, "/examples/.uniref50" },
193    { "/exampl.es/uniref50", null, "/exampl.es/uniref50" },
194    { "/examples/uniref50.", "", "/examples/uniref50." },
195    { "examples/uniref50.fa", "fa", "examples/uniref50." },
196    { "examples/uniref50", null, "examples/uniref50" },
197    { "examples/.uniref50", null, "examples/.uniref50" },
198    { "exampl.es/uniref50", null, "exampl.es/uniref50" },
199    { "examples/uniref50.", "", "examples/uniref50." },
200    { "https://www.jalview.org:443/examples/uniref50.fa", "fa",
201    "https://www.jalview.org:443/examples/uniref50." },
202    { "https://www.jalview.org:443/examples/uniref50", null,
203    "https://www.jalview.org:443/examples/uniref50" },
204    { "https://www.jalview.org:443/examples/.uniref50", null,
205    "https://www.jalview.org:443/examples/.uniref50" },
206    { "https://www.jalview.org:443/exampl.es/uniref50", null,
207    "https://www.jalview.org:443/exampl.es/uniref50" },
208    { "https://www.jalview.org:443/examples/uniref50.", "",
209    "https://www.jalview.org:443/examples/uniref50." },
210    /*
211    */
212    //
213    };
214    }
215    }