|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | +package xpath; |
| 24 | + |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.InputStream; |
| 27 | +import javax.xml.parsers.DocumentBuilder; |
| 28 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 29 | +import javax.xml.xpath.XPath; |
| 30 | +import javax.xml.xpath.XPathConstants; |
| 31 | +import javax.xml.xpath.XPathFactory; |
| 32 | +import org.testng.Assert; |
| 33 | +import org.testng.annotations.DataProvider; |
| 34 | +import org.testng.annotations.Test; |
| 35 | +import org.w3c.dom.Document; |
| 36 | +import org.w3c.dom.Node; |
| 37 | +import org.w3c.dom.NodeList; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @bug 8284920 |
| 42 | + * @run testng/othervm xpath.XPathExpTest |
| 43 | + * @summary Tests for various XPath Expressions. |
| 44 | + */ |
| 45 | +public class XPathExpTest { |
| 46 | + |
| 47 | + private static final String XML = |
| 48 | + "<root>" |
| 49 | + + " <child id='1'>" |
| 50 | + + " <grandchild id='1'/>" |
| 51 | + + " <grandchild id='2'/>" |
| 52 | + + " </child>" |
| 53 | + + " <child id='2'>" |
| 54 | + + " <grandchild id='3'/>" |
| 55 | + + " <grandchild id='4'/>" |
| 56 | + + " </child>" |
| 57 | + + " <child id='3'>" |
| 58 | + + " <grandchild id='5'/>" |
| 59 | + + " <grandchild id='6'/>" |
| 60 | + + " </child>" |
| 61 | + + " </root>"; |
| 62 | + private static final String PARENT_CHILD = "child(2)"; |
| 63 | + |
| 64 | + /* |
| 65 | + * DataProvider for XPath expression test. |
| 66 | + * Data columns: |
| 67 | + * see parameters of the test "test" |
| 68 | + */ |
| 69 | + @DataProvider(name = "xpathExp") |
| 70 | + public Object[][] getXPathExpression() throws Exception { |
| 71 | + |
| 72 | + return new Object[][]{ |
| 73 | + // verifies various forms of the parent axis |
| 74 | + {"/root/child[@id='2']", PARENT_CHILD}, |
| 75 | + {"//grandchild[@id='3']/parent::child", PARENT_CHILD}, |
| 76 | + {"//grandchild[@id='3']/parent::node()", PARENT_CHILD}, |
| 77 | + {"//grandchild[@id='3']/parent::*", PARENT_CHILD}, |
| 78 | + {"//grandchild[@id='3']/parent::node()/grandchild[@id='4']/parent::node()", PARENT_CHILD}, |
| 79 | + {"//grandchild[@id='3']/..", PARENT_CHILD}, |
| 80 | + {"//grandchild[@id='3']/../grandchild[@id='4']/..", PARENT_CHILD}, |
| 81 | + {"//grandchild[@id='3']/parent::node()/grandchild[@id='4']/..", PARENT_CHILD}, |
| 82 | + |
| 83 | + // verifies various forms of the self axis |
| 84 | + {"/root/child[@id='2']/self::child", PARENT_CHILD}, |
| 85 | + {"/root/child[@id='2']/self::node()", PARENT_CHILD}, |
| 86 | + {"/root/child[@id='2']/self::*", PARENT_CHILD}, |
| 87 | + {"self::node()/root/child[@id='2']", PARENT_CHILD}, |
| 88 | + {"/root/child[@id='2']/.", PARENT_CHILD}, |
| 89 | + {"./root/child[@id='2']", PARENT_CHILD}, |
| 90 | + {".//child[@id='2']", PARENT_CHILD}, |
| 91 | + {"//grandchild[@id='3']/./../grandchild[@id='4']/..", PARENT_CHILD}, |
| 92 | + {"//grandchild[@id='3']/./parent::node()/grandchild[@id='4']/..", PARENT_CHILD}, |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Verifies XPath expressions. |
| 98 | + * |
| 99 | + * @param exp XPath expression |
| 100 | + * @param expected expected result |
| 101 | + * @throws Exception |
| 102 | + */ |
| 103 | + @Test(dataProvider = "xpathExp") |
| 104 | + void test(String exp, String expected) throws Exception { |
| 105 | + Document doc = getDoc(XML); |
| 106 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 107 | + NodeList nl = (NodeList) xPath.evaluate(exp, doc, XPathConstants.NODESET); |
| 108 | + Node child = nl.item(0); |
| 109 | + Assert.assertEquals( |
| 110 | + child.getNodeName() + "(" + child.getAttributes().item(0).getNodeValue() + ")", |
| 111 | + expected); |
| 112 | + } |
| 113 | + |
| 114 | + Document getDoc(String xml) throws Exception { |
| 115 | + DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); |
| 116 | + dfactory.setNamespaceAware(true); |
| 117 | + DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); |
| 118 | + InputStream is = new ByteArrayInputStream(xml.getBytes()); |
| 119 | + return docBuilder.parse(is); |
| 120 | + } |
| 121 | +} |
0 commit comments