-
Notifications
You must be signed in to change notification settings - Fork 203
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
#838 close body in RsPrint #879
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,19 +24,24 @@ | |
package org.takes.rs; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.Writer; | ||
import java.nio.charset.StandardCharsets; | ||
import org.cactoos.io.InputStreamOf; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.hamcrest.core.IsEqual; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test case for {@link RsPrint}. | ||
* @author Yegor Bugayenko (yegor256@gmail.com) | ||
* @version $Id$ | ||
* @since 0.8 | ||
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines) | ||
*/ | ||
@SuppressWarnings("PMD.TooManyMethods") | ||
public final class RsPrintTest { | ||
|
||
/** | ||
|
@@ -70,6 +75,30 @@ public void flushBodyEvenWhenExceptionHappens() throws IOException { | |
); | ||
} | ||
|
||
/** | ||
* RsPrint can close body contents even when exception happens. | ||
* @throws IOException If some problem inside | ||
*/ | ||
@Test | ||
public void closeBodyEvenWhenExceptionHappens() throws IOException { | ||
final IOException exception = new IOException("Smth went wrong"); | ||
final FailOutputStream output = new FailOutputStream(exception); | ||
final FakeInput input = new FakeInput(new InputStreamOf("abc")); | ||
try { | ||
new RsPrint(new RsText(input)) | ||
.printBody(output); | ||
} catch (final IOException ex) { | ||
if (!ex.equals(exception)) { | ||
throw ex; | ||
} | ||
} | ||
MatcherAssert.assertThat( | ||
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. @olenagerasimova it's a good practice to add description text to |
||
"Input body was not closed", | ||
input.isClosed(), | ||
new IsEqual<>(true) | ||
); | ||
} | ||
|
||
/** | ||
* RsPrint can flush head contents even when exception happens. | ||
* @throws IOException If some problem inside | ||
|
@@ -190,4 +219,52 @@ public boolean haveFlushed() { | |
return this.flushed; | ||
} | ||
} | ||
|
||
/** | ||
* Fake wrapper for InputStream to make sure body is closed. | ||
* | ||
* @author Alena Gerasimova (olena.gerasimova@gmail.com) | ||
* @version $Id$ | ||
* @since 2.0 | ||
*/ | ||
private static final class FakeInput extends InputStream { | ||
|
||
/** | ||
* Have input been closed? | ||
*/ | ||
private boolean closed; | ||
|
||
/** | ||
* Origin. | ||
*/ | ||
private final InputStream origin; | ||
|
||
/** | ||
* Ctor. | ||
* @param origin Origin input | ||
*/ | ||
FakeInput(final InputStream origin) { | ||
super(); | ||
this.origin = origin; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
return this.origin.read(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
this.origin.close(); | ||
this.closed = true; | ||
} | ||
|
||
/** | ||
* Have input been closed? | ||
* @return True, if input wes closed, false - otherwise | ||
*/ | ||
public boolean isClosed() { | ||
return this.closed; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@olenagerasimova it's better to use assertions in tests instead of exception throwing: