|
| 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 org.testng.Assert; |
| 26 | +import org.testng.annotations.DataProvider; |
| 27 | +import org.testng.annotations.Test; |
| 28 | +import org.w3c.dom.Document; |
| 29 | +import org.w3c.dom.Node; |
| 30 | + |
| 31 | +import javax.xml.xpath.*; |
| 32 | + |
| 33 | +/* |
| 34 | + * @test |
| 35 | + * @bug 8289948 |
| 36 | + * @library /javax/xml/jaxp/unittest |
| 37 | + * @run testng xpath.XPathNodeSetFnTest |
| 38 | + * @summary Tests the XPath Node Set Functions |
| 39 | + */ |
| 40 | +public class XPathNodeSetFnTest extends XPathTestBase { |
| 41 | + |
| 42 | + private static final Document doc = getDtdDocument(); |
| 43 | + |
| 44 | + /* |
| 45 | + * DataProvider for testing the id function. |
| 46 | + * Data columns: |
| 47 | + * see parameters of the test "testIdFn" |
| 48 | + */ |
| 49 | + @DataProvider(name = "idExpTestCases") |
| 50 | + public Object[][] getIdExp() { |
| 51 | + return new Object[][]{ |
| 52 | + {"id('x3')", "Customer_x3"}, |
| 53 | + {"id('x1 x2 x3')[3]", "Customer_x3"}, |
| 54 | + {"id('x1 | x2 | x3')[3]", "Customer_x3"}, |
| 55 | + {"id('x')", "Email_x"}, |
| 56 | + {"id(//Customer[3]/@id)", "Customer_x3"}, |
| 57 | + {"id(//*[.='123@xyz.com']/@id)", "Email_x"}, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + /* |
| 62 | + * DataProvider for testing the count function. |
| 63 | + * Data columns: |
| 64 | + * see parameters of the test "testCountFn" |
| 65 | + */ |
| 66 | + @DataProvider(name = "countExpTestCases") |
| 67 | + public Object[][] getCountExp() { |
| 68 | + return new Object[][]{ |
| 69 | + {"count(//Customer)", CUSTOMERS}, |
| 70 | + {"count(//@id)", ID_ATTRIBUTES}, |
| 71 | + {"count(//Customer/@id)", CUSTOMERS}, |
| 72 | + {"count(//@*)", ID_ATTRIBUTES + FOO_ID_ATTRIBUTES}, |
| 73 | + {"count(//*)", |
| 74 | + ROOT + CUSTOMERS + FOO_CUSTOMERS + |
| 75 | + (CUSTOMERS + FOO_CUSTOMERS) * |
| 76 | + CUSTOMER_ELEMENTS}, |
| 77 | + {"count(//*[@id])", ID_ATTRIBUTES}, |
| 78 | + {"count(./*)", ROOT}, |
| 79 | + {"count(//Customer[1]/following::*)", |
| 80 | + CUSTOMERS - 1 + FOO_CUSTOMERS + |
| 81 | + (CUSTOMERS - 1 + FOO_CUSTOMERS) * |
| 82 | + CUSTOMER_ELEMENTS}, |
| 83 | + {"count(//Customer[1]/following-sibling::*)", |
| 84 | + CUSTOMERS - 1 + FOO_CUSTOMERS}, |
| 85 | + {"count(//Customer[3]/preceding::*)", |
| 86 | + CUSTOMERS - 1 + (CUSTOMERS - 1) * CUSTOMER_ELEMENTS}, |
| 87 | + {"count(//Customer[3]/preceding-sibling::*)", CUSTOMERS - 1}, |
| 88 | + {"count(//Customer[1]/ancestor::*)", ROOT}, |
| 89 | + {"count(//Customer[1]/ancestor-or-self::*)", ROOT + 1}, |
| 90 | + {"count(//Customer[1]/descendant::*)", CUSTOMER_ELEMENTS}, |
| 91 | + {"count(//Customer[1]/descendant-or-self::*)", |
| 92 | + CUSTOMER_ELEMENTS + 1}, |
| 93 | + {"count(//Customer/node())", |
| 94 | + ID_ATTRIBUTES + CUSTOMERS * CUSTOMER_ELEMENTS}, |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + /* |
| 99 | + * DataProvider for testing the position function. |
| 100 | + * Data columns: |
| 101 | + * see parameters of the test "testPositionFn" |
| 102 | + */ |
| 103 | + @DataProvider(name = "positionExpTestCases") |
| 104 | + public Object[][] getPositionExp() { |
| 105 | + return new Object[][]{ |
| 106 | + {"//Customer[position()=1]", "Customer_x1"}, |
| 107 | + {"//Customer[position()=last()]", "Customer_x3"}, |
| 108 | + {"//Customer[position()>1 and position()<last()]", |
| 109 | + "Customer_x2"}, |
| 110 | + {"//Customer[position() mod 2 =0]", "Customer_x2"}, |
| 111 | + {"//Customer[last()]", "Customer_x3"}, |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + /* |
| 116 | + * DataProvider for testing the name and local-name functions. |
| 117 | + * Data columns: |
| 118 | + * see parameters of the test "testNameFn" |
| 119 | + */ |
| 120 | + @DataProvider(name = "nameExpTestCases") |
| 121 | + public Object[][] getNameExp() { |
| 122 | + return new Object[][]{ |
| 123 | + {"local-name(//Customer)", "Customer"}, |
| 124 | + {"local-name(//foo:Customer)", "Customer"}, |
| 125 | + {"local-name(//Customer/@id)", "id"}, |
| 126 | + {"local-name(//foo:Customer/@foo:id)", "id"}, |
| 127 | + {"local-name(//*[local-name()='Customer'])", "Customer"}, |
| 128 | + {"namespace-uri(.)", ""}, |
| 129 | + {"namespace-uri(//Customers)", ""}, |
| 130 | + {"namespace-uri(//Customer)", ""}, |
| 131 | + {"namespace-uri(//foo:Customer)", "foo"}, |
| 132 | + {"namespace-uri(//@id)", ""}, |
| 133 | + {"namespace-uri(//@foo:id)", "foo"}, |
| 134 | + {"name(//*[namespace-uri()=\"foo\"])", "foo:Customer"}, |
| 135 | + {"name(//Customer)", "Customer"}, |
| 136 | + {"name(//foo:Customer)", "foo:Customer"}, |
| 137 | + {"name(//Customer/@id)", "id"}, |
| 138 | + {"name(//foo:Customer/@foo:id)", "foo:id"}, |
| 139 | + {"name(//*[name()='foo:Customer'])", "foo:Customer"}, |
| 140 | + }; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Verifies that the result of evaluating the id function matches the |
| 145 | + * expected result. |
| 146 | + * |
| 147 | + * @param exp XPath expression |
| 148 | + * @param expected expected result |
| 149 | + * @throws Exception if test fails |
| 150 | + */ |
| 151 | + @Test(dataProvider = "idExpTestCases") |
| 152 | + void testIdFn(String exp, String expected) throws Exception { |
| 153 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 154 | + |
| 155 | + Node node = xPath.evaluateExpression(exp, doc, Node.class); |
| 156 | + Node node2 = (Node) xPath.evaluate(exp, doc, XPathConstants.NODE); |
| 157 | + |
| 158 | + Assert.assertEquals(node.getNodeName() + "_" + |
| 159 | + node.getAttributes().item(0).getNodeValue() |
| 160 | + , expected); |
| 161 | + Assert.assertEquals(node2, node); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Verifies that the result of evaluating the count function matches the |
| 166 | + * expected result. |
| 167 | + * |
| 168 | + * @param exp XPath expression |
| 169 | + * @param expected expected result |
| 170 | + * @throws Exception if test fails |
| 171 | + */ |
| 172 | + @Test(dataProvider = "countExpTestCases") |
| 173 | + void testCountFn(String exp, int expected) throws Exception { |
| 174 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 175 | + |
| 176 | + double num = xPath.evaluateExpression(exp, doc, Double.class); |
| 177 | + double num2 = (double) xPath.evaluate(exp, doc, XPathConstants.NUMBER); |
| 178 | + |
| 179 | + Assert.assertEquals(num, expected); |
| 180 | + Assert.assertEquals(num2, num); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Verifies that the result of evaluating the position function matches the |
| 185 | + * expected result. |
| 186 | + * |
| 187 | + * @param exp XPath expression |
| 188 | + * @param expected expected result |
| 189 | + * @throws Exception if test fails |
| 190 | + */ |
| 191 | + @Test(dataProvider = "positionExpTestCases") |
| 192 | + void testPositionFn(String exp, String expected) throws Exception { |
| 193 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 194 | + |
| 195 | + Node node = xPath.evaluateExpression(exp, doc, Node.class); |
| 196 | + Node node2 = (Node) xPath.evaluate(exp, doc, XPathConstants.NODE); |
| 197 | + |
| 198 | + Assert.assertEquals(node.getNodeName() + "_" + |
| 199 | + node.getAttributes().item(0).getNodeValue() |
| 200 | + , expected); |
| 201 | + Assert.assertEquals(node2, node); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Verifies that the result of evaluating the name and local-name functions |
| 206 | + * matches the expected result. |
| 207 | + * |
| 208 | + * @param exp XPath expression |
| 209 | + * @param expected expected result |
| 210 | + * @throws Exception if test fails |
| 211 | + */ |
| 212 | + @Test(dataProvider = "nameExpTestCases") |
| 213 | + void testNameFn(String exp, String expected) throws Exception { |
| 214 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 215 | + |
| 216 | + String s = xPath.evaluateExpression(exp, doc, String.class); |
| 217 | + String s2 = (String) xPath.evaluate(exp, doc, XPathConstants.STRING); |
| 218 | + |
| 219 | + Assert.assertEquals(s, expected); |
| 220 | + Assert.assertEquals(s2, s); |
| 221 | + } |
| 222 | +} |
0 commit comments