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