Skip to content

Commit 5ed7321

Browse files
committed
The tests were using explicit Unix-style path separators, whereas the utils
themselves were using File.separator, so the tests failed on Windows. Now the tests also use File.separator, and work on Windows. The one tricky part is that paths for regex searches need to be escaped if the separator is a backslash. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140609 13f79535-47bb-0310-9956-ffa450edef68
1 parent 41f13af commit 5ed7321

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/test/org/apache/commons/io/FilenameUtilsTestCase.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* @author Peter Donald
3131
* @author Matthew Hawthorne
32-
* @version $Id: FilenameUtilsTestCase.java,v 1.8 2004/06/13 05:13:57 bayard Exp $
32+
* @version $Id: FilenameUtilsTestCase.java,v 1.9 2004/10/24 00:10:05 martinc Exp $
3333
* @see FilenameUtils
3434
*/
3535
public class FilenameUtilsTestCase extends FileBasedTestCase {
@@ -208,9 +208,9 @@ public void testGetExtension() {
208208
{ "README", "" },
209209
{ "domain.dot.com", "com" },
210210
{ "image.jpeg", "jpeg" },
211-
{ "a.b/c", "" },
212-
{ "a.b/c.txt", "txt" },
213-
{ "a/b/c", "" },
211+
{ "a.b" + File.separator + "c", "" },
212+
{ "a.b" + File.separator + "c.txt", "txt" },
213+
{ "a" + File.separator + "b" + File.separator + "c", "" },
214214
};
215215
for (int i = 0; i < tests.length; i++) {
216216
assertEquals(tests[i][1], FilenameUtils.getExtension(tests[i][0]));
@@ -244,9 +244,9 @@ public void testRemoveExtension() {
244244
{ "README", "README" },
245245
{ "domain.dot.com", "domain.dot" },
246246
{ "image.jpeg", "image" },
247-
{ "a.b/c", "a.b/c" },
248-
{ "a.b/c.txt", "a.b/c" },
249-
{ "a/b/c", "a/b/c" },
247+
{ "a.b" + File.separator + "c", "a.b" + File.separator + "c" },
248+
{ "a.b" + File.separator + "c.txt", "a.b" + File.separator + "c" },
249+
{ "a" + File.separator + "b" + File.separator + "c", "a" + File.separator + "b" + File.separator + "c" },
250250
};
251251

252252
for (int i = 0; i < tests.length; i++) {

src/test/org/apache/commons/io/find/FileFinderTest.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class FileFinderTest extends TestCase {
2929

3030
private HashMap options;
3131
private FileFinder finder;
32-
private String dirStr = "src/test/find-data/";
32+
private String dirStr = "src" + File.separator + "test" + File.separator + "find-data" + File.separator;
3333
private File dir = new File(dirStr);
3434

3535
public FileFinderTest(String name) {
@@ -61,13 +61,13 @@ public void testFindIName() {
6161
}
6262

6363
public void testFindPath() {
64-
options.put(Finder.PATH, dirStr+"path/dir/file");
64+
options.put(Finder.PATH, dirStr+"path" + File.separator + "dir" + File.separator + "file");
6565
File[] files = finder.find(new File(dir, "path"), options);
6666
assertEquals(1, files.length);
6767
}
6868

6969
public void testFindIPath() {
70-
options.put(Finder.IPATH, dirStr+"PAth/dIR/fILe");
70+
options.put(Finder.IPATH, dirStr+"PAth" + File.separator + "dIR" + File.separator + "fILe");
7171
File[] files = finder.find(new File(dir, "path"), options);
7272
assertEquals(1, files.length);
7373
}
@@ -79,13 +79,13 @@ public void testFindNotPath() {
7979
}
8080

8181
public void testFindRegex() {
82-
options.put(Finder.REGEX, dirStr+"regex/f.*");
82+
options.put(Finder.REGEX, escapePath(dirStr+"regex" + File.separator + "f.*"));
8383
File[] files = finder.find(new File(dir, "regex"), options);
8484
assertEquals(3, files.length);
8585
}
8686

8787
public void testFindIRegex() {
88-
options.put(Finder.IREGEX, dirStr+"REgeX/F.*");
88+
options.put(Finder.IREGEX, escapePath(dirStr+"REgeX" + File.separator + "F.*"));
8989
File[] files = finder.find(new File(dir, "regex"), options);
9090
assertEquals(3, files.length);
9191
}
@@ -140,6 +140,20 @@ public void testCanWriteFalse() {
140140
assertEquals(0, files.length);
141141
}
142142

143+
private static String escapePath(String text) {
144+
String repl = "\\";
145+
String with = "\\\\";
146+
147+
StringBuffer buf = new StringBuffer(text.length());
148+
int start = 0, end = 0;
149+
while ((end = text.indexOf(repl, start)) != -1) {
150+
buf.append(text.substring(start, end)).append(with);
151+
start = end + repl.length();
152+
}
153+
buf.append(text.substring(start));
154+
return buf.toString();
155+
}
156+
143157
}
144158

145159
class DebugListener implements FindListener {

0 commit comments

Comments
 (0)