Skip to content

Commit

Permalink
Merge pull request #721 from orionhealth/make_standard_out_non_buffered
Browse files Browse the repository at this point in the history
Make standard out non buffered
  • Loading branch information
dkowis committed Jun 3, 2014
2 parents 4b15dbf + 68faa4b commit ab81ffd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
Expand Down Expand Up @@ -51,9 +51,9 @@ public class FormatterFactory {
put("rerun", RerunFormatter.class);
}};
private static final Pattern FORMATTER_WITH_FILE_PATTERN = Pattern.compile("([^:]+):(.*)");
private Appendable defaultOut = new OutputStreamWriter(System.out) {
private Appendable defaultOut = new PrintStream(System.out) {
@Override
public void close() throws IOException {
public void close() {
// We have no intention to close System.out
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
import cucumber.runtime.Utils;
import cucumber.runtime.io.UTF8OutputStreamWriter;
import gherkin.formatter.Formatter;
import gherkin.formatter.model.Result;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URL;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -73,11 +78,32 @@ public void instantiates_usage_formatter_with_file_arg() throws IOException {
Formatter formatter = fc.create("usage:" + TempDir.createTempFile().getAbsolutePath());
assertEquals(UsageFormatter.class, formatter.getClass());
}

@Test
public void formatter_does_not_buffer_its_output() throws IOException {
PrintStream previousSystemOut = System.out;
OutputStream mockSystemOut = new ByteArrayOutputStream();

try {
System.setOut(new PrintStream(mockSystemOut));

// Need to create a new formatter factory here since we need it to pick up the new value of System.out
fc = new FormatterFactory();

ProgressFormatter formatter = (ProgressFormatter) fc.create("progress");

formatter.result(new Result("passed", null, null));

assertThat(mockSystemOut.toString(), is(not("")));
} finally {
System.setOut(previousSystemOut);
}
}

@Test
public void instantiates_single_custom_appendable_formatter_with_stdout() {
WantsAppendable formatter = (WantsAppendable) fc.create("cucumber.runtime.formatter.FormatterFactoryTest$WantsAppendable");
assertThat(formatter.out, is(instanceOf(OutputStreamWriter.class)));
assertThat(formatter.out, is(instanceOf(PrintStream.class)));
try {
fc.create("cucumber.runtime.formatter.FormatterFactoryTest$WantsAppendable");
fail();
Expand All @@ -89,7 +115,7 @@ public void instantiates_single_custom_appendable_formatter_with_stdout() {
@Test
public void instantiates_custom_appendable_formatter_with_stdout_and_file() throws IOException {
WantsAppendable formatter = (WantsAppendable) fc.create("cucumber.runtime.formatter.FormatterFactoryTest$WantsAppendable");
assertThat(formatter.out, is(instanceOf(OutputStreamWriter.class)));
assertThat(formatter.out, is(instanceOf(PrintStream.class)));

WantsAppendable formatter2 = (WantsAppendable) fc.create("cucumber.runtime.formatter.FormatterFactoryTest$WantsAppendable:" + TempDir.createTempFile().getAbsolutePath());
assertEquals(UTF8OutputStreamWriter.class, formatter2.out.getClass());
Expand Down

0 comments on commit ab81ffd

Please sign in to comment.