< prev index next >

test/javax/xml/jaxp/functional/javax/xml/xpath/ptests/XPathTest.java

Print this page


   1 /*
   2  * Copyright (c) 2014, 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 java.io.IOException;
  27 import java.io.InputStream;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.Iterator;
  32 import javax.xml.XMLConstants;
  33 import javax.xml.namespace.NamespaceContext;
  34 import javax.xml.namespace.QName;
  35 import javax.xml.parsers.DocumentBuilderFactory;
  36 import javax.xml.parsers.ParserConfigurationException;
  37 import javax.xml.xpath.XPath;
  38 import static javax.xml.xpath.XPathConstants.BOOLEAN;
  39 import static javax.xml.xpath.XPathConstants.NODE;
  40 import static javax.xml.xpath.XPathConstants.NODESET;
  41 import static javax.xml.xpath.XPathConstants.NUMBER;
  42 import static javax.xml.xpath.XPathConstants.STRING;
  43 import javax.xml.xpath.XPathExpressionException;
  44 import javax.xml.xpath.XPathFactory;
  45 import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR;
  46 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  47 import static org.testng.AssertJUnit.assertEquals;
  48 import static org.testng.AssertJUnit.assertNotNull;
  49 import static org.testng.AssertJUnit.assertNull;
  50 import org.testng.annotations.BeforeTest;
  51 import org.testng.annotations.Test;
  52 import org.w3c.dom.Attr;
  53 import org.w3c.dom.Document;
  54 import org.w3c.dom.NodeList;
  55 import org.xml.sax.InputSource;
  56 import org.xml.sax.SAXException;
  57 
  58 /**
  59  * Class containing the test cases for XPath API.
  60  */
  61 public class XPathTest {
  62     /**
  63      * Document object for testing XML file.
  64      */
  65     private Document document;
  66 
  67     /**
  68      * A XPath for evaluation environment and expressions.
  69      */
  70     private XPath xpath;
  71 
  72     /**
  73      * A QName using default name space.
  74      */
  75     private static final QName TEST_QNAME = new QName(XMLConstants.XML_NS_URI, "");
  76 
  77     /**
  78      * XML File Path.
  79      */
  80     private static final Path XML_PATH = Paths.get(XML_DIR + "widgets.xml");
  81 
  82     /**
  83      * An expression name which locate at "/widgets/widget[@name='a']/@quantity"
  84      */
  85     private static final String EXPRESSION_NAME_A = "/widgets/widget[@name='a']/@quantity";
  86 
  87     /**
  88      * An expression name which locate at "/widgets/widget[@name='b']/@quantity"
  89      */
  90     private static final String EXPRESSION_NAME_B = "/widgets/widget[@name='b']/@quantity";
  91 
  92     /**
  93      * Create Document object and XPath object for every time
  94      * @throws ParserConfigurationException If the factory class cannot be
  95      *                                      loaded, instantiated
  96      * @throws SAXException If any parse errors occur.
  97      * @throws IOException If operation on xml file failed.
  98      */
  99     @BeforeTest
 100     public void setup() throws ParserConfigurationException, SAXException, IOException {

 101         document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile());
 102         xpath = XPathFactory.newInstance().newXPath();
 103     }
 104 
 105     /**
 106      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 107      * item, QName returnType) which return type is String.


 108      */
 109     @Test
 110     public void testCheckXPath01() {
 111         try {
 112             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, STRING), "6");
 113         } catch (XPathExpressionException ex) {
 114             failUnexpected(ex);
 115         }
 116     }
 117 
 118 
 119     /**
 120      * Test for XPath.compile(java.lang.String expression) and then
 121      * evaluate(java.lang.Object item, QName returnType).


 122      */
 123     @Test
 124     public void testCheckXPath02() {
 125         try {
 126             assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document, STRING), "6");
 127         } catch (XPathExpressionException ex) {
 128             failUnexpected(ex);
 129         }
 130     }
 131 
 132     /**
 133      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 134      * item) when the third argument is left off of the XPath.evaluate method,
 135      * all expressions are evaluated to a String value.


 136      */
 137     @Test
 138     public void testCheckXPath03() {
 139         try {
 140             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document), "6");
 141         } catch (XPathExpressionException ex) {
 142             failUnexpected(ex);
 143         }
 144     }
 145 
 146     /**
 147      * Test for XPath.compile(java.lang.String expression). If expression is
 148      * null, should throw NPE.


 149      */
 150     @Test(expectedExceptions = NullPointerException.class)
 151     public void testCheckXPath04() {
 152         try {
 153             xpath.compile(null);
 154         } catch (XPathExpressionException ex) {
 155             failUnexpected(ex);
 156         }
 157     }
 158 
 159     /**
 160      * Test for XPath.compile(java.lang.String expression). If expression cannot
 161      * be compiled junk characters, should throw XPathExpressionException.
 162      *
 163      * @throws XPathExpressionException
 164      */
 165     @Test(expectedExceptions = XPathExpressionException.class)
 166     public void testCheckXPath05() throws XPathExpressionException {
 167         xpath.compile("-*&");
 168     }
 169 
 170     /**
 171      * Test for XPath.compile(java.lang.String expression). If expression is
 172      * blank, should throw XPathExpressionException
 173      *
 174      * @throws XPathExpressionException
 175      */
 176     @Test(expectedExceptions = XPathExpressionException.class)
 177     public void testCheckXPath06() throws XPathExpressionException {
 178         xpath.compile(" ");
 179     }
 180 
 181     /**
 182      * Test for XPath.compile(java.lang.String expression). The expression
 183      * cannot be evaluated as this does not exist.


 184      */
 185     @Test
 186     public void testCheckXPath07() {
 187         try {
 188             assertEquals(xpath.compile(EXPRESSION_NAME_B).evaluate(document, STRING), "");
 189         } catch (XPathExpressionException ex) {
 190             failUnexpected(ex);
 191         }
 192 
 193     }
 194 
 195 
 196     /**
 197      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 198      * item, QName returnType). If String expression is null, should throw NPE


 199      */
 200     @Test(expectedExceptions = NullPointerException.class)
 201     public void testCheckXPath08() {
 202         try {
 203             xpath.evaluate(null, document, STRING);
 204         } catch (XPathExpressionException ex) {
 205             failUnexpected(ex);
 206         }
 207     }
 208 
 209     /**
 210      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 211      * item, QName returnType). If item is null, should throw NPE.


 212      */
 213     @Test(expectedExceptions = NullPointerException.class)
 214     public void testCheckXPath09() {
 215         try {
 216             xpath.evaluate(EXPRESSION_NAME_A, null, STRING);
 217         } catch (XPathExpressionException ex) {
 218             failUnexpected(ex);
 219         }
 220     }
 221 
 222     /**
 223      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 224      * item, QName returnType). If returnType is null, should throw NPE.


 225      */
 226     @Test(expectedExceptions = NullPointerException.class)
 227     public void testCheckXPath10() {
 228         try {
 229             xpath.evaluate(EXPRESSION_NAME_A, document, null);
 230         } catch (XPathExpressionException ex) {
 231             failUnexpected(ex);
 232         }
 233     }
 234 
 235     /**
 236      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 237      * item, QName returnType). If a request is made to evaluate the expression
 238      * in the absence of a context item, simple expressions, such as "1+1", can
 239      * be evaluated.


 240      */
 241     @Test
 242     public void testCheckXPath11() {
 243         try {
 244             assertEquals(xpath.evaluate("1+1", document, STRING), "2");
 245         } catch (XPathExpressionException ex) {
 246             failUnexpected(ex);
 247         }
 248     }
 249 
 250     /**
 251      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 252      * returnType) throws XPathExpressionException if expression is a empty
 253      * string "".
 254      * .
 255      *
 256      * @throws XPathExpressionException
 257      */
 258     @Test(expectedExceptions = XPathExpressionException.class)
 259     public void testCheckXPath12() throws XPathExpressionException {
 260         xpath.evaluate("", document, STRING);
 261     }
 262 
 263     /**
 264      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 265      * returnType) throws IllegalArgumentException if returnType is not one of
 266      * the types defined in XPathConstants.


 267      */
 268     @Test(expectedExceptions = IllegalArgumentException.class)
 269     public void testCheckXPath13() {
 270         try {
 271             xpath.evaluate(EXPRESSION_NAME_A, document, TEST_QNAME);
 272         } catch (XPathExpressionException ex) {
 273             failUnexpected(ex);
 274         }
 275     }
 276 
 277     /**
 278      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 279      * returnType) returns correct boolean value if returnType is Boolean.


 280      */
 281     @Test
 282     public void testCheckXPath14() {
 283         try {
 284             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, BOOLEAN), true);
 285         } catch (XPathExpressionException ex) {
 286             failUnexpected(ex);
 287         }
 288     }
 289 
 290     /**
 291      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 292      * returnType) returns false as  expression is not successful in evaluating
 293      * to any result if returnType is Boolean.


 294      */
 295     @Test
 296     public void testCheckXPath15() {
 297         try {
 298             assertEquals(xpath.evaluate(EXPRESSION_NAME_B, document, BOOLEAN), false);
 299         } catch (XPathExpressionException ex) {
 300             failUnexpected(ex);
 301         }
 302     }
 303 
 304     /**
 305      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 306      * returnType) returns correct number value if return type is Number.


 307      */
 308     @Test
 309     public void testCheckXPath16() {
 310         try {
 311             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, NUMBER), 6d);
 312         } catch (XPathExpressionException ex) {
 313             failUnexpected(ex);
 314         }
 315     }
 316 
 317 
 318     /**
 319      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 320      * returnType) returns correct string value if return type is Node.


 321      */
 322     @Test
 323     public void testCheckXPath17() {
 324         try {
 325             assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A, document, NODE)).getValue(), "6");
 326         } catch (XPathExpressionException ex) {
 327             failUnexpected(ex);
 328         }
 329     }
 330 
 331     /**
 332      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 333      * item, QName returnType). If return type is NodeList,the evaluated value
 334      * equals to "6" as expected.


 335      */
 336     @Test
 337     public void testCheckXPath18() {
 338         try {
 339             NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A, document, NODESET);
 340             assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
 341         } catch (XPathExpressionException ex) {
 342             failUnexpected(ex);
 343         }
 344     }
 345 
 346     /**
 347      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 348      * item). If expression is null, should throw NPE.


 349      */
 350     @Test(expectedExceptions = NullPointerException.class)
 351     public void testCheckXPath19() {
 352         try {
 353             xpath.evaluate(null, document);
 354         } catch (XPathExpressionException ex) {
 355             failUnexpected(ex);
 356         }
 357     }
 358 
 359     /**
 360      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 361      * item). If a request is made to evaluate the expression in the absence of
 362      * a context item, simple expressions, such as "1+1", can be evaluated.


 363      */
 364     @Test
 365     public void testCheckXPath20() {
 366         try {
 367             assertEquals(xpath.evaluate("1+1", document), "2");
 368         } catch (XPathExpressionException ex) {
 369             failUnexpected(ex);
 370         }
 371     }
 372 
 373     /**
 374      * XPath.evaluate(java.lang.String expression, java.lang.Object item) throws
 375      * NPE if InputSource is null.


 376      */
 377     @Test(expectedExceptions = NullPointerException.class)
 378     public void testCheckXPath21() {
 379         try {
 380             xpath.evaluate(EXPRESSION_NAME_A, null);
 381         } catch (XPathExpressionException ex) {
 382             failUnexpected(ex);
 383         }
 384     }
 385 
 386     /**
 387      * XPath.evaluate(java.lang.String expression, InputSource source) return
 388      * correct value by looking for Node.


 389      */
 390     @Test
 391     public void testCheckXPath22() {
 392         try (InputStream is = Files.newInputStream(XML_PATH)) {
 393             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is)), "6");
 394         } catch (XPathExpressionException | IOException ex) {
 395             failUnexpected(ex);
 396         }
 397     }
 398 
 399     /**
 400      * XPath.evaluate(java.lang.String expression, InputSource source) throws
 401      * NPE if InputSource is null.


 402      */
 403     @Test(expectedExceptions = NullPointerException.class)
 404     public void testCheckXPath23() {
 405         try {
 406             xpath.evaluate(EXPRESSION_NAME_A, null);
 407         } catch (XPathExpressionException ex) {
 408             failUnexpected(ex);
 409         }
 410     }
 411 
 412     /**
 413      * XPath.evaluate(java.lang.String expression, InputSource source) throws
 414      * NPE if String expression is null.


 415      */
 416     @Test(expectedExceptions = NullPointerException.class)
 417     public void testCheckXPath24() {
 418         try (InputStream is = Files.newInputStream(XML_PATH)) {
 419             xpath.evaluate(null, new InputSource(is));
 420         } catch (XPathExpressionException | IOException ex) {
 421             failUnexpected(ex);
 422         }
 423     }
 424 
 425     /**
 426      * Test for XPath.evaluate(java.lang.String expression, InputSource source).
 427      * If expression is junk characters, expression cannot be evaluated, should
 428      * throw XPathExpressionException.
 429      *
 430      * @throws XPathExpressionException
 431      */
 432     @Test(expectedExceptions = XPathExpressionException.class)
 433     public void testCheckXPath25() throws XPathExpressionException {
 434         try (InputStream is = Files.newInputStream(XML_PATH)) {
 435             xpath.evaluate("-*&", new InputSource(is));
 436         } catch (IOException ex) {
 437             failUnexpected(ex);
 438         }
 439     }
 440 
 441     /**
 442      * XPath.evaluate(java.lang.String expression, InputSource source) throws
 443      * XPathExpressionException if expression is blank " ".
 444      *
 445      * @throws XPathExpressionException
 446      */
 447     @Test(expectedExceptions = XPathExpressionException.class)
 448     public void testCheckXPath26() throws XPathExpressionException {
 449         try (InputStream is = Files.newInputStream(XML_PATH)) {
 450             xpath.evaluate(" ", new InputSource(is));
 451         } catch (IOException ex) {
 452             failUnexpected(ex);
 453         }
 454     }
 455 
 456     /**
 457      * XPath.evaluate(java.lang.String expression, InputSource source, QName
 458      * returnType) returns correct string value which return type is String.


 459      */
 460     @Test
 461     public void testCheckXPath27() {
 462         try (InputStream is = Files.newInputStream(XML_PATH)) {
 463             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), STRING), "6");
 464         } catch (XPathExpressionException | IOException ex) {
 465             failUnexpected(ex);
 466         }
 467     }
 468 
 469     /**
 470      * XPath.evaluate(java.lang.String expression, InputSource source, QName
 471      * returnType) throws NPE if source is null.


 472      */
 473     @Test(expectedExceptions = NullPointerException.class)
 474     public void testCheckXPath28() {
 475         try {
 476             xpath.evaluate(EXPRESSION_NAME_A, null, STRING);
 477         } catch (XPathExpressionException ex) {
 478             failUnexpected(ex);
 479         }
 480     }
 481 
 482     /**
 483      * XPath.evaluate(java.lang.String expression, InputSource source, QName
 484      * returnType) throws NPE if expression is null.


 485      */
 486     @Test(expectedExceptions = NullPointerException.class)
 487     public void testCheckXPath29() {
 488         try (InputStream is = Files.newInputStream(XML_PATH)) {
 489             xpath.evaluate(null, new InputSource(is), STRING);
 490         } catch (XPathExpressionException | IOException ex) {
 491             failUnexpected(ex);
 492         }
 493     }
 494 
 495     /**
 496      * XPath.evaluate(java.lang.String expression, InputSource source,
 497      * QName returnType) throws NPE if returnType is null .


 498      */
 499     @Test(expectedExceptions = NullPointerException.class)
 500     public void testCheckXPath30() {
 501         try (InputStream is = Files.newInputStream(XML_PATH)) {
 502             xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), null);
 503         } catch (XPathExpressionException | IOException ex) {
 504             failUnexpected(ex);
 505         }
 506     }
 507 
 508     /**
 509      * XPath.evaluate(java.lang.String expression, InputSource source, QName
 510      * returnType) throws XPathExpressionException if expression is junk characters.
 511      *
 512      * @throws XPathExpressionException
 513      */
 514     @Test(expectedExceptions = XPathExpressionException.class)
 515     public void testCheckXPath31() throws XPathExpressionException {
 516         try (InputStream is = Files.newInputStream(XML_PATH)) {
 517             xpath.evaluate("-*&", new InputSource(is), STRING);
 518         } catch (IOException ex) {
 519             failUnexpected(ex);
 520         }
 521     }
 522 
 523     /**
 524      * XPath.evaluate(java.lang.String expression, InputSource source, QName
 525      * returnType) throws XPathExpressionException if expression is blank " ".
 526      *
 527      * @throws XPathExpressionException
 528      */
 529     @Test(expectedExceptions = XPathExpressionException.class)
 530     public void testCheckXPath32() throws XPathExpressionException {
 531         try (InputStream is = Files.newInputStream(XML_PATH)) {
 532             xpath.evaluate(" ", new InputSource(is), STRING);
 533         } catch (IOException ex) {
 534             failUnexpected(ex);
 535         }
 536     }
 537 
 538     /**
 539      * XPath.evaluate(java.lang.String expression, InputSource source,
 540      * QName returnType) throws IllegalArgumentException if returnType is not
 541      * one of the types defined in XPathConstants.


 542      */
 543     @Test(expectedExceptions = IllegalArgumentException.class)
 544     public void testCheckXPath33() {
 545         try (InputStream is = Files.newInputStream(XML_PATH)) {
 546             xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), TEST_QNAME);
 547         } catch (XPathExpressionException | IOException ex) {
 548             failUnexpected(ex);
 549         }
 550     }
 551 
 552     /**
 553      * XPath.evaluate(java.lang.String expression, InputSource source,
 554      * QName returnType) return correct boolean value if return type is Boolean.


 555      */
 556     @Test
 557     public void testCheckXPath34() {
 558         try (InputStream is = Files.newInputStream(XML_PATH)) {
 559             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
 560                 BOOLEAN), true);
 561         } catch (XPathExpressionException | IOException ex) {
 562             failUnexpected(ex);
 563         }
 564     }
 565 
 566     /**
 567      * XPath.evaluate(java.lang.String expression, InputSource source,
 568      * QName returnType) return correct boolean value if return type is Boolean.


 569      */
 570     @Test
 571     public void testCheckXPath35() {
 572         try (InputStream is = Files.newInputStream(XML_PATH)) {
 573             assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is),
 574                 BOOLEAN), false);
 575         } catch (XPathExpressionException | IOException ex) {
 576             failUnexpected(ex);
 577         }
 578     }
 579 
 580     /**
 581      * XPath.evaluate(java.lang.String expression, InputSource source,
 582      * QName returnType) return correct number value if return type is Number.


 583      */
 584     @Test
 585     public void testCheckXPath36() {
 586         try (InputStream is = Files.newInputStream(XML_PATH)) {
 587             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
 588                 NUMBER), 6d);
 589         } catch (XPathExpressionException | IOException ex) {
 590             failUnexpected(ex);
 591         }
 592     }
 593 
 594     /**
 595      * XPath.evaluate(java.lang.String expression, InputSource source,
 596      * QName returnType) return correct string value if return type is Node.


 597      */
 598     @Test
 599     public void testCheckXPath37() {
 600         try (InputStream is = Files.newInputStream(XML_PATH)) {
 601             assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A,
 602                 new InputSource(is), NODE)).getValue(), "6");
 603         } catch (XPathExpressionException | IOException ex) {
 604             failUnexpected(ex);
 605         }
 606     }
 607 
 608     /**
 609      * Test for XPath.evaluate(java.lang.String expression, InputSource source,
 610      * QName returnType) which return type is NodeList.


 611      */
 612     @Test
 613     public void testCheckXPath38() {
 614         try (InputStream is = Files.newInputStream(XML_PATH)) {
 615             NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A,
 616                 new InputSource(is), NODESET);
 617             assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
 618         } catch (XPathExpressionException | IOException ex) {
 619             failUnexpected(ex);
 620         }
 621     }
 622 
 623     /**
 624      * Test for XPath.evaluate(java.lang.String expression, InputSource iSource,
 625      * QName returnType). If return type is Boolean, should return false as
 626      * expression is not successful in evaluating to any result.


 627      */
 628     @Test
 629     public void testCheckXPath52() {
 630         try (InputStream is = Files.newInputStream(XML_PATH)) {
 631             assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is),
 632                 BOOLEAN), false);
 633         } catch (XPathExpressionException | IOException ex) {
 634             failUnexpected(ex);
 635         }
 636     }
 637 
 638     /**
 639      * XPath.evaluate(java.lang.String expression, InputSource iSource, QName
 640      * returnType) returns correct number value which return type is Number.


 641      */
 642     @Test
 643     public void testCheckXPath53() {
 644         try (InputStream is = Files.newInputStream(XML_PATH)) {
 645             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
 646                 NUMBER), 6d);
 647         } catch (XPathExpressionException | IOException ex) {
 648             failUnexpected(ex);
 649         }
 650     }
 651 
 652     /**
 653      * XPath.evaluate(java.lang.String expression, InputSource iSource, QName
 654      * returnType) returns a node value if returnType is Node.


 655      */
 656     @Test
 657     public void testCheckXPath54() {
 658         try (InputStream is = Files.newInputStream(XML_PATH)) {
 659             assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A,
 660                 new InputSource(is), NODE)).getValue(), "6");
 661         } catch (XPathExpressionException | IOException ex) {
 662             failUnexpected(ex);
 663         }
 664     }
 665 
 666     /**
 667      * XPath.evaluate(java.lang.String expression, InputSource iSource, QName
 668      * returnType) returns a node list if returnType is NodeList.


 669      */
 670     @Test
 671     public void testCheckXPath55() {
 672         try (InputStream is = Files.newInputStream(XML_PATH)) {
 673             NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A,
 674                 new InputSource(is), NODESET);
 675             assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
 676         } catch (XPathExpressionException | IOException ex) {
 677             failUnexpected(ex);
 678         }
 679     }
 680 
 681     /**
 682      * Test for XPath.getNamespaceContext() returns the current namespace
 683      * context, null is returned if no namespace context is in effect.
 684      */
 685     @Test
 686     public void testCheckXPath56() {
 687         // CR 6376058 says that an impl will be provided, but by
 688         // default we still return null here
 689         assertNull(xpath.getNamespaceContext());
 690     }
 691 
 692     /**
 693      * Test for XPath.setNamespaceContext(NamespaceContext nsContext) Establish
 694      * a namespace context. Set a valid nsContext and retrieve it using
 695      * getNamespaceContext(), should return the same.
 696      */
 697     @Test


   1 /*
   2  * Copyright (c) 2003, 2015, 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 java.io.FilePermission;
  27 import java.io.InputStream;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.Iterator;
  32 import javax.xml.XMLConstants;
  33 import javax.xml.namespace.NamespaceContext;
  34 import javax.xml.namespace.QName;
  35 import javax.xml.parsers.DocumentBuilderFactory;

  36 import javax.xml.xpath.XPath;
  37 import static javax.xml.xpath.XPathConstants.BOOLEAN;
  38 import static javax.xml.xpath.XPathConstants.NODE;
  39 import static javax.xml.xpath.XPathConstants.NODESET;
  40 import static javax.xml.xpath.XPathConstants.NUMBER;
  41 import static javax.xml.xpath.XPathConstants.STRING;
  42 import javax.xml.xpath.XPathExpressionException;
  43 import javax.xml.xpath.XPathFactory;
  44 import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR;
  45 import jaxp.library.JAXPFileReadOnlyBaseTest;
  46 import static org.testng.AssertJUnit.assertEquals;
  47 import static org.testng.AssertJUnit.assertNotNull;
  48 import static org.testng.AssertJUnit.assertNull;
  49 import org.testng.annotations.BeforeTest;
  50 import org.testng.annotations.Test;
  51 import org.w3c.dom.Attr;
  52 import org.w3c.dom.Document;
  53 import org.w3c.dom.NodeList;
  54 import org.xml.sax.InputSource;

  55 
  56 /**
  57  * Class containing the test cases for XPath API.
  58  */
  59 public class XPathTest extends JAXPFileReadOnlyBaseTest {
  60     /**
  61      * Document object for testing XML file.
  62      */
  63     private Document document;
  64 
  65     /**
  66      * A XPath for evaluation environment and expressions.
  67      */
  68     private XPath xpath;
  69 
  70     /**
  71      * A QName using default name space.
  72      */
  73     private static final QName TEST_QNAME = new QName(XMLConstants.XML_NS_URI, "");
  74 
  75     /**
  76      * XML File Path.
  77      */
  78     private static final Path XML_PATH = Paths.get(XML_DIR + "widgets.xml");
  79 
  80     /**
  81      * An expression name which locate at "/widgets/widget[@name='a']/@quantity"
  82      */
  83     private static final String EXPRESSION_NAME_A = "/widgets/widget[@name='a']/@quantity";
  84 
  85     /**
  86      * An expression name which locate at "/widgets/widget[@name='b']/@quantity"
  87      */
  88     private static final String EXPRESSION_NAME_B = "/widgets/widget[@name='b']/@quantity";
  89 
  90     /**
  91      * Create Document object and XPath object for every time
  92      * @throws Exception If any errors occur.



  93      */
  94     @BeforeTest
  95     public void setup() throws Exception {
  96         setPermissions(new FilePermission(XML_DIR + "-", "read"));
  97         document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile());
  98         xpath = XPathFactory.newInstance().newXPath();
  99     }
 100 
 101     /**
 102      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 103      * item, QName returnType) which return type is String.
 104      * 
 105      * @throws XPathExpressionException If the expression cannot be evaluated.
 106      */
 107     @Test
 108     public void testCheckXPath01() throws XPathExpressionException {

 109         assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, STRING), "6");



 110     }
 111 
 112 
 113     /**
 114      * Test for XPath.compile(java.lang.String expression) and then
 115      * evaluate(java.lang.Object item, QName returnType).
 116      * 
 117      * @throws XPathExpressionException If the expression cannot be evaluated.
 118      */
 119     @Test
 120     public void testCheckXPath02() throws XPathExpressionException {

 121         assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document, STRING), "6");



 122     }
 123 
 124     /**
 125      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 126      * item) when the third argument is left off of the XPath.evaluate method,
 127      * all expressions are evaluated to a String value.
 128      * 
 129      * @throws XPathExpressionException If the expression cannot be evaluated.
 130      */
 131     @Test
 132     public void testCheckXPath03() throws XPathExpressionException {

 133         assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document), "6");



 134     }
 135 
 136     /**
 137      * Test for XPath.compile(java.lang.String expression). If expression is
 138      * null, should throw NPE.
 139      * 
 140      * @throws XPathExpressionException If the expression cannot be evaluated.
 141      */
 142     @Test(expectedExceptions = NullPointerException.class)
 143     public void testCheckXPath04() throws XPathExpressionException {

 144         xpath.compile(null);



 145     }
 146 
 147     /**
 148      * Test for XPath.compile(java.lang.String expression). If expression cannot
 149      * be compiled junk characters, should throw XPathExpressionException.
 150      * 
 151      * @throws XPathExpressionException If the expression cannot be evaluated.
 152      */
 153     @Test(expectedExceptions = XPathExpressionException.class)
 154     public void testCheckXPath05() throws XPathExpressionException {
 155         xpath.compile("-*&");
 156     }
 157 
 158     /**
 159      * Test for XPath.compile(java.lang.String expression). If expression is
 160      * blank, should throw XPathExpressionException
 161      * 
 162      * @throws XPathExpressionException If the expression cannot be evaluated.
 163      */
 164     @Test(expectedExceptions = XPathExpressionException.class)
 165     public void testCheckXPath06() throws XPathExpressionException {
 166         xpath.compile(" ");
 167     }
 168 
 169     /**
 170      * Test for XPath.compile(java.lang.String expression). The expression
 171      * cannot be evaluated as this does not exist.
 172      * 
 173      * @throws XPathExpressionException If the expression cannot be evaluated.
 174      */
 175     @Test
 176     public void testCheckXPath07() throws XPathExpressionException {

 177         assertEquals(xpath.compile(EXPRESSION_NAME_B).evaluate(document, STRING), "");




 178     }
 179 
 180 
 181     /**
 182      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 183      * item, QName returnType). If String expression is null, should throw NPE.
 184      * 
 185      * @throws XPathExpressionException If the expression cannot be evaluated.
 186      */
 187     @Test(expectedExceptions = NullPointerException.class)
 188     public void testCheckXPath08() throws XPathExpressionException {

 189         xpath.evaluate(null, document, STRING);



 190     }
 191 
 192     /**
 193      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 194      * item, QName returnType). If item is null, should throw NPE.
 195      * 
 196      * @throws XPathExpressionException If the expression cannot be evaluated.
 197      */
 198     @Test(expectedExceptions = NullPointerException.class)
 199     public void testCheckXPath09() throws XPathExpressionException {

 200         xpath.evaluate(EXPRESSION_NAME_A, null, STRING);



 201     }
 202 
 203     /**
 204      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 205      * item, QName returnType). If returnType is null, should throw NPE.
 206      * 
 207      * @throws XPathExpressionException If the expression cannot be evaluated.
 208      */
 209     @Test(expectedExceptions = NullPointerException.class)
 210     public void testCheckXPath10() throws XPathExpressionException {

 211         xpath.evaluate(EXPRESSION_NAME_A, document, null);



 212     }
 213 
 214     /**
 215      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 216      * item, QName returnType). If a request is made to evaluate the expression
 217      * in the absence of a context item, simple expressions, such as "1+1", can
 218      * be evaluated.
 219      * 
 220      * @throws XPathExpressionException If the expression cannot be evaluated.
 221      */
 222     @Test
 223     public void testCheckXPath11() throws XPathExpressionException {

 224         assertEquals(xpath.evaluate("1+1", document, STRING), "2");



 225     }
 226 
 227     /**
 228      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 229      * returnType) throws XPathExpressionException if expression is a empty
 230      * string "".

 231      * 
 232      * @throws XPathExpressionException If the expression cannot be evaluated.
 233      */
 234     @Test(expectedExceptions = XPathExpressionException.class)
 235     public void testCheckXPath12() throws XPathExpressionException {
 236         xpath.evaluate("", document, STRING);
 237     }
 238 
 239     /**
 240      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 241      * returnType) throws IllegalArgumentException if returnType is not one of
 242      * the types defined in XPathConstants.
 243      * 
 244      * @throws XPathExpressionException If the expression cannot be evaluated.
 245      */
 246     @Test(expectedExceptions = IllegalArgumentException.class)
 247     public void testCheckXPath13() throws XPathExpressionException {

 248         xpath.evaluate(EXPRESSION_NAME_A, document, TEST_QNAME);



 249     }
 250 
 251     /**
 252      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 253      * returnType) returns correct boolean value if returnType is Boolean.
 254      * 
 255      * @throws XPathExpressionException If the expression cannot be evaluated.
 256      */
 257     @Test
 258     public void testCheckXPath14() throws XPathExpressionException {

 259         assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, BOOLEAN), true);



 260     }
 261 
 262     /**
 263      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 264      * returnType) returns false as  expression is not successful in evaluating
 265      * to any result if returnType is Boolean.
 266      * 
 267      * @throws XPathExpressionException If the expression cannot be evaluated.
 268      */
 269     @Test
 270     public void testCheckXPath15() throws XPathExpressionException {

 271         assertEquals(xpath.evaluate(EXPRESSION_NAME_B, document, BOOLEAN), false);



 272     }
 273 
 274     /**
 275      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 276      * returnType) returns correct number value if return type is Number.
 277      * 
 278      * @throws XPathExpressionException If the expression cannot be evaluated.
 279      */
 280     @Test
 281     public void testCheckXPath16() throws XPathExpressionException {

 282         assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, NUMBER), 6d);



 283     }
 284 
 285 
 286     /**
 287      * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName
 288      * returnType) returns correct string value if return type is Node.
 289      * 
 290      * @throws XPathExpressionException If the expression cannot be evaluated.
 291      */
 292     @Test
 293     public void testCheckXPath17() throws XPathExpressionException {

 294         assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A, document, NODE)).getValue(), "6");



 295     }
 296 
 297     /**
 298      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 299      * item, QName returnType). If return type is NodeList,the evaluated value
 300      * equals to "6" as expected.
 301      * 
 302      * @throws XPathExpressionException If the expression cannot be evaluated.
 303      */
 304     @Test
 305     public void testCheckXPath18() throws XPathExpressionException {

 306         NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A, document, NODESET);
 307         assertEquals(((Attr) nodeList.item(0)).getValue(), "6");



 308     }
 309 
 310     /**
 311      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 312      * item). If expression is null, should throw NPE.
 313      * 
 314      * @throws XPathExpressionException If the expression cannot be evaluated.
 315      */
 316     @Test(expectedExceptions = NullPointerException.class)
 317     public void testCheckXPath19() throws XPathExpressionException {

 318         xpath.evaluate(null, document);



 319     }
 320 
 321     /**
 322      * Test for XPath.evaluate(java.lang.String expression, java.lang.Object
 323      * item). If a request is made to evaluate the expression in the absence of
 324      * a context item, simple expressions, such as "1+1", can be evaluated.
 325      * 
 326      * @throws XPathExpressionException If the expression cannot be evaluated.
 327      */
 328     @Test
 329     public void testCheckXPath20() throws XPathExpressionException {

 330         assertEquals(xpath.evaluate("1+1", document), "2");



 331     }
 332 
 333     /**
 334      * XPath.evaluate(java.lang.String expression, java.lang.Object item) throws
 335      * NPE if InputSource is null.
 336      * 
 337      * @throws XPathExpressionException If the expression cannot be evaluated.
 338      */
 339     @Test(expectedExceptions = NullPointerException.class)
 340     public void testCheckXPath21() throws XPathExpressionException {

 341         xpath.evaluate(EXPRESSION_NAME_A, null);



 342     }
 343 
 344     /**
 345      * XPath.evaluate(java.lang.String expression, InputSource source) return
 346      * correct value by looking for Node.
 347      * 
 348      * @throws Exception If any errors occur.
 349      */
 350     @Test (groups = {"readLocalFiles"})
 351     public void testCheckXPath22() throws Exception {
 352         try (InputStream is = Files.newInputStream(XML_PATH)) {
 353             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is)), "6");


 354         }
 355     }
 356 
 357     /**
 358      * XPath.evaluate(java.lang.String expression, InputSource source) throws
 359      * NPE if InputSource is null.
 360      * 
 361      * @throws XPathExpressionException If the expression cannot be evaluated.
 362      */
 363     @Test(expectedExceptions = NullPointerException.class)
 364     public void testCheckXPath23() throws XPathExpressionException {

 365         xpath.evaluate(EXPRESSION_NAME_A, null);



 366     }
 367 
 368     /**
 369      * XPath.evaluate(java.lang.String expression, InputSource source) throws
 370      * NPE if String expression is null.
 371      * 
 372      * @throws Exception If any errors occur.
 373      */
 374     @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
 375     public void testCheckXPath24() throws Exception {
 376         try (InputStream is = Files.newInputStream(XML_PATH)) {
 377             xpath.evaluate(null, new InputSource(is));


 378         }
 379     }
 380 
 381     /**
 382      * Test for XPath.evaluate(java.lang.String expression, InputSource source).
 383      * If expression is junk characters, expression cannot be evaluated, should
 384      * throw XPathExpressionException.
 385      * 
 386      * @throws Exception If any errors occur.
 387      */
 388     @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
 389     public void testCheckXPath25() throws Exception {
 390         try (InputStream is = Files.newInputStream(XML_PATH)) {
 391             xpath.evaluate("-*&", new InputSource(is));


 392         }
 393     }
 394 
 395     /**
 396      * XPath.evaluate(java.lang.String expression, InputSource source) throws
 397      * XPathExpressionException if expression is blank " ".
 398      * 
 399      * @throws Exception If any errors occur.
 400      */
 401     @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
 402     public void testCheckXPath26() throws Exception {
 403         try (InputStream is = Files.newInputStream(XML_PATH)) {
 404             xpath.evaluate(" ", new InputSource(is));


 405         }
 406     }
 407 
 408     /**
 409      * XPath.evaluate(java.lang.String expression, InputSource source, QName
 410      * returnType) returns correct string value which return type is String.
 411      * 
 412      * @throws Exception If any errors occur.
 413      */
 414     @Test(groups = {"readLocalFiles"})
 415     public void testCheckXPath27() throws Exception {
 416         try (InputStream is = Files.newInputStream(XML_PATH)) {
 417             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), STRING), "6");


 418         }
 419     }
 420 
 421     /**
 422      * XPath.evaluate(java.lang.String expression, InputSource source, QName
 423      * returnType) throws NPE if source is null.
 424      * 
 425      * @throws XPathExpressionException If the expression cannot be evaluated.
 426      */
 427     @Test(expectedExceptions = NullPointerException.class)
 428     public void testCheckXPath28() throws XPathExpressionException {

 429         xpath.evaluate(EXPRESSION_NAME_A, null, STRING);



 430     }
 431 
 432     /**
 433      * XPath.evaluate(java.lang.String expression, InputSource source, QName
 434      * returnType) throws NPE if expression is null.
 435      * 
 436      * @throws Exception If any errors occur.
 437      */
 438     @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
 439     public void testCheckXPath29() throws Exception {
 440         try (InputStream is = Files.newInputStream(XML_PATH)) {
 441             xpath.evaluate(null, new InputSource(is), STRING);


 442         }
 443     }
 444 
 445     /**
 446      * XPath.evaluate(java.lang.String expression, InputSource source,
 447      * QName returnType) throws NPE if returnType is null.
 448      * 
 449      * @throws Exception If any errors occur.
 450      */
 451     @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
 452     public void testCheckXPath30() throws Exception {
 453         try (InputStream is = Files.newInputStream(XML_PATH)) {
 454             xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), null);


 455         }
 456     }
 457 
 458     /**
 459      * XPath.evaluate(java.lang.String expression, InputSource source, QName
 460      * returnType) throws XPathExpressionException if expression is junk characters.
 461      * 
 462      * @throws Exception If any errors occur.
 463      */
 464     @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
 465     public void testCheckXPath31() throws Exception {
 466         try (InputStream is = Files.newInputStream(XML_PATH)) {
 467             xpath.evaluate("-*&", new InputSource(is), STRING);


 468         }
 469     }
 470 
 471     /**
 472      * XPath.evaluate(java.lang.String expression, InputSource source, QName
 473      * returnType) throws XPathExpressionException if expression is blank " ".
 474      * 
 475      * @throws Exception If any errors occur.
 476      */
 477     @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
 478     public void testCheckXPath32() throws Exception {
 479         try (InputStream is = Files.newInputStream(XML_PATH)) {
 480             xpath.evaluate(" ", new InputSource(is), STRING);


 481         }
 482     }
 483 
 484     /**
 485      * XPath.evaluate(java.lang.String expression, InputSource source,
 486      * QName returnType) throws IllegalArgumentException if returnType is not
 487      * one of the types defined in XPathConstants.
 488      * 
 489      * @throws Exception If any errors occur.
 490      */
 491     @Test(groups = {"readLocalFiles"}, expectedExceptions = IllegalArgumentException.class)
 492     public void testCheckXPath33() throws Exception {
 493         try (InputStream is = Files.newInputStream(XML_PATH)) {
 494             xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), TEST_QNAME);


 495         }
 496     }
 497 
 498     /**
 499      * XPath.evaluate(java.lang.String expression, InputSource source,
 500      * QName returnType) return correct boolean value if return type is Boolean.
 501      * 
 502      * @throws Exception If any errors occur.
 503      */
 504     @Test(groups = {"readLocalFiles"})
 505     public void testCheckXPath34() throws Exception {
 506         try (InputStream is = Files.newInputStream(XML_PATH)) {
 507             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
 508                 BOOLEAN), true);


 509         }
 510     }
 511 
 512     /**
 513      * XPath.evaluate(java.lang.String expression, InputSource source,
 514      * QName returnType) return correct boolean value if return type is Boolean.
 515      * 
 516      * @throws Exception If any errors occur.
 517      */
 518     @Test(groups = {"readLocalFiles"})
 519     public void testCheckXPath35() throws Exception {
 520         try (InputStream is = Files.newInputStream(XML_PATH)) {
 521             assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is),
 522                 BOOLEAN), false);


 523         }
 524     }
 525 
 526     /**
 527      * XPath.evaluate(java.lang.String expression, InputSource source,
 528      * QName returnType) return correct number value if return type is Number.
 529      * 
 530      * @throws Exception If any errors occur.
 531      */
 532     @Test(groups = {"readLocalFiles"})
 533     public void testCheckXPath36() throws Exception {
 534         try (InputStream is = Files.newInputStream(XML_PATH)) {
 535             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
 536                 NUMBER), 6d);


 537         }
 538     }
 539 
 540     /**
 541      * XPath.evaluate(java.lang.String expression, InputSource source,
 542      * QName returnType) return correct string value if return type is Node.
 543      * 
 544      * @throws Exception If any errors occur.
 545      */
 546     @Test(groups = {"readLocalFiles"})
 547     public void testCheckXPath37() throws Exception {
 548         try (InputStream is = Files.newInputStream(XML_PATH)) {
 549             assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A,
 550                 new InputSource(is), NODE)).getValue(), "6");


 551         }
 552     }
 553 
 554     /**
 555      * Test for XPath.evaluate(java.lang.String expression, InputSource source,
 556      * QName returnType) which return type is NodeList.
 557      * 
 558      * @throws Exception If any errors occur.
 559      */
 560     @Test(groups = {"readLocalFiles"})
 561     public void testCheckXPath38() throws Exception {
 562         try (InputStream is = Files.newInputStream(XML_PATH)) {
 563             NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A,
 564                 new InputSource(is), NODESET);
 565             assertEquals(((Attr) nodeList.item(0)).getValue(), "6");


 566         }
 567     }
 568 
 569     /**
 570      * Test for XPath.evaluate(java.lang.String expression, InputSource iSource,
 571      * QName returnType). If return type is Boolean, should return false as
 572      * expression is not successful in evaluating to any result.
 573      * 
 574      * @throws Exception If any errors occur.
 575      */
 576     @Test(groups = {"readLocalFiles"})
 577     public void testCheckXPath52() throws Exception {
 578         try (InputStream is = Files.newInputStream(XML_PATH)) {
 579             assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is),
 580                 BOOLEAN), false);


 581         }
 582     }
 583 
 584     /**
 585      * XPath.evaluate(java.lang.String expression, InputSource iSource, QName
 586      * returnType) returns correct number value which return type is Number.
 587      * 
 588      * @throws Exception If any errors occur.
 589      */
 590     @Test(groups = {"readLocalFiles"})
 591     public void testCheckXPath53() throws Exception {
 592         try (InputStream is = Files.newInputStream(XML_PATH)) {
 593             assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is),
 594                 NUMBER), 6d);


 595         }
 596     }
 597 
 598     /**
 599      * XPath.evaluate(java.lang.String expression, InputSource iSource, QName
 600      * returnType) returns a node value if returnType is Node.
 601      * 
 602      * @throws Exception If any errors occur.
 603      */
 604     @Test(groups = {"readLocalFiles"})
 605     public void testCheckXPath54() throws Exception {
 606         try (InputStream is = Files.newInputStream(XML_PATH)) {
 607             assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A,
 608                 new InputSource(is), NODE)).getValue(), "6");


 609         }
 610     }
 611 
 612     /**
 613      * XPath.evaluate(java.lang.String expression, InputSource iSource, QName
 614      * returnType) returns a node list if returnType is NodeList.
 615      * 
 616      * @throws Exception If any errors occur.
 617      */
 618     @Test(groups = {"readLocalFiles"})
 619     public void testCheckXPath55() throws Exception {
 620         try (InputStream is = Files.newInputStream(XML_PATH)) {
 621             NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A,
 622                 new InputSource(is), NODESET);
 623             assertEquals(((Attr) nodeList.item(0)).getValue(), "6");


 624         }
 625     }
 626 
 627     /**
 628      * Test for XPath.getNamespaceContext() returns the current namespace
 629      * context, null is returned if no namespace context is in effect.
 630      */
 631     @Test
 632     public void testCheckXPath56() {
 633         // CR 6376058 says that an impl will be provided, but by
 634         // default we still return null here
 635         assertNull(xpath.getNamespaceContext());
 636     }
 637 
 638     /**
 639      * Test for XPath.setNamespaceContext(NamespaceContext nsContext) Establish
 640      * a namespace context. Set a valid nsContext and retrieve it using
 641      * getNamespaceContext(), should return the same.
 642      */
 643     @Test


< prev index next >