jaxp/test/javax/xml/jaxp/unittest/javax/xml/xpath/XPathAnyTypeTest.java
author joehw
Tue, 27 Jan 2015 22:01:46 -0800
changeset 28695 427254b89b9e
permissions -rw-r--r--
8054196: XPath: support any type Reviewed-by: alanb, lancea, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28695
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     1
/*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     4
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     8
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    13
 * accompanied this code).
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    14
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    18
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    21
 * questions.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    22
 */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    23
package javax.xml.xpath;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    24
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    25
import java.io.File;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    26
import javax.xml.xpath.*;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    27
import static org.testng.Assert.assertEquals;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    28
import static org.testng.Assert.assertTrue;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    29
import org.testng.annotations.Test;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    30
import org.w3c.dom.Document;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    31
import org.w3c.dom.Node;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    32
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    33
/*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    34
 * @bug 8054196
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    35
 * @summary Test for the project XPath: support any type. This test covers the new
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    36
 * evaluateExpression methods of XPath, as well as XPathNodes and XPathEvaluationResult.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    37
 */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    38
public class XPathAnyTypeTest extends XPathTestBase {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    39
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    40
     Test for resolveFunction(QName functionName,int arity); evaluate throws
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    41
     NPE if functionName  is null.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    42
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    43
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    44
    @Test(dataProvider = "xpath", expectedExceptions = NullPointerException.class)
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    45
    public void testCheckXPathFunctionResolver02(XPath xpath) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    46
        xpath.setXPathFunctionResolver((functionName, arity) -> null);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    47
        assertEquals(xpath.evaluate(null, "5"), "2");
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    48
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    49
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    50
     Check that NPE is thrown when expression is null.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    51
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    52
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    53
    @Test(dataProvider = "xpath", expectedExceptions = NullPointerException.class)
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    54
    public void test01(XPath xpath) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    55
        double result = xpath.evaluateExpression(null, (Object) null, Double.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    56
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    57
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    58
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    59
     Check that NPE is thrown when the class type is null.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    60
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    61
    @Test(dataProvider = "xpath", expectedExceptions = NullPointerException.class)
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    62
    public void test02(XPath xpath) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    63
        double result = xpath.evaluateExpression("1+1", (Object) null, null);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    64
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    65
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    66
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    67
     Parameter item can be null when the expression does not depends on the
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    68
     context.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    69
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    70
    @Test(dataProvider = "xpath")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    71
    public void test03(XPath xpath) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    72
        int result = xpath.evaluateExpression("1+1", (Object) null, Integer.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    73
        assertTrue(result == 2);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    74
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    75
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    76
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    77
     * Test return type: boolean.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    78
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    79
    @Test(dataProvider = "document")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    80
    public void test04(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    81
        boolean result1 = xpath.evaluateExpression("boolean(/Customers/Customer[@id=3])", doc, Boolean.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    82
        assertTrue(result1);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    83
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    84
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    85
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    86
     * Test return type: numeric. Subtypes supported: Double, Integer and Long
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    87
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    88
    @Test(dataProvider = "document")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    89
    public void test05(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    90
        double result1 = xpath.evaluateExpression("count(/Customers/Customer)", doc, Double.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    91
        assertTrue(result1 == 3.0);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    92
        int result2 = xpath.evaluateExpression("count(/Customers/Customer)", doc, Integer.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    93
        assertTrue(result2 == 3);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    94
        long result3 = xpath.evaluateExpression("count(/Customers/Customer)", doc, Long.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    95
        assertTrue(result3 == 3);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    96
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    97
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    98
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    99
     * Test return type: numeric.  Of the subtypes of Number, only Double,
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   100
     * Integer and Long are required.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   101
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   102
    @Test(dataProvider = "invalidNumericTypes", expectedExceptions = IllegalArgumentException.class)
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   103
    public void test06(XPath xpath, Class<Number> type) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   104
        xpath.evaluateExpression("1+1", (Object) null, type);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   105
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   106
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   107
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   108
     * Test return type: String.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   109
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   110
    @Test(dataProvider = "document")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   111
    public void test07(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   112
        String result1 = xpath.evaluateExpression("string(/Customers/Customer[@id=3]/Phone/text())", doc, String.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   113
        assertTrue(result1.equals("3333333333"));
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   114
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   115
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   116
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   117
     * Test return type: NodeSet.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   118
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   119
    @Test(dataProvider = "document")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   120
    public void test08(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   121
        XPathNodes nodes = xpath.evaluateExpression("/Customers/Customer", doc, XPathNodes.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   122
        assertTrue(nodes.size() == 3);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   123
        for (Node n : nodes) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   124
            assertEquals(n.getLocalName(), "Customer");
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   125
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   126
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   127
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   128
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   129
     * Test return type: Node.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   130
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   131
    @Test(dataProvider = "document")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   132
    public void test09(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   133
        Node n = xpath.evaluateExpression("/Customers/Customer[@id=3]", doc, Node.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   134
        assertEquals(n.getLocalName(), "Customer");
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   135
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   136
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   137
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   138
     * Test return type: Unsupported type.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   139
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   140
    @Test(dataProvider = "document", expectedExceptions = IllegalArgumentException.class)
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   141
    public void test10(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   142
        File n = xpath.evaluateExpression("/Customers/Customer[@id=3]", doc, File.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   143
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   144
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   145
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   146
     * Test return type: Any::Boolean.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   147
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   148
    @Test(dataProvider = "document")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   149
    public void test11(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   150
        XPathEvaluationResult<?> result = xpath.evaluateExpression("boolean(/Customers/Customer[@id=3])", doc);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   151
        verifyResult(result, true);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   152
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   153
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   154
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   155
     * Test return type: Any::Number.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   156
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   157
    @Test(dataProvider = "document")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   158
    public void test12(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   159
        XPathEvaluationResult<?> result = xpath.evaluateExpression("count(/Customers/Customer)", doc);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   160
        verifyResult(result, 3.0);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   161
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   162
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   163
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   164
     * Test return type: Any::String.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   165
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   166
    @Test(dataProvider = "document")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   167
    public void test13(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   168
        XPathEvaluationResult<?> result = xpath.evaluateExpression(
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   169
                "string(/Customers/Customer[@id=3]/Phone/text())", doc, XPathEvaluationResult.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   170
        verifyResult(result, "3333333333");
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   171
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   172
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   173
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   174
     * Test return type: Any::Nodeset.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   175
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   176
    @Test(dataProvider = "document")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   177
    public void test14(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   178
        XPathEvaluationResult<?> result = xpath.evaluateExpression("/Customers/Customer", doc);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   179
        verifyResult(result, "Customer");
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   180
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   181
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   182
    /*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   183
     * Test return type: Any::Node.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   184
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   185
    @Test(dataProvider = "document")
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   186
    public void test15(XPath xpath, Document doc) throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   187
        XPathEvaluationResult<?> result = xpath.evaluateExpression("/Customers/Customer[@id=3]", doc);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   188
        verifyResult(result, "Customer");
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   189
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   190
}