### Question:
JsonFormatUtil { public static String formatJson(String jsonStr) { if (null == jsonStr || "".equals(jsonStr)) { return ""; } StringBuilder sb = new StringBuilder(); char last = '\0'; char current = '\0'; int indent = 0; for (int i = 0; i < jsonStr.length(); i++) { last = current; current = jsonStr.charAt(i); switch (current) { case '{': case '[': sb.append(current); sb.append('\n'); indent++; addIndentBlank(sb, indent); break; case '}': case ']': sb.append('\n'); indent--; addIndentBlank(sb, indent); sb.append(current); break; case ',': sb.append(current); if (last != '\\') { sb.append('\n'); addIndentBlank(sb, indent); } break; default: sb.append(current); } } return sb.toString(); } private  JsonFormatUtil(); static String printJson(String jsonStr); static String formatJson(String jsonStr);  }### Answer:
@Test public void testFormatJsonDefault() { Assert.assertEquals("fooBar", JsonFormatUtil.formatJson("fooBar")); }
@Test public void testFormatJsonNull() { Assert.assertEquals("", JsonFormatUtil.formatJson(null)); Assert.assertEquals("", JsonFormatUtil.formatJson("")); }
@Test public void testFormatJsonComma() { Assert.assertEquals("\\,", JsonFormatUtil.formatJson("\\,")); Assert.assertEquals(",\n\\", JsonFormatUtil.formatJson(",\\")); }
@Test public void testFormatJsonOpenBracket() { Assert.assertEquals("foo{\n ", JsonFormatUtil.formatJson("foo{")); Assert.assertEquals("foo[\n ", JsonFormatUtil.formatJson("foo[")); }
@Test public void testFormatJsonCloseBracket() { Assert.assertEquals("foo\n}", JsonFormatUtil.formatJson("foo}")); Assert.assertEquals("foo\n]", JsonFormatUtil.formatJson("foo]")); }