1 /*
   2  * Copyright (c) 2003, 2016, 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 javax.xml.xpath.ptests;
  25 
  26 import static javax.xml.xpath.XPathConstants.BOOLEAN;
  27 import static javax.xml.xpath.XPathConstants.NODE;
  28 import static javax.xml.xpath.XPathConstants.NODESET;
  29 import static javax.xml.xpath.XPathConstants.NUMBER;
  30 import static javax.xml.xpath.XPathConstants.STRING;
  31 import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR;
  32 import static org.testng.Assert.assertEquals;
  33 
  34 import java.io.InputStream;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 
  39 import javax.xml.XMLConstants;
  40 import javax.xml.namespace.QName;
  41 import javax.xml.parsers.DocumentBuilderFactory;
  42 import javax.xml.xpath.XPath;
  43 import javax.xml.xpath.XPathExpressionException;
  44 import javax.xml.xpath.XPathFactory;
  45 
  46 import org.testng.annotations.BeforeTest;
  47 import org.testng.annotations.Listeners;
  48 import org.testng.annotations.Test;
  49 import org.w3c.dom.Attr;
  50 import org.w3c.dom.Document;
  51 import org.w3c.dom.NodeList;
  52 import org.xml.sax.InputSource;
  53 
  54 /**
  55  * Class containing the test cases for XPathExpression API.
  56  */
  57 @Listeners({jaxp.library.FilePolicy.class})
  58 public class XPathExpressionTest {
  59     /**
  60      * Document object for testing XML file.
  61      */
  62     private Document document;
  63 
  64     /**
  65      * A XPath for evaluation environment and expressions.
  66      */
  67     private XPath xpath;
  68 
  69     /**
  70      * A QName using default name space.
  71      */
  72     private static final QName TEST_QNAME = new QName(XMLConstants.XML_NS_URI, "");
  73 
  74     /**
  75      * XML File Path.
  76      */
  77     private static final Path XML_PATH = Paths.get(XML_DIR + "widgets.xml");
  78 
  79     /**
  80      * An expression name which locate at "/widgets/widget[@name='a']/@quantity"
  81      */
  82     private static final String EXPRESSION_NAME_A = "/widgets/widget[@name='a']/@quantity";
  83 
  84     /**
  85      * An expression name which locate at "/widgets/widget[@name='b']/@quantity"
  86      */
  87     private static final String EXPRESSION_NAME_B = "/widgets/widget[@name='b']/@quantity";
  88 
  89     /**
  90      * Create Document object and XPath object for every time
  91      * @throws Exception If any errors occur.
  92      */
  93     @BeforeTest
  94     public void setup() throws Exception {
  95         document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile());
  96         xpath = XPathFactory.newInstance().newXPath();
  97     }
  98 
  99     /**
 100      * Test for evaluate(java.lang.Object item,QName returnType)throws
 101      * XPathExpressionException.
 102      *
 103      * @throws XPathExpressionException If the expression cannot be evaluated.
 104      */
 105     @Test
 106     public void testCheckXPathExpression01() throws XPathExpressionException {
 107         assertEquals(xpath.compile(EXPRESSION_NAME_A).
 108                 evaluate(document, STRING), "6");
 109     }
 110 
 111     /**
 112      * evaluate(java.lang.Object item,QName returnType) throws NPE if input
 113      * source is null.
 114      *
 115      * @throws XPathExpressionException If the expression cannot be evaluated.
 116      */
 117     @Test(expectedExceptions = NullPointerException.class)
 118     public void testCheckXPathExpression02() throws XPathExpressionException {
 119         xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING);
 120     }
 121 
 122     /**
 123      * evaluate(java.lang.Object item,QName returnType) throws NPE if returnType
 124      * is null.
 125      *
 126      * @throws XPathExpressionException If the expression cannot be evaluated.
 127      */
 128     @Test(expectedExceptions = NullPointerException.class)
 129     public void testCheckXPathExpression03() throws XPathExpressionException {
 130         xpath.compile(EXPRESSION_NAME_A).evaluate(document, null);
 131     }
 132 
 133     /**
 134      * Test for method evaluate(java.lang.Object item,QName returnType).If a
 135      * request is made to evaluate the expression in the absence of a context
 136      * item, simple expressions, such as "1+1", can be evaluated.
 137      *
 138      * @throws XPathExpressionException If the expression cannot be evaluated.
 139      */
 140     @Test
 141     public void testCheckXPathExpression04() throws XPathExpressionException {
 142         assertEquals(xpath.compile("1+1").evaluate(document, STRING), "2");
 143     }
 144 
 145     /**
 146      * evaluate(java.lang.Object item,QName returnType) throws IAE If returnType
 147      * is not one of the types defined in XPathConstants.
 148      *
 149      * @throws XPathExpressionException If the expression cannot be evaluated.
 150      */
 151     @Test(expectedExceptions = IllegalArgumentException.class)
 152     public void testCheckXPathExpression05() throws XPathExpressionException {
 153         xpath.compile(EXPRESSION_NAME_A).evaluate(document, TEST_QNAME);
 154     }
 155 
 156     /**
 157      * evaluate(java.lang.Object item,QName returnType) return correct boolean
 158      * value if returnType is Boolean.
 159      *
 160      * @throws XPathExpressionException If the expression cannot be evaluated.
 161      */
 162     @Test
 163     public void testCheckXPathExpression06() throws XPathExpressionException {
 164         assertEquals(xpath.compile(EXPRESSION_NAME_A).
 165                 evaluate(document, BOOLEAN), true);
 166     }
 167 
 168     /**
 169      * evaluate(java.lang.Object item,QName returnType) return correct boolean
 170      * value if returnType is Boolean.
 171      *
 172      * @throws XPathExpressionException If the expression cannot be evaluated.
 173      */
 174     @Test
 175     public void testCheckXPathExpression07() throws XPathExpressionException {
 176         assertEquals(xpath.compile(EXPRESSION_NAME_B).
 177             evaluate(document, BOOLEAN), false);
 178     }
 179 
 180     /**
 181      * evaluate(java.lang.Object item,QName returnType) return correct number
 182      * value when return type is Double.
 183      *
 184      * @throws XPathExpressionException If the expression cannot be evaluated.
 185      */
 186     @Test
 187     public void testCheckXPathExpression08() throws XPathExpressionException {
 188         assertEquals(xpath.compile(EXPRESSION_NAME_A).
 189             evaluate(document, NUMBER), 6d);
 190     }
 191 
 192     /**
 193      * evaluate(java.lang.Object item,QName returnType) evaluate an attribute
 194      * value which returnType is Node.
 195      *
 196      * @throws XPathExpressionException If the expression cannot be evaluated.
 197      */
 198     @Test
 199     public void testCheckXPathExpression09() throws XPathExpressionException {
 200         Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A).
 201                 evaluate(document, NODE);
 202         assertEquals(attr.getValue(), "6");
 203     }
 204 
 205     /**
 206      * evaluate(java.lang.Object item,QName returnType) evaluate an attribute
 207      * value which returnType is NodeList.
 208      *
 209      * @throws XPathExpressionException If the expression cannot be evaluated.
 210      */
 211     @Test
 212     public void testCheckXPathExpression10() throws XPathExpressionException {
 213         NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A).
 214                 evaluate(document, NODESET);
 215         Attr attr = (Attr) nodeList.item(0);
 216         assertEquals(attr.getValue(), "6");
 217     }
 218 
 219     /**
 220      * Test for evaluate(java.lang.Object item) when returnType is left off of
 221      * the XPath.evaluate method, all expressions are evaluated to a String
 222      * value.
 223      *
 224      * @throws XPathExpressionException If the expression cannot be evaluated.
 225      */
 226     @Test
 227     public void testCheckXPathExpression11() throws XPathExpressionException {
 228         assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document), "6");
 229     }
 230 
 231     /**
 232      * evaluate(java.lang.Object item) throws NPE if expression is null.
 233      *
 234      * @throws XPathExpressionException If the expression cannot be evaluated.
 235      */
 236     @Test(expectedExceptions = NullPointerException.class)
 237     public void testCheckXPathExpression12() throws XPathExpressionException {
 238         xpath.compile(null).evaluate(document);
 239     }
 240 
 241     /**
 242      * evaluate(java.lang.Object item) when a request is made to evaluate the
 243      * expression in the absence of a context item, simple expressions, such as
 244      * "1+1", can be evaluated.
 245      *
 246      * @throws XPathExpressionException If the expression cannot be evaluated.
 247      */
 248     @Test
 249     public void testCheckXPathExpression13() throws XPathExpressionException {
 250         assertEquals(xpath.compile("1+1").evaluate(document), "2");
 251     }
 252 
 253     /**
 254      * evaluate(java.lang.Object item) throws NPE if document is null.
 255      *
 256      * @throws XPathExpressionException If the expression cannot be evaluated.
 257      */
 258     @Test(expectedExceptions = NullPointerException.class)
 259     public void testCheckXPathExpression14() throws XPathExpressionException {
 260         xpath.compile(EXPRESSION_NAME_A).evaluate(null);
 261     }
 262 
 263     /**
 264      * valuate(InputSource source) return a string value if return type is
 265      * String.
 266      *
 267      * @throws Exception If any errors occur.
 268      */
 269     @Test 
 270     public void testCheckXPathExpression15() throws Exception {
 271         try (InputStream is = Files.newInputStream(XML_PATH)) {
 272             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 273                     evaluate(new InputSource(is)), "6");
 274         }
 275     }
 276 
 277     /**
 278      * evaluate(InputSource source) throws NPE if input source is null.
 279      *
 280      * @throws XPathExpressionException If the expression cannot be evaluated.
 281      */
 282     @Test(expectedExceptions = NullPointerException.class)
 283     public void testCheckXPathExpression16() throws XPathExpressionException {
 284         xpath.compile(EXPRESSION_NAME_A).evaluate(null);
 285     }
 286 
 287     /**
 288      * evaluate(InputSource source) throws NPE if expression is null.
 289      *
 290      * @throws Exception If any errors occur.
 291      */
 292     @Test(expectedExceptions = NullPointerException.class)
 293     public void testCheckXPathExpression17() throws Exception {
 294         try (InputStream is = Files.newInputStream(XML_PATH)) {
 295             xpath.compile(null).evaluate(new InputSource(is));
 296         }
 297     }
 298 
 299     /**
 300      * evaluate(InputSource source) throws XPathExpressionException if
 301      * returnType is String junk characters.
 302      *
 303      * @throws Exception If any errors occur.
 304      */
 305     @Test(expectedExceptions = XPathExpressionException.class)
 306     public void testCheckXPathExpression18() throws Exception {
 307         try (InputStream is = Files.newInputStream(XML_PATH)) {
 308             xpath.compile("-*&").evaluate(new InputSource(is));
 309         }
 310     }
 311 
 312     /**
 313      * evaluate(InputSource source) throws XPathExpressionException if
 314      * expression is a blank string " ".
 315      *
 316      * @throws Exception If any errors occur.
 317      */
 318     @Test(expectedExceptions = XPathExpressionException.class)
 319     public void testCheckXPathExpression19() throws Exception {
 320         try (InputStream is = Files.newInputStream(XML_PATH)) {
 321             xpath.compile(" ").evaluate(new InputSource(is));
 322         }
 323     }
 324 
 325     /**
 326      * Test for evaluate(InputSource source,QName returnType) returns a string
 327      * value if returnType is String.
 328      *
 329      * @throws Exception If any errors occur.
 330      */
 331     @Test
 332     public void testCheckXPathExpression20() throws Exception {
 333         try (InputStream is = Files.newInputStream(XML_PATH)) {
 334             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 335                 evaluate(new InputSource(is), STRING), "6");
 336         }
 337     }
 338 
 339     /**
 340      * evaluate(InputSource source,QName returnType) throws NPE if source is
 341      * null.
 342      *
 343      * @throws XPathExpressionException If the expression cannot be evaluated.
 344      */
 345     @Test(expectedExceptions = NullPointerException.class)
 346     public void testCheckXPathExpression21() throws XPathExpressionException {
 347         xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING);
 348     }
 349 
 350     /**
 351      * evaluate(InputSource source,QName returnType) throws NPE if expression is
 352      * null.
 353      *
 354      * @throws Exception If any errors occur.
 355      */
 356     @Test(expectedExceptions = NullPointerException.class)
 357     public void testCheckXPathExpression22() throws Exception {
 358         try (InputStream is = Files.newInputStream(XML_PATH)) {
 359             xpath.compile(null).evaluate(new InputSource(is), STRING);
 360         }
 361     }
 362 
 363     /**
 364      * evaluate(InputSource source,QName returnType) throws NPE if returnType is
 365      * null.
 366      *
 367      * @throws Exception If any errors occur.
 368      */
 369     @Test(expectedExceptions = NullPointerException.class)
 370     public void testCheckXPathExpression23() throws Exception {
 371         try (InputStream is = Files.newInputStream(XML_PATH)) {
 372             xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), null);
 373         }
 374     }
 375 
 376     /**
 377      * evaluate(InputSource source,QName returnType) throws
 378      * XPathExpressionException if expression is junk characters.
 379      *
 380      * @throws Exception If any errors occur.
 381      */
 382     @Test(expectedExceptions = XPathExpressionException.class)
 383     public void testCheckXPathExpression24() throws Exception {
 384         try (InputStream is = Files.newInputStream(XML_PATH)) {
 385             xpath.compile("-*&").evaluate(new InputSource(is), STRING);
 386         }
 387     }
 388 
 389     /**
 390      * evaluate(InputSource source,QName returnType) throws
 391      * XPathExpressionException if expression is blank " ".
 392      *
 393      * @throws Exception If any errors occur.
 394      */
 395     @Test(expectedExceptions = XPathExpressionException.class)
 396     public void testCheckXPathExpression25() throws Exception {
 397         try (InputStream is = Files.newInputStream(XML_PATH)) {
 398             xpath.compile(" ").evaluate(new InputSource(is), STRING);
 399         }
 400     }
 401 
 402     /**
 403      * evaluate(InputSource source,QName returnType) throws
 404      * IllegalArgumentException if returnType is not one of the types defined
 405      * in XPathConstants.
 406      *
 407      * @throws Exception If any errors occur.
 408      */
 409     @Test(expectedExceptions = IllegalArgumentException.class)
 410     public void testCheckXPathExpression26() throws Exception {
 411         try (InputStream is = Files.newInputStream(XML_PATH)) {
 412             xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), TEST_QNAME);
 413         }
 414     }
 415 
 416     /**
 417      * evaluate(InputSource source,QName returnType) return a correct boolean
 418      * value if returnType is Boolean.
 419      *
 420      * @throws Exception If any errors occur.
 421      */
 422     @Test
 423     public void testCheckXPathExpression27() throws Exception {
 424         try (InputStream is = Files.newInputStream(XML_PATH)) {
 425             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 426                 evaluate(new InputSource(is), BOOLEAN), true);
 427         }
 428     }
 429 
 430     /**
 431      * evaluate(InputSource source,QName returnType) return a correct boolean
 432      * value if returnType is Boolean.
 433      *
 434      * @throws Exception If any errors occur.
 435      */
 436     @Test
 437     public void testCheckXPathExpression28() throws Exception {
 438         try (InputStream is = Files.newInputStream(XML_PATH)) {
 439             assertEquals(xpath.compile(EXPRESSION_NAME_B).
 440                 evaluate(new InputSource(is), BOOLEAN), false);
 441         }
 442     }
 443 
 444     /**
 445      * evaluate(InputSource source,QName returnType) return a correct number
 446      * value if returnType is Number.
 447      *
 448      * @throws Exception If any errors occur.
 449      */
 450     @Test
 451     public void testCheckXPathExpression29() throws Exception {
 452         try (InputStream is = Files.newInputStream(XML_PATH)) {
 453             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 454                 evaluate(new InputSource(is), NUMBER), 6d);
 455         }
 456     }
 457 
 458     /**
 459      * Test for evaluate(InputSource source,QName returnType) returns a node if
 460      * returnType is Node.
 461      *
 462      * @throws Exception If any errors occur.
 463      */
 464     @Test
 465     public void testCheckXPathExpression30() throws Exception {
 466         try (InputStream is = Files.newInputStream(XML_PATH)) {
 467             Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A).
 468                 evaluate(new InputSource(is), NODE);
 469             assertEquals(attr.getValue(), "6");
 470         }
 471     }
 472 
 473     /**
 474      * Test for evaluate(InputSource source,QName returnType) return a node list
 475      * if returnType is NodeList.
 476      *
 477      * @throws Exception If any errors occur.
 478      */
 479     @Test
 480     public void testCheckXPathExpression31() throws Exception {
 481         try (InputStream is = Files.newInputStream(XML_PATH)) {
 482             NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A).
 483                 evaluate(new InputSource(is), NODESET);
 484             assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
 485         }
 486     }
 487 }