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