XMLUtils { @Deprecated public static String escape(Object content) { return escape(Objects.toString(content, null)); } private  XMLUtils(); static String extractXML(Node node, int start, int length); static String escapeXMLComment(String content); static String unescapeXMLComment(String content); @Deprecated static String escape(Object content); @Unstable static String escape(String content); @Deprecated static String escapeAttributeValue(Object content); @Unstable static String escapeAttributeValue(String content); @Unstable static String escapeElementText(String content); @Deprecated static String escapeElementContent(Object content); @Deprecated static String unescape(Object content); static Document createDOMDocument(); static Document parse(LSInput source); static String serialize(Node node); static String serialize(Node node, boolean withXmlDeclaration); static String transform(Source xml, Source xslt); static String formatXMLContent(String content);  }
@Test void escape() { String escapedText = XMLUtils.escape("a < a' && a' < a\" => a < a\""); assertFalse(escapedText.contains("<"), "Failed to escape <"); assertFalse(escapedText.contains(">"), "Failed to escape >"); assertFalse(escapedText.contains("'"), "Failed to escape '"); assertFalse(escapedText.contains("\""), "Failed to escape \""); assertFalse(escapedText.contains("&&"), "Failed to escape &"); }
@Test void escapeObjectContent() { String content = "a < a' && a' < a\" => a < a\""; StringBuilder sb = new StringBuilder(); sb.append(content); String expected = XMLUtils.escape(content); String actual = XMLUtils.escape(sb); assertEquals(expected, actual); }
@Test void escapeApos() { assertNotEquals("&apos;", XMLUtils.escape("'"), "' wrongly escaped to non-HTML &apos;"); }
@Test void escapeEmptyString() { assertEquals("", XMLUtils.escape(""), "\"\" should be \"\""); }
@Test void escapeWithNull() { assertNull(XMLUtils.escape(null), "null should be null"); }
@Test void escapeNonAscii() { assertEquals("\u0123", XMLUtils.escape("\u0123"), "Non-ASCII characters were escaped"); }