Permalink
Browse files

Implemented a plugin for Cucumber

  • Loading branch information...
1 parent e5cc8a2 commit 2faa915903229b14e4f1079c92ddf0fa4d257273 @vasiliygagin vasiliygagin committed Feb 9, 2014
Showing with 788 additions and 80 deletions.
  1. +4 −0 junitrunner-cucumber/.gitignore
  2. +29 −0 junitrunner-cucumber/pom.xml
  3. +32 −0 junitrunner-cucumber/src/main/java/com/github/junitrunner/cucumber/CucumberExamplesSuite.java
  4. +44 −0 junitrunner-cucumber/src/main/java/com/github/junitrunner/cucumber/CucumberFeatureSuite.java
  5. +79 −0 junitrunner-cucumber/src/main/java/com/github/junitrunner/cucumber/CucumberPlugin.java
  6. +33 −0 junitrunner-cucumber/src/main/java/com/github/junitrunner/cucumber/CucumberRunner.java
  7. +45 −0 junitrunner-cucumber/src/main/java/com/github/junitrunner/cucumber/CucumberRunnerListener.java
  8. +30 −0 junitrunner-cucumber/src/main/java/com/github/junitrunner/cucumber/CucumberScenarioOutlineSuite.java
  9. +146 −0 junitrunner-cucumber/src/main/java/com/github/junitrunner/cucumber/CucumberScenarioTest.java
  10. +61 −0 junitrunner-cucumber/src/main/java/com/github/junitrunner/cucumber/CucumberStep.java
  11. +64 −0 junitrunner-cucumber/src/main/java/com/github/junitrunner/cucumber/FormatterDelegate.java
  12. +38 −0 junitrunner-cucumber/src/main/java/com/github/junitrunner/cucumber/ReporterDelegate.java
  13. +11 −0 junitrunner-cucumber/src/test/java/com/github/junitrunner/cucumber/Cucumber_Runner_Test.java
  14. +38 −0 junitrunner-cucumber/src/test/java/com/github/junitrunner/cucumber/TestStepDefs.java
  15. +18 −0 junitrunner-cucumber/src/test/resources/com/github/junitrunner/cucumber/test.feature
  16. +1 −1 junitrunner-mockito/pom.xml
  17. +1 −1 junitrunner-spring/pom.xml
  18. +1 −1 junitrunner-unitils/pom.xml
  19. +5 −3 junitrunner/src/main/java/com/github/junitrunner/JUnitRunner.java
  20. +0 −5 junitrunner/src/main/java/com/github/junitrunner/JUnitStep.java
  21. +1 −20 junitrunner/src/main/java/com/github/junitrunner/JUnitTask.java
  22. +22 −17 junitrunner/src/main/java/com/github/junitrunner/RunNotifierWrapper.java
  23. +24 −0 junitrunner/src/main/java/com/github/junitrunner/StepRunnerListenerDelegate.java
  24. +26 −0 junitrunner/src/main/java/com/github/junitrunner/Task.java
  25. +6 −5 junitrunner/src/main/java/com/github/junitrunner/listener/BaseRunnerListener.java
  26. +5 −5 junitrunner/src/main/java/com/github/junitrunner/listener/RunnerListener.java
  27. +11 −10 junitrunner/src/main/java/com/github/junitrunner/listener/RunnerListenerContainer.java
  28. +12 −12 junitrunner/src/main/java/com/github/junitrunner/plugin/annotation/AnnotationTestRuleFactory.java
  29. +1 −0 pom.xml
@@ -0,0 +1,4 @@
+.settings
+.classpath
+.project
+target
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.github.junitrunner</groupId>
+ <artifactId>junitrunner-cucumber</artifactId>
+ <version>0.0.2-SNAPSHOT</version>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>com.github.junitrunner</groupId>
+ <artifactId>junitrunner</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>info.cukes</groupId>
+ <artifactId>cucumber-junit</artifactId>
+ <version>1.1.5</version>
+ </dependency>
+ <dependency>
+ <groupId>info.cukes</groupId>
+ <artifactId>cucumber-java</artifactId>
+ <version>1.1.5</version>
+ </dependency>
+ </dependencies>
+
+</project>
@@ -0,0 +1,32 @@
+package com.github.junitrunner.cucumber;
+
+import java.util.List;
+
+import org.junit.runner.Description;
+
+import com.github.junitrunner.JUnitSuite;
+
+import cucumber.runtime.Runtime;
+import cucumber.runtime.model.CucumberExamples;
+import cucumber.runtime.model.CucumberScenario;
+
+public class CucumberExamplesSuite extends JUnitSuite {
+
+ private final CucumberExamples cucumberExamples;
+
+ public CucumberExamplesSuite(Class<?> testClass, Runtime runtime, CucumberExamples cucumberExamples, CucumberPlugin cucumberPlugin) {
+ super(testClass);
+ this.cucumberExamples = cucumberExamples;
+
+ List<CucumberScenario> exampleScenarios = cucumberExamples.createExampleScenarios();
+ for (CucumberScenario scenario : exampleScenarios) {
+ super.addTask(new CucumberScenarioTest(runtime, scenario, cucumberPlugin));
+ }
+ }
+
+ @Override
+ protected Description describeSelf() {
+ return Description.createSuiteDescription(cucumberExamples.getExamples().getKeyword() + ": " + cucumberExamples.getExamples().getName(),
+ cucumberExamples.getExamples());
+ }
+}
@@ -0,0 +1,44 @@
+package com.github.junitrunner.cucumber;
+
+import gherkin.formatter.model.Feature;
+
+import org.junit.runner.Description;
+
+import com.github.junitrunner.JUnitSuite;
+
+import cucumber.runtime.Runtime;
+import cucumber.runtime.model.CucumberFeature;
+import cucumber.runtime.model.CucumberScenario;
+import cucumber.runtime.model.CucumberScenarioOutline;
+import cucumber.runtime.model.CucumberTagStatement;
+
+public class CucumberFeatureSuite extends JUnitSuite {
+
+ final CucumberPlugin cucumberPlugin;
+
+ final CucumberFeature cucumberFeature;
+
+ public CucumberFeatureSuite(Class<?> testClass, CucumberPlugin cucumberPlugin, CucumberFeature cucumberFeature, Runtime runtime) {
+ super(testClass);
+ this.cucumberPlugin = cucumberPlugin;
+ this.cucumberFeature = cucumberFeature;
+
+ for (CucumberTagStatement cucumberTagStatement : cucumberFeature.getFeatureElements()) {
+ if (cucumberTagStatement instanceof CucumberScenario) {
+ addTask(new CucumberScenarioTest(runtime, (CucumberScenario)cucumberTagStatement, cucumberPlugin));
+ } else {
+ addTask(new CucumberScenarioOutlineSuite(testClass, runtime, (CucumberScenarioOutline)cucumberTagStatement, cucumberPlugin));
+ }
+ }
+ }
+
+ @Override
+ protected Description describeSelf() {
+ return Description.createSuiteDescription(getFeatureName(), cucumberFeature.getGherkinFeature());
+ }
+
+ private String getFeatureName() {
+ Feature feature = cucumberFeature.getGherkinFeature();
+ return feature.getKeyword() + ": " + feature.getName();
+ }
+}
@@ -0,0 +1,79 @@
+package com.github.junitrunner.cucumber;
+
+import gherkin.formatter.Formatter;
+import gherkin.formatter.Reporter;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.runner.notification.RunNotifier;
+
+import com.github.junitrunner.JUnitBasePlugin;
+import com.github.junitrunner.JUnitRunner;
+import com.github.junitrunner.JUnitTask;
+import com.github.junitrunner.StepRunnerListenerDelegate;
+
+import cucumber.api.CucumberOptions;
+import cucumber.api.junit.Cucumber.Options;
+import cucumber.runtime.ClassFinder;
+import cucumber.runtime.Runtime;
+import cucumber.runtime.RuntimeOptions;
+import cucumber.runtime.RuntimeOptionsFactory;
+import cucumber.runtime.io.MultiLoader;
+import cucumber.runtime.io.ResourceLoader;
+import cucumber.runtime.io.ResourceLoaderClassFinder;
+import cucumber.runtime.junit.Assertions;
+import cucumber.runtime.model.CucumberFeature;
+
+public class CucumberPlugin extends JUnitBasePlugin {
+
+ private final Class<?> testClass;
+
+ Reporter reporter;
+ Formatter formatter;
+ boolean strict;
+
+ private Runtime runtime;
+ private List<CucumberFeature> cucumberFeatures;
+
+ // TODO remove notifier
+ RunNotifier notifier;
+
+ StepRunnerListenerDelegate stepRunnerListenerDelegate;
+
+ public CucumberPlugin(Class<?> testClass) {
+ this.testClass = testClass;
+ }
+
+ public void plug(JUnitRunner jUnitRunner) {
+
+ ClassLoader classLoader = testClass.getClassLoader();
+ Assertions.assertNoCucumberAnnotatedMethods(testClass);
+
+ RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(testClass, new Class[] { CucumberOptions.class, Options.class });
+ RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
+
+ ResourceLoader resourceLoader = new MultiLoader(classLoader);
+ ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
+ runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
+
+ reporter = runtimeOptions.reporter(classLoader);
+ formatter = runtimeOptions.formatter(classLoader);
+ strict = runtimeOptions.isStrict();
+
+ jUnitRunner.registerListener(new CucumberRunnerListener(formatter, runtime));
+
+ cucumberFeatures = runtimeOptions.cucumberFeatures(resourceLoader);
+
+ stepRunnerListenerDelegate = jUnitRunner.getStepRunnerListenerDelegate();
+ }
+
+ public List<JUnitTask> createTasks(List<Throwable> errors) {
+
+ List<JUnitTask> result = new ArrayList<JUnitTask>();
+ for (CucumberFeature cucumberFeature : cucumberFeatures) {
+ result.add(new CucumberFeatureSuite(testClass, this, cucumberFeature, runtime));
+ }
+ return result;
+ }
+}
@@ -0,0 +1,33 @@
+package com.github.junitrunner.cucumber;
+
+import java.util.List;
+
+import org.junit.runner.notification.RunNotifier;
+import org.junit.runners.model.InitializationError;
+
+import com.github.junitrunner.JUnitPlugin;
+import com.github.junitrunner.JUnitRunner;
+
+public class CucumberRunner extends JUnitRunner {
+
+ private CucumberPlugin cucumberPlugin;
+
+ public CucumberRunner(Class<?> clazz) throws InitializationError {
+ super(clazz);
+ }
+
+ @Override
+ protected List<JUnitPlugin> discoverPlugins() throws InitializationError {
+ List<JUnitPlugin> plugins = super.discoverPlugins();
+ cucumberPlugin = new CucumberPlugin(testClass);
+ plugins.add(cucumberPlugin);
+ return plugins;
+ }
+
+ // TODO remove
+ @Override
+ public void run(RunNotifier notifier) {
+ cucumberPlugin.notifier = notifier;
+ super.run(notifier);
+ }
+}
@@ -0,0 +1,45 @@
+package com.github.junitrunner.cucumber;
+
+import gherkin.formatter.Formatter;
+
+import org.junit.runner.Description;
+
+import com.github.junitrunner.Task;
+import com.github.junitrunner.listener.BaseRunnerListener;
+
+import cucumber.runtime.Runtime;
+import cucumber.runtime.snippets.SummaryPrinter;
+
+public class CucumberRunnerListener extends BaseRunnerListener {
+
+ private final Formatter formatter;
+ private final Runtime runtime;
+
+ public CucumberRunnerListener(Formatter formatter, Runtime runtime) {
+ this.formatter = formatter;
+ this.runtime = runtime;
+ }
+
+ @Override
+ public void testClassFinished(Description description) {
+ formatter.done();
+ formatter.close();
+ new SummaryPrinter(System.out).print(runtime);
+ }
+
+ @Override
+ public void taskStarted(Task task) {
+ if (task instanceof CucumberFeatureSuite) {
+ CucumberFeatureSuite feature = (CucumberFeatureSuite) task;
+ formatter.uri(feature.cucumberFeature.getUri());
+ formatter.feature(feature.cucumberFeature.getGherkinFeature());
+ }
+ }
+
+ @Override
+ public void taskFinished(Task task, Throwable failure) {
+ if (task instanceof CucumberFeatureSuite) {
+ formatter.eof();
+ }
+ }
+}
@@ -0,0 +1,30 @@
+package com.github.junitrunner.cucumber;
+
+import org.junit.runner.Description;
+
+import com.github.junitrunner.JUnitSuite;
+
+import cucumber.runtime.Runtime;
+import cucumber.runtime.model.CucumberExamples;
+import cucumber.runtime.model.CucumberScenarioOutline;
+
+public class CucumberScenarioOutlineSuite extends JUnitSuite {
+
+ private final CucumberScenarioOutline cucumberScenarioOutline;
+
+ public CucumberScenarioOutlineSuite(Class<?> testClass, Runtime runtime, CucumberScenarioOutline cucumberScenarioOutline,
+ CucumberPlugin cucumberPlugin) {
+ super(testClass);
+ this.cucumberScenarioOutline = cucumberScenarioOutline;
+
+ for (CucumberExamples cucumberExamples : cucumberScenarioOutline.getCucumberExamplesList()) {
+ CucumberExamplesSuite examplesSuite = new CucumberExamplesSuite(testClass, runtime, cucumberExamples, cucumberPlugin);
+ super.addTask(examplesSuite);
+ }
+ }
+
+ @Override
+ protected Description describeSelf() {
+ return Description.createSuiteDescription(cucumberScenarioOutline.getVisualName(), cucumberScenarioOutline.getGherkinModel());
+ }
+}
Oops, something went wrong.

0 comments on commit 2faa915

Please sign in to comment.