PatchExplanation { public void writeJson(PrintWriter writer) { StringBuilder buffer = new StringBuilder(); buffer.append("{\n"); buffer.append(" estimatedNewSize: ").append(getEstimatedNewSize()).append(",\n"); buffer.append(" estimatedChangedSize: ").append(getEstimatedChangedSize()).append(",\n"); buffer .append(" estimatedResourceConstrainedSize: ") .append(getEstimatedResourceConstrainedSize()) .append(",\n"); dumpJson(getExplainedAsNew(), "explainedAsNew", buffer, " "); buffer.append(",\n"); dumpJson(getExplainedAsChanged(), "explainedAsChanged", buffer, " "); buffer.append(",\n"); dumpJson(getExplainedAsUnchangedOrFree(), "explainedAsUnchangedOrFree", buffer, " "); buffer.append(",\n"); dumpJson(getExplainedAsResourceConstrained(), "explainedAsResourceConstrained", buffer, " "); buffer.append("\n"); buffer.append("}"); writer.write(buffer.toString()); writer.flush(); } PatchExplanation(List<EntryExplanation> entryExplanations); List<EntryExplanation> getExplainedAsNew(); List<EntryExplanation> getExplainedAsChanged(); List<EntryExplanation> getExplainedAsUnchangedOrFree(); List<EntryExplanation> getExplainedAsResourceConstrained(); long getEstimatedNewSize(); long getEstimatedChangedSize(); long getEstimatedResourceConstrainedSize(); void writeJson(PrintWriter writer);  }
@Test public void testToJson() throws IOException { PatchExplanation patchExplanation = new PatchExplanation(ALL_EXPLANATIONS); try (ByteArrayOutputStream buffer = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(buffer)) { patchExplanation.writeJson(writer); writer.flush(); String asString = buffer.toString(); Assert.assertTrue(asString.startsWith("{")); Assert.assertFalse(asString.isEmpty()); for (EntryExplanation explanation : ALL_EXPLANATIONS) { Assert.assertTrue(asString.contains(new String(explanation.getPath().getData(), "UTF-8"))); } Assert.assertTrue(asString.endsWith("}")); } }