Skip to content

Commit

Permalink
Don't close streams we don't create, and only close them once. Closes c…
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Dec 11, 2011
1 parent d51b441 commit d9cae30
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 50 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/cucumber/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static void run(List<String> filesOrDirs, List<Object> filters, String f
FormatterFactory formatterFactory = new FormatterFactory();
Formatter formatter = formatterFactory.createFormatter(format, System.out);
runtime.run(filesOrDirs, filters, formatter, formatterFactory.reporter(formatter));
formatter.close();
formatter.done();
}

private static void printSummary(Runtime runtime) {
Expand Down
27 changes: 10 additions & 17 deletions core/src/main/java/cucumber/formatter/HTMLFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
import java.util.Map;

public class HTMLFormatter implements Formatter, Reporter {

private final NiceAppendable out;
private final String htmlReportDir = getHtmlReportDir();
private BufferedWriter bufferedWriter;
private boolean firstFeature = true;
private int embeddedIndex;
private static final String JS_FORMATTER_VAR = "formatter";
private static final String JS_REPORT_FILENAME = "report.js";
private static final String[] REPORT_ITEMS = new String[]{"formatter.js", "index.html", "jquery-1.6.4.min.js", "style.css"};
Expand All @@ -33,9 +27,15 @@ public class HTMLFormatter implements Formatter, Reporter {
}
};

private final NiceAppendable out;
private final String htmlReportDir = getHtmlReportDir();

private boolean firstFeature = true;
private int embeddedIndex;

public HTMLFormatter() {
try {
bufferedWriter = new BufferedWriter(new FileWriter(htmlReportDir + JS_REPORT_FILENAME));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(htmlReportDir + JS_REPORT_FILENAME));
out = new NiceAppendable(bufferedWriter);
} catch (IOException e) {
throw new CucumberException("Unable to create javascript report file: " + htmlReportDir
Expand Down Expand Up @@ -85,24 +85,17 @@ public void step(Step step) {

@Override
public void eof() {
try {
//TODO should do this stuff only after the last feature.
out.append("});");
bufferedWriter.close();
copyReportFiles();
} catch (IOException e) {

}
}

@Override
public void syntaxError(String state, String event, List<String> legalEvents, String uri, int line) {
}

@Override
public void close() {
// TODO - Close the HTML
public void done() {
out.append("});");
out.close();
copyReportFiles();
}

private void writeToJsReport(String functionName, Mappable statement) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/cucumber/formatter/NullReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
}

@Override
public void close() {
public void done() {
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/cucumber/formatter/ProgressFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
}

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

@Override
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/cucumber/runtime/FeatureBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
}

@Override
public void close() {
public void done() {
}

public void parse(Resource resource, List<Object> filters) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/cucumber/runtime/RuntimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void runs_feature_with_json_formatter() throws Exception {
StringBuilder out = new StringBuilder();
JSONFormatter jsonFormatter = new JSONFormatter(out);
new Runtime().run(feature, jsonFormatter, jsonFormatter);
jsonFormatter.close();
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());
}
Expand Down
8 changes: 4 additions & 4 deletions cucumber-jvm.ipr
Original file line number Diff line number Diff line change
Expand Up @@ -594,15 +594,15 @@
<root url="jar://$MAVEN_REPOSITORY$/info/cukes/cucumber-picocontainer/1.0.0.RC1-SNAPSHOT/cucumber-picocontainer-1.0.0.RC1-SNAPSHOT-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: info.cukes:gherkin:2.6.9">
<library name="Maven: info.cukes:gherkin:2.7.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/info/cukes/gherkin/2.6.9/gherkin-2.6.9.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/info/cukes/gherkin/2.7.1/gherkin-2.7.1.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/info/cukes/gherkin/2.6.9/gherkin-2.6.9-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/info/cukes/gherkin/2.7.1/gherkin-2.7.1-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/info/cukes/gherkin/2.6.9/gherkin-2.6.9-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/info/cukes/gherkin/2.7.1/gherkin-2.7.1-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: javassist:javassist:3.12.0.GA">
Expand Down
2 changes: 1 addition & 1 deletion examples/java-calculator/java-calculator.iml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<orderEntry type="module" module-name="cucumber-picocontainer" scope="TEST" />
<orderEntry type="module" module-name="cucumber-java" scope="TEST" />
<orderEntry type="module" module-name="cucumber-core" scope="TEST" />
<orderEntry type="library" scope="TEST" name="Maven: info.cukes:gherkin:2.6.9" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: info.cukes:gherkin:2.7.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.googlecode.json-simple:json-simple:1.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.iharder:base64:2.3.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.thoughtworks.xstream:xstream:1.4.2" level="project" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<orderEntry type="module" module-name="cucumber-picocontainer" scope="TEST" />
<orderEntry type="module" module-name="cucumber-java" scope="TEST" />
<orderEntry type="module" module-name="cucumber-core" scope="TEST" />
<orderEntry type="library" scope="TEST" name="Maven: info.cukes:gherkin:2.6.9" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: info.cukes:gherkin:2.7.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.googlecode.json-simple:json-simple:1.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.iharder:base64:2.3.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.thoughtworks.xstream:xstream:1.4.2" level="project" />
Expand Down
4 changes: 2 additions & 2 deletions junit/src/main/java/cucumber/junit/JUnitReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
}

@Override
public void close() {
formatter.close();
public void done() {
formatter.done();
}

public Formatter getFormatter() {
Expand Down
40 changes: 22 additions & 18 deletions junit/src/main/java/cucumber/junit/JUnitReporterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class JUnitReporterFactory {
* @param reporterString what underlying reporter/formatter to create
* @return a reporter
*/

static JUnitReporter create(String reporterString) {
Formatter formatter = null;
FormatterFactory formatterFactory = new FormatterFactory();
Expand All @@ -23,23 +22,7 @@ static JUnitReporter create(String reporterString) {
String name = nameAndOut[0];
Appendable appendable;
try {
if (nameAndOut.length < 2) {
appendable = System.out;
} else {
final FileWriter fw = new FileWriter(nameAndOut[1]);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
fw.flush();
fw.close();
} catch (IOException ignore) {
}
}
});

appendable = fw;
}
appendable = createAutoClosingOutput(nameAndOut);
} catch (IOException e) {
System.err.println("ERROR: Failed to create file " + nameAndOut[0] + ". Using STDOUT instead.");
appendable = System.out;
Expand All @@ -51,4 +34,25 @@ public void run() {
}
return new JUnitReporter(formatterFactory.reporter(formatter), formatter);
}

private static Appendable createAutoClosingOutput(String[] nameAndOut) throws IOException {
Appendable appendable;
if (nameAndOut.length < 2) {
appendable = System.out;
} else {
final FileWriter fw = new FileWriter(nameAndOut[1]);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
fw.flush();
fw.close();
} catch (IOException ignore) {
}
}
});
appendable = fw;
}
return appendable;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.6.9</version>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.java-diff-utils</groupId>
Expand Down

0 comments on commit d9cae30

Please sign in to comment.