JAL-4281 store/restore ID width and manual adjustment flag in Jalview projects
[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.io.IOException;
25 import java.util.List;
26
27 import org.testng.Assert;
28 import org.testng.annotations.DataProvider;
29 import org.testng.annotations.Test;
30
31 @Test
32 public class FileUtilsTest
33 {
34   @Test(groups = "Functional", dataProvider = "patternsAndMinNumFiles")
35   public void testJavaFileGlob(String pattern, int atLeast, int atMost)
36   {
37     List<File> files = FileUtils.getFilesFromGlob(pattern);
38     if (atLeast != -1)
39     {
40       Assert.assertTrue(files.size() > atLeast,
41               "Did not find more than " + atLeast + " files with " + pattern
42                       + " (found " + files.size() + ")");
43     }
44     if (atLeast != -1)
45     {
46       Assert.assertTrue(files.size() > atLeast,
47               "Did not find more than " + atLeast + " files with " + pattern
48                       + " (found " + files.size() + ")");
49     }
50     if (atMost != -1)
51     {
52       Assert.assertTrue(files.size() < atMost,
53               "Did not find fewer than " + atMost + " files with " + pattern
54                       + " (found " + files.size() + ")");
55     }
56   }
57
58   @Test(groups = "Functional", dataProvider = "dirnamesAndBasenames")
59   public void testDirnamesAndBasenames(String filename, int where,
60           String dirname, String basename, String notInDirname)
61   {
62     File file = new File(filename);
63     String d = FileUtils.getDirname(file);
64     String b = FileUtils.getBasename(file);
65     Assert.assertEquals(b, basename);
66     if (where == 0)
67       Assert.assertEquals(d, dirname);
68     else if (where < 0)
69       Assert.assertTrue(d.startsWith(dirname),
70               "getDirname(" + file.getPath() + ")=" + d
71                       + " didn't start with '" + dirname + "'");
72     else if (where > 0)
73       Assert.assertTrue(d.endsWith(dirname), "getDirname(" + file.getPath()
74               + ")=" + d + " didn't end with '" + d + "'");
75
76     // ensure dirname doesn't end with basename (which means you can't use same
77     // filename as dir in tests!)
78     Assert.assertFalse(d.endsWith(b));
79
80     if (notInDirname != null)
81       Assert.assertFalse(d.contains(notInDirname));
82   }
83
84   @DataProvider(name = "patternsAndMinNumFiles")
85   public Object[][] patternsAndMinNumFiles()
86   {
87     return new Object[][] { { "src/**/*.java", 900, 100000 },
88         { "src/**.java", 900, 100000 },
89         { "test/**/*.java", 250, 2500 },
90         { "test/**.java", 250, 2500 },
91         { "help/**/*.html", 100, 1000 },
92         { "test/**/F*.java", 15, 150 },
93         { "test/jalview/*/F*.java", 10, 15 }, // 12 at time of writing
94         { "test/jalview/**/F*.java", 18, 30 }, // 20 at time of writing
95         { "test/jalview/util/F**.java", 1, 5 }, // 2 at time of writing
96         { "src/jalview/b*/*.java", 14, 19 }, // 15 at time of writing
97         { "src/jalview/b**/*.java", 20, 25 }, // 22 at time of writing
98     };
99   }
100
101   @DataProvider(name = "dirnamesAndBasenames")
102   public Object[][] dirnamesAndBasenames()
103   {
104     String homeDir = null;
105     try
106     {
107       homeDir = new File(System.getProperty("user.home"))
108               .getCanonicalPath();
109     } catch (IOException e)
110     {
111       System.err.println("Problem getting canonical home dir");
112       e.printStackTrace();
113     }
114     return new Object[][] { // -1=startsWith, 0=equals, 1=endsWith
115         { "~/hello/sailor", -1, homeDir, "sailor", "~" }, //
116         { "~/hello/sailor", 1, "/hello", "sailor", "~" }, //
117         { "./examples/uniref50.fa", -1, "/", "uniref50", "." }, //
118         { "./examples/uniref50.fa", 1, "/examples", "uniref50", "." }, //
119         { "examples/uniref50.fa", 1, "/examples", "uniref50", ".fa" }, //
120     };
121   }
122 }