1 /* 2 * Copyright (c) 2015, 2016, 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.File; 26 27 import javax.xml.xpath.XPath; 28 import javax.xml.xpath.XPathNodes; 29 import javax.xml.xpath.XPathEvaluationResult; 30 import javax.xml.xpath.XPathExpressionException; 31 32 import static org.testng.Assert.assertEquals; 33 import static org.testng.Assert.assertTrue; 34 35 import org.testng.annotations.Listeners; 36 import org.testng.annotations.Test; 37 import org.w3c.dom.Document; 38 import org.w3c.dom.Node; 39 40 /* 41 * @test 42 * @bug 8054196 43 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 44 * @run testng/othervm -DrunSecMngr=true xpath.XPathAnyTypeTest 45 * @run testng/othervm xpath.XPathAnyTypeTest 46 * @summary Test for the project XPath: support any type. This test covers the new 47 * evaluateExpression methods of XPath, as well as XPathNodes and XPathEvaluationResult. 48 */ 49 @Listeners({jaxp.library.BasePolicy.class}) 50 public class XPathAnyTypeTest extends XPathTestBase { 51 /* 52 Test for resolveFunction(QName functionName,int arity); evaluate throws 53 NPE if functionName is null. 54 */ 55 56 @Test(dataProvider = "xpath", expectedExceptions = NullPointerException.class) 57 public void testCheckXPathFunctionResolver02(XPath xpath) throws XPathExpressionException { 58 xpath.setXPathFunctionResolver((functionName, arity) -> null); 59 assertEquals(xpath.evaluate(null, "5"), "2"); 60 } 61 /* 62 Check that NPE is thrown when expression is null. 63 */ 64 65 @Test(dataProvider = "xpath", expectedExceptions = NullPointerException.class) 66 public void test01(XPath xpath) throws XPathExpressionException { 67 double result = xpath.evaluateExpression(null, (Object) null, Double.class); 68 } 69 70 /* 71 Check that NPE is thrown when the class type is null. 72 */ 73 @Test(dataProvider = "xpath", expectedExceptions = NullPointerException.class) 74 public void test02(XPath xpath) throws XPathExpressionException { 75 double result = xpath.evaluateExpression("1+1", (Object) null, null); 76 } 77 78 /* 79 Parameter item can be null when the expression does not depends on the 80 context. 81 */ 82 @Test(dataProvider = "xpath") 83 public void test03(XPath xpath) throws XPathExpressionException { 84 int result = xpath.evaluateExpression("1+1", (Object) null, Integer.class); 85 assertTrue(result == 2); 86 } 87 88 /* 89 * Test return type: boolean. 90 */ 91 @Test(dataProvider = "document") 92 public void test04(XPath xpath, Document doc) throws XPathExpressionException { 93 boolean result1 = xpath.evaluateExpression("boolean(/Customers/Customer[@id=3])", doc, Boolean.class); 94 assertTrue(result1); 95 } 96 97 /* 98 * Test return type: numeric. Subtypes supported: Double, Integer and Long 99 */ 100 @Test(dataProvider = "document") 101 public void test05(XPath xpath, Document doc) throws XPathExpressionException { 102 double result1 = xpath.evaluateExpression("count(/Customers/Customer)", doc, Double.class); 103 assertTrue(result1 == 3.0); 104 int result2 = xpath.evaluateExpression("count(/Customers/Customer)", doc, Integer.class); 105 assertTrue(result2 == 3); 106 long result3 = xpath.evaluateExpression("count(/Customers/Customer)", doc, Long.class); 107 assertTrue(result3 == 3); 108 } 109 110 /* 111 * Test return type: numeric. Of the subtypes of Number, only Double, 112 * Integer and Long are required. 113 */ 114 @Test(dataProvider = "invalidNumericTypes", expectedExceptions = IllegalArgumentException.class) 115 public void test06(XPath xpath, Class<Number> type) throws XPathExpressionException { 116 xpath.evaluateExpression("1+1", (Object) null, type); 117 } 118 119 /* 120 * Test return type: String. 121 */ 122 @Test(dataProvider = "document") 123 public void test07(XPath xpath, Document doc) throws XPathExpressionException { 124 String result1 = xpath.evaluateExpression("string(/Customers/Customer[@id=3]/Phone/text())", doc, String.class); 125 assertTrue(result1.equals("3333333333")); 126 } 127 128 /* 129 * Test return type: NodeSet. 130 */ 131 @Test(dataProvider = "document") 132 public void test08(XPath xpath, Document doc) throws XPathExpressionException { 133 XPathNodes nodes = xpath.evaluateExpression("/Customers/Customer", doc, XPathNodes.class); 134 assertTrue(nodes.size() == 3); 135 for (Node n : nodes) { 136 assertEquals(n.getLocalName(), "Customer"); 137 } 138 } 139 140 /* 141 * Test return type: Node. 142 */ 143 @Test(dataProvider = "document") 144 public void test09(XPath xpath, Document doc) throws XPathExpressionException { 145 Node n = xpath.evaluateExpression("/Customers/Customer[@id=3]", doc, Node.class); 146 assertEquals(n.getLocalName(), "Customer"); 147 } 148 149 /* 150 * Test return type: Unsupported type. 151 */ 152 @Test(dataProvider = "document", expectedExceptions = IllegalArgumentException.class) 153 public void test10(XPath xpath, Document doc) throws XPathExpressionException { 154 File n = xpath.evaluateExpression("/Customers/Customer[@id=3]", doc, File.class); 155 } 156 157 /* 158 * Test return type: Any::Boolean. 159 */ 160 @Test(dataProvider = "document") 161 public void test11(XPath xpath, Document doc) throws XPathExpressionException { 162 XPathEvaluationResult<?> result = xpath.evaluateExpression("boolean(/Customers/Customer[@id=3])", doc); 163 verifyResult(result, true); 164 } 165 166 /* 167 * Test return type: Any::Number. 168 */ 169 @Test(dataProvider = "document") 170 public void test12(XPath xpath, Document doc) throws XPathExpressionException { 171 XPathEvaluationResult<?> result = xpath.evaluateExpression("count(/Customers/Customer)", doc); 172 verifyResult(result, 3.0); 173 } 174 175 /* 176 * Test return type: Any::String. 177 */ 178 @Test(dataProvider = "document") 179 public void test13(XPath xpath, Document doc) throws XPathExpressionException { 180 XPathEvaluationResult<?> result = xpath.evaluateExpression( 181 "string(/Customers/Customer[@id=3]/Phone/text())", doc, XPathEvaluationResult.class); 182 verifyResult(result, "3333333333"); 183 } 184 185 /* 186 * Test return type: Any::Nodeset. 187 */ 188 @Test(dataProvider = "document") 189 public void test14(XPath xpath, Document doc) throws XPathExpressionException { 190 XPathEvaluationResult<?> result = xpath.evaluateExpression("/Customers/Customer", doc); 191 verifyResult(result, "Customer"); 192 } 193 194 /* 195 * Test return type: Any::Node. 196 */ 197 @Test(dataProvider = "document") 198 public void test15(XPath xpath, Document doc) throws XPathExpressionException { 199 XPathEvaluationResult<?> result = xpath.evaluateExpression("/Customers/Customer[@id=3]", doc); 200 verifyResult(result, "Customer"); 201 } 202 } 203