From 1434c83694274c933ee7063b065598287bfc73cf Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 24 Aug 2013 19:22:20 -0700 Subject: [PATCH] Fix NoClassDefFoundError with OutputCapture --- .../springframework/boot/OutputCapture.java | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/spring-boot/src/test/java/org/springframework/boot/OutputCapture.java b/spring-boot/src/test/java/org/springframework/boot/OutputCapture.java index e2d9fe2168..3eb01cd2bc 100644 --- a/spring-boot/src/test/java/org/springframework/boot/OutputCapture.java +++ b/spring-boot/src/test/java/org/springframework/boot/OutputCapture.java @@ -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); + } + + } + }