8b5257773b02e1e05aae7b3573f13bc500f25794
[jalview.git] / test / jalview / util / FileUtilsTest.java
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   @Test(groups = "Functional", dataProvider = "patternsAndMinNumFiles")
34   public void testJavaFileGlob(String pattern, int atLeast, int atMost)
35   {
36     List<File> files = FileUtils.getFilesFromGlob(pattern);
37     if (atLeast != -1)
38     {
39       Assert.assertTrue(files.size() > atLeast,
40               "Did not find more than " + atLeast + " files with " + pattern
41                       + " (found " + files.size() + ")");
42     }
43     if (atLeast != -1)
44     {
45       Assert.assertTrue(files.size() > atLeast,
46               "Did not find more than " + atLeast + " files with " + pattern
47                       + " (found " + files.size() + ")");
48     }
49     if (atMost != -1)
50     {
51       Assert.assertTrue(files.size() < atMost,
52               "Did not find fewer than " + atMost + " files with " + pattern
53                       + " (found " + files.size() + ")");
54     }
55   }
56
57   @Test(groups = "Functional", dataProvider = "dirnamesAndBasenames")
58   public void testDirnamesAndBasenames(String filename, int where,
59           String dirname, String basename)
60   {
61     File file = new File(filename);
62     String d = FileUtils.getDirname(file);
63     String b = FileUtils.getBasename(file);
64     Assert.assertEquals(b, basename);
65     if (where == 0)
66       Assert.assertEquals(d, dirname);
67     else if (where < 0)
68       Assert.assertTrue(d.startsWith(dirname),
69               "getDirname(" + file.getPath() + ")=" + d
70                       + " didn't start with '" + d + "'");
71     else if (where > 0)
72       Assert.assertTrue(d.endsWith(dirname), "getDirname(" + file.getPath()
73               + ")=" + d + " didn't end with '" + d + "'");
74   }
75
76   @DataProvider(name = "patternsAndMinNumFiles")
77   public Object[][] patternsAndMinNumFiles()
78   {
79     return new Object[][] { { "src/**/*.java", 900, 100000 },
80         { "src/**.java", 900, 100000 },
81         { "test/**/*.java", 250, 2500 },
82         { "test/**.java", 250, 2500 },
83         { "help/**/*.html", 100, 1000 },
84         { "test/**/F*.java", 15, 150 },
85         { "test/jalview/*/F*.java", 10, 15 }, // 12 at time of writing
86         { "test/jalview/**/F*.java", 18, 30 }, // 20 at time of writing
87         { "test/jalview/util/F**.java", 1, 5 }, // 2 at time of writing
88         { "src/jalview/b*/*.java", 14, 19 }, // 15 at time of writing
89         { "src/jalview/b**/*.java", 20, 25 }, // 22 at time of writing
90     };
91   }
92
93   @DataProvider(name = "dirnamesAndBasenames")
94   public Object[][] dirnamesAndBasenames()
95   {
96     return new Object[][] { // -1=startsWith, 0=equals, 1=endsWith
97         { "~/hello/sailor", -1, System.getProperty("user.home"), "sailor" }, //
98         { "~/hello/sailor", 1, "/hello", "sailor" }, //
99         { "./examples/uniref50.fa", -1, "/", "uniref50" }, //
100         { "./examples/uniref50.fa", 1, "/examples", "uniref50" }, //
101         { "examples/uniref50.fa", 1, "/examples", "uniref50" }, //
102     };
103   }
104 }