### Question:
XmlUtil { public static String prettyFormatXml(CharSequence xml) { String xmlString = xml.toString(); StreamSource source = new StreamSource(new StringReader(xmlString)); StringWriter stringWriter = new StringWriter(); StreamResult result = new StreamResult(stringWriter); try { Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http: transformer.transform(source, result); } catch (TransformerException | IllegalArgumentException e) { LOGGER.log(Level.SEVERE, "Transformer error", e); return xmlString; } return stringWriter.toString(); }  static String prettyFormatXml(CharSequence xml);  }### Answer:
@Test public void prettyFormatXmlTest() { final String uglyXml = "<foo attr1='value1' attr2='value2'><inner-element attr1='value1'>Test</inner-element></foo>"; final String prettyXml = XmlUtil.prettyFormatXml(uglyXml); assertEquals("<foo attr1=\"value1\" attr2=\"value2\">\n <inner-element attr1=\"value1\">Test</inner-element>\n</foo>\n", prettyXml); }
@Test public void prettyFormatIncompleteXmlTest() { final String uglyXml = "<foo attr1='value1' attr2='value2'><inner-element attr1='value1'>Test</inner-element>"; final String prettyXml = XmlUtil.prettyFormatXml(uglyXml); assertEquals("<foo attr1='value1' attr2='value2'><inner-element attr1='value1'>Test</inner-element>", prettyXml); }