Skip to content
Browse files

WIP

  • Loading branch information...
1 parent d2e6bac commit 2e18a7da81a5b06ef7f326f999e88b951ce36baf DFUK committed Jan 16, 2012
View
52 core/src/main/java/cucumber/cli/Main.java
@@ -4,12 +4,11 @@
import cucumber.formatter.MultiFormatter;
import cucumber.io.FileResourceLoader;
import cucumber.runtime.Runtime;
+import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.snippets.SummaryPrinter;
import gherkin.formatter.Formatter;
-import gherkin.formatter.Reporter;
import java.io.File;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
@@ -21,13 +20,10 @@
static final String VERSION = ResourceBundle.getBundle("cucumber.version").getString("cucumber-jvm.version");
public static void main(String[] argv) throws Throwable {
- List<String> featurePaths = new ArrayList<String>();
- List<String> gluePaths = new ArrayList<String>();
- List<Object> filters = new ArrayList<Object>();
- String format = "progress";
List<String> args = new ArrayList<String>(asList(argv));
- String dotCucumber = null;
- boolean isDryRun = false;
+ RuntimeOptions opts = new RuntimeOptions();
+
+ String format = "progress";
FormatterFactory formatterFactory = new FormatterFactory();
MultiFormatter multiFormatter = new MultiFormatter();
@@ -43,57 +39,39 @@ public static void main(String[] argv) throws Throwable {
System.exit(0);
} else if (arg.equals("--glue") || arg.equals("-g")) {
String gluePath = args.remove(0);
- gluePaths.add(gluePath);
+ opts.addGluePath(gluePath);
} else if (arg.equals("--tags") || arg.equals("-t")) {
- filters.add(args.remove(0));
+ String tags = args.remove(0);
+ opts.addTags(tags);
} else if (arg.equals("--format") || arg.equals("-f")) {
format = args.remove(0);
+ opts.addFormat(format);
} else if (arg.equals("--out") || arg.equals("-o")) {
File out = new File(args.remove(0));
+ opts.addOut(out);
Formatter formatter = formatterFactory.createFormatter(format, out);
multiFormatter.add(formatter);
} else if (arg.equals("--dotcucumber")) {
- dotCucumber = args.remove(0);
+ File dotCucumber = new File(args.remove(0));
+ opts.setDotCucumber(dotCucumber);
} else if (arg.equals("--dry-run") || arg.equals("-d")) {
- isDryRun = true;
+ opts.setDryRun(true);
} else {
- // TODO: Use PathWithLines and add line filter if any
- featurePaths.add(arg);
+ opts.addFeaturePath(arg);
}
}
if (multiFormatter.isEmpty()) {
multiFormatter.add(formatterFactory.createFormatter(format, System.out));
}
- if (gluePaths.isEmpty()) {
- System.out.println("Missing option: --glue");
- System.exit(1);
- }
-
- Runtime runtime = new Runtime(gluePaths, new FileResourceLoader(), isDryRun);
+ Runtime runtime = new Runtime(opts, new FileResourceLoader());
- if (dotCucumber != null) {
- writeDotCucumber(featurePaths, dotCucumber, runtime);
- }
- run(featurePaths, filters, multiFormatter, runtime);
+ runtime.run();
printSummary(runtime);
System.exit(runtime.exitStatus());
}
- private static void writeDotCucumber(List<String> featurePaths, String dotCucumberPath, Runtime runtime) throws IOException {
- File dotCucumber = new File(dotCucumberPath);
- dotCucumber.mkdirs();
- runtime.writeStepdefsJson(featurePaths, dotCucumber);
- }
-
- private static void run(List<String> featurePaths, List<Object> filters, MultiFormatter multiFormatter, Runtime runtime) throws IOException {
- Formatter formatter = multiFormatter.formatterProxy();
- Reporter reporter = multiFormatter.reporterProxy();
- runtime.run(featurePaths, filters, formatter, reporter);
- formatter.done();
- }
-
private static void printSummary(Runtime runtime) {
new SummaryPrinter(System.out).print(runtime);
}
View
60 core/src/main/java/cucumber/runtime/Runtime.java
@@ -1,56 +1,39 @@
package cucumber.runtime;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
import cucumber.io.ClasspathResourceLoader;
import cucumber.io.ResourceLoader;
-import cucumber.runtime.autocomplete.MetaStepdef;
-import cucumber.runtime.autocomplete.StepdefGenerator;
import cucumber.runtime.model.CucumberFeature;
import cucumber.runtime.model.CucumberTagStatement;
import gherkin.formatter.Formatter;
-import gherkin.formatter.Reporter;
import gherkin.formatter.model.Step;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
-import static cucumber.runtime.model.CucumberFeature.load;
-import static java.util.Collections.emptyList;
+import static cucumber.runtime.model.CucumberFeature.loadFeatures;
/**
* This is the main entry point for running Cucumber features.
*/
public class Runtime {
private static final byte ERRORS = 0x1;
- private static final List<Object> NO_FILTERS = emptyList();
- private static final Collection<String> NO_TAGS = emptyList();
private final UndefinedStepsTracker tracker;
private final List<Throwable> errors = new ArrayList<Throwable>();
private final Collection<? extends Backend> backends;
- private final boolean isDryRun;
- private final List<String> gluePaths;
+ private final RuntimeOptions runtimeOptions;
private final ResourceLoader resourceLoader;
- public Runtime(List<String> gluePaths, ResourceLoader resourceLoader) {
- this(gluePaths, resourceLoader, false);
+ public Runtime(RuntimeOptions runtimeOptions, ResourceLoader resourceLoader) {
+ this(runtimeOptions, resourceLoader, loadBackends(resourceLoader));
}
- public Runtime(List<String> gluePaths, ResourceLoader resourceLoader, boolean isDryRun) {
- this(gluePaths, resourceLoader, loadBackends(resourceLoader), isDryRun);
- }
-
- public Runtime(List<String> gluePaths, ResourceLoader resourceLoader, Collection<? extends Backend> backends, boolean isDryRun) {
- this.gluePaths = gluePaths;
+ public Runtime(RuntimeOptions runtimeOptions, ResourceLoader resourceLoader, Collection<? extends Backend> backends) {
+ this.runtimeOptions = runtimeOptions;
this.backends = backends;
this.resourceLoader = resourceLoader;
- this.isDryRun = isDryRun;
this.tracker = new UndefinedStepsTracker(backends);
}
@@ -62,24 +45,27 @@ public void addError(Throwable error) {
errors.add(error);
}
- public void run(List<String> featurePaths, final List<Object> filters, gherkin.formatter.Formatter formatter, Reporter reporter) {
- for (CucumberFeature cucumberFeature : load(resourceLoader, featurePaths, filters)) {
- run(cucumberFeature, formatter, reporter);
+ public void run() {
+ runtimeOptions.writeDotCucumber(runtime);
+ for (CucumberFeature cucumberFeature : loadFeatures(resourceLoader, runtimeOptions.getFeaturePaths(), runtimeOptions.getFilters())) {
+ run(cucumberFeature);
}
+ runtimeOptions.getFormatter().done();
}
- public void run(CucumberFeature cucumberFeature, Formatter formatter, Reporter reporter) {
+ public void run(CucumberFeature cucumberFeature) {
+ Formatter formatter = runtimeOptions.getFormatter();
formatter.uri(cucumberFeature.getUri());
formatter.feature(cucumberFeature.getFeature());
for (CucumberTagStatement cucumberTagStatement : cucumberFeature.getFeatureElements()) {
- cucumberTagStatement.run(formatter, reporter, this);
+ cucumberTagStatement.run(formatter, runtimeOptions.getReporter(), this);
}
formatter.eof();
}
public void buildBackendWorlds(World world) {
for (Backend backend : backends) {
- backend.buildWorld(gluePaths, world);
+ backend.buildWorld(runtimeOptions.getGluePaths(), world);
}
tracker.reset();
}
@@ -90,22 +76,8 @@ public void disposeBackendWorlds() {
}
}
- public void writeStepdefsJson(List<String> featurePaths, File dotCucumber) throws IOException {
- List<CucumberFeature> features = load(resourceLoader, featurePaths, NO_FILTERS);
- World world = new RuntimeWorld(this, NO_TAGS);
- buildBackendWorlds(world);
- List<StepDefinition> stepDefs = world.getStepDefinitions();
- List<MetaStepdef> metaStepdefs = new StepdefGenerator().generate(stepDefs, features);
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- String json = gson.toJson(metaStepdefs);
-
- FileWriter metaJson = new FileWriter(new File(dotCucumber, "stepdefs.json"));
- metaJson.append(json);
- metaJson.close();
- }
-
public boolean isDryRun() {
- return isDryRun;
+ return runtimeOptions.isDryRun();
}
public List<Throwable> getErrors() {
View
104 core/src/main/java/cucumber/runtime/RuntimeOptions.java
@@ -0,0 +1,104 @@
+package cucumber.runtime;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import cucumber.formatter.MultiFormatter;
+import cucumber.runtime.autocomplete.MetaStepdef;
+import cucumber.runtime.autocomplete.StepdefGenerator;
+import cucumber.runtime.model.CucumberFeature;
+import gherkin.formatter.Formatter;
+import gherkin.formatter.Reporter;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import static cucumber.runtime.model.CucumberFeature.loadFeatures;
+import static java.util.Collections.emptyList;
+
+public class RuntimeOptions {
+ private static final List<Object> NO_FILTERS = emptyList();
+ private static final Collection<String> NO_TAGS = emptyList();
+
+ private final MultiFormatter multiFormatter = new MultiFormatter();
+ private final List<Object> filters = new ArrayList<Object>();
+ private final List<String> featurePaths = new ArrayList<String>();
+ private final List<String> gluePaths = new ArrayList<String>();
+ private boolean dryRun;
+ private File dotCucumber;
+
+ public void addGluePath(String gluePath) {
+ gluePaths.add(gluePath);
+ }
+
+ public void addTags(String tags) {
+ filters.add(tags);
+ }
+
+ public void addFormat(String format) {
+ }
+
+ public void addOut(File out) {
+ }
+
+ public void setDotCucumber(File dotCucumber) {
+ this.dotCucumber = dotCucumber;
+ }
+
+ public void setDryRun(boolean dryRun) {
+ this.dryRun = dryRun;
+ }
+
+ // TODO: Use PathWithLines and add line filter if any
+ public void addFeaturePath(String path) {
+ featurePaths.add(path);
+ }
+
+ public void validate() {
+ if (gluePaths.isEmpty()) {
+ throw new CucumberException("Missing option: --glue");
+ }
+ }
+
+ public List<String> getGluePaths() {
+ return gluePaths;
+ }
+
+ public boolean isDryRun() {
+ return dryRun;
+ }
+
+ public List<Object> getFilters() {
+ return filters;
+ }
+
+ public List<String> getFeaturePaths() {
+ return featurePaths;
+ }
+
+ public Formatter getFormatter() {
+ return multiFormatter.formatterProxy();
+ }
+
+ public Reporter getReporter() {
+ return multiFormatter.reporterProxy();
+ }
+
+ public void writeDotCucumber(Runtime runtime) throws IOException {
+ dotCucumber.mkdirs();
+ List<CucumberFeature> features = loadFeatures(resourceLoader, featurePaths, NO_FILTERS);
+ World world = new RuntimeWorld(runtime, NO_TAGS);
+ runtime.buildBackendWorlds(world);
+ List<StepDefinition> stepDefs = world.getStepDefinitions();
+ List<MetaStepdef> metaStepdefs = new StepdefGenerator().generate(stepDefs, features);
+ Gson gson = new GsonBuilder().setPrettyPrinting().create();
+ String json = gson.toJson(metaStepdefs);
+
+ FileWriter metaJson = new FileWriter(new File(dotCucumber, "stepdefs.json"));
+ metaJson.append(json);
+ metaJson.close();
+ }
+}
View
2 core/src/main/java/cucumber/runtime/model/CucumberFeature.java
@@ -24,7 +24,7 @@
private CucumberScenarioOutline currentScenarioOutline;
// TODO: Use an iterable here, wrapping around
- public static List<CucumberFeature> load(ResourceLoader resourceLoader, List<String> featurePaths, final List<Object> filters) {
+ public static List<CucumberFeature> loadFeatures(ResourceLoader resourceLoader, List<String> featurePaths, final List<Object> filters) {
final List<CucumberFeature> cucumberFeatures = new ArrayList<CucumberFeature>();
final FeatureBuilder builder = new FeatureBuilder(cucumberFeatures);
for (String featurePath : featurePaths) {
View
3 core/src/test/java/cucumber/runtime/BackgroundTest.java
@@ -1,6 +1,5 @@
package cucumber.runtime;
-import cucumber.io.ClasspathResourceLoader;
import cucumber.runtime.model.CucumberFeature;
import gherkin.formatter.PrettyFormatter;
import gherkin.formatter.model.Step;
@@ -18,7 +17,7 @@
@Test
public void should_run_background() throws IOException {
Backend backend = new TestBackend();
- Runtime runtime = new Runtime(new ArrayList<String>(), new ClasspathResourceLoader(), asList(backend), false);
+ Runtime runtime = new Runtime(new ArrayList<String>(), asList(backend));
CucumberFeature feature = feature("test.feature", "" +
"Feature:\n" +
" Background:\n" +
View
3 core/src/test/java/cucumber/runtime/HookTest.java
@@ -1,6 +1,5 @@
package cucumber.runtime;
-import cucumber.io.ClasspathResourceLoader;
import gherkin.formatter.Reporter;
import org.junit.Test;
import org.mockito.InOrder;
@@ -29,7 +28,7 @@ public void after_hooks_execute_before_objects_are_disposed() throws Throwable {
HookDefinition hook = mock(HookDefinition.class);
when(hook.matches(anyListOf(String.class))).thenReturn(true);
- Runtime runtime = new Runtime(CODE_PATHS, new ClasspathResourceLoader(), asList(backend), false);
+ Runtime runtime = new Runtime(CODE_PATHS, asList(backend));
World world = new RuntimeWorld(runtime, TAGS);
world.addAfterHook(hook);
View
3 core/src/test/java/cucumber/runtime/RuntimeTest.java
@@ -1,6 +1,5 @@
package cucumber.runtime;
-import cucumber.io.ClasspathResourceLoader;
import cucumber.runtime.model.CucumberFeature;
import gherkin.formatter.JSONFormatter;
import org.junit.Test;
@@ -25,7 +24,7 @@ public void runs_feature_with_json_formatter() throws Exception {
StringBuilder out = new StringBuilder();
JSONFormatter jsonFormatter = new JSONFormatter(out);
List<Backend> backends = asList(mock(Backend.class));
- new Runtime(Collections.<String>emptyList(), new ClasspathResourceLoader(), backends, true).run(feature, jsonFormatter, jsonFormatter);
+ new Runtime(Collections.<String>emptyList(), backends).run(feature, jsonFormatter, jsonFormatter);
jsonFormatter.done();
String expected = "[{\"id\":\"feature-name\",\"description\":\"\",\"name\":\"feature name\",\"keyword\":\"Feature\",\"line\":1,\"elements\":[{\"description\":\"\",\"name\":\"background name\",\"keyword\":\"Background\",\"line\":2,\"steps\":[{\"result\":{\"status\":\"undefined\"},\"name\":\"b\",\"keyword\":\"Given \",\"line\":3,\"match\":{}}],\"type\":\"background\"},{\"id\":\"feature-name;scenario-name\",\"description\":\"\",\"name\":\"scenario name\",\"keyword\":\"Scenario\",\"line\":4,\"steps\":[{\"result\":{\"status\":\"undefined\"},\"name\":\"s\",\"keyword\":\"When \",\"line\":5,\"match\":{}}],\"type\":\"scenario\"}],\"uri\":\"test.feature\"}]";
assertEquals(expected, out.toString());
View
3 java/src/test/java/cucumber/runtime/java/JavaStepDefinitionTest.java
@@ -1,7 +1,6 @@
package cucumber.runtime.java;
import cucumber.annotation.en.Given;
-import cucumber.io.ClasspathResourceLoader;
import cucumber.runtime.AmbiguousStepDefinitionsException;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeWorld;
@@ -38,7 +37,7 @@
private final Defs defs = new Defs();
private final JavaBackend backend = new JavaBackend(new SingletonFactory(defs));
- private final Runtime runtime = new Runtime(NO_PATHS, new ClasspathResourceLoader(), asList(backend), false);
+ private final Runtime runtime = new Runtime(NO_PATHS, asList(backend));
private final World fooWorld = new RuntimeWorld(runtime, asList("@foo"));
@Test(expected = AmbiguousStepDefinitionsException.class)
View
34 junit/src/main/java/cucumber/junit/Cucumber.java
@@ -17,7 +17,7 @@
import java.util.List;
import static cucumber.runtime.Utils.packagePath;
-import static cucumber.runtime.model.CucumberFeature.load;
+import static cucumber.runtime.model.CucumberFeature.loadFeatures;
import static java.util.Arrays.asList;
/**
@@ -27,9 +27,9 @@
* Cucumber will look for a {@code .feature} file on the classpath, using the same resource
* path as the annotated class ({@code .class} substituted by {@code .feature}).
* <p/>
- * Additional hints can be given to Cucumber by annotating the class with {@link cucumber.junit.Feature}.
+ * Additional hints can be given to Cucumber by annotating the class with {@link Options}.
*
- * @see cucumber.junit.Feature
+ * @see Options
*/
public class Cucumber extends ParentRunner<FeatureRunner> {
private final ResourceLoader resourceLoader = new ClasspathResourceLoader();
@@ -51,7 +51,7 @@ public Cucumber(Class clazz) throws InitializationError, IOException {
List<String> featurePaths = featurePaths(clazz);
List<String> gluePaths = gluePaths(clazz);
- runtime = new Runtime(gluePaths, resourceLoader);
+ runtime = new Runtime(runtimeOptions, gluePaths, resourceLoader);
// TODO: Create formatter(s) based on Annotations. Use same technique as in cli.Main for MultiFormatter
jUnitReporter = new JUnitReporter(new NullReporter(), new NullReporter());
@@ -98,10 +98,10 @@ private void assertNoDeclaredMethods(Class clazz) {
* @return either a path to a single feature, or to a directory or classpath entry containing them
*/
private List<String> featurePaths(Class clazz) {
- cucumber.junit.Feature featureAnnotation = getFeatureAnnotation(clazz);
+ Options optionsAnnotation = getOptionsAnnotation(clazz);
String featurePath;
- if (featureAnnotation != null) {
- featurePath = featureAnnotation.value();
+ if (optionsAnnotation != null) {
+ featurePath = optionsAnnotation.value();
} else {
featurePath = packagePath(clazz);
}
@@ -114,29 +114,29 @@ private void assertNoDeclaredMethods(Class clazz) {
gluePaths.add(packagePath(clazz));
// Add additional ones
- cucumber.junit.Feature featureAnnotation = getFeatureAnnotation(clazz);
- if (featureAnnotation != null) {
- for (String packageName : featureAnnotation.packages()) {
+ Options optionsAnnotation = getOptionsAnnotation(clazz);
+ if (optionsAnnotation != null) {
+ for (String packageName : optionsAnnotation.glue()) {
gluePaths.add(packagePath(packageName));
}
}
return gluePaths;
}
private List<Object> filters(Class clazz) {
- cucumber.junit.Feature featureAnnotation = getFeatureAnnotation(clazz);
+ Options optionsAnnotation = getOptionsAnnotation(clazz);
Object[] filters = new Object[0];
- if (featureAnnotation != null) {
- filters = toLong(featureAnnotation.lines());
+ if (optionsAnnotation != null) {
+ filters = toLong(optionsAnnotation.lines());
if (filters.length == 0) {
- filters = featureAnnotation.tags();
+ filters = optionsAnnotation.tags();
}
}
return asList(filters);
}
private void addChildren(List<String> featurePaths, final List<Object> filters, List<String> gluePaths) throws InitializationError {
- List<CucumberFeature> cucumberFeatures = load(resourceLoader, featurePaths, filters);
+ List<CucumberFeature> cucumberFeatures = loadFeatures(resourceLoader, featurePaths, filters);
for (CucumberFeature cucumberFeature : cucumberFeatures) {
children.add(new FeatureRunner(cucumberFeature, gluePaths, runtime, jUnitReporter));
}
@@ -150,7 +150,7 @@ private void addChildren(List<String> featurePaths, final List<Object> filters,
return longs;
}
- private Feature getFeatureAnnotation(Class clazz) {
- return (Feature) clazz.getAnnotation(Feature.class);
+ private Options getOptionsAnnotation(Class clazz) {
+ return (Options) clazz.getAnnotation(Options.class);
}
}
View
6 ...src/main/java/cucumber/junit/Feature.java → ...src/main/java/cucumber/junit/Options.java
@@ -7,11 +7,11 @@
/**
* This annotation can be used to give additional hints to the {@link Cucumber} runner
- * about what to run.
+ * about what to run. The various values are similar to what's accepted by {@link cucumber.cli.Main}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
-public @interface Feature {
+public @interface Options {
/**
* @return the path to the .feature
*/
@@ -30,5 +30,5 @@
/**
* @return where to look for glue code (stepdefs and hooks)
*/
- String[] packages() default {};
+ String[] glue() default {};
}
View
4 junit/src/test/java/cucumber/junit/CucumberTest.java
@@ -52,11 +52,11 @@ public void finds_no_features_when_explicit_package_has_nothnig() throws IOExcep
private class ImplicitPackage {
}
- @Feature("cucumber/junit")
+ @Options("cucumber/junit")
private class ExplicitPackage {
}
- @Feature("gibber/ish")
+ @Options("gibber/ish")
private class ExplicitPackageWithNoFeatures {
}
}
View
1 spring/src/test/java/cucumber/runtime/java/spring/RunCukesTest.java
@@ -1,7 +1,6 @@
package cucumber.runtime.java.spring;
import cucumber.junit.Cucumber;
-import cucumber.junit.Feature;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)

0 comments on commit 2e18a7d

Please sign in to comment.
Something went wrong with that request. Please try again.