LoadFromRelativeFile function loadStoryAsTextfails to unescape URLs passed into the constructor that have escaped characters like %20 for a space. As a result, the story files cannot be found.
Test case snippet follows... add it to StoryLoaderBehaviour.java. Also need to add folder "foldername has spaces" to jbehave-core/src/test/java/org/jbehave/core/io/stories/ and copy MyPendingStory.txt into it. Also including revised code snippet for loadStoryAsText function. I didn't immediately see a way to do it without either checked exceptions or deprecated functions (ex UrlDecoder.decode(string)). Not sure if InvalidStoryResource was the right exception to convert to.
@Test
public void shouldLoadStoryFromRelativeFilePathsWithSpace() throws MalformedURLException, URISyntaxException {
String storyPath = "MyPendingStory.txt";
String storyAsText = "Given my step";
java.net.URL url = CodeLocations.codeLocationFromClass(MyPendingStory.class);
java.io.File folderWithSpacesInName = new java.io.File(url.toURI().getPath() + "/org/jbehave/core/io/stories/foldername has spaces");
java.net.URL urlThatHasEscapedSpaces = folderWithSpacesInName.toURI().toURL();
assertThat(folderWithSpacesInName.exists(), is(true));
StoryLoader loader = new LoadFromRelativeFile(urlThatHasEscapedSpaces);
assertThat(loader.loadStoryAsText(storyPath), equalTo(storyAsText));
}
public String loadStoryAsText(String storyPath) {
List<String> traversalPaths = new ArrayList<String>();
String locationPath;
try {
locationPath = new File(URLDecoder.decode(location.getFile(),"UTF-8")).getAbsolutePath();
} catch (UnsupportedEncodingException e) {
throw new InvalidStoryResource(storyPath, e);
}
for (StoryFilePath traversal : traversals) {
String filePath = locationPath.replace(traversal.toRemove, traversal.relativePath) + "/" + storyPath;
File file = new File(filePath);
if (file.exists()) {
return loadContent(filePath);
} else {
traversalPaths.add(filePath);
}
}
throw new StoryResourceNotFound(storyPath, traversalPaths);
}
I had a similar issue, my project was in [mage_hg] folder and I got "story not found" exception.
Couldn't understand what was going wrong until downloaded sources and debugged where I found out that "[mage_hg]" is converted to "%5bmage%5d".
If anyone don't want to fix sources, here is the workaround I came to: extend LoadFromRelativeFile and use it in configuration: