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