2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
24 import java.util.List;
26 import org.testng.Assert;
27 import org.testng.annotations.DataProvider;
28 import org.testng.annotations.Test;
31 public class FileUtilsTest
33 @Test(groups = "Functional", dataProvider = "patternsAndMinNumFiles")
34 public void testJavaFileGlob(String pattern, int atLeast, int atMost)
36 List<File> files = FileUtils.getFilesFromGlob(pattern);
39 Assert.assertTrue(files.size() > atLeast,
40 "Did not find more than " + atLeast + " files with " + pattern
41 + " (found " + files.size() + ")");
45 Assert.assertTrue(files.size() > atLeast,
46 "Did not find more than " + atLeast + " files with " + pattern
47 + " (found " + files.size() + ")");
51 Assert.assertTrue(files.size() < atMost,
52 "Did not find fewer than " + atMost + " files with " + pattern
53 + " (found " + files.size() + ")");
57 @Test(groups = "Functional", dataProvider = "dirnamesAndBasenames")
58 public void testDirnamesAndBasenames(String filename, String dirname,
59 String endsWith, String basename, String notInDirname)
61 File file = new File(filename);
62 String d = FileUtils.getDirname(file);
63 String b = FileUtils.getBasename(file);
64 Assert.assertEquals(b, basename);
65 Assert.assertTrue(d.startsWith(dirname), "getDirname(" + file.getPath()
66 + ")=" + d + " didn't start with '" + dirname + "'");
67 Assert.assertTrue(d.endsWith(endsWith), "getDirname(" + file.getPath()
68 + ")=" + d + " didn't end with '" + endsWith + "'");
70 // ensure dirname doesn't end with basename (which means you can't use same
71 // filename as dir in tests!)
72 Assert.assertFalse(d.endsWith(b), "Processed dirname '" + d
73 + "' ends with '" + b + "' when it shouldn't");
75 if (notInDirname != null)
76 Assert.assertFalse(d.contains(notInDirname), "Processed directory '"
77 + d + "' contains '" + notInDirname + "' when it shouldn't");
80 @DataProvider(name = "patternsAndMinNumFiles")
81 public Object[][] patternsAndMinNumFiles()
83 return new Object[][] { { "src/**/*.java", 900, 100000 },
84 { "src/**.java", 900, 100000 },
85 { "test/**/*.java", 250, 2500 },
86 { "test/**.java", 250, 2500 },
87 { "help/**/*.html", 100, 1000 },
88 { "test/**/F*.java", 15, 150 },
89 { "test/jalview/*/F*.java", 10, 15 }, // 12 at time of writing
90 { "test/jalview/**/F*.java", 18, 30 }, // 20 at time of writing
91 { "test/jalview/util/F**.java", 1, 5 }, // 2 at time of writing
92 { "src/jalview/b*/*.java", 14, 19 }, // 15 at time of writing
93 { "src/jalview/b**/*.java", 20, 25 }, // 22 at time of writing
97 @DataProvider(name = "dirnamesAndBasenames")
98 public Object[][] dirnamesAndBasenames()
100 String homeDir = new File(System.getProperty("user.home")).getPath();
101 return new Object[][] { // -1=startsWith, 0=equals, 1=endsWith
102 { "~/hello/sailor", homeDir, "/hello", "sailor", "~" }, //
103 { "./examples/uniref50.fa", "./", "examples", "uniref50", "Users" }, //
104 { "./examples/uniref50.1.fa", "./", "examples", "uniref50.1",
106 { "examples/uniref50.fa", "examples", "examples", "uniref50",
111 @Test(groups = "Functional", dataProvider = "convertWildcardsToPathData")
112 public void convertWildcardsToPathTest(String value, String wildcard,
113 String dirname, String basename, String path)
117 FileUtils.convertWildcardsToPath(value, wildcard, dirname,
119 path, "Conversion of wildcard output path is not right.");
123 @DataProvider(name = "convertWildcardsToPathData")
124 public Object[][] convertWildcardsToPathData()
126 return new Object[][] {
129 * Arg (null if only testing headless)
130 * String value if there is one (null otherwise)
131 * boolean value if String value is null
132 * expected value of isHeadless()
136 { "*/*", "*", "{dirname}", "{basename}", "{dirname}/{basename}" },
137 { "*/", "*", "{dirname}", "{basename}", "{dirname}/" },
138 { "/*", "*", "{dirname}", "{basename}", "/{basename}" },
139 { "*", "*", "{dirname}", "{basename}", "{basename}" },
140 { "tmp/output/*/file-*.stk", "*", "{dirname}", "{basename}",
141 "tmp/output/{dirname}/file-{basename}.stk" },
142 { "/*.file", "*", "{dirname}", "{basename}", "/{basename}.file" },
143 { "*/file.stk", "*", "{dirname}", "{basename}",
144 "{dirname}/file.stk" },
145 { "tmp/abc*def/file.stk", "*", "{dirname}", "{basename}",
146 "tmp/abc{dirname}def/file.stk" },
147 { "a*b/c*d", "*", "{dirname}", "{basename}",
148 "a{dirname}b/c{basename}d" },
149 { "a*b/c", "*", "{dirname}", "{basename}", "a{dirname}b/c" },
150 { "a/b*c", "*", "{dirname}", "{basename}", "a/b{basename}c" },
151 { "a*b", "*", "{dirname}", "{basename}", "a{basename}b" },
152 { "aSTARb/cSTARd", "STAR", "BEFORE", "AFTER", "aBEFOREb/cAFTERd" },
153 { "aSTARb/c", "STAR", "BEFORE", "AFTER", "aBEFOREb/c" },
154 { "a/bSTARc", "STAR", "BEFORE", "AFTER", "a/bAFTERc" },
155 { "aSTARb", "STAR", "BEFORE", "AFTER", "aAFTERb" },
161 groups = "Functional",
162 dataProvider = "stringFilenamesBaseAndExtensionsData")
163 public void stringGetBaseAndExtensionTest(String filename,
164 String extension, String base)
166 String thisBase = FileUtils.getBase(filename);
167 Assert.assertEquals(thisBase, base,
168 "base part of path and filename not as expected");
169 String thisExtension = FileUtils.getExtension(filename);
170 Assert.assertEquals(thisExtension, extension,
171 "extension part of filename not as expected");
174 @DataProvider(name = "stringFilenamesBaseAndExtensionsData")
175 public Object[][] stringFilenamesBaseAndExtensionsData()
177 return new Object[][] {
179 * String full URL or path
180 * String base the above but without the extension if there is one
181 * String extension the filename extension if there is one
185 { "/examples/uniref50.fa", "fa", "/examples/uniref50." },
186 { "/examples/uniref50", null, "/examples/uniref50" },
187 { "/examples/.uniref50", null, "/examples/.uniref50" },
188 { "/exampl.es/uniref50", null, "/exampl.es/uniref50" },
189 { "/examples/uniref50.", "", "/examples/uniref50." },
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 { "https://www.jalview.org:443/examples/uniref50.fa", "fa",
196 "https://www.jalview.org:443/examples/uniref50." },
197 { "https://www.jalview.org:443/examples/uniref50", null,
198 "https://www.jalview.org:443/examples/uniref50" },
199 { "https://www.jalview.org:443/examples/.uniref50", null,
200 "https://www.jalview.org:443/examples/.uniref50" },
201 { "https://www.jalview.org:443/exampl.es/uniref50", null,
202 "https://www.jalview.org:443/exampl.es/uniref50" },
203 { "https://www.jalview.org:443/examples/uniref50.", "",
204 "https://www.jalview.org:443/examples/uniref50." },