--- /dev/null 2014-07-04 18:21:30.080776318 +0800 +++ new/test/javax/xml/jaxp/functional/TEST.properties 2014-07-25 20:52:46.989386419 +0800 @@ -0,0 +1,2 @@ +# jaxp test uses TestNG +TestNG.dirs = . --- /dev/null 2014-07-04 18:21:30.080776318 +0800 +++ new/test/javax/xml/jaxp/functional/javax/xml/testutils/TestBase.java 2014-07-25 20:52:51.050328865 +0800 @@ -0,0 +1,104 @@ +/* + * 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.testutils; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; + +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; + +/** + * Base class of jaxp test, also contains utility methods. + */ +public abstract class TestBase { + public static final String ERROR_MSG_HEADER = "Unexcepted exception thrown!"; + public static final String FILE_SEP = System.getProperty("file.separator"); + public static final String USER_DIR = System.getProperty("user.dir", "."); + public static final String SRC_DIR = System.getProperty("test.src", "."); + public static final String TEMP_DIR = System.getProperty("java.io.tmpdir", "."); + + /** + * Compare the contents of 2 files. If equal return true, otherwise, return + * false. + */ + protected boolean compareWithGold(File goldfile, File outputfile) throws IOException { + return Files.readAllLines(goldfile.toPath()).equals(Files.readAllLines(outputfile.toPath())); + } + + /** + * Print log before test starts + */ + @BeforeMethod + protected void startTest(Method methodName) { + System.out.println("\n\n****************************"); + System.out.println("Starting test: " + methodName); + } + + /** + * Print log after test ends + */ + @AfterMethod + protected void endTest(Method methodName) { + System.out.println("Ending test: " + methodName); + System.out.println("\n\n****************************"); + } + + /** + * This method is called by child tests to prepare xml files for testing. + */ + protected void setup(String srcDirName, String dstDirName, String... fileNames) throws IOException { + for (String fileName : fileNames) { + File src = new File(srcDirName, fileName); + File dstDir = new File(dstDirName); + if (!dstDir.exists()) { + dstDir.mkdirs(); + } + File dst = new File(dstDirName, fileName); + Files.copy(src.toPath(), dst.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + } + + /** + * Assert test failed with customized message. + * + * @param message + * @param e + */ + public static void fail(String message, Throwable e) { + Assert.fail(message, e); + } + + /** + * Assert test failed with default message. + * + * @param e + */ + public static void fail(Throwable e) { + fail(ERROR_MSG_HEADER, e); + } +} --- /dev/null 2014-07-04 18:21:30.080776318 +0800 +++ new/test/javax/xml/jaxp/functional/javax/xml/xpath/ptests/TestUtils.java 2014-07-25 20:52:55.090354210 +0800 @@ -0,0 +1,33 @@ +/* + * 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.testutils.TestBase; + +/** + * Utility interface which includes final variables about xml and golden file + * directories. + */ +interface TestUtils { + String XML_DIR = TestBase.SRC_DIR + TestBase.FILE_SEP + "javax/xml/xpath/xmlfiles/"; +} --- /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; + } + } +} --- /dev/null 2014-07-04 18:21:30.080776318 +0800 +++ new/test/javax/xml/jaxp/functional/javax/xml/xpath/ptests/XPathExpression01.java 2014-07-25 20:53:04.960348132 +0800 @@ -0,0 +1,652 @@ +/* + * 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); + } + } +} --- /dev/null 2014-07-04 18:21:30.080776318 +0800 +++ new/test/javax/xml/jaxp/functional/javax/xml/xpath/ptests/XPathFactory01.java 2014-07-25 20:53:09.272388084 +0800 @@ -0,0 +1,133 @@ +/* + * 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.assertNotNull; + +import javax.xml.testutils.TestBase; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathFactory; +import javax.xml.xpath.XPathFactoryConfigurationException; + +import org.testng.annotations.Test; + +/** + * Class containing the test cases for XPathFactory API. + */ +public class XPathFactory01 extends TestBase { + + /** + * Test for constructor - XPathFactory.newInstance(). + */ + @Test + public void testCheckXPathFactory01() { + assertNotNull(XPathFactory.newInstance()); + } + + /** + * Test for constructor - XPathFactory.newInstance(String uri) uri is null, + * should throw NPE. + */ + @Test(expectedExceptions = NullPointerException.class) + private void testCheckXPathFactory02() { + try { + XPathFactory.newInstance(null); + } catch (XPathFactoryConfigurationException e) { + fail(e); + } + } + + /** + * Test for constructor - XPathFactory.newInstance(String uri) uri is + * invalid , 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 vaild + * url - "http://java.sun.com/jaxp/xpath/dom". + */ + @Test + public void testCheckXPathFactory04() { + try { + assertNotNull(XPathFactory.newInstance("http://java.sun.com/jaxp/xpath/dom")); + } catch (XPathFactoryConfigurationException e) { + fail(e); + } + } + + /** + * 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("http://java.sun.com/jaxp/xpath/dom1"); + } + + /** + * Test for constructor - XPathFactory.newInstance() and creating XPath with + * newXPath(). + */ + @Test + public void testCheckXPathFactory06() { + XPathFactory xpathFactory = XPathFactory.newInstance(); + assertNotNull(xpathFactory.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 { + XPathFactory xpathFactory = XPathFactory.newInstance("http://java.sun.com/jaxp/xpath/dom"); + assertNotNull(xpathFactory.newXPath()); + } catch (XPathFactoryConfigurationException e) { + fail(e); + } + } + + /** + * Test for constructor - XPathFactory.newInstance(String uri) with vaild + * uri - DOM_OBJECT_MODEL.toString(). + */ + @Test + public void testCheckXPathFactory08() { + try { + assertNotNull(XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL)); + } catch (XPathFactoryConfigurationException e) { + fail(e); + } + } +} --- /dev/null 2014-07-04 18:21:30.080776318 +0800 +++ new/test/javax/xml/jaxp/functional/javax/xml/xpath/ptests/XPathFunctionResolver01.java 2014-07-25 20:53:13.654372618 +0800 @@ -0,0 +1,64 @@ +/* + * 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 javax.xml.testutils.TestBase; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import javax.xml.xpath.XPathFunctionResolver; + +import org.testng.annotations.Test; + +/** + * Class containing the test cases for XPathFunctionResolver. + */ +public class XPathFunctionResolver01 extends TestBase { + + /** + * Test for resolveFunction(QName functionName,int arity). If functionName + * or arity is null, then a NullPointerException is thrown. + * + */ + @Test + public void testCheckXPathFunctionResolver01() { + try { + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + XPathFunctionResolver xpathFunctionResolver = xpath.getXPathFunctionResolver(); + if (xpathFunctionResolver == null) { + System.out.println("xpathFunctionResolver is null , so creating and setting new MyXPathFunctionResolver "); + xpath.setXPathFunctionResolver((q, i) -> { + return null; + }); + } + String result = xpath.evaluate("round(1.7)", (Object) null); + assertEquals(result, "2"); + } catch (XPathExpressionException e) { + fail(e); + } + } +} --- /dev/null 2014-07-04 18:21:30.080776318 +0800 +++ new/test/javax/xml/jaxp/functional/javax/xml/xpath/xmlfiles/widgets.xml 2014-07-25 20:53:18.373346960 +0800 @@ -0,0 +1,11 @@ + + + + + + + + + + +