Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core, TestNg] Clean up stream closing #1175

Merged
merged 1 commit into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public NiceAppendable println(CharSequence csq) {
public void close() {
try {
tryFlush();
if (out instanceof Closeable && out != System.out && out != System.err) {
if (out instanceof Closeable) {
Copy link
Contributor Author

@mpkorstanje mpkorstanje Jul 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the responsibility of TestNGCucumberRunner. It knows it is passing ownership of System.out

((Closeable) out).close();
}
} catch (IOException e) {
Expand Down
66 changes: 43 additions & 23 deletions core/src/main/java/cucumber/runtime/formatter/HTMLFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import gherkin.pickles.PickleTable;
import gherkin.pickles.PickleTag;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -119,11 +120,11 @@ public void receive(TestRunFinished event) {
};

public HTMLFormatter(URL htmlReportDir) {
this.htmlReportDir = htmlReportDir;
this(htmlReportDir, createJsOut(htmlReportDir));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idiomatic java.

}

public HTMLFormatter(URL htmlReportDir, NiceAppendable jsOut) {
this(htmlReportDir);
HTMLFormatter(URL htmlReportDir, NiceAppendable jsOut) {
this.htmlReportDir = htmlReportDir;
this.jsOut = jsOut;
}

Expand All @@ -144,7 +145,7 @@ private void handleTestSourceRead(TestSourceRead event) {

private void handleTestCaseStarted(TestCaseStarted event) {
if (firstFeature) {
jsOut().append("$(document).ready(function() {").append("var ")
jsOut.append("$(document).ready(function() {").append("var ")
.append(JS_FORMATTER_VAR).append(" = new CucumberHTML.DOMFormatter($('.cucumber-report'));");
firstFeature = false;
}
Expand Down Expand Up @@ -188,7 +189,7 @@ private void handleEmbed(EmbedEvent event) {
String extension = MIME_TYPES_EXTENSIONS.get(mimeType);
if (extension != null) {
StringBuilder fileName = new StringBuilder("embedded").append(embeddedIndex++).append(".").append(extension);
writeBytesAndClose(event.data, reportFileOutputStream(fileName.toString()));
writeBytesToURL(event.data, toUrl(fileName.toString()));
jsFunctionCall("embedding", mimeType, fileName);
}
}
Expand All @@ -200,10 +201,10 @@ private void handleWrite(WriteEvent event) {

private void finishReport() {
if (!firstFeature) {
jsOut().append("});");
jsOut.append("});");
copyReportFiles();
}
jsOut().close();
jsOut.close();
}

private void handleStartOfFeature(TestCase testCase) {
Expand Down Expand Up @@ -441,7 +442,7 @@ private Map<String, Object> createResultMap(Result result) {
}

private void jsFunctionCall(String functionName, Object... args) {
NiceAppendable out = jsOut().append(JS_FORMATTER_VAR + ".").append(functionName).append("(");
NiceAppendable out = jsOut.append(JS_FORMATTER_VAR + ".").append(functionName).append("(");
boolean comma = false;
for (Object arg : args) {
if (comma) {
Expand All @@ -463,50 +464,69 @@ private void copyReportFiles() {
if (textAssetStream == null) {
throw new CucumberException("Couldn't find " + textAsset + ". Is cucumber-html on your classpath? Make sure you have the right version.");
}
String baseName = new File(textAsset).getName();
writeStreamAndClose(textAssetStream, reportFileOutputStream(baseName));
String fileName = new File(textAsset).getName();
writeStreamToURL(textAssetStream, toUrl(fileName));
}
}

private void writeStreamAndClose(InputStream in, OutputStream out) {
private URL toUrl(String fileName) {
try {
return new URL(htmlReportDir, fileName);
} catch (IOException e) {
throw new CucumberException(e);
}
}

private static void writeStreamToURL(InputStream in, URL url) {
OutputStream out = createReportFileOutputStream(url);

Copy link
Contributor Author

@mpkorstanje mpkorstanje Jul 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimizes the ownership scope of the output stream

byte[] buffer = new byte[16 * 1024];
try {
int len = in.read(buffer);
while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);
}
out.close();
} catch (IOException e) {
throw new CucumberException("Unable to write to report file item: ", e);
} finally {
closeQuietly(out);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idiomatic java. For lack of being able to use try-with-resources

}
}

private void writeBytesAndClose(byte[] buf, OutputStream out) {
private static void writeBytesToURL(byte[] buf, URL url) throws CucumberException {
OutputStream out = createReportFileOutputStream(url);
try {
out.write(buf);
} catch (IOException e) {
throw new CucumberException("Unable to write to report file item: ", e);
} finally {
closeQuietly(out);
}
}

private NiceAppendable jsOut() {
if (jsOut == null) {
try {
jsOut = new NiceAppendable(new OutputStreamWriter(reportFileOutputStream(JS_REPORT_FILENAME), "UTF-8"));
} catch (IOException e) {
throw new CucumberException(e);
}
private static NiceAppendable createJsOut(URL htmlReportDir) {
try {
return new NiceAppendable(new OutputStreamWriter(createReportFileOutputStream(new URL(htmlReportDir, JS_REPORT_FILENAME)), "UTF-8"));
} catch (IOException e) {
throw new CucumberException(e);
}
return jsOut;
}

private OutputStream reportFileOutputStream(String fileName) {
private static OutputStream createReportFileOutputStream(URL url) {
try {
return new URLOutputStream(new URL(htmlReportDir, fileName));
return new URLOutputStream(url);
} catch (IOException e) {
throw new CucumberException(e);
}
}

private static void closeQuietly(Closeable out) {
try {
out.close();
} catch (IOException ignored) {
// go gentle into that good night
}
}

}
11 changes: 11 additions & 0 deletions core/src/main/java/cucumber/runtime/formatter/JUnitFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
Expand Down Expand Up @@ -151,6 +154,7 @@ private void finishReport() {
StreamResult result = new StreamResult(out);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
closeQuietly(out);
} catch (TransformerException e) {
throw new CucumberException("Error while transforming.", e);
}
Expand Down Expand Up @@ -332,4 +336,11 @@ private Element createElement(Document doc, StringBuilder sb, String elementType

}

private static void closeQuietly(Closeable out) {
try {
out.close();
} catch (IOException ignored) {
// go gentle into that good night
}
}
}
11 changes: 11 additions & 0 deletions core/src/main/java/cucumber/runtime/formatter/TestNGFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.Closeable;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand Down Expand Up @@ -149,6 +151,7 @@ private void finishReport() {
StreamResult streamResult = new StreamResult(writer);
DOMSource domSource = new DOMSource(document);
transformer.transform(domSource, streamResult);
closeQuietly(writer);
} catch (TransformerException e) {
throw new CucumberException("Error transforming report.", e);
}
Expand Down Expand Up @@ -301,4 +304,12 @@ private Element createException(Document doc, String clazz, String message, Stri
return exceptionElement;
}
}

private static void closeQuietly(Closeable out) {
try {
out.close();
} catch (IOException ignored) {
// go gentle into that good night
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3rd duplication but I'd rather wait for java7 try-with-resources

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import cucumber.runtime.io.ResourceLoaderClassFinder;
import cucumber.runtime.model.CucumberFeature;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -36,7 +37,12 @@ public TestNGCucumberRunner(Class clazz) {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(clazz);
runtimeOptions = runtimeOptionsFactory.create();

reporter = new TestNgReporter(System.out);
reporter = new TestNgReporter(new PrintStream(System.out) {
@Override
public void close() {
// We have no intention to close System.out
}
});
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
resultListener = new FeatureResultListener(runtimeOptions.isStrict());
runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
Expand Down