Skip to content
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

Make standard out non buffered #721

Merged
merged 2 commits into from
Jun 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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