Skip to content

Commit

Permalink
Use a classpath: prefix if you want to load resources (features and s…
Browse files Browse the repository at this point in the history
…tepdef scripts) from the classpath and not the file system. Should be a transparent change for JUnit users. See #312
  • Loading branch information
aslakhellesoy committed May 2, 2012
1 parent 250a977 commit cdf1080
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/cucumber/cli/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cucumber.cli;

import cucumber.io.FileResourceLoader;
import cucumber.io.MultiLoader;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;

Expand All @@ -15,7 +16,7 @@ public static void main(String[] argv) throws Throwable {
public static void run(String[] argv, ClassLoader classLoader) throws IOException {
RuntimeOptions runtimeOptions = new RuntimeOptions(argv);

Runtime runtime = new Runtime(new FileResourceLoader(), classLoader, runtimeOptions);
Runtime runtime = new Runtime(new MultiLoader(classLoader), classLoader, runtimeOptions);
runtime.writeStepdefsJson();
runtime.run();
System.exit(runtime.exitStatus());
Expand Down
22 changes: 22 additions & 0 deletions core/src/main/java/cucumber/io/MultiLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cucumber.io;

public class MultiLoader implements ResourceLoader {
public static final String CLASSPATH_SCHEME = "classpath://";

private final ClasspathResourceLoader classpath;
private final FileResourceLoader fs;

public MultiLoader(ClassLoader classLoader) {
classpath = new ClasspathResourceLoader(classLoader);
fs = new FileResourceLoader();
}

@Override
public Iterable<Resource> resources(String path, String suffix) {
if (path.startsWith(CLASSPATH_SCHEME)) {
return classpath.resources(path.substring(CLASSPATH_SCHEME.length()), suffix);
} else {
return fs.resources(path, suffix);
}
}
}
1 change: 1 addition & 0 deletions core/src/main/java/cucumber/runtime/Runtime.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cucumber.runtime;

import cucumber.io.ClasspathResourceLoader;
import cucumber.io.MultiLoader;
import cucumber.io.ResourceLoader;
import cucumber.runtime.converters.LocalizedXStreams;
import cucumber.runtime.model.CucumberFeature;
Expand Down
9 changes: 4 additions & 5 deletions junit/src/main/java/cucumber/junit/Cucumber.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cucumber.junit;

import cucumber.io.ClasspathResourceLoader;
import cucumber.io.MultiLoader;
import cucumber.io.ResourceLoader;
import cucumber.runtime.CucumberException;
import cucumber.runtime.Runtime;
Expand Down Expand Up @@ -49,11 +49,12 @@ public class Cucumber extends ParentRunner<FeatureRunner> {
public Cucumber(Class clazz) throws InitializationError, IOException {
super(clazz);
ClassLoader classLoader = clazz.getClassLoader();
ResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader);
assertNoCucumberAnnotatedMethods(clazz);

RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(clazz);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();

ResourceLoader resourceLoader = new MultiLoader(classLoader);
runtime = new Runtime(resourceLoader, classLoader, runtimeOptions);

jUnitReporter = new JUnitReporter(runtimeOptions.reporter(classLoader), runtimeOptions.formatter(classLoader), runtimeOptions.strict);
Expand Down Expand Up @@ -119,9 +120,7 @@ private void addChildren(List<CucumberFeature> cucumberFeatures) throws Initiali
boolean dryRun() default false;

/**
* Scenarios fail if
*
* @return
* @return true if strict mode is enabled (fail if there are undefined or pending steps)
*/
boolean strict() default false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cucumber.junit;

import cucumber.io.MultiLoader;
import cucumber.runtime.RuntimeOptions;

import java.util.ArrayList;
Expand Down Expand Up @@ -95,7 +96,7 @@ private void addFeatures(Cucumber.Options options, Class clazz, List<String> arg
if (options != null && options.features().length != 0) {
Collections.addAll(args, options.features());
} else {
args.add(packagePath(clazz));
args.add(MultiLoader.CLASSPATH_SCHEME + packagePath(clazz));
}
}

Expand Down
4 changes: 2 additions & 2 deletions junit/src/test/java/cucumber/junit/CucumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public void no_stepdefs_in_cucumber_runner_invalid() {
private class ImplicitPackage {
}

@Cucumber.Options(features = {"cucumber/junit"})
@Cucumber.Options(features = {"classpath://cucumber/junit"})
private class ExplicitPackage {
}

@Cucumber.Options(features = {"gibber/ish"})
@Cucumber.Options(features = {"classpath://gibber/ish"})
private class ExplicitPackageWithNoFeatures {
}
}

0 comments on commit cdf1080

Please # to comment.