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

Sometimes I need to test some underlying code which closes the standa… #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.flattened-pom.xml
target
/.idea/
/.mvn/
.idea/
system-rules.iml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ public SystemOutRule mute() {
return this;
}

public SystemOutRule untrack() {
logPrintStream.unTrackOriginalStream();
return this;
}

/**
* Suppress the output to {@code System.out} for successful tests only.
* The output is still written to {@code System.out} for failing tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

public class LogPrintStream {
private final PrintStreamHandler printStreamHandler;
private final MuteableLogStream muteableLogStream;
private final MutableLogStream mutableLogStream;

public LogPrintStream(PrintStreamHandler printStreamHandler) {
this.printStreamHandler = printStreamHandler;
this.muteableLogStream = new MuteableLogStream(printStreamHandler.getStream());
this.mutableLogStream = new MutableLogStream(printStreamHandler.getStream());
}

public Statement createStatement(final Statement base) {
Expand All @@ -26,36 +26,36 @@ public void evaluate() throws Throwable {
printStreamHandler.createRestoreStatement(new Statement() {
@Override
public void evaluate() throws Throwable {
printStreamHandler.replaceCurrentStreamWithOutputStream(muteableLogStream);
printStreamHandler.replaceCurrentStreamWithOutputStream(mutableLogStream);
base.evaluate();
}
}).evaluate();
} catch (Throwable e) {
muteableLogStream.failureLog.writeTo(printStreamHandler.getStream());
mutableLogStream.failureLog.writeTo(printStreamHandler.getStream());
throw e;
}
}
};
}

public void clearLog() {
muteableLogStream.log.reset();
mutableLogStream.log.reset();
}

public void enableLog() {
muteableLogStream.logMuted = false;
mutableLogStream.logMuted = false;
}

public String getLog() {
/* The MuteableLogStream is created with the default encoding
/* The MutableLogStream is created with the default encoding
* because it writes to System.out or System.err if not muted and
* System.out/System.err uses the default encoding. As a result all
* other streams receive input that is encoded with the default
* encoding.
*/
String encoding = getProperty("file.encoding");
try {
return muteableLogStream.log.toString(encoding);
return mutableLogStream.log.toString(encoding);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Expand All @@ -67,23 +67,27 @@ public String getLogWithNormalizedLineSeparator() {
}

public void mute() {
muteableLogStream.originalStreamMuted = true;
mutableLogStream.originalStreamMuted = true;
}
public void unTrackOriginalStream(){
mutableLogStream.trackOriginalStream = false;
}

public void muteForSuccessfulTests() {
mute();
muteableLogStream.failureLogMuted = false;
mutableLogStream.failureLogMuted = false;
}

private static class MuteableLogStream extends OutputStream {
private static class MutableLogStream extends OutputStream {
final OutputStream originalStream;
final ByteArrayOutputStream failureLog = new ByteArrayOutputStream();
final ByteArrayOutputStream log = new ByteArrayOutputStream();
boolean originalStreamMuted = false;
boolean failureLogMuted = true;
boolean logMuted = true;
boolean trackOriginalStream = true;

MuteableLogStream(OutputStream originalStream) {
MutableLogStream(OutputStream originalStream) {
this.originalStream = originalStream;
}

Expand All @@ -99,13 +103,17 @@ public void write(int b) throws IOException {

@Override
public void flush() throws IOException {
originalStream.flush();
//ByteArrayOutputStreams don't have to be closed
if (trackOriginalStream) {
originalStream.flush();
}
//ByteArrayOutputStreams don't have to be flushed
}

@Override
public void close() throws IOException {
originalStream.close();
if (trackOriginalStream) {
originalStream.close();
}
//ByteArrayOutputStreams don't have to be closed
}
}
Expand Down