Skip to content

Commit

Permalink
#778 Add test case and todo
Browse files Browse the repository at this point in the history
  • Loading branch information
Tolegen Izbassar committed Feb 24, 2018
1 parent c0fc037 commit b8d6955
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/main/java/org/takes/rs/RsPrint.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,17 @@ public void printBody(final OutputStream output) throws IOException {
final InputStream body = this.body();
//@checkstyle MagicNumberCheck (1 line)
final byte[] buf = new byte[4096];
while (true) {
final int bytes = body.read(buf);
if (bytes < 0) {
break;
try {
while (true) {
final int bytes = body.read(buf);
if (bytes < 0) {
break;
}
output.write(buf, 0, bytes);
}
output.write(buf, 0, bytes);
} finally {
output.flush();
}
output.flush();
}

}
99 changes: 99 additions & 0 deletions src/test/java/org/takes/rs/RsPrintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
package org.takes.rs;

import java.io.IOException;
import java.io.OutputStream;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;

/**
Expand All @@ -43,4 +47,99 @@ public void failsOnInvalidHeader() throws IOException {
new RsPrint(new RsWithHeader("name", "\n\n\n")).print();
}

/**
* RsPrint can flush body contents even when exception happens.
* @throws IOException If some problem inside
*/
@Test
public void flushBodyEvenWhenExceptionHappens() throws IOException {
final IOException exception = new IOException("Failure");
final FailOutputStream output = new FailOutputStream(exception);
try {
new RsPrint(new RsText("Hello"))
.printBody(output);
} catch (final IOException ex) {
if (!ex.equals(exception)) {
throw ex;
}
}
MatcherAssert.assertThat(
output.haveFlushed(),
Matchers.is(true)
);
}

/**
* RsPrint can flush head contents even when exception happens.
* @throws IOException If some problem inside
* @todo #778:30min Fix flushHeadEvenWhenExceptionHappens test
* and remove Ignore annotation. Implementation could not rely
* on simple try-finally block as OutputStreamWriter do not call
* to the flush() of OutputStream. It has additional checks.
*/
@Test
@Ignore
public void flushHeadEvenWhenExceptionHappens() throws IOException {
final IOException exception = new IOException("Error");
final FailOutputStream output = new FailOutputStream(exception);
try {
new RsPrint(new RsText("World!"))
.printHead(output);
} catch (final IOException ex) {
if (!ex.equals(exception)) {
throw ex;
}
}
MatcherAssert.assertThat(
output.haveFlushed(),
Matchers.is(true)
);
}

/**
* FailOutputStream that throws IOException on method write.
*
* @author Izbassar Tolegen (t.izbassar@gmail.com)
* @version $Id$
* @since 2.0
*/
private static final class FailOutputStream extends OutputStream {

/**
* Have contents been flushed?
*/
private boolean flushed;

/**
* Exception, that needs to be thrown.
*/
private final IOException exception;

/**
* Ctor.
* @param exception Exception to throw
*/
FailOutputStream(final IOException exception) {
super();
this.exception = exception;
}

@Override
public void write(final int value) throws IOException {
throw this.exception;
}

@Override
public void flush() throws IOException {
this.flushed = true;
}

/**
* Have contents been flushed?
* @return True, if contents were flushed, false - otherwise
*/
public boolean haveFlushed() {
return this.flushed;
}
}
}

0 comments on commit b8d6955

Please sign in to comment.