1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
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 |
|
|
| 88% |
Uncovered Elements: 13 (108) |
Complexity: 24 |
Complexity Density: 0.32 |
|
34 |
|
public class FileUtilsTest |
35 |
|
{ |
|
|
| 76.9% |
Uncovered Elements: 3 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
1PASS
|
|
36 |
11 |
@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 |
|
|
|
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
60 |
4 |
@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 |
|
|
74 |
|
|
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 |
|
|
85 |
|
|
86 |
|
@return |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
1 |
@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 }, |
98 |
|
{ "test/jalview/**/F*.java", 18, 30 }, |
99 |
|
{ "test/jalview/util/F**.java", 1, 5 }, |
100 |
|
{ "src/jalview/b*/*.java", 14, 19 }, |
101 |
|
{ "src/jalview/b**/*.java", 23, 26 }, |
102 |
|
}; |
103 |
|
} |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
105 |
1 |
@DataProvider(name = "dirnamesAndBasenames")... |
106 |
|
public Object[][] dirnamesAndBasenames() |
107 |
|
{ |
108 |
1 |
String homeDir = new File(System.getProperty("user.home")).getPath(); |
109 |
1 |
return new Object[][] { |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
119 |
16 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
1 |
@DataProvider(name = "convertWildcardsToPathData")... |
132 |
|
public Object[][] convertWildcardsToPathData() |
133 |
|
{ |
134 |
1 |
return new Object[][] { |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
168 |
15 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
182 |
1 |
@DataProvider(name = "stringFilenamesBaseAndExtensionsData")... |
183 |
|
public Object[][] stringFilenamesBaseAndExtensionsData() |
184 |
|
{ |
185 |
1 |
return new Object[][] { |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
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 |
|
|
|
|
| 94.7% |
Uncovered Elements: 2 (38) |
Complexity: 6 |
Complexity Density: 0.2 |
1PASS
|
|
221 |
7 |
@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 |
|
|
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 |
|
|
283 |
7 |
for (File f : createdFiles) |
284 |
|
{ |
285 |
21 |
f.delete(); |
286 |
|
} |
287 |
7 |
rmEmptyDirs(root0); |
288 |
|
} |
289 |
|
|
|
|
| 58.8% |
Uncovered Elements: 7 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
290 |
77 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
314 |
1 |
@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 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
|
337 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
383 |
|
|
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 |
|
} |