Fix NoClassDefFoundError with OutputCapture

This commit is contained in:
Phillip Webb 2013-08-24 19:22:20 -07:00
parent 7ecb99757b
commit 1434c83694

View File

@ -57,7 +57,7 @@ public class OutputCapture implements TestRule {
}
protected void captureOutput() {
AnsiOutput.setEnabled(Enabled.NEVER);
AnsiOutputControl.get().disableAnsiOutput();
this.copy = new ByteArrayOutputStream();
this.captureOut = new CaptureOutputStream(System.out, this.copy);
this.captureErr = new CaptureOutputStream(System.err, this.copy);
@ -66,7 +66,7 @@ public class OutputCapture implements TestRule {
}
protected void releaseOutput() {
AnsiOutput.setEnabled(Enabled.DETECT);
AnsiOutputControl.get().enabledAnsiOutput();
System.setOut(this.captureOut.getOriginal());
System.setErr(this.captureErr.getOriginal());
this.copy = null;
@ -121,4 +121,40 @@ public class OutputCapture implements TestRule {
}
}
/**
* Allow AnsiOutput to not be on the test classpath.
*/
private static class AnsiOutputControl {
public void disableAnsiOutput() {
}
public void enabledAnsiOutput() {
}
public static AnsiOutputControl get() {
try {
Class.forName("org.springframework.boot.ansi.AnsiOutput");
return new AnsiPresentOutputControl();
}
catch (ClassNotFoundException e) {
return new AnsiOutputControl();
}
}
}
private static class AnsiPresentOutputControl extends AnsiOutputControl {
@Override
public void disableAnsiOutput() {
AnsiOutput.setEnabled(Enabled.NEVER);
}
@Override
public void enabledAnsiOutput() {
AnsiOutput.setEnabled(Enabled.DETECT);
}
}
}