|
| 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 javax.xml.xpath.XPath; |
| 26 | +import javax.xml.xpath.XPathConstants; |
| 27 | +import javax.xml.xpath.XPathExpressionException; |
| 28 | +import javax.xml.xpath.XPathFactory; |
| 29 | + |
| 30 | +import org.testng.Assert; |
| 31 | +import org.testng.annotations.DataProvider; |
| 32 | +import org.testng.annotations.Test; |
| 33 | +import org.w3c.dom.Document; |
| 34 | +import org.w3c.dom.Node; |
| 35 | +import org.w3c.dom.NodeList; |
| 36 | + |
| 37 | +/* |
| 38 | + * @test |
| 39 | + * @bug 8289509 |
| 40 | + * @library /javax/xml/jaxp/unittest |
| 41 | + * @run testng/othervm xpath.XPathExpFollowingTest |
| 42 | + * @summary Tests for XPath following/following-sibling axis specifier. |
| 43 | + */ |
| 44 | +public class XPathExpFollowingTest extends XPathTestBase { |
| 45 | + /* |
| 46 | + * DataProvider: provides XPath Axis following expressions and equivalent xpath expression. |
| 47 | + */ |
| 48 | + @DataProvider(name = "followingXpath") |
| 49 | + public Object[][] getFollowingXpathExpression() { |
| 50 | + return new Object[][] { |
| 51 | + {"/Customers/following::*", "/None"}, |
| 52 | + {"/Customers/Customer/following::Customer", "//Customer[@id != 'x1']"}, |
| 53 | + {"/Customers/Customer/following::foo:Customer", "//foo:Customer"}, |
| 54 | + {"/Customers/Customer[@id='x1']/following::Address", |
| 55 | + "/Customers/Customer[@id != 'x1']/Address"}, |
| 56 | + {"/Customers/Customer[@id='x1']/following::Street", |
| 57 | + "/Customers/Customer[@id != 'x1']/Address/Street"}, |
| 58 | + {"/Customers/Customer[@id='x1']/following::Street[2]", |
| 59 | + "/Customers/Customer[@id='x2']/Address/Street"}, |
| 60 | + {"/Customers/Customer[@id='x1']/following::*", |
| 61 | + "/Customers/Customer[@id != 'x1']/descendant-or-self::*" + |
| 62 | + " | /Customers/foo:Customer/descendant-or-self::*"}, |
| 63 | + {"/Customers/foo:Customer/foo:Address/following::*", |
| 64 | + "/Customers/foo:Customer/foo:Age | /Customers/foo:Customer/foo:ClubMember"}, |
| 65 | + {"/Customers/Customer[@id = 'x1']/*[following::Street]", "/Customers/Customer[@id = 'x1']/*"}, |
| 66 | + {"/Customers/foo:Customer/*[following::foo:Name]", "/None"}, |
| 67 | + {"/Customers/foo:Customer/*[not(following::foo:Name)]", "/Customers/foo:Customer/*"}, |
| 68 | + {"/Customers/following-sibling::*", "/None"}, |
| 69 | + {"/Customers/Customer/following-sibling::Customer", |
| 70 | + "/Customers/Customer[@id != 'x1']"}, |
| 71 | + {"/Customers/Customer/following-sibling::foo:Customer", |
| 72 | + "/Customers/foo:Customer"}, |
| 73 | + {"/Customers/Customer[@id='x1']/Name/following-sibling::Address", |
| 74 | + "/Customers/Customer[@id='x1']/Address"}, |
| 75 | + {"/Customers/Customer/Name/following-sibling::Address", |
| 76 | + "/Customers//Address"}, |
| 77 | + {"(/Customers/Customer/Address/Street/following-sibling::State)[3]", |
| 78 | + "/Customers/Customer[@id='x3']/Address/State"}, |
| 79 | + {"/Customers/Customer[@id='x1']/Address/Street/following-sibling::*[2]", |
| 80 | + "/Customers/Customer[@id='x3']/Address/State"}, |
| 81 | + {"/Customers/Customer[@id='x1']/following-sibling::*", |
| 82 | + "/Customers/Customer[@id != 'x1'] | /Customers/foo:Customer"}, |
| 83 | + {"/Customers/foo:Customer/foo:Address/following-sibling::*", |
| 84 | + "/Customers/foo:Customer/foo:Age | /Customers/foo:Customer/foo:ClubMember"}, |
| 85 | + {"/Customers/Customer[@id = 'x1']/*[following-sibling::Street]", "/None"}, |
| 86 | + {"/Customers/foo:Customer/*[following-sibling::foo:Address]", "/Customers/foo:Customer/foo:Name |" + |
| 87 | + "/Customers/foo:Customer/foo:Phone | /Customers/foo:Customer/foo:Email"}, |
| 88 | + {"/Customers/foo:Customer/*[not(following-sibling::foo:Address)]", "/Customers/foo:Customer/foo:Age | " + |
| 89 | + "/Customers/foo:Customer/foo:ClubMember | /Customers/foo:Customer/foo:Address"} |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + /* |
| 94 | + * DataProvider: provides XPath following expressions and expected number of following nodes returned |
| 95 | + */ |
| 96 | + @DataProvider(name = "followingXpathNodeCount") |
| 97 | + public Object[][] getFollowingXpathExpressionNodeCount() { |
| 98 | + return new Object[][] { |
| 99 | + {"/Customers/following::*", 0}, |
| 100 | + {"/Customers/Customer/following::*", 30}, |
| 101 | + {"/Customers/Customer/following::Customer", 2}, |
| 102 | + {"/Customers/Customer/following::foo:Customer", 1}, |
| 103 | + {"/Customers/Customer[@id='x1']/Name/following::*", 38}, |
| 104 | + {"/Customers/Customer/Address/following::*", 32}, |
| 105 | + {"/Customers/foo:Customer/foo:Address/following::*", 2}, |
| 106 | + {"/Customers/foo:Customer/foo:Name/following::*", 8}, |
| 107 | + {"/Customers/foo:Customer/*[following::foo:Name]", 0}, |
| 108 | + {"/Customers/foo:Customer/*[not(following::foo:Name)]", 6}, |
| 109 | + {"/Customers/following-sibling::*", 0}, |
| 110 | + {"/Customers/Customer/following-sibling::*", 3}, |
| 111 | + {"/Customers/Customer/following-sibling::Customer", 2}, |
| 112 | + {"/Customers/Customer/following-sibling::foo:Customer", 1}, |
| 113 | + {"/Customers/Customer[@id='x1']/Name/following-sibling::*", 5}, |
| 114 | + {"/Customers/Customer/Address/following-sibling::*", 6}, |
| 115 | + {"/Customers/Customer[@id='x1']/Address/following-sibling::*", 2}, |
| 116 | + {"/Customers/foo:Customer/foo:Address/following-sibling::*", 2}, |
| 117 | + {"/Customers/Customer[@id = 'x1']/*[following-sibling::Street]", 0}, |
| 118 | + {"/Customers/foo:Customer/*[following-sibling::foo:Address]", 3}, |
| 119 | + {"/Customers/foo:Customer/*[not(following-sibling::foo:Address)]", 3} |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + /* |
| 124 | + * DataProvider: provides XPath following expressions which should not return any node. |
| 125 | + */ |
| 126 | + @DataProvider(name = "followingXpathEmpty") |
| 127 | + public Object[][] getFollowingXpathExpressionEmpty() { |
| 128 | + return new Object[][] { |
| 129 | + {"/Customers/following::*"}, |
| 130 | + {"/Customers/foo:Customer/following::*"}, |
| 131 | + {"/Customers/Customer[@id = 'x3' ]/following::Customer"}, |
| 132 | + {"/Customers/following::id"}, |
| 133 | + {"/Customers/Customer[@id = 'x3' ]/following-sibling::Customer"}, |
| 134 | + {"/Customers/foo:Customer/following-sibling::*"}, |
| 135 | + {"/Customers/Customer/following-sibling::foo:Name"}, |
| 136 | + {"/Customers/following-sibling::id"} |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Verifies Axis following xpath expression returns same nodes as returns when used normal xpath expression |
| 142 | + * @param descexp Axis following XPath expression. |
| 143 | + * @param expath normal xPath expression |
| 144 | + * @throws XPathExpressionException |
| 145 | + */ |
| 146 | + @Test(dataProvider = "followingXpath") |
| 147 | + public void followingExpTests(String descexp, String expath) throws XPathExpressionException { |
| 148 | + Document doc = documentOf(DECLARATION + RAW_XML); |
| 149 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 150 | + NodeList actualNodeList = (NodeList) xPath.evaluate(descexp, doc, XPathConstants.NODESET); |
| 151 | + NodeList expectedNodeList = (NodeList) xPath.evaluate(expath, doc, XPathConstants.NODESET); |
| 152 | + Assert.assertEquals(actualNodeList.getLength(), expectedNodeList.getLength()); |
| 153 | + |
| 154 | + for(int i = 0; i < actualNodeList.getLength(); i++) { |
| 155 | + actualNodeList.item(i).equals(expectedNodeList.item(i)); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Verifies following xpath expression return following nodes list with correct number of nodes. |
| 161 | + * @param exp XPath expression. |
| 162 | + * @param nodeCount number of following nodes in nodelist. |
| 163 | + * @throws XPathExpressionException |
| 164 | + */ |
| 165 | + @Test(dataProvider = "followingXpathNodeCount") |
| 166 | + public void followingNodesCountTests(String exp, int nodeCount) throws XPathExpressionException { |
| 167 | + Document doc = documentOf(DECLARATION + RAW_XML); |
| 168 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 169 | + NodeList nodeList = (NodeList) xPath.evaluate(exp, doc, XPathConstants.NODESET); |
| 170 | + Assert.assertEquals(nodeList.getLength(), nodeCount); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Verifies following xpath expression return no nodes if following expression context nodes don't have matching following elements. |
| 175 | + * @param exp XPath expression. |
| 176 | + * @throws XPathExpressionException |
| 177 | + */ |
| 178 | + @Test(dataProvider = "followingXpathEmpty") |
| 179 | + public void FollowingScopeTests(String exp) throws XPathExpressionException { |
| 180 | + Document doc = documentOf(DECLARATION + RAW_XML); |
| 181 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 182 | + Node node = xPath.evaluateExpression(exp, doc, Node.class); |
| 183 | + Assert.assertNull(node); |
| 184 | + } |
| 185 | +} |
0 commit comments