/* * 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.assertTrue; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.xml.XMLConstants; 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 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 XPathExpression API. */ public class XPathExpression01 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 evaluate(java.lang.Object item,QName returnType)throws * XPathExpressionException. */ @Test public void testCheckXPathExpression01() { 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 evaluate(java.lang.Object item,QName returnType)throws * XPathExpressionException. If input source is null, should throw NPE. */ @Test(expectedExceptions = NullPointerException.class) public void testCheckXPathExpression02() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='a']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(null, XPathConstants.STRING); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item,QName returnType) which * returnType is null, should throw NPE. */ @Test(expectedExceptions = NullPointerException.class) public void testCheckXPathExpression03() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='a']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(document, null); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item,QName returnType)throws * XPathExpressionException.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 { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "1+1"; XPathExpression xpathExpression = xpath.compile(expression); String quantity = (String) xpathExpression.evaluate(document, XPathConstants.STRING); assertEquals(quantity, "2"); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item,QName returnType)throws * XPathExpressionException. If returnType is not one of the types defined * in XPathConstants, should throw IllegalArgumentException. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testCheckXPathExpression05() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='a']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(document, qname); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item,QName returnType)throws * XPathExpressionException. If returnType is Boolean, should return true. */ @Test public void testCheckXPathExpression06() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='a']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); Boolean result = (Boolean) xpathExpression.evaluate(document, XPathConstants.BOOLEAN); assertTrue(result); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item,QName returnType)throws * XPathExpressionException. If returnType is Boolean, should return false * as expression is not successful in evaluating to any result. */ @Test public void testCheckXPathExpression07() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='b']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); Boolean result = (Boolean) xpathExpression.evaluate(document, XPathConstants.BOOLEAN); assertFalse(result); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item,QName returnType)throws * XPathExpressionException which return type is Double. */ @Test public void testCheckXPathExpression08() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='a']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); Double quantity = (Double) xpathExpression.evaluate(document, XPathConstants.NUMBER); assertEquals(quantity, 6d); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item,QName returnType)throws * XPathExpressionException which returnType is Node. */ @Test public void testCheckXPathExpression09() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='a']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); Node quantity = (Node) xpathExpression.evaluate(document, XPathConstants.NODE); Attr attr = (Attr) quantity; String result = attr.getValue(); assertEquals(result, "6"); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item,QName returnType)throws * XPathExpressionException which returnType is NodeList. */ @Test public void testCheckXPathExpression10() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='a']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); NodeList nodeList = (NodeList) xpathExpression.evaluate(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 evaluate(java.lang.Object item)throws XPathExpressionException. * When returnType is left off of the XPath.evaluate method, all expressions * are evaluated to a String value. */ @Test public void testCheckXPathExpression11() { 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); assertEquals(quantity, "6"); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item)throws XPathExpressionException. * If expression is null, should throw NPE. */ @Test(expectedExceptions = NullPointerException.class) public void testCheckXPathExpression12() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); XPathExpression xpathExpression = xpath.compile(null); xpathExpression.evaluate(document); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item)throws XPathExpressionException. * 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 testCheckXPathExpression13() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "1+1"; XPathExpression xpathExpression = xpath.compile(expression); String quantity = (String) xpathExpression.evaluate(document); assertEquals(quantity, "2"); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(java.lang.Object item)throws XPathExpressionException. * If document is null, should throw NPE. */ @Test(expectedExceptions = NullPointerException.class) public void testCheckXPathExpression14() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='a']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(null); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(InputSource source)throws XPathExpressionException * which return type is String. */ @Test public void testCheckXPathExpression15() { try (FileInputStream fis = new FileInputStream(new File(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"; XPathExpression xpathExpression = xpath.compile(expression); String quantity = (String) xpathExpression.evaluate(iSource); assertEquals(quantity, "6"); } catch (XPathExpressionException | IOException e) { fail(e); } } /** * Test for evaluate(InputSource source)throws XPathExpressionException. - * If input source is null, should throw NPE. */ @Test(expectedExceptions = NullPointerException.class) public void testCheckXPathExpression16() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='a']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(null); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(InputSource source)throws XPathExpressionException. - * if expression is null, should throw NPE. */ @Test(expectedExceptions = NullPointerException.class) public void testCheckXPathExpression17() { try (FileInputStream fis = new FileInputStream(new File(TestUtils.XML_DIR, "widgets.xml"))) { InputSource iSource = new InputSource(fis); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); XPathExpression xpathExpression = xpath.compile(null); xpathExpression.evaluate(iSource); } catch (XPathExpressionException | IOException e) { fail(e); } } /** * Test for evaluate(InputSource source)throws XPathExpressionException. - * If returnType is String junk characters, should throw * XPathExpressionException. * * @throws XPathExpressionException */ @Test(expectedExceptions = XPathExpressionException.class) public void testCheckXPathExpression18() throws XPathExpressionException { try (FileInputStream fis = new FileInputStream(new File(TestUtils.XML_DIR, "widgets.xml"))) { InputSource iSource = new InputSource(fis); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "-*&"; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(iSource); } catch (IOException e) { fail(e); } } /** * Test for evaluate(InputSource source)throws XPathExpressionException * which expression is blank - " ", should throw XPathExpressionException. * * @throws XPathExpressionException */ @Test(expectedExceptions = XPathExpressionException.class) public void testCheckXPathExpression19() throws XPathExpressionException { try (FileInputStream fis = new FileInputStream(new File(TestUtils.XML_DIR, "widgets.xml"))) { InputSource iSource = new InputSource(fis); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); XPathExpression xpathExpression = xpath.compile(" "); xpathExpression.evaluate(iSource); } catch (IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException which returnType is String. */ @Test public void testCheckXPathExpression20() { try (FileInputStream fis = new FileInputStream(new File(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"; XPathExpression xpathExpression = xpath.compile(expression); String quantity = (String) xpathExpression.evaluate(iSource, XPathConstants.STRING); assertEquals(quantity, "6"); } catch (XPathExpressionException | IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException source is null, should throw NPE. */ @Test(expectedExceptions = NullPointerException.class) public void testCheckXPathExpression21() { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "/widgets/widget[@name='a']/@quantity"; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(null, XPathConstants.STRING); } catch (XPathExpressionException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException. If expression is null, should throw NPE. */ @Test(expectedExceptions = NullPointerException.class) public void testCheckXPathExpression22() { try (FileInputStream fis = new FileInputStream(new File(TestUtils.XML_DIR, "widgets.xml"))) { InputSource iSource = new InputSource(fis); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); XPathExpression xpathExpression = xpath.compile(null); xpathExpression.evaluate(iSource, XPathConstants.STRING); } catch (XPathExpressionException | IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException. If returnType is null, should throw NPE. */ @Test(expectedExceptions = NullPointerException.class) public void testCheckXPathExpression23() { try (FileInputStream fis = new FileInputStream(new File(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"; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(iSource, null); } catch (XPathExpressionException | IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException. If expression is junk characters, should throw * XPathExpressionException. * * @throws XPathExpressionException */ @Test(expectedExceptions = XPathExpressionException.class) public void testCheckXPathExpression24() throws XPathExpressionException { try (FileInputStream fis = new FileInputStream(new File(TestUtils.XML_DIR, "widgets.xml"))) { InputSource iSource = new InputSource(fis); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = "-*&"; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(iSource, XPathConstants.STRING); } catch (IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException. If expression is blank " ", should throw * XPathExpressionException. * * @throws XPathExpressionException */ @Test(expectedExceptions = XPathExpressionException.class) public void testCheckXPathExpression25() throws XPathExpressionException { try (FileInputStream fis = new FileInputStream(new File(TestUtils.XML_DIR, "widgets.xml"))) { InputSource iSource = new InputSource(fis); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String expression = " "; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(iSource, XPathConstants.STRING); } catch (IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException. If returnType is not one of the types defined * in XPathConstants, should throw IllegalArgumentException. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testCheckXPathExpression26() { try (FileInputStream fis = new FileInputStream(new File(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"; XPathExpression xpathExpression = xpath.compile(expression); xpathExpression.evaluate(iSource, qname); } catch (XPathExpressionException | IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException. If returnType is Boolean, should return true. */ @Test public void testCheckXPathExpression27() { try (FileInputStream fis = new FileInputStream(new File(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"; XPathExpression xpathExpression = xpath.compile(expression); Boolean result = (Boolean) xpathExpression.evaluate(iSource, XPathConstants.BOOLEAN); assertTrue(result); } catch (XPathExpressionException | IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException. If returnType is Boolean, should return false * as expression is not successful in evaluating to any result. */ @Test public void testCheckXPathExpression28() { try (FileInputStream fis = new FileInputStream(new File(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"; XPathExpression xpathExpression = xpath.compile(expression); Boolean result = (Boolean) xpathExpression.evaluate(iSource, XPathConstants.BOOLEAN); assertFalse(result); } catch (XPathExpressionException | IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException which returnType is Double. */ @Test public void testCheckXPathExpression29() { try (FileInputStream fis = new FileInputStream(new File(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"; XPathExpression xpathExpression = xpath.compile(expression); Double quantity = (Double) xpathExpression.evaluate(iSource, XPathConstants.NUMBER); assertEquals(quantity, 6d); } catch (XPathExpressionException | IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException which returnType is Node. */ @Test public void testCheckXPathExpression30() { try (FileInputStream fis = new FileInputStream(new File(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"; XPathExpression xpathExpression = xpath.compile(expression); Node quantity = (Node) xpathExpression.evaluate(iSource, XPathConstants.NODE); Attr attr = (Attr) quantity; String result = attr.getValue(); assertEquals(result, "6"); } catch (XPathExpressionException | IOException e) { fail(e); } } /** * Test for evaluate(InputSource source,QName returnType)throws * XPathExpressionException which returnType is NodeList. */ @Test public void testCheckXPathExpression31() { try (FileInputStream fis = new FileInputStream(new File(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"; XPathExpression xpathExpression = xpath.compile(expression); NodeList nodeList = (NodeList) xpathExpression.evaluate(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); } } }