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