/** Creates a `StringPrinter` directed at {@link System#err}. */ public static StringPrinter systemErr() { return new StringPrinter(System.err::print); } }
/** Returns a StringPrinter for {@link System#err}. */ public static StringPrinter systemErr() { return new StringPrinter(System.err::print); } }
/** Returns a StringPrinter for {@link System#err}. */ public static StringPrinter systemErr() { return new StringPrinter(System.err::print); } }
/** Returns a StringPrinter for {@link System#out}. */ public static StringPrinter systemOut() { return new StringPrinter(System.out::print); }
/** Creates a `StringPrinter` directed at {@link System#out}. */ public static StringPrinter systemOut() { return new StringPrinter(System.out::print); }
/** Returns a StringPrinter for {@link System#out}. */ public static StringPrinter systemOut() { return new StringPrinter(System.out::print); }
/** Easy way to create a String using a StringPrinter. */ public static String buildString(Consumer<StringPrinter> printer) { StringBuilder builder = new StringBuilder(); printer.accept(new StringPrinter(builder::append)); return builder.toString(); }
/** Easy way to create a String using a StringPrinter. */ public static String buildString(Consumer<StringPrinter> printer) { StringBuilder builder = new StringBuilder(); printer.accept(new StringPrinter(builder::append)); return builder.toString(); }
/** * Easy way to create a String using a StringPrinter. * * ```java * String currentStackTrace = StringPrinter.buildString(printer -> { * new Throwable().printStackTrace(printer.toPrintStream()); * }); * ``` */ public static String buildString(Consumer<StringPrinter> printer) { StringBuilder builder = new StringBuilder(); printer.accept(new StringPrinter(builder::append)); return builder.toString(); }
/** * Returns a PrintStream which will redirect all of its output to the source PrintStream. If * the trigger string is passed through the wrapped PrintStream, then it will dump the * stack trace of the call that printed the trigger. * * @param source * the returned PrintStream will delegate to this stream * @param trigger * the string which triggers a stack dump * @return a PrintStream with the above properties */ public static PrintStream wrapAndDumpWhenContains(PrintStream source, String trigger) { StringPrinter wrapped = new StringPrinter(StringPrinter.stringsToLines(perLine -> { source.println(perLine); if (perLine.contains(trigger)) { dump("Triggered by " + trigger); } })); return wrapped.toPrintStream(); }
/** * Returns a PrintStream which will redirect all of its output to the source PrintStream. If * the trigger string is passed through the wrapped PrintStream, then it will dump the * stack trace of the call that printed the trigger. * * @param source * the returned PrintStream will delegate to this stream * @param trigger * the string which triggers a stack dump * @return a PrintStream with the above properties */ public static PrintStream wrapAndDumpWhenContains(PrintStream source, String trigger) { StringPrinter wrapped = new StringPrinter(StringPrinter.stringsToLines(perLine -> { source.println(perLine); if (perLine.contains(trigger)) { dump("Triggered by " + trigger); } })); return wrapped.toPrintStream(); }
@Before public void setupStreams() { testCaseOut.setLength(0); testCaseErr.setLength(0); cleanOut = System.out; cleanErr = System.err; System.setOut(new StringPrinter(str -> { testCaseOut.append(str.replace("\r", "")); }).toPrintStream()); System.setErr(new StringPrinter(str -> { testCaseErr.append(str.replace("\r", "")); }).toPrintStream()); StackDumper.pristineSysErr = new StringPrinter(System.err::print); }
@Test public void testBasicFunctionality() { StringBuilder builder = new StringBuilder(); StringPrinter printer = new StringPrinter(builder::append); printTestStr(printer); Assert.assertEquals(TEST_STR, builder.toString()); }
@Test public void ensureOverrideWorks() throws IOException { write("build.gradle", "plugins {", " id 'com.diffplug.gradle.oomph.ide'", "}", "ext.goomph_override_p2bootstrapUrl='somewhere'", "com.diffplug.gradle.GoomphCacheLocations.initFromProject(project)", "System.out.println(com.diffplug.gradle.GoomphCacheLocations.p2bootstrapUrl())", // if we leave it, it will muck with future testkit tests "project.afterEvaluate { com.diffplug.gradle.GoomphCacheLocations.override_p2bootstrapUrl=null }"); StringBuilder buffer = new StringBuilder(); StringPrinter printer = new StringPrinter(buffer::append); try (Writer writer = printer.toPrintWriter()) { gradleRunner().forwardStdOutput(writer).build(); } String firstLine = buffer.toString().split(System.getProperty("line.separator"))[0]; Assert.assertEquals("Optional[somewhere]", firstLine); } }