1597718be10e054bff17405aebf6e984950887ba
[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, String dirname,
60           String endsWith, 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     Assert.assertTrue(d.startsWith(dirname), "getDirname(" + file.getPath()
67             + ")=" + d + " didn't start with '" + dirname + "'");
68     Assert.assertTrue(d.endsWith(endsWith), "getDirname(" + file.getPath()
69             + ")=" + d + " didn't end with '" + endsWith + "'");
70
71     // ensure dirname doesn't end with basename (which means you can't use same
72     // filename as dir in tests!)
73     Assert.assertFalse(d.endsWith(b), "Processed dirname '" + d
74             + "' ends with '" + b + "' when it shouldn't");
75
76     if (notInDirname != null)
77       Assert.assertFalse(d.contains(notInDirname), "Processed directory '"
78               + d + "' contains '" + notInDirname + "' when it shouldn't");
79   }
80
81   @DataProvider(name = "patternsAndMinNumFiles")
82   public Object[][] patternsAndMinNumFiles()
83   {
84     return new Object[][] { { "src/**/*.java", 900, 100000 },
85         { "src/**.java", 900, 100000 },
86         { "test/**/*.java", 250, 2500 },
87         { "test/**.java", 250, 2500 },
88         { "help/**/*.html", 100, 1000 },
89         { "test/**/F*.java", 15, 150 },
90         { "test/jalview/*/F*.java", 10, 15 }, // 12 at time of writing
91         { "test/jalview/**/F*.java", 18, 30 }, // 20 at time of writing
92         { "test/jalview/util/F**.java", 1, 5 }, // 2 at time of writing
93         { "src/jalview/b*/*.java", 14, 19 }, // 15 at time of writing
94         { "src/jalview/b**/*.java", 20, 25 }, // 22 at time of writing
95     };
96   }
97
98   @DataProvider(name = "dirnamesAndBasenames")
99   public Object[][] dirnamesAndBasenames()
100   {
101     String homeDir = null;
102     try
103     {
104       homeDir = new File(System.getProperty("user.home"))
105               .getCanonicalPath();
106     } catch (IOException e)
107     {
108       System.err.println("Problem getting canonical home dir");
109       e.printStackTrace();
110     }
111     return new Object[][] { // -1=startsWith, 0=equals, 1=endsWith
112         { "~/hello/sailor", homeDir, "/hello", "sailor", "~" }, //
113         { "./examples/uniref50.fa", "./", "examples", "uniref50", "Users" }, //
114         { "./examples/uniref50.1.fa", "./", "examples", "uniref50.1",
115             "Users" }, //
116         { "examples/uniref50.fa", "examples", "examples", "uniref50",
117             ".fa" }, //
118     };
119   }
120
121   @Test(groups = "Functional", dataProvider = "convertWildcardsToPathData")
122   public void convertWildcardsToPathTest(String value, String wildcard,
123           String dirname, String basename, String path)
124   {
125
126     Assert.assertEquals(
127             FileUtils.convertWildcardsToPath(value, wildcard, dirname,
128                     basename),
129             path, "Conversion of wildcard output path is not right.");
130
131   }
132
133   @DataProvider(name = "convertWildcardsToPathData")
134   public Object[][] convertWildcardsToPathData()
135   {
136     return new Object[][] {
137         /*
138          * cmdline args
139          * Arg (null if only testing headless)
140          * String value if there is one (null otherwise)
141          * boolean value if String value is null
142          * expected value of isHeadless()
143          */
144         /*
145         */
146         { "*/*", "*", "{dirname}", "{basename}", "{dirname}/{basename}" },
147         { "*/", "*", "{dirname}", "{basename}", "{dirname}/" },
148         { "/*", "*", "{dirname}", "{basename}", "/{basename}" },
149         { "*", "*", "{dirname}", "{basename}", "{basename}" },
150         { "tmp/output/*/file-*.stk", "*", "{dirname}", "{basename}",
151             "tmp/output/{dirname}/file-{basename}.stk" },
152         { "/*.file", "*", "{dirname}", "{basename}", "/{basename}.file" },
153         { "*/file.stk", "*", "{dirname}", "{basename}",
154             "{dirname}/file.stk" },
155         { "tmp/abc*def/file.stk", "*", "{dirname}", "{basename}",
156             "tmp/abc{dirname}def/file.stk" },
157         { "a*b/c*d", "*", "{dirname}", "{basename}",
158             "a{dirname}b/c{basename}d" },
159         { "a*b/c", "*", "{dirname}", "{basename}", "a{dirname}b/c" },
160         { "a/b*c", "*", "{dirname}", "{basename}", "a/b{basename}c" },
161         { "a*b", "*", "{dirname}", "{basename}", "a{basename}b" },
162         { "aSTARb/cSTARd", "STAR", "BEFORE", "AFTER", "aBEFOREb/cAFTERd" },
163         { "aSTARb/c", "STAR", "BEFORE", "AFTER", "aBEFOREb/c" },
164         { "a/bSTARc", "STAR", "BEFORE", "AFTER", "a/bAFTERc" },
165         { "aSTARb", "STAR", "BEFORE", "AFTER", "aAFTERb" },
166         //
167     };
168   }
169
170 }