XmlUtil { public static String xpath(String xml, String xpath) { return (String) xpath(xml, xpath, XPathConstants.STRING); }  static String xpath(String xml, String xpath); static Object xpath(String xml, String xpath, QName returnType); static Object xpathHandlingIllegalChars(String xml, String xpath); @Beta static Object xpathHandlingIllegalChars(String xml, String xpath, QName returnType);  }
@Test public void testXpath() throws Exception { String xml = "<a><b>myb</b></a>"; for (int i = 0; i < 2; i++) { assertEquals(XmlUtil.xpath(xml, "/a/b[text()]"), "myb"); } }
@Test public void testXpathGetsNumber() throws Exception { String xml = "<a><b>1</b></a>"; assertEquals(XmlUtil.xpath(xml, "/a/b[text()]", XPathConstants.NUMBER), 1.0D); }
@Test public void testXpathGetsBoolean() throws Exception { String xml = "<a><b>true</b></a>"; assertEquals(XmlUtil.xpath(xml, "/a/b[text()]", XPathConstants.BOOLEAN), Boolean.TRUE); }
@Test public void testXpathGetsNodeSet() throws Exception { String xml = "<a><b><string>v1</string><string>v2</string></b></a>"; NodeList resultSet = (NodeList) XmlUtil.xpath(xml, "/a/b/string", XPathConstants.NODESET); List<String> results = Lists.newArrayList(); for (int i = 0; i < resultSet.getLength(); i++) { Node item = resultSet.item(i); results.add(item.getTextContent()); } assertEquals(results, ImmutableList.of("v1", "v2")); }
@Test public void testXpathWithEscapedCharsAndXmlVersion1_1() throws Exception { StringBuilder xml = new StringBuilder("<?xml version=\"1.1\" encoding=\"UTF-8\"?>"+"\n"+ "<a><b>myb</b><c>"); for (int i = 0; i < Integer.valueOf("FFFF", 16); i++) { if (isValidUnicodeInXml1_1(i)) { String unicode = Integer.toHexString(i); xml.append("&#x"+unicode+";"); } } xml.append("</c></a>"); assertEquals(XmlUtil.xpath(xml.toString(), "/a/b[text()]"), "myb"); }
@Test public void testXpathWithEscapedCharsAndXmlUnversioned() throws Exception { StringBuilder xml = new StringBuilder("<a><b>myb</b><c>"); for (int i = 0; i < Integer.valueOf("FFFF", 16); i++) { if (isValidUnicodeInXml1_0(i)) { String unicode = Integer.toHexString(i); xml.append("&#x"+unicode+";"); } } xml.append("</c></a>"); assertEquals(XmlUtil.xpath(xml.toString(), "/a/b[text()]"), "myb"); }
@Test(groups={"WIP"}, enabled=false) public void testXpathWithEachEscapeCharacterAndXmlVersion() throws Exception { List<Integer> errsInXml1_1 = Lists.newArrayList(); List<Integer> errsInXmlUnversioned = Lists.newArrayList(); for (int i = 0; i < Integer.valueOf("FFFF", 16); i++) { String unicode = Integer.toHexString(i); try { String xml = Joiner.on("\n").join( "<?xml version=\"1.1\" encoding=\"UTF-8\"?>", "<a><b>myb</b><c>&#x"+unicode+";</c></a>"); assertEquals(XmlUtil.xpath(xml, "/a/b[text()]"), "myb"); assertTrue(isValidUnicodeInXml1_1(i), "i="+i+"; unicode="+unicode); } catch (Throwable t) { LOG.debug("Failed for code "+unicode+": "+t.getMessage()); errsInXml1_1.add(Integer.valueOf(unicode, 16)); assertFalse(isValidUnicodeInXml1_1(i), "i="+i+"; unicode="+unicode); } try { String xml = "<a><b>myb</b><c>&#x"+unicode+";</c></a>"; assertEquals(XmlUtil.xpath(xml, "/a/b[text()]"), "myb"); assertTrue(isValidUnicodeInXml1_0(i), "i="+i+"; unicode="+unicode); } catch (Throwable t) { LOG.debug("Failed for code "+unicode+": "+t.getMessage()); errsInXmlUnversioned.add(Integer.valueOf(unicode, 16)); assertFalse(isValidUnicodeInXml1_0(i), "i="+i+"; unicode="+unicode); } } LOG.info("XML version 1.1 invalidCodes="+errsInXml1_1); LOG.info("XML unversioned invalidCodes="+errsInXmlUnversioned); }