|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +package xpath; |
| 25 | + |
| 26 | +import java.io.StringReader; |
| 27 | +import javax.xml.parsers.DocumentBuilder; |
| 28 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 29 | +import javax.xml.xpath.XPath; |
| 30 | +import javax.xml.xpath.XPathExpression; |
| 31 | +import javax.xml.xpath.XPathExpressionException; |
| 32 | +import javax.xml.xpath.XPathFactory; |
| 33 | +import org.testng.Assert; |
| 34 | +import org.testng.annotations.DataProvider; |
| 35 | +import org.testng.annotations.Test; |
| 36 | +import org.w3c.dom.Document; |
| 37 | +import org.w3c.dom.Node; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @bug 8284548 |
| 42 | + * @run testng xpath.XPathExceptionTest |
| 43 | + * @summary This is a general test for Exception handling. Additional cases may |
| 44 | + * be added with a bug id in the test cases. |
| 45 | + */ |
| 46 | +public class XPathExceptionTest { |
| 47 | + |
| 48 | + /* |
| 49 | + * DataProvider: invalid XPath expressions |
| 50 | + * Illegal expressions and structures that may escape the validation check. |
| 51 | + */ |
| 52 | + @DataProvider(name = "invalid") |
| 53 | + public Object[][] getInvalid() throws Exception { |
| 54 | + return new Object[][]{ |
| 55 | + // @bug JDK-8284548: expressions ending with relational operators |
| 56 | + // throw StringIndexOutOfBoundsException instead of XPathExpressionException |
| 57 | + {"/a/b/c[@d >"}, |
| 58 | + {"/a/b/c[@d <"}, |
| 59 | + {"/a/b/c[@d >="}, |
| 60 | + {">>"}, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Verifies that the XPath processor throws XPathExpressionException upon |
| 66 | + * encountering illegal XPath expressions. |
| 67 | + * @param invalidExp an illegal XPath expression |
| 68 | + * @throws Exception if the test fails |
| 69 | + */ |
| 70 | + @Test(dataProvider = "invalid") |
| 71 | + public void testIllegalExp(String invalidExp) throws Exception { |
| 72 | + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 73 | + Document doc = builder.parse(new org.xml.sax.InputSource(new StringReader("<A/>"))); |
| 74 | + Assert.assertThrows(XPathExpressionException.class, () -> evaluate(doc, invalidExp)); |
| 75 | + } |
| 76 | + |
| 77 | + private void evaluate(Document doc, String s) throws XPathExpressionException { |
| 78 | + XPath xp = XPathFactory.newInstance().newXPath(); |
| 79 | + XPathExpression xe = xp.compile(s); |
| 80 | + xe.evaluateExpression(doc, Node.class); |
| 81 | + } |
| 82 | +} |
0 commit comments