8051561: Convert JAXP functional tests to jtreg(TestNG): javax.xml.xpath.*
Co-authored-by: Yiming Wang <yiming.wang@oracle.com> Reviewed-by: joehw
This commit is contained in:
parent
1cd48f2b65
commit
12b7532ad0
jaxp/test/javax/xml/jaxp
functional/javax/xml/xpath
ptests
xmlfiles
libs/javax/xml/xpath/ptests
@ -0,0 +1,529 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package javax.xml.xpath.ptests;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.xpath.XPath;
|
||||
import static javax.xml.xpath.XPathConstants.BOOLEAN;
|
||||
import static javax.xml.xpath.XPathConstants.NODE;
|
||||
import static javax.xml.xpath.XPathConstants.NODESET;
|
||||
import static javax.xml.xpath.XPathConstants.NUMBER;
|
||||
import static javax.xml.xpath.XPathConstants.STRING;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR;
|
||||
import static jaxp.library.JAXPTestUtilities.failUnexpected;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* Class containing the test cases for XPathExpression API.
|
||||
*/
|
||||
public class XPathExpressionTest {
|
||||
/**
|
||||
* Document object for testing XML file.
|
||||
*/
|
||||
private Document document;
|
||||
|
||||
/**
|
||||
* A XPath for evaluation environment and expressions.
|
||||
*/
|
||||
private XPath xpath;
|
||||
|
||||
/**
|
||||
* A QName using default name space.
|
||||
*/
|
||||
private static final QName TEST_QNAME = new QName(XMLConstants.XML_NS_URI, "");
|
||||
|
||||
/**
|
||||
* XML File Path.
|
||||
*/
|
||||
private static final Path XML_PATH = Paths.get(XML_DIR + "widgets.xml");
|
||||
|
||||
/**
|
||||
* An expression name which locate at "/widgets/widget[@name='a']/@quantity"
|
||||
*/
|
||||
private static final String EXPRESSION_NAME_A = "/widgets/widget[@name='a']/@quantity";
|
||||
|
||||
/**
|
||||
* An expression name which locate at "/widgets/widget[@name='b']/@quantity"
|
||||
*/
|
||||
private static final String EXPRESSION_NAME_B = "/widgets/widget[@name='b']/@quantity";
|
||||
|
||||
/**
|
||||
* Create Document object and XPath object for every time
|
||||
* @throws ParserConfigurationException If the factory class cannot be
|
||||
* loaded, instantiated
|
||||
* @throws SAXException If any parse errors occur.
|
||||
* @throws IOException If operation on xml file failed.
|
||||
*/
|
||||
@BeforeTest
|
||||
public void setup() throws ParserConfigurationException, SAXException, IOException {
|
||||
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile());
|
||||
xpath = XPathFactory.newInstance().newXPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for evaluate(java.lang.Object item,QName returnType)throws
|
||||
* XPathExpressionException.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression01() {
|
||||
try {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(document, STRING), "6");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item,QName returnType) throws NPE if input
|
||||
* source is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPathExpression02() {
|
||||
try {
|
||||
xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item,QName returnType) throws NPE if returnType
|
||||
* is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPathExpression03() {
|
||||
try {
|
||||
xpath.compile(EXPRESSION_NAME_A).evaluate(document, null);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for method evaluate(java.lang.Object item,QName returnType).If a
|
||||
* request is made to evaluate the expression in the absence of a context
|
||||
* item, simple expressions, such as "1+1", can be evaluated.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression04() {
|
||||
try {
|
||||
assertEquals(xpath.compile("1+1").evaluate(document, STRING), "2");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item,QName returnType) throws IAE If returnType
|
||||
* is not one of the types defined in XPathConstants.
|
||||
*/
|
||||
@Test(expectedExceptions = IllegalArgumentException.class)
|
||||
public void testCheckXPathExpression05() {
|
||||
try {
|
||||
xpath.compile(EXPRESSION_NAME_A).evaluate(document, TEST_QNAME);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item,QName returnType) return correct boolean
|
||||
* value if returnType is Boolean.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression06() {
|
||||
try {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(document, BOOLEAN), true);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item,QName returnType) return correct boolean
|
||||
* value if returnType is Boolean.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression07() {
|
||||
try {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_B).
|
||||
evaluate(document, BOOLEAN), false);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item,QName returnType) return correct number
|
||||
* value when return type is Double.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression08() {
|
||||
try {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(document, NUMBER), 6d);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item,QName returnType) evaluate an attribute
|
||||
* value which returnType is Node.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression09() {
|
||||
try {
|
||||
Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(document, NODE);
|
||||
assertEquals(attr.getValue(), "6");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item,QName returnType) evaluate an attribute
|
||||
* value which returnType is NodeList.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression10() {
|
||||
try {
|
||||
NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(document, NODESET);
|
||||
Attr attr = (Attr) nodeList.item(0);
|
||||
assertEquals(attr.getValue(), "6");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for evaluate(java.lang.Object item) when returnType is left off of
|
||||
* the XPath.evaluate method, all expressions are evaluated to a String
|
||||
* value.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression11() {
|
||||
try {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document), "6");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item) throws NPE if expression is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPathExpression12() {
|
||||
try {
|
||||
xpath.compile(null).evaluate(document);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item) when a request is made to evaluate the
|
||||
* expression in the absence of a context item, simple expressions, such as
|
||||
* "1+1", can be evaluated.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression13() {
|
||||
try {
|
||||
assertEquals(xpath.compile("1+1").evaluate(document), "2");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(java.lang.Object item) throws NPE if document is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPathExpression14() {
|
||||
try {
|
||||
xpath.compile(EXPRESSION_NAME_A).evaluate(null);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* valuate(InputSource source) return a string value if return type is
|
||||
* String.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression15() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(new InputSource(is)), "6");
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source) throws NPE if input source is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPathExpression16() {
|
||||
try {
|
||||
xpath.compile(EXPRESSION_NAME_A).evaluate(null);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source) throws NPE if expression is null
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPathExpression17() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.compile(null).evaluate(new InputSource(is));
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source) throws XPathExpressionException if
|
||||
* returnType is String junk characters.
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPathExpression18() throws XPathExpressionException {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.compile("-*&").evaluate(new InputSource(is));
|
||||
} catch (IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source) throws XPathExpressionException if
|
||||
* expression is a blank string " ".
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPathExpression19() throws XPathExpressionException {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.compile(" ").evaluate(new InputSource(is));
|
||||
} catch (IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for evaluate(InputSource source,QName returnType) returns a string
|
||||
* value if returnType is String.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression20() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(new InputSource(is), STRING), "6");
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source,QName returnType) throws NPE if source is
|
||||
* null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPathExpression21() {
|
||||
try {
|
||||
xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source,QName returnType) throws NPE if expression is
|
||||
* null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPathExpression22() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.compile(null).evaluate(new InputSource(is), STRING);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source,QName returnType) throws NPE if returnType is
|
||||
* null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPathExpression23() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), null);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source,QName returnType) throws
|
||||
* XPathExpressionException if expression is junk characters.
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPathExpression24() throws XPathExpressionException {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.compile("-*&").evaluate(new InputSource(is), STRING);
|
||||
} catch (IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source,QName returnType) throws
|
||||
* XPathExpressionException if expression is blank " ".
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPathExpression25() throws XPathExpressionException {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.compile(" ").evaluate(new InputSource(is), STRING);
|
||||
} catch (IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source,QName returnType) throws
|
||||
* IllegalArgumentException if returnType is not one of the types defined
|
||||
* in XPathConstants.
|
||||
*/
|
||||
@Test(expectedExceptions = IllegalArgumentException.class)
|
||||
public void testCheckXPathExpression26() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), TEST_QNAME);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source,QName returnType) return a correct boolean
|
||||
* value if returnType is Boolean.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression27() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(new InputSource(is), BOOLEAN), true);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source,QName returnType) return a correct boolean
|
||||
* value if returnType is Boolean.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression28() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_B).
|
||||
evaluate(new InputSource(is), BOOLEAN), false);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* evaluate(InputSource source,QName returnType) return a correct number
|
||||
* value if returnType is Number.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression29() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(new InputSource(is), NUMBER), 6d);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for evaluate(InputSource source,QName returnType) returns a node if
|
||||
* returnType is Node.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression30() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(new InputSource(is), NODE);
|
||||
assertEquals(attr.getValue(), "6");
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for evaluate(InputSource source,QName returnType) return a node list
|
||||
* if returnType is NodeList.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathExpression31() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A).
|
||||
evaluate(new InputSource(is), NODESET);
|
||||
assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package javax.xml.xpath.ptests;
|
||||
|
||||
import static javax.xml.xpath.XPathConstants.DOM_OBJECT_MODEL;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
import javax.xml.xpath.XPathFactoryConfigurationException;
|
||||
import static jaxp.library.JAXPTestUtilities.failUnexpected;
|
||||
import static org.testng.AssertJUnit.assertNotNull;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* Class containing the test cases for XPathFactory API.
|
||||
*/
|
||||
public class XPathFactoryTest {
|
||||
/**
|
||||
* Valid URL for creating a XPath factory.
|
||||
*/
|
||||
private static final String VALID_URL = "http://java.sun.com/jaxp/xpath/dom";
|
||||
|
||||
/**
|
||||
* Invalid URL not able to create a XPath factory.
|
||||
*/
|
||||
private static final String INVALID_URL = "http://java.sun.com/jaxp/xpath/dom1";
|
||||
|
||||
/**
|
||||
* Test for constructor - XPathFactory.newInstance().
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathFactory01() {
|
||||
assertNotNull(XPathFactory.newInstance());
|
||||
}
|
||||
|
||||
/**
|
||||
* XPathFactory.newInstance(String uri) throws NPE if uri is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
private void testCheckXPathFactory02() {
|
||||
try {
|
||||
XPathFactory.newInstance(null);
|
||||
} catch (XPathFactoryConfigurationException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPathFactory.newInstance(String uri) throws XPFCE if uri is just a blank
|
||||
* string.
|
||||
*
|
||||
* @throws XPathFactoryConfigurationException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathFactoryConfigurationException.class)
|
||||
public void testCheckXPathFactory03() throws XPathFactoryConfigurationException {
|
||||
XPathFactory.newInstance(" ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for constructor - XPathFactory.newInstance(String uri) with valid
|
||||
* url - "http://java.sun.com/jaxp/xpath/dom".
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathFactory04() {
|
||||
try {
|
||||
assertNotNull(XPathFactory.newInstance(VALID_URL));
|
||||
} catch (XPathFactoryConfigurationException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for constructor - XPathFactory.newInstance(String uri) with invalid
|
||||
* url - "http://java.sun.com/jaxp/xpath/dom1".
|
||||
*
|
||||
* @throws XPathFactoryConfigurationException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathFactoryConfigurationException.class)
|
||||
public void testCheckXPathFactory05() throws XPathFactoryConfigurationException {
|
||||
XPathFactory.newInstance(INVALID_URL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for constructor - XPathFactory.newInstance() and creating XPath with
|
||||
* newXPath().
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathFactory06() {
|
||||
assertNotNull(XPathFactory.newInstance().newXPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for constructor - XPathFactory.newInstance(String uri) with valid
|
||||
* url - "http://java.sun.com/jaxp/xpath/dom" and creating XPath with
|
||||
* newXPath().
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathFactory07() {
|
||||
try {
|
||||
assertNotNull(XPathFactory.newInstance(VALID_URL).newXPath());
|
||||
} catch (XPathFactoryConfigurationException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for constructor - XPathFactory.newInstance(String uri) with valid
|
||||
* uri - DOM_OBJECT_MODEL.toString().
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathFactory08() {
|
||||
try {
|
||||
assertNotNull(XPathFactory.newInstance(DOM_OBJECT_MODEL));
|
||||
} catch (XPathFactoryConfigurationException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
}
|
79
jaxp/test/javax/xml/jaxp/functional/javax/xml/xpath/ptests/XPathFunctionResolverTest.java
Normal file
79
jaxp/test/javax/xml/jaxp/functional/javax/xml/xpath/ptests/XPathFunctionResolverTest.java
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package javax.xml.xpath.ptests;
|
||||
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
import static jaxp.library.JAXPTestUtilities.failUnexpected;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* Class containing the test cases for XPathFunctionResolver.
|
||||
*/
|
||||
public class XPathFunctionResolverTest {
|
||||
/**
|
||||
* A XPath for evaluation environment and expressions.
|
||||
*/
|
||||
private XPath xpath;
|
||||
|
||||
/**
|
||||
* Create XPath object before every test. Make sure function resolver has
|
||||
* been set for XPath object.
|
||||
*/
|
||||
@BeforeTest
|
||||
public void setup() {
|
||||
xpath = XPathFactory.newInstance().newXPath();
|
||||
if (xpath.getXPathFunctionResolver() == null) {
|
||||
xpath.setXPathFunctionResolver((functionName,arity) -> null);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Test for resolveFunction(QName functionName,int arity). evaluate will
|
||||
* continue as long as functionName is meaningful.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPathFunctionResolver01() {
|
||||
try {
|
||||
assertEquals(xpath.evaluate("round(1.7)", (Object)null), "2");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for resolveFunction(QName functionName,int arity); evaluate throws
|
||||
* NPE if functionName is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPathFunctionResolver02() {
|
||||
try {
|
||||
assertEquals(xpath.evaluate(null, "5"), "2");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,805 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package javax.xml.xpath.ptests;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Iterator;
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.namespace.NamespaceContext;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.xpath.XPath;
|
||||
import static javax.xml.xpath.XPathConstants.BOOLEAN;
|
||||
import static javax.xml.xpath.XPathConstants.NODE;
|
||||
import static javax.xml.xpath.XPathConstants.NODESET;
|
||||
import static javax.xml.xpath.XPathConstants.NUMBER;
|
||||
import static javax.xml.xpath.XPathConstants.STRING;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR;
|
||||
import static jaxp.library.JAXPTestUtilities.failUnexpected;
|
||||
import static org.testng.AssertJUnit.assertEquals;
|
||||
import static org.testng.AssertJUnit.assertNotNull;
|
||||
import static org.testng.AssertJUnit.assertNull;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* Class containing the test cases for XPath API.
|
||||
*/
|
||||
public class XPathTest {
|
||||
/**
|
||||
* Document object for testing XML file.
|
||||
*/
|
||||
private Document document;
|
||||
|
||||
/**
|
||||
* A XPath for evaluation environment and expressions.
|
||||
*/
|
||||
private XPath xpath;
|
||||
|
||||
/**
|
||||
* A QName using default name space.
|
||||
*/
|
||||
private static final QName TEST_QNAME = new QName(XMLConstants.XML_NS_URI, "");
|
||||
|
||||
/**
|
||||
* XML File Path.
|
||||
*/
|
||||
private static final Path XML_PATH = Paths.get(XML_DIR + "widgets.xml");
|
||||
|
||||
/**
|
||||
* An expression name which locate at "/widgets/widget[@name='a']/@quantity"
|
||||
*/
|
||||
private static final String EXPRESSION_NAME_A = "/widgets/widget[@name='a']/@quantity";
|
||||
|
||||
/**
|
||||
* An expression name which locate at "/widgets/widget[@name='b']/@quantity"
|
||||
*/
|
||||
private static final String EXPRESSION_NAME_B = "/widgets/widget[@name='b']/@quantity";
|
||||
|
||||
/**
|
||||
* Create Document object and XPath object for every time
|
||||
* @throws ParserConfigurationException If the factory class cannot be
|
||||
* loaded, instantiated
|
||||
* @throws SAXException If any parse errors occur.
|
||||
* @throws IOException If operation on xml file failed.
|
||||
*/
|
||||
@BeforeTest
|
||||
public void setup() throws ParserConfigurationException, SAXException, IOException {
|
||||
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile());
|
||||
xpath = XPathFactory.newInstance().newXPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object
|
||||
* item, QName returnType) which return type is String.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath01() {
|
||||
try {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, STRING), "6");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test for XPath.compile(java.lang.String expression) and then
|
||||
* evaluate(java.lang.Object item, QName returnType).
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath02() {
|
||||
try {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document, STRING), "6");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object
|
||||
* item) when the third argument is left off of the XPath.evaluate method,
|
||||
* all expressions are evaluated to a String value.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath03() {
|
||||
try {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document), "6");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.compile(java.lang.String expression). If expression is
|
||||
* null, should throw NPE.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath04() {
|
||||
try {
|
||||
xpath.compile(null);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.compile(java.lang.String expression). If expression cannot
|
||||
* be compiled junk characters, should throw XPathExpressionException.
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPath05() throws XPathExpressionException {
|
||||
xpath.compile("-*&");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.compile(java.lang.String expression). If expression is
|
||||
* blank, should throw XPathExpressionException
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPath06() throws XPathExpressionException {
|
||||
xpath.compile(" ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.compile(java.lang.String expression). The expression
|
||||
* cannot be evaluated as this does not exist.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath07() {
|
||||
try {
|
||||
assertEquals(xpath.compile(EXPRESSION_NAME_B).evaluate(document, STRING), "");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object
|
||||
* item, QName returnType). If String expression is null, should throw NPE
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath08() {
|
||||
try {
|
||||
xpath.evaluate(null, document, STRING);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object
|
||||
* item, QName returnType). If item is null, should throw NPE.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath09() {
|
||||
try {
|
||||
xpath.evaluate(EXPRESSION_NAME_A, null, STRING);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object
|
||||
* item, QName returnType). If returnType is null, should throw NPE.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath10() {
|
||||
try {
|
||||
xpath.evaluate(EXPRESSION_NAME_A, document, null);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object
|
||||
* item, QName returnType). If a request is made to evaluate the expression
|
||||
* in the absence of a context item, simple expressions, such as "1+1", can
|
||||
* be evaluated.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath11() {
|
||||
try {
|
||||
assertEquals(xpath.evaluate("1+1", document, STRING), "2");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
|
||||
* returnType) throws XPathExpressionException if expression is a empty
|
||||
* string "".
|
||||
* .
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPath12() throws XPathExpressionException {
|
||||
xpath.evaluate("", document, STRING);
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
|
||||
* returnType) throws IllegalArgumentException if returnType is not one of
|
||||
* the types defined in XPathConstants.
|
||||
*/
|
||||
@Test(expectedExceptions = IllegalArgumentException.class)
|
||||
public void testCheckXPath13() {
|
||||
try {
|
||||
xpath.evaluate(EXPRESSION_NAME_A, document, TEST_QNAME);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
|
||||
* returnType) returns correct boolean value if returnType is Boolean.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath14() {
|
||||
try {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, BOOLEAN), true);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
|
||||
* returnType) returns false as expression is not successful in evaluating
|
||||
* to any result if returnType is Boolean.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath15() {
|
||||
try {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_B, document, BOOLEAN), false);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
|
||||
* returnType) returns correct number value if return type is Number.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath16() {
|
||||
try {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, NUMBER), 6d);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
|
||||
* returnType) returns correct string value if return type is Node.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath17() {
|
||||
try {
|
||||
assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A, document, NODE)).getValue(), "6");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object
|
||||
* item, QName returnType). If return type is NodeList,the evaluated value
|
||||
* equals to "6" as expected.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath18() {
|
||||
try {
|
||||
NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A, document, NODESET);
|
||||
assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object
|
||||
* item). If expression is null, should throw NPE.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath19() {
|
||||
try {
|
||||
xpath.evaluate(null, document);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, java.lang.Object
|
||||
* item). If a request is made to evaluate the expression in the absence of
|
||||
* a context item, simple expressions, such as "1+1", can be evaluated.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath20() {
|
||||
try {
|
||||
assertEquals(xpath.evaluate("1+1", document), "2");
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, java.lang.Object item) throws
|
||||
* NPE if InputSource is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath21() {
|
||||
try {
|
||||
xpath.evaluate(EXPRESSION_NAME_A, null);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source) return
|
||||
* correct value by looking for Node.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath22() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is)), "6");
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source) throws
|
||||
* NPE if InputSource is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath23() {
|
||||
try {
|
||||
xpath.evaluate(EXPRESSION_NAME_A, null);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source) throws
|
||||
* NPE if String expression is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath24() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.evaluate(null, new InputSource(is));
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, InputSource source).
|
||||
* If expression is junk characters, expression cannot be evaluated, should
|
||||
* throw XPathExpressionException.
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPath25() throws XPathExpressionException {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.evaluate("-*&", new InputSource(is));
|
||||
} catch (IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source) throws
|
||||
* XPathExpressionException if expression is blank " ".
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPath26() throws XPathExpressionException {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.evaluate(" ", new InputSource(is));
|
||||
} catch (IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source, QName
|
||||
* returnType) returns correct string value which return type is String.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath27() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), STRING), "6");
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source, QName
|
||||
* returnType) throws NPE if source is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath28() {
|
||||
try {
|
||||
xpath.evaluate(EXPRESSION_NAME_A, null, STRING);
|
||||
} catch (XPathExpressionException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source, QName
|
||||
* returnType) throws NPE if expression is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath29() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.evaluate(null, new InputSource(is), STRING);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source,
|
||||
* QName returnType) throws NPE if returnType is null .
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath30() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), null);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source, QName
|
||||
* returnType) throws XPathExpressionException if expression is junk characters.
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPath31() throws XPathExpressionException {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.evaluate("-*&", new InputSource(is), STRING);
|
||||
} catch (IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source, QName
|
||||
* returnType) throws XPathExpressionException if expression is blank " ".
|
||||
*
|
||||
* @throws XPathExpressionException
|
||||
*/
|
||||
@Test(expectedExceptions = XPathExpressionException.class)
|
||||
public void testCheckXPath32() throws XPathExpressionException {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.evaluate(" ", new InputSource(is), STRING);
|
||||
} catch (IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source,
|
||||
* QName returnType) throws IllegalArgumentException if returnType is not
|
||||
* one of the types defined in XPathConstants.
|
||||
*/
|
||||
@Test(expectedExceptions = IllegalArgumentException.class)
|
||||
public void testCheckXPath33() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), TEST_QNAME);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source,
|
||||
* QName returnType) return correct boolean value if return type is Boolean.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath34() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
|
||||
BOOLEAN), true);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source,
|
||||
* QName returnType) return correct boolean value if return type is Boolean.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath35() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is),
|
||||
BOOLEAN), false);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source,
|
||||
* QName returnType) return correct number value if return type is Number.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath36() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
|
||||
NUMBER), 6d);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource source,
|
||||
* QName returnType) return correct string value if return type is Node.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath37() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A,
|
||||
new InputSource(is), NODE)).getValue(), "6");
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, InputSource source,
|
||||
* QName returnType) which return type is NodeList.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath38() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A,
|
||||
new InputSource(is), NODESET);
|
||||
assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.evaluate(java.lang.String expression, InputSource iSource,
|
||||
* QName returnType). If return type is Boolean, should return false as
|
||||
* expression is not successful in evaluating to any result.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath52() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is),
|
||||
BOOLEAN), false);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource iSource, QName
|
||||
* returnType) returns correct number value which return type is Number.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath53() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
|
||||
NUMBER), 6d);
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource iSource, QName
|
||||
* returnType) returns a node value if returnType is Node.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath54() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A,
|
||||
new InputSource(is), NODE)).getValue(), "6");
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XPath.evaluate(java.lang.String expression, InputSource iSource, QName
|
||||
* returnType) returns a node list if returnType is NodeList.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath55() {
|
||||
try (InputStream is = Files.newInputStream(XML_PATH)) {
|
||||
NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A,
|
||||
new InputSource(is), NODESET);
|
||||
assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
|
||||
} catch (XPathExpressionException | IOException ex) {
|
||||
failUnexpected(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.getNamespaceContext() returns the current namespace
|
||||
* context, null is returned if no namespace context is in effect.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath56() {
|
||||
// CR 6376058 says that an impl will be provided, but by
|
||||
// default we still return null here
|
||||
assertNull(xpath.getNamespaceContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.setNamespaceContext(NamespaceContext nsContext) Establish
|
||||
* a namespace context. Set a valid nsContext and retrieve it using
|
||||
* getNamespaceContext(), should return the same.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath57() {
|
||||
MyNamespaceContext myNamespaceContext = new MyNamespaceContext();
|
||||
xpath.setNamespaceContext(myNamespaceContext);
|
||||
assertEquals(xpath.getNamespaceContext(), myNamespaceContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.setNamespaceContext(NamespaceContext nsContext) Establish
|
||||
* a namespace context. NullPointerException is thrown if nsContext is null.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath58() {
|
||||
xpath.setNamespaceContext(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.getXPathFunctionResolver() Return the current function
|
||||
* resolver. Null is returned if no function resolver is in effect.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath59() {
|
||||
assertNull(xpath.getXPathFunctionResolver());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.setXPathFunctionResolver(XPathFunctionResolver resolver).
|
||||
* Set a valid resolver and retrieve it using getXPathFunctionResolver(),
|
||||
* should return the same.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath60() {
|
||||
xpath.setXPathFunctionResolver((functionName, arity) -> null);
|
||||
assertNotNull(xpath.getXPathFunctionResolver());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.setXPathFunctionResolver(XPathFunctionResolver resolver).
|
||||
* set resolver as null, should throw NPE.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath61() {
|
||||
xpath.setXPathFunctionResolver(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.getXPathVariableResolver() Return the current variable
|
||||
* resolver. null is returned if no variable resolver is in effect.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath62() {
|
||||
assertNull(xpath.getXPathVariableResolver());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.setXPathVariableResolver(XPathVariableResolver resolver).
|
||||
* Set a valid resolver and retrieve it using getXPathVariableResolver(),
|
||||
* should return the same.
|
||||
*/
|
||||
@Test
|
||||
public void testCheckXPath63() {
|
||||
xpath.setXPathVariableResolver(qname -> null);
|
||||
assertNotNull(xpath.getXPathVariableResolver());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for XPath.setXPathVariableResolver(XPathVariableResolver resolver).
|
||||
* Set resolver as null, should throw NPE.
|
||||
*/
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testCheckXPath64() {
|
||||
xpath.setXPathVariableResolver(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Customized NamespaceContext used for test
|
||||
*/
|
||||
private class MyNamespaceContext implements NamespaceContext {
|
||||
/**
|
||||
* et Namespace URI bound to a prefix in the current scope.
|
||||
* @param prefix prefix to look up
|
||||
* @return a Namespace URI identical to prefix
|
||||
*/
|
||||
@Override
|
||||
public String getNamespaceURI(String prefix) {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get prefix bound to Namespace URI in the current scope.
|
||||
* @param namespaceURI URI of Namespace to lookup
|
||||
* @return prefix identical to URI of Namespace
|
||||
*/
|
||||
@Override
|
||||
public String getPrefix(String namespaceURI) {
|
||||
return namespaceURI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all prefixes bound to a Namespace URI in the current scope.
|
||||
* @param namespaceURI URI of Namespace to lookup
|
||||
* @return null
|
||||
*/
|
||||
@Override
|
||||
public Iterator getPrefixes(String namespaceURI) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" standalone="yes"?>
|
||||
<widgets>
|
||||
<widget name="a" style="red" quantity="6"/>
|
||||
<widget name="b" style="blue"/>
|
||||
<widget name="c">
|
||||
<style>green</style>
|
||||
</widget>
|
||||
</widgets>
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package javax.xml.xpath.ptests;
|
||||
|
||||
import static jaxp.library.JAXPTestUtilities.FILE_SEP;
|
||||
import static jaxp.library.JAXPTestUtilities.USER_DIR;
|
||||
|
||||
/**
|
||||
* This is the Base test class provide basic support for XPath functional test
|
||||
*/
|
||||
public class XPathTestConst {
|
||||
/**
|
||||
* Package name that separates by slash.
|
||||
*/
|
||||
public static final String PACKAGE_NAME = FILE_SEP +
|
||||
XPathTestConst.class.getPackage().getName().replaceAll("[.]", FILE_SEP);
|
||||
|
||||
/**
|
||||
* Test base directory. Every package has its own test package directory.
|
||||
*/
|
||||
public static final String BASE_DIR
|
||||
= System.getProperty("test.src", USER_DIR).replaceAll("\\" + System.getProperty("file.separator"), "/")
|
||||
+ PACKAGE_NAME + FILE_SEP + "..";
|
||||
|
||||
/**
|
||||
* Source XML file directory.
|
||||
*/
|
||||
public static final String XML_DIR = BASE_DIR + FILE_SEP + "xmlfiles" + FILE_SEP;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user