--- /dev/null 2014-07-04 18:21:30.080776318 +0800 +++ new/test/javax/xml/jaxp/functional/javax/xml/xpath/ptests/XPath01.java 2014-07-25 20:53:00.543407604 +0800 @@ -0,0 +1,968 @@ +/* + * 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 org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.testutils.TestBase; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionResolver; +import javax.xml.xpath.XPathVariableResolver; + +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +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 XPath01 extends TestBase { + + private Document document = null; + private static final javax.xml.namespace.QName qname = new QName(XMLConstants.XML_NS_URI, ""); + + @BeforeTest + public void setup() throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = dbf.newDocumentBuilder(); + document = builder.parse(new File(TestUtils.XML_DIR + "widgets.xml")); + } + + /** + * Test for XPath.evaluate(java.lang.String expression, java.lang.Object + * item, QName returnType) which return type is String. + */ + @Test + public void testCheckXPath01() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + String quantity = (String) xpath.evaluate(expression, document, XPathConstants.STRING); + assertEquals(quantity, "6"); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.compile(java.lang.String expression) and then + * evaluate(java.lang.Object item, QName returnType). + */ + @Test + public void testCheckXPath02() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + XPathExpression xpathExpression = xpath.compile(expression); + String quantity = (String) xpathExpression.evaluate(document, XPathConstants.STRING); + assertEquals(quantity, "6"); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + String quantity = xpath.evaluate(expression, document); + assertEquals(quantity, "6"); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.compile(java.lang.String expression). If expression is + * null, should throw NPE. + */ + @Test(expectedExceptions = NullPointerException.class) + public void testCheckXPath04() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.compile(null); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "-*&"; + xpath.compile(expression); + } + + /** + * 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='b']/@quantity"; + XPathExpression xpathExpression = xpath.compile(expression); + String quantity = (String) xpathExpression.evaluate(document, XPathConstants.STRING); + assertEquals(quantity, ""); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.evaluate(null, document, XPathConstants.STRING); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + xpath.evaluate(expression, null, XPathConstants.STRING); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + xpath.evaluate(expression, document, null); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "1+1"; + String quantity = (String) xpath.evaluate(expression, document, XPathConstants.STRING); + assertEquals(quantity, "2"); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, java.lang.Object + * item, QName returnType). If expression is a blank string "", should throw + * XPathExpressionException. + * + * @throws XPathExpressionException + */ + @Test(expectedExceptions = XPathExpressionException.class) + public void testCheckXPath12() throws XPathExpressionException { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.evaluate("", document, XPathConstants.STRING); + } + + /** + * Test for XPath.evaluate(java.lang.String expression, java.lang.Object + * item, QName returnType). If returnType is not one of the types defined in + * XPathConstants, should throw IllegalArgumentException. + */ + @Test(expectedExceptions = IllegalArgumentException.class) + public void testCheckXPath13() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + xpath.evaluate(expression, document, qname); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, java.lang.Object + * item, QName returnType). if returnType is Boolean, should return true. + */ + @Test + public void testCheckXPath14() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + Boolean result = (Boolean) xpath.evaluate(expression, document, XPathConstants.BOOLEAN); + assertTrue(result); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, java.lang.Object + * item, QName returnType). if returnType is Boolean, should return false as + * expression is not successful in evaluating to any result. + */ + @Test + public void testCheckXPath15() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='b']/@quantity"; + Boolean result = (Boolean) xpath.evaluate(expression, document, XPathConstants.BOOLEAN); + assertFalse(result); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, java.lang.Object + * item, QName returnType). If return type is Double, the evaluated value + * equals to 6.0 as expected. + */ + @Test + public void testCheckXPath16() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + Double quantity = (Double) xpath.evaluate(expression, document, XPathConstants.NUMBER); + assertEquals(quantity, 6d); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, java.lang.Object + * item, QName returnType). If return type is Node, the evaluated value + * equals to "6" as expected. + */ + @Test + public void testCheckXPath17() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + Node quantity = (Node) xpath.evaluate(expression, document, XPathConstants.NODE); + Attr attr = (Attr) quantity; + String result = attr.getValue(); + assertEquals(result, "6"); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + NodeList nodeList = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET); + Node quantity = nodeList.item(0); + Attr attr = (Attr) quantity; + String result = attr.getValue(); + assertEquals(result, "6"); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.evaluate(null, document); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * 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 { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "1+1"; + String quantity = xpath.evaluate(expression, document); + assertEquals(quantity, "2"); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, java.lang.Object + * item). If InputSource is null, should throw NPE. + */ + @Test(expectedExceptions = NullPointerException.class) + public void testCheckXPath21() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + xpath.evaluate(expression, null); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source) + */ + @Test + public void testCheckXPath22() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + String quantity = xpath.evaluate(expression, iSource); + assertEquals(quantity, "6"); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source). + * If InputSource is null, should throw NPE. + */ + @Test(expectedExceptions = NullPointerException.class) + public void testCheckXPath23() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + xpath.evaluate(expression, null); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source). + * If String expression is null, should throw NPE. + */ + @Test(expectedExceptions = NullPointerException.class) + public void testCheckXPath24() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.evaluate(null, iSource); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * 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 (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.evaluate("-*&", iSource); + } catch (IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source). + * If expression is blank " ", should throw XPathExpressionException. + * + * @throws XPathExpressionException + */ + @Test(expectedExceptions = XPathExpressionException.class) + public void testCheckXPath26() throws XPathExpressionException { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.evaluate(" ", iSource); + } catch (IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType) which return type is String. The evaluated value equals + * to "6" as expected. + */ + @Test + public void testCheckXPath27() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + String quantity = (String) xpath.evaluate(expression, iSource, XPathConstants.STRING); + assertEquals(quantity, "6"); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType). If source is null, should throw NPE. + */ + @Test(expectedExceptions = NullPointerException.class) + public void testCheckXPath28() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + xpath.evaluate(expression, null, XPathConstants.STRING); + } catch (XPathExpressionException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType). If expression is null, should throw NPE. + */ + @Test(expectedExceptions = NullPointerException.class) + public void testCheckXPath29() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.evaluate(null, iSource, XPathConstants.STRING); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType). If returnType is null, should throw NPE. + */ + @Test(expectedExceptions = NullPointerException.class) + public void testCheckXPath30() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + xpath.evaluate(expression, iSource, null); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType). If expression is junk characters, should throw + * XPathExpressionException. + * + * @throws XPathExpressionException + */ + @Test(expectedExceptions = XPathExpressionException.class) + public void testCheckXPath31() throws XPathExpressionException { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.evaluate("-*&", iSource, XPathConstants.STRING); + } catch (IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType). If expression is blank " ", should throw + * XPathExpressionException. + * + * @throws XPathExpressionException + */ + @Test(expectedExceptions = XPathExpressionException.class) + public void testCheckXPath32() throws XPathExpressionException { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = " "; + xpath.evaluate(expression, iSource, XPathConstants.STRING); + } catch (IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType). If returnType is not one of the types defined in + * XPathConstants, should throw IllegalArgumentException. + */ + @Test(expectedExceptions = IllegalArgumentException.class) + public void testCheckXPath33() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + xpath.evaluate(expression, iSource, qname); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType). if return type is Boolean, should return true. + */ + @Test + public void testCheckXPath34() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + Boolean result = (Boolean) xpath.evaluate(expression, iSource, XPathConstants.BOOLEAN); + assertTrue(result); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType). If returnType is Boolean, should return false as + * expression is not successful in evaluating to any result. + */ + @Test + public void testCheckXPath35() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='b']/@quantity"; + Boolean result = (Boolean) xpath.evaluate(expression, iSource, XPathConstants.BOOLEAN); + assertFalse(result); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType). The evaluated value equals to 6.0 as expected. + */ + @Test + public void testCheckXPath36() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + Double quantity = (Double) xpath.evaluate(expression, iSource, XPathConstants.NUMBER); + assertEquals(quantity, 6d); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType) which returnType is Node. + */ + @Test + public void testCheckXPath37() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + Node quantity = (Node) xpath.evaluate(expression, iSource, XPathConstants.NODE); + Attr attr = (Attr) quantity; + String result = attr.getValue(); + assertEquals(result, "6"); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource source, + * QName returnType) which return type is NodeList. + */ + @Test + public void testCheckXPath38() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + NodeList nodeList = (NodeList) xpath.evaluate(expression, iSource, XPathConstants.NODESET); + Node quantity = nodeList.item(0); + Attr attr = (Attr) quantity; + String result = attr.getValue(); + assertEquals(result, "6"); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * 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 (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='b']/@quantity"; + Boolean result = (Boolean) xpath.evaluate(expression, iSource, XPathConstants.BOOLEAN); + assertFalse(result); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource iSource, + * QName returnType) which return type is Double. + */ + @Test + public void testCheckXPath53() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + Double quantity = (Double) xpath.evaluate(expression, iSource, XPathConstants.NUMBER); + assertEquals(quantity, 6d); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource iSource, + * QName returnType) which returnType is Node. + */ + @Test + public void testCheckXPath54() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + Node quantity = (Node) xpath.evaluate(expression, iSource, XPathConstants.NODE); + Attr attr = (Attr) quantity; + String result = attr.getValue(); + assertEquals(result, "6"); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.evaluate(java.lang.String expression, InputSource iSource, + * QName returnType) which returnType is NodeList. + */ + @Test + public void testCheckXPath55() { + try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { + InputSource iSource = new InputSource(fis); + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + String expression = "/widgets/widget[@name='a']/@quantity"; + NodeList nodeList = (NodeList) xpath.evaluate(expression, iSource, XPathConstants.NODESET); + Node quantity = nodeList.item(0); + Attr attr = (Attr) quantity; + String result = attr.getValue(); + assertEquals(result, "6"); + } catch (XPathExpressionException | IOException e) { + fail(e); + } + } + + /** + * Test for XPath.getNamespaceContext() returns the current namespace + * context, null is returned if no namespace context is in effect. + */ + @Test + public void testCheckXPath56() { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + NamespaceContext namespaceContext = xpath.getNamespaceContext(); + // CR 6376058 says that an impl will be provided, but by + // default we still return null here + assertNull(namespaceContext); + } + + /** + * 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() { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + MyNamespaceContext myNamespaceContext = new MyNamespaceContext(); + xpath.setNamespaceContext(myNamespaceContext); + NamespaceContext returned = xpath.getNamespaceContext(); + assertEquals(returned, 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() { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + 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() { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + XPathFunctionResolver xpfResolver = xpath.getXPathFunctionResolver(); + assertNull(xpfResolver); + } + + /** + * Test for XPath.setXPathFunctionResolver(XPathFunctionResolver resolver). + * Set a valid resolver and retrieve it using getXPathFunctionResolver(), + * should return the same. + */ + @Test + public void testCheckXPath60() { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + MyXPathFunctionResolver myXPFResolver = new MyXPathFunctionResolver(); + xpath.setXPathFunctionResolver(myXPFResolver); + XPathFunctionResolver returned = xpath.getXPathFunctionResolver(); + assertEquals(returned, myXPFResolver); + } + + /** + * Test for XPath.setXPathFunctionResolver(XPathFunctionResolver resolver). + * set resolver as null, should throw NPE. + */ + @Test(expectedExceptions = NullPointerException.class) + public void testCheckXPath61() { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + 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() { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + XPathVariableResolver xpvResolver = xpath.getXPathVariableResolver(); + assertNull(xpvResolver); + } + + /** + * Test for XPath.setXPathVariableResolver(XPathVariableResolver resolver). + * Set a valid resolver and retrieve it using getXPathVariableResolver(), + * should return the same. + */ + @Test + public void testCheckXPath63() { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + MyXPathVariableResolver myXPVResolver = new MyXPathVariableResolver(); + xpath.setXPathVariableResolver(myXPVResolver); + XPathVariableResolver returned = xpath.getXPathVariableResolver(); + assertEquals(returned, myXPVResolver); + } + + /** + * Test for XPath.setXPathVariableResolver(XPathVariableResolver resolver). + * Set resolver as null, should throw NPE. + */ + @Test(expectedExceptions = NullPointerException.class) + public void testCheckXPath64() { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.setXPathVariableResolver(null); + } + + /** + * Customized NamespaceContext used for test. + */ + private class MyNamespaceContext implements NamespaceContext { + public java.lang.String getNamespaceURI(java.lang.String prefix) { + System.out.println("XPath01 - in MyNamespaceContext - getNamespaceURI "); + return prefix; + } + + public java.lang.String getPrefix(java.lang.String namespaceURI) { + System.out.println("XPath01 - in MyNamespaceContext - getPrefix "); + return namespaceURI; + } + + public java.util.Iterator getPrefixes(java.lang.String namespaceURI) { + System.out.println("XPath01 - in MyNamespaceContext - getPrefixes "); + return null; + } + } + + /** + * Customized XPathFunctionResolver used for test. + */ + private class MyXPathFunctionResolver implements XPathFunctionResolver { + public XPathFunction resolveFunction(QName functionName, int arity) { + System.out.println("XPath01 - in MyXPathFunctionResolver - resolveFunction "); + return null; + } + } + + /** + * Customized XPathVariableResolver used for test. + */ + private class MyXPathVariableResolver implements XPathVariableResolver { + public java.lang.Object resolveVariable(QName variableName) { + System.out.println("XPath01 - in MyXPathVariableResolver - resolveVariable "); + return null; + } + } +}