Skip to content

Commit

Permalink
Our first cucumber-features scenario is passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Jun 27, 2011
1 parent 0305720 commit 6762f16
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
target
*.ipr
*.iws
tmp/
doc/
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "cucumber-features"]
path = cucumber-features
url = git://github.com/cucumber/cucumber-features.git
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm @cucumber --create
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'http://rubygems.org'

gem 'cucumber', '1.0.0'
gem 'aruba', '0.4.3'
43 changes: 43 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,43 @@
GEM
remote: http://rubygems.org/
specs:
aruba (0.4.3)
bcat (>= 0.6.1)
childprocess (>= 0.1.9)
cucumber (>= 0.10.7)
rdiscount (>= 1.6.8)
rspec (>= 2.6.0)
bcat (0.6.1)
rack (~> 1.0)
builder (3.0.0)
childprocess (0.1.9)
ffi (~> 1.0.6)
cucumber (1.0.0)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
gherkin (~> 2.4.1)
json (>= 1.4.6)
term-ansicolor (>= 1.0.5)
diff-lcs (1.1.2)
ffi (1.0.9)
gherkin (2.4.1)
json (>= 1.4.6)
json (1.5.3)
rack (1.3.0)
rdiscount (1.6.8)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
term-ansicolor (1.0.5)

PLATFORMS
ruby

DEPENDENCIES
aruba (= 0.4.3)
cucumber (= 1.0.0)
7 changes: 7 additions & 0 deletions Rakefile
@@ -0,0 +1,7 @@
require 'cucumber/rake/task'

Cucumber::Rake::Task.new(:picocontainer) do |t|
t.cucumber_opts = '-r java/src/test/resources/cucumber-features -r cucumber-features cucumber-features'
end

task :default => :picocontainer
9 changes: 7 additions & 2 deletions core/src/main/java/cucumber/junit/Cucumber.java
Expand Up @@ -22,7 +22,7 @@ public class Cucumber extends ParentRunner<ParentRunner> {

private static Runtime runtime(Class testClass) {
String packageName = testClass.getName().substring(0, testClass.getName().lastIndexOf("."));
final Runtime runtime = new Runtime();
final Runtime runtime = new Runtime(packageName);
java.lang.Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Expand All @@ -44,7 +44,12 @@ public Cucumber(Class featureClass) throws InitializationError {
public Cucumber(Class featureClass, final Runtime runtime) throws InitializationError {
// Why aren't we passing the class to super? I don't remember, but there is probably a good reason.
super(null);
pathName = featureClass.getName().replace('.', '/') + ".feature";
cucumber.junit.Feature featureAnnotation = (cucumber.junit.Feature) featureClass.getAnnotation(cucumber.junit.Feature.class);
if(featureAnnotation != null) {
pathName = featureAnnotation.value();
} else {
pathName = featureClass.getName().replace('.', '/') + ".feature";
}
builder = new RunnerBuilder(runtime, children);
feature = parseFeature();
}
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/cucumber/junit/Feature.java
@@ -0,0 +1,12 @@
package cucumber.junit;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Feature {
String value();
}
7 changes: 4 additions & 3 deletions core/src/main/java/cucumber/junit/RunnerBuilder.java
Expand Up @@ -2,6 +2,7 @@

import cucumber.runtime.CucumberException;
import cucumber.runtime.Runtime;
import gherkin.GherkinParser;
import gherkin.formatter.Formatter;
import gherkin.formatter.model.*;
import org.junit.runners.ParentRunner;
Expand All @@ -14,7 +15,7 @@ class RunnerBuilder implements Formatter {
private final Runtime runtime;
private final List<ParentRunner> children;
private List<ScenarioRunner> scenarioRunners = new ArrayList<ScenarioRunner>();
private Feature feature;
private gherkin.formatter.model.Feature feature;
private ScenarioRunner scenarioRunner;

public RunnerBuilder(Runtime runtime, List<ParentRunner> children) {
Expand All @@ -25,7 +26,7 @@ public RunnerBuilder(Runtime runtime, List<ParentRunner> children) {
public void uri(String uri) {
}

public void feature(Feature feature) {
public void feature(gherkin.formatter.model.Feature feature) {
this.feature = feature;
}

Expand Down Expand Up @@ -57,7 +58,7 @@ public void eof() {
public void syntaxError(String state, String event, List<String> legalEvents, String uri, int line) {
}

public Feature getFeature() {
public gherkin.formatter.model.Feature getFeature() {
return feature;
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/cucumber/runtime/Runtime.java
Expand Up @@ -21,8 +21,8 @@ public Runtime(Backend... backends) {
this.backends = asList(backends);
}

public Runtime() {
backends = Classpath.instantiateSubclasses(Backend.class, "cucumber.runtime");
public Runtime(String packageName) {
backends = Classpath.instantiateSubclasses(Backend.class, packageName);
}

public StepDefinitionMatch stepDefinitionMatch(Step step) {
Expand Down
1 change: 1 addition & 0 deletions cucumber-features
Submodule cucumber-features added at 5dcb56
104 changes: 104 additions & 0 deletions java/src/test/resources/cucumber-features/cucumber_java_mappings.rb
@@ -0,0 +1,104 @@
require 'erb'

module CucumberJavaMappings
def features_dir
"src/test/resources"
end

def run_scenario(scenario_name)
write_pom
write_test_unit_classes
run_simple "mvn test", false
end

def write_pom
write_file('pom.xml', <<-EOF)
<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>
<parent>
<groupId>cucumber</groupId>
<artifactId>parent</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.4.3-SNAPSHOT</version>
</parent>
<artifactId>cucumber-picocontainer-test</artifactId>
<version>0.4.3-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Cucumber: PicoContainer Test</name>
<dependencies>
<dependency>
<groupId>cucumber</groupId>
<artifactId>picocontainer</artifactId>
<version>0.4.3-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
EOF
end

@@mappings_counter = 1

def write_passing_mapping(step_name)
erb = ERB.new(<<-EOF, nil, '-')
package cucumber.test;
import cucumber.annotation.EN.Given;
public class Mappings<%= @@mappings_counter %> {
@Given("<%= step_name -%>")
public void <%= step_name.gsub(/ /, '_') -%>() {
// ARUBA_IGNORE_START
try {
new java.io.FileWriter("<%= step_file(step_name) %>");
} catch(java.io.IOException e) {
throw new RuntimeException(e);
}
// ARUBA_IGNORE_END
}
}
EOF
write_file("src/test/java/cucumber/test/Mappings#{@@mappings_counter}.java", erb.result(binding))
@@mappings_counter += 1
end

def write_test_unit_classes
features = in_current_dir do
Dir.chdir(features_dir) do
Dir["**/*.feature"]
end
end
features.each do |feature|
class_name = File.basename(feature).match(/(.*)\.feature$/)[1] + "_Test"
write_file("src/test/java/cucumber/test/#{class_name}.java", <<-EOF)
package cucumber.test;
import cucumber.junit.Cucumber;
import cucumber.junit.Feature;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@Feature("#{feature}")
public class #{class_name} {}
EOF
end
end

def assert_passing_scenario
2 tests because we have 2 steps
assert_partial_output("Tests run: 2, Failures: 0, Errors: 0, Skipped: 0", all_output)
assert_success true
end
end

World(CucumberJavaMappings)

Before do
@aruba_timeout_seconds = 10
end
@@ -1,14 +1,17 @@
package cucumber.runtime.junit;

import cucumber.junit.Cucumber;
import cucumber.junit.Feature;
import org.junit.runner.RunWith;

/**
* In order to run a Cucumber Feature from JUnit - all you need is an empty class annotated like below.
* The class must be named the same as the feature file and be in the same package.
* The @Feature annotation is not required if the test class is named the same as the feature file and
* in the same package.
*
* You can write this stub class by hand, but Cucumber will also have a tool that can generate them
* from .feature sources.
*/
@RunWith(Cucumber.class)
public class cucumber_runner {}
@Feature("cucumber_runner.feature")
public class cucumber_runner_Test {}

0 comments on commit 6762f16

Please sign in to comment.