666b33b5aac66941893c74d2408d60aceb615f9d
[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, String dirname,
59           String endsWith, String basename, String notInDirname)
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     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 + "'");
69
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");
74
75     if (notInDirname != null)
76       Assert.assertFalse(d.contains(notInDirname), "Processed directory '"
77               + d + "' contains '" + notInDirname + "' when it shouldn't");
78   }
79
80   @DataProvider(name = "patternsAndMinNumFiles")
81   public Object[][] patternsAndMinNumFiles()
82   {
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
94     };
95   }
96
97   @DataProvider(name = "dirnamesAndBasenames")
98   public Object[][] dirnamesAndBasenames()
99   {
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",
105             "Users" }, //
106         { "examples/uniref50.fa", "examples", "examples", "uniref50",
107             ".fa" }, //
108     };
109   }
110
111   @Test(groups = "Functional", dataProvider = "convertWildcardsToPathData")
112   public void convertWildcardsToPathTest(String value, String wildcard,
113           String dirname, String basename, String path)
114   {
115
116     Assert.assertEquals(
117             FileUtils.convertWildcardsToPath(value, wildcard, dirname,
118                     basename),
119             path, "Conversion of wildcard output path is not right.");
120
121   }
122
123   @DataProvider(name = "convertWildcardsToPathData")
124   public Object[][] convertWildcardsToPathData()
125   {
126     return new Object[][] {
127         /*
128          * cmdline args
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()
133          */
134         /*
135         */
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" },
156         //
157     };
158   }
159
160 }