Skip to content

Commit

Permalink
fix a bug for issue #122. (retry)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmi committed Jun 28, 2014
1 parent e19e4b3 commit 1a53767
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/jp/vmi/selenium/selenese/ErrorSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class ErrorSource implements Selenese, ITestTarget {
* @return this.
*/
public ErrorSource initialize(String filename, InvalidSeleneseException e) {
this.filename = PathUtils.normalizeSeparator(filename);
this.filename = PathUtils.normalize(filename);
this.e = e;
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jp/vmi/selenium/selenese/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static Selenese setContextForBackwardCompatibility(Selenese selenese, Con
protected final Document docucment;

protected Parser(String filename, Document document) {
this.filename = PathUtils.normalizeSeparator(filename);
this.filename = PathUtils.normalize(filename);
this.docucment = document;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jp/vmi/selenium/selenese/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void takeScreenshot(String filename) throws UnsupportedOperationException
TakesScreenshot tss = getTakesScreenshot();
if (tss == null)
throw new UnsupportedOperationException("webdriver does not support capturing screenshot.");
File file = new File(PathUtils.normalizeSeparator(filename));
File file = new File(PathUtils.normalize(filename));
if (screenshotDir != null)
file = new File(screenshotDir, file.getName());
takeScreenshot(tss, file);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jp/vmi/selenium/selenese/TestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public TestCase initialize(String filename, String name, Runner runner, String b
* @return this.
*/
public TestCase initialize(String filename, String name, String baseURL) {
this.filename = PathUtils.normalizeSeparator(filename);
this.filename = PathUtils.normalize(filename);
if (filename != null)
this.baseName = FilenameUtils.getBaseName(filename);
this.name = name;
Expand Down
4 changes: 2 additions & 2 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.normalizeSeparator(filename);
this.filename = PathUtils.normalize(filename);
if (filename != null)
this.parentDir = FilenameUtils.getFullPathNoEndSeparator(filename);
if (name != null)
Expand Down Expand Up @@ -82,7 +82,7 @@ public void addSelenese(Selenese selenese) {
* @param commandFactory command factory.
*/
public void addSeleneseFile(String filename, ICommandFactory commandFactory) {
filename = PathUtils.normalizeSeparator(filename);
filename = PathUtils.normalize(filename);
if (FilenameUtils.getPrefixLength(filename) == 0 && parentDir != null)
filename = FilenameUtils.concat(parentDir, filename);
addSelenese(Parser.parse(filename, commandFactory));
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/jp/vmi/selenium/selenese/utils/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

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

Expand Down Expand Up @@ -76,16 +78,28 @@ public static File searchExecutableFile(final String filename) {

private static final Pattern SEP_REGEX = Pattern.compile("[/\\\\]");
private static final String SEP_REPL = Matcher.quoteReplacement(File.separator);
private static final String PARENT_DIR = ".." + File.separator;

/**
* Normalize filename separator.
* Normalize separators and relative path in filename.
*
* @param filename filename with any platform separator.
* @param filename filename.
* @return normalized filename.
*/
public static String normalizeSeparator(String filename) {
public static String normalize(String filename) {
if (filename == null)
return null;
return SEP_REGEX.matcher(filename).replaceAll(SEP_REPL);
filename = SEP_REGEX.matcher(filename).replaceAll(SEP_REPL);
if (filename.startsWith(PARENT_DIR))
filename = new File(filename).getAbsolutePath();
String nfn = FilenameUtils.normalize(filename);
if (nfn == null) {
try {
nfn = new File(filename).getCanonicalPath();
} catch (IOException e) {
throw new IllegalArgumentException("Filename normalization failed: " + filename, e);
}
}
return nfn;
}
}
14 changes: 14 additions & 0 deletions src/test/java/jp/vmi/selenium/selenese/utils/PathUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ public void testSearchExecutableFiles() {
File file = PathUtils.searchExecutableFile(exeName);
assertThat(file, is(notNullValue()));
}

public String sep(String s) {
return s.replace('/', File.separatorChar);
}

@Test
public void testNormalize() {
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")));
}
}

0 comments on commit 1a53767

Please sign in to comment.