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