-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -119,11 +120,11 @@ public void receive(TestRunFinished event) { | |
}; | ||
|
||
public HTMLFormatter(URL htmlReportDir) { | ||
this.htmlReportDir = htmlReportDir; | ||
this(htmlReportDir, createJsOut(htmlReportDir)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
@@ -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); | ||
} | ||
} | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
@@ -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 | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3rd duplication but I'd rather wait for java7 try-with-resources |
||
} |
There was a problem hiding this comment.
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