Skip to content

Commit

Permalink
Properly flush and close formatters. Closes #173
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Jan 25, 2012
1 parent f7ee6f3 commit e50308a
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 37 deletions.
2 changes: 2 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## In Git

* [Core] Properly flush and close formatters ([#173](https://github.com/cucumber/cucumber-jvm/pull/173) Aslak Hellesøy, David Kowis)
* [Core] Use Gherkin's internal Gson (Aslak Hellesøy)
* [JUnit] Better reporting of BEfore and After blocks (Aslak Hellesøy)
* [Core] Bugfix: Scenario Outlines failing ([#170](https://github.com/cucumber/cucumber-jvm/issues/170) David Kowis, Aslak Hellesøy)
* [OpenEJB] It's back (was excluded from previous releases because it depended on unreleased libs). (Aslak Hellesøy)
Expand Down
4 changes: 0 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
<groupId>com.googlecode.java-diff-utils</groupId>
<artifactId>diffutils</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-html</artifactId>
Expand Down
15 changes: 6 additions & 9 deletions core/src/main/java/cucumber/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void run(String[] argv, ClassLoader classLoader, RuntimeFactory ru
String dotCucumber = null;
boolean isDryRun = false;

FormatterFactory formatterFactory = new FormatterFactory();
FormatterFactory formatterFactory = new FormatterFactory(classLoader);
MultiFormatter multiFormatter = new MultiFormatter(classLoader);

while (!args.isEmpty()) {
Expand Down Expand Up @@ -87,8 +87,12 @@ public static void run(String[] argv, ClassLoader classLoader, RuntimeFactory ru
if (dotCucumber != null) {
writeDotCucumber(featurePaths, dotCucumber, runtime);
}
run(featurePaths, filters, multiFormatter, runtime);
Formatter formatter = multiFormatter.formatterProxy();
Reporter reporter = multiFormatter.reporterProxy();
runtime.run(featurePaths, filters, formatter, reporter);
formatter.done();
printSummary(runtime);
formatter.close();
System.exit(runtime.exitStatus());
}

Expand All @@ -98,13 +102,6 @@ private static void writeDotCucumber(List<String> featurePaths, String dotCucumb
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);
}
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/cucumber/formatter/FormatterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cucumber.runtime.CucumberException;
import gherkin.formatter.Formatter;
import gherkin.formatter.JSONFormatter;
import gherkin.formatter.JSONPrettyFormatter;
import gherkin.formatter.PrettyFormatter;

import java.io.File;
Expand All @@ -12,13 +13,20 @@

public class FormatterFactory {

private final ClassLoader classLoader;

private static final Map<String, String> BUILTIN_FORMATTERS = new HashMap<String, String>() {{
put("progress", ProgressFormatter.class.getName());
put("html", HTMLFormatter.class.getName());
put("json", JSONFormatter.class.getName());
put("json-pretty", JSONPrettyFormatter.class.getName());
put("pretty", PrettyFormatter.class.getName());
}};

public FormatterFactory(ClassLoader classLoader) {
this.classLoader = classLoader;
}

public Formatter createFormatter(String formatterName, Object out) {
String className = BUILTIN_FORMATTERS.containsKey(formatterName) ? BUILTIN_FORMATTERS.get(formatterName) : formatterName;
return createFormatterFromClassName(className, out);
Expand Down Expand Up @@ -53,7 +61,7 @@ private Formatter createFormatterFromClassName(String className, Object out) {

private Class<Formatter> getFormatterClass(String className) {
try {
return (Class<Formatter>) Class.forName(className);
return (Class<Formatter>) classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
throw new CucumberException("Formatter class not found: " + className, e);
}
Expand Down
10 changes: 7 additions & 3 deletions core/src/main/java/cucumber/formatter/HTMLFormatter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cucumber.formatter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import cucumber.runtime.CucumberException;
import gherkin.deps.com.google.gson.Gson;
import gherkin.deps.com.google.gson.GsonBuilder;
import gherkin.formatter.Formatter;
import gherkin.formatter.Mappable;
import gherkin.formatter.NiceAppendable;
Expand Down Expand Up @@ -104,10 +104,14 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
@Override
public void done() {
jsOut().append("});");
jsOut().close();
copyReportFiles();
}

@Override
public void close() {
jsOut().close();
}

private void writeToJsReport(String functionName, Mappable statement) {
String json = gson.toJson(statement.toMap());
writeToJsReport(functionName, json);
Expand Down
7 changes: 3 additions & 4 deletions core/src/main/java/cucumber/formatter/MultiFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import java.util.ArrayList;
import java.util.List;

/**
* Utility for creating a formatter that delegates to multiple underlying formatters.
*/
public class MultiFormatter {
private final List<Formatter> formatters = new ArrayList<Formatter>();
private final ClassLoader classLoader;
Expand All @@ -21,10 +24,6 @@ public void add(Formatter formatter) {
formatters.add(formatter);
}

public boolean isEmpty() {
return formatters.isEmpty();
}

public Formatter formatterProxy() {
return (Formatter) Proxy.newProxyInstance(classLoader, new Class<?>[]{Formatter.class}, new InvocationHandler() {
@Override
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/cucumber/formatter/NullReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
public void done() {
}

@Override
public void close() {
}

@Override
public void result(Result result) {
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/cucumber/formatter/ProgressFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public void done() {
out.println();
}

@Override
public void close() {
out.close();
}

@Override
public void result(Result result) {
if (!monochrome) {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/cucumber/runtime/FeatureBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
public void done() {
}

@Override
public void close() {
}

public void parse(Resource resource, List<Object> filters) {
Formatter formatter = this;
if (!filters.isEmpty()) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/cucumber/runtime/RuntimeGlue.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cucumber.runtime;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import cucumber.io.FileResourceLoader;
import cucumber.runtime.autocomplete.MetaStepdef;
import cucumber.runtime.autocomplete.StepdefGenerator;
import cucumber.runtime.converters.LocalizedXStreams;
import cucumber.runtime.model.CucumberFeature;
import gherkin.deps.com.google.gson.Gson;
import gherkin.deps.com.google.gson.GsonBuilder;
import gherkin.formatter.Argument;
import gherkin.formatter.model.Step;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import gherkin.formatter.Formatter;
import gherkin.formatter.JSONFormatter;
import gherkin.formatter.JSONPrettyFormatter;
import gherkin.formatter.PrettyFormatter;
import org.junit.Test;

Expand All @@ -14,13 +15,18 @@

public class FormatterFactoryTest {

private final FormatterFactory formatterFactory = new FormatterFactory();
private final FormatterFactory formatterFactory = new FormatterFactory(Thread.currentThread().getContextClassLoader());

@Test
public void shouldInstantiateJsonFormatter() {
assertThat(formatterFactory.createFormatter("json", System.out), is(JSONFormatter.class));
}

@Test
public void shouldInstantiateJsonPrettyFormatter() {
assertThat(formatterFactory.createFormatter("json-pretty", System.out), is(JSONPrettyFormatter.class));
}

@Test
public void shouldInstantiatePrettyFormatter() {
assertThat(formatterFactory.createFormatter("pretty", System.out), is(PrettyFormatter.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void writes_proper_html() throws IOException {
Runtime runtime = new Runtime(resourceLoader, gluePaths, classLoader, asList(mock(Backend.class)), false);
runtime.run(features.get(0), f, f);
f.done();
f.close();

// Let's verify that the JS we wrote parses nicely

Expand Down
5 changes: 5 additions & 0 deletions core/src/test/java/cucumber/formatter/TestFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
public void done() {
throw new UnsupportedOperationException();
}

@Override
public void close() {
throw new UnsupportedOperationException();
}
}
55 changes: 52 additions & 3 deletions core/src/test/java/cucumber/runtime/RuntimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cucumber.io.ClasspathResourceLoader;
import cucumber.runtime.model.CucumberFeature;
import gherkin.formatter.JSONFormatter;
import gherkin.formatter.JSONPrettyFormatter;
import org.junit.Test;

import java.util.Collections;
Expand All @@ -23,12 +23,61 @@ public void runs_feature_with_json_formatter() throws Exception {
" Scenario: scenario name\n" +
" When s\n");
StringBuilder out = new StringBuilder();
JSONFormatter jsonFormatter = new JSONFormatter(out);
JSONPrettyFormatter jsonFormatter = new JSONPrettyFormatter(out);
List<Backend> backends = asList(mock(Backend.class));
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
new Runtime(new ClasspathResourceLoader(classLoader), Collections.<String>emptyList(), classLoader, backends, true).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\"}]";
String expected = "" +
"[\n" +
" {\n" +
" \"id\": \"feature-name\",\n" +
" \"description\": \"\",\n" +
" \"name\": \"feature name\",\n" +
" \"keyword\": \"Feature\",\n" +
" \"line\": 1,\n" +
" \"elements\": [\n" +
" {\n" +
" \"description\": \"\",\n" +
" \"name\": \"background name\",\n" +
" \"keyword\": \"Background\",\n" +
" \"line\": 2,\n" +
" \"steps\": [\n" +
" {\n" +
" \"result\": {\n" +
" \"status\": \"undefined\"\n" +
" },\n" +
" \"name\": \"b\",\n" +
" \"keyword\": \"Given \",\n" +
" \"line\": 3,\n" +
" \"match\": {}\n" +
" }\n" +
" ],\n" +
" \"type\": \"background\"\n" +
" },\n" +
" {\n" +
" \"id\": \"feature-name;scenario-name\",\n" +
" \"description\": \"\",\n" +
" \"name\": \"scenario name\",\n" +
" \"keyword\": \"Scenario\",\n" +
" \"line\": 4,\n" +
" \"steps\": [\n" +
" {\n" +
" \"result\": {\n" +
" \"status\": \"undefined\"\n" +
" },\n" +
" \"name\": \"s\",\n" +
" \"keyword\": \"When \",\n" +
" \"line\": 5,\n" +
" \"match\": {}\n" +
" }\n" +
" ],\n" +
" \"type\": \"scenario\"\n" +
" }\n" +
" ],\n" +
" \"uri\": \"test.feature\"\n" +
" }\n" +
"]";
assertEquals(expected, out.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package cucumber.runtime.autocomplete;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import cucumber.io.Resource;
import cucumber.runtime.FeatureBuilder;
import cucumber.runtime.JdkPatternArgumentMatcher;
import cucumber.runtime.ParameterType;
import cucumber.runtime.StepDefinition;
import cucumber.runtime.model.CucumberFeature;
import gherkin.deps.com.google.gson.Gson;
import gherkin.deps.com.google.gson.GsonBuilder;
import gherkin.formatter.Argument;
import gherkin.formatter.model.Step;
import org.junit.Test;
Expand Down
2 changes: 0 additions & 2 deletions java/src/test/java/cucumber/runtime/java/JavaBackendTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import cucumber.runtime.StepDefinitionMatch;
import cucumber.runtime.java.test.Stepdefs;
import gherkin.formatter.model.Step;
import gherkin.formatter.model.DataTableRow;
import gherkin.formatter.model.DocString;
import org.junit.Test;

import java.io.File;
Expand Down
1 change: 1 addition & 0 deletions junit/src/main/java/cucumber/junit/Cucumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public void run(RunNotifier notifier) {
super.run(notifier);
jUnitReporter.done();
new SummaryPrinter(System.out).print(runtime);
jUnitReporter.close();
}

private void assertNoDeclaredMethods(Class clazz) {
Expand Down
5 changes: 5 additions & 0 deletions junit/src/main/java/cucumber/junit/JUnitReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,9 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
public void done() {
formatter.done();
}

@Override
public void close() {
formatter.close();
}
}
7 changes: 1 addition & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<outputDirectory>${project.build.directory}</outputDirectory>
<gherkin.version>2.7.4</gherkin.version>
<gherkin.version>2.7.6</gherkin.version>
<groovy.version>1.8.5</groovy.version>
</properties>
<licenses>
Expand Down Expand Up @@ -168,11 +168,6 @@
<artifactId>mockito-all</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
Expand Down

0 comments on commit e50308a

Please sign in to comment.