Skip to content

Commit

Permalink
JBEHAVE-729: When running jbehave unit tests from inside a jar, the c…
Browse files Browse the repository at this point in the history
…urrent path is converted to file:/home/.../

remove trailing "file:" from path if it appears on CodeLocations.codeLocationFromClass
this broke building the path later from the string (confused as relative path)
  • Loading branch information
alexlehm committed Mar 2, 2012
1 parent 3e32827 commit 31ef505
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jbehave.core.io;

import static org.apache.commons.lang.StringUtils.removeEnd;
import static org.apache.commons.lang.StringUtils.removeStart;

import java.io.File;
import java.net.URL;
Expand All @@ -20,7 +21,7 @@ public class CodeLocations {
public static URL codeLocationFromClass(Class<?> codeLocationClass) {
String pathOfClass = codeLocationClass.getName().replace(".", "/") + ".class";
URL classResource = codeLocationClass.getClassLoader().getResource(pathOfClass);
String codeLocationPath = removeEnd(classResource.getFile(), pathOfClass);
String codeLocationPath = removeStart(removeEnd(classResource.getFile(), pathOfClass),"file:");
return codeLocationFromPath(codeLocationPath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;

import java.net.URL;

import org.jbehave.core.io.CodeLocations.InvalidCodeLocation;
import org.junit.Test;
import org.junit.runner.JUnitCore;

public class CodeLocationsBehaviour {

Expand Down Expand Up @@ -43,4 +46,13 @@ public void shouldAllowInstantiation() {
assertThat(new CodeLocations(), is(notNullValue()));
}

}
// wrong output looks like this:
// "C:/Projects/jbehave/file:/C:/Users/Name/.m2/repository/junit/junit-dep/4.8.2/junit-dep-4.8.2.jar!"

@Test
public void shouldCreateValidPathFromJarClass() {
assertThat(CodeLocations.codeLocationFromClass(this.getClass()).getFile(), not(containsString("/file:")));
assertThat(CodeLocations.codeLocationFromClass(JUnitCore.class).getFile(), not(containsString("/file:")));
}

}

0 comments on commit 31ef505

Please sign in to comment.