-
-
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
Conversation
@@ -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) { |
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
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Idiomatic java.
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Minimizes the ownership scope of the output stream
} 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 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 reportFileOutputStream = createReportFileOutputStream(url); |
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.
Minimizes the ownership scope of the output stream
Refactored the code around streams so it follows these rules: - Streams should be closed by their owners - A stream is owned by its creator or when provided as a constructor argument - Stream ownership should be the smallest possible unit. The method, class, process, system. - System.out and System.error may never be closed Fixes #1108
9edd86b
to
486b82c
Compare
} catch (IOException ignored) { | ||
// go gentle into that good night | ||
} | ||
} |
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.
3rd duplication but I'd rather wait for java7 try-with-resources
Yes, this looks like a better solution than the one I did in 51f0c9b. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
Refactored the code around streams so it follows these rules:
Fixes #1108
How Has This Been Tested?
Checked the code for all occurrences of
.close()
,System.out
,System.error
,new NiceAppendable
,new .*OutputStream
andnew .*Writer
in non-test code.Automated tests should be passing.
Screenshots (if appropriate):
Types of changes
Checklist: