Skip to content

Commit

Permalink
fix filename normalization for issue #122.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmi committed Jul 5, 2014
1 parent c1be4ab commit 5db961a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/main/java/jp/vmi/selenium/selenese/TestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class TestSuite implements Selenese, ITestSuite {
* @return this.
*/
public TestSuite initialize(String filename, String name) {
this.filename = PathUtils.normalize(filename);
this.filename = filename = PathUtils.normalize(filename);
if (filename != null)
this.parentDir = FilenameUtils.getFullPathNoEndSeparator(filename);
if (name != null)
Expand Down Expand Up @@ -82,9 +82,10 @@ public void addSelenese(Selenese selenese) {
* @param commandFactory command factory.
*/
public void addSeleneseFile(String filename, ICommandFactory commandFactory) {
filename = PathUtils.normalize(filename);
if (FilenameUtils.getPrefixLength(filename) == 0 && parentDir != null)
filename = FilenameUtils.concat(parentDir, filename);
filename = PathUtils.concat(parentDir, filename);
else
filename = PathUtils.normalize(filename);
addSelenese(Parser.parse(filename, commandFactory));
}

Expand Down
53 changes: 43 additions & 10 deletions src/main/java/jp/vmi/selenium/selenese/utils/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.apache.commons.exec.OS;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import static org.apache.commons.io.FilenameUtils.*;

Expand Down Expand Up @@ -80,16 +81,7 @@ public static File searchExecutableFile(final String filename) {
private static final String SEP_REPL = Matcher.quoteReplacement(File.separator);
private static final String PARENT_DIR = ".." + File.separator;

/**
* Normalize separators and relative path in filename.
*
* @param filename filename.
* @return normalized filename.
*/
public static String normalize(String filename) {
if (filename == null)
return null;
filename = SEP_REGEX.matcher(filename).replaceAll(SEP_REPL);
private static String normalizeInternal(String filename) {
if (filename.startsWith(PARENT_DIR))
filename = new File(filename).getAbsolutePath();
String nfn = FilenameUtils.normalize(filename);
Expand All @@ -102,4 +94,45 @@ public static String normalize(String filename) {
}
return nfn;
}

/**
* Normalize filename separator.
*
* @param filename filename.
* @return separator normalized filename.
*/
public static String normalizeSeparator(String filename) {
if (filename == null)
return null;
return SEP_REGEX.matcher(filename).replaceAll(SEP_REPL);
}

/**
* Normalize filename.
*
* @param filename filename.
* @return normalized filename.
*/
public static String normalize(String filename) {
if (filename == null)
return null;
return normalizeInternal(normalizeSeparator(filename));
}

/**
* Concatinate filename.
*
* @param parent parent directory.
* @param child child file or directory.
* @return concatinated and normalized filename.
*/
public static String concat(String parent, String child) {
if (StringUtils.isEmpty(parent))
return normalize(child);
StringBuilder s = new StringBuilder(normalizeSeparator(parent));
if (!parent.endsWith(File.separator))
s.append(File.separatorChar);
s.append(normalizeSeparator(child));
return normalizeInternal(s.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ public String sep(String s) {

@Test
public void testNormalize() {
assertThat(PathUtils.normalizeSeparator("a/b/c"), equalTo(sep("a/b/c")));
assertThat(PathUtils.normalizeSeparator("a\\b\\c"), equalTo(sep("a/b/c")));
assertThat(PathUtils.normalize("a/b/c"), equalTo(sep("a/b/c")));
assertThat(PathUtils.normalize("./a/b/c"), equalTo(sep("a/b/c")));
String userDir = System.getProperty("user.dir");
String parentDir = new File(userDir).getParent();
assertThat(PathUtils.normalize("../a/b/c"), equalTo(sep(parentDir + "/a/b/c")));
assertThat(PathUtils.normalize("a/b/../c"), equalTo(sep("a/c")));
assertThat(PathUtils.concat("a", "b"), equalTo(sep("a/b")));
assertThat(PathUtils.concat("a/", "b"), equalTo(sep("a/b")));
assertThat(PathUtils.concat("./a", "b"), equalTo(sep("a/b")));
assertThat(PathUtils.concat("a", "../b"), equalTo(sep("b")));
assertThat(PathUtils.concat("../a", "b"), equalTo(sep(parentDir + "/a/b")));
assertThat(PathUtils.concat("../a", "../b"), equalTo(sep(parentDir + "/b")));
}
}

0 comments on commit 5db961a

Please sign in to comment.