PrettyPrinter { public static String prettyPrint(PrettyPrintable tree) { StringBuilder sb = new StringBuilder(); prettyPrint(tree.getNode(), sb, "", true, true); return sb.toString(); } PrettyPrinter(); static String prettyPrint(PrettyPrintable tree); static void prettyPrint(PrettyPrintable tree, Appendable appendable);  }
@Test(expected = IllegalStateException.class) public void testPrettyPrint_WrapIOException() { Node root = getHandBuiltTestTree(); Appendable appendable = new Appendable() { @Override public Appendable append(CharSequence csq) throws IOException { throw new IOException("Intentional exception"); } @Override public Appendable append(CharSequence csq, int start, int end) throws IOException { throw new IOException("Intentional exception"); } @Override public Appendable append(char c) throws IOException { throw new IOException("Intentional exception"); } }; PrettyPrinter.prettyPrint(wrapNodeForPrinting(root), appendable); }
@Test public void testPrettyPrint_ToString() throws Exception { Node root = getHandBuiltTestTree(); String expected1 = "○\n" + "└── ○ B (1)\n" + " └── ○ A (2)\n" + " └── ○ N (3)\n" + " ├── ○ AN (5)\n" + " │ └── ○ A (6)\n" + " └── ○ DANA (4)\n"; String actual1 = PrettyPrinter.prettyPrint(wrapNodeForPrinting(root)); Assert.assertEquals(expected1, actual1); String expected2 = "○ B (1)\n" + "└── ○ A (2)\n" + " └── ○ N (3)\n" + " ├── ○ AN (5)\n" + " │ └── ○ A (6)\n" + " └── ○ DANA (4)\n"; String actual2 = PrettyPrinter.prettyPrint(wrapNodeForPrinting(root.getOutgoingEdge('B'))); Assert.assertEquals(expected2, actual2); }
@Test public void testPrettyPrint_ToAppendable() throws Exception { Node root = getHandBuiltTestTree(); String expected1 = "○\n" + "└── ○ B (1)\n" + " └── ○ A (2)\n" + " └── ○ N (3)\n" + " ├── ○ AN (5)\n" + " │ └── ○ A (6)\n" + " └── ○ DANA (4)\n"; StringBuilder appendable = new StringBuilder(); PrettyPrinter.prettyPrint(wrapNodeForPrinting(root), appendable); Assert.assertEquals(expected1, appendable.toString()); }