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.FilePermission; 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.nio.file.Files; 30 import java.nio.file.Path; 31 import java.nio.file.Paths; 32 import java.util.Iterator; 33 import javax.xml.XMLConstants; 34 import javax.xml.namespace.NamespaceContext; 35 import javax.xml.namespace.QName; 36 import javax.xml.parsers.DocumentBuilderFactory; 37 import javax.xml.parsers.ParserConfigurationException; 38 import javax.xml.xpath.XPath; 39 import static javax.xml.xpath.XPathConstants.BOOLEAN; 40 import static javax.xml.xpath.XPathConstants.NODE; 41 import static javax.xml.xpath.XPathConstants.NODESET; 42 import static javax.xml.xpath.XPathConstants.NUMBER; 43 import static javax.xml.xpath.XPathConstants.STRING; 44 import javax.xml.xpath.XPathExpressionException; 45 import javax.xml.xpath.XPathFactory; 46 import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR; 47 import jaxp.library.JAXPBaseTest; 48 import static org.testng.AssertJUnit.assertEquals; 49 import static org.testng.AssertJUnit.assertNotNull; 50 import static org.testng.AssertJUnit.assertNull; 51 import org.testng.annotations.AfterGroups; 52 import org.testng.annotations.BeforeGroups; 53 import org.testng.annotations.BeforeTest; 54 import org.testng.annotations.Test; 55 import org.w3c.dom.Attr; 56 import org.w3c.dom.Document; 57 import org.w3c.dom.NodeList; 58 import org.xml.sax.InputSource; 59 import org.xml.sax.SAXException; 60 61 /** 62 * Class containing the test cases for XPath API. 63 */ 64 public class XPathTest extends JAXPBaseTest { 65 /** 66 * Document object for testing XML file. 67 */ 68 private Document document; 69 70 /** 71 * A XPath for evaluation environment and expressions. 72 */ 73 private XPath xpath; 74 75 /** 76 * A QName using default name space. 77 */ 78 private static final QName TEST_QNAME = new QName(XMLConstants.XML_NS_URI, ""); 79 80 /** 81 * XML File Path. 82 */ 83 private static final Path XML_PATH = Paths.get(XML_DIR + "widgets.xml"); 84 85 /** 86 * An expression name which locate at "/widgets/widget[@name='a']/@quantity" 87 */ 88 private static final String EXPRESSION_NAME_A = "/widgets/widget[@name='a']/@quantity"; 89 90 /** 91 * An expression name which locate at "/widgets/widget[@name='b']/@quantity" 92 */ 93 private static final String EXPRESSION_NAME_B = "/widgets/widget[@name='b']/@quantity"; 94 95 /** 96 * Create Document object and XPath object for every time 97 * @throws ParserConfigurationException If the factory class cannot be 98 * loaded, instantiated 99 * @throws SAXException If any parse errors occur. 100 * @throws IOException If operation on XML file failed. 101 */ 102 @BeforeTest 103 public void setup() throws ParserConfigurationException, SAXException, IOException { 104 setPermissions(new FilePermission(XML_DIR + "-", "read")); 105 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile()); 106 xpath = XPathFactory.newInstance().newXPath(); 107 } 108 109 /** 110 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 111 * item, QName returnType) which return type is String. 112 * 113 * @throws XPathExpressionException If the expression cannot be evaluated. 114 */ 115 @Test 116 public void testCheckXPath01() throws XPathExpressionException { 117 assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, STRING), "6"); 118 } 119 120 121 /** 122 * Test for XPath.compile(java.lang.String expression) and then 123 * evaluate(java.lang.Object item, QName returnType). 124 * 125 * @throws XPathExpressionException If the expression cannot be evaluated. 126 */ 127 @Test 128 public void testCheckXPath02() throws XPathExpressionException { 129 assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document, STRING), "6"); 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 * @throws XPathExpressionException If the expression cannot be evaluated. 138 */ 139 @Test 140 public void testCheckXPath03() throws XPathExpressionException { 141 assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document), "6"); 142 } 143 144 /** 145 * Test for XPath.compile(java.lang.String expression). If expression is 146 * null, should throw NPE. 147 * 148 * @throws XPathExpressionException If the expression cannot be evaluated. 149 */ 150 @Test(expectedExceptions = NullPointerException.class) 151 public void testCheckXPath04() throws XPathExpressionException { 152 xpath.compile(null); 153 } 154 155 /** 156 * Test for XPath.compile(java.lang.String expression). If expression cannot 157 * be compiled junk characters, should throw XPathExpressionException. 158 * 159 * @throws XPathExpressionException If the expression cannot be evaluated. 160 */ 161 @Test(expectedExceptions = XPathExpressionException.class) 162 public void testCheckXPath05() throws XPathExpressionException { 163 xpath.compile("-*&"); 164 } 165 166 /** 167 * Test for XPath.compile(java.lang.String expression). If expression is 168 * blank, should throw XPathExpressionException 169 * 170 * @throws XPathExpressionException If the expression cannot be evaluated. 171 */ 172 @Test(expectedExceptions = XPathExpressionException.class) 173 public void testCheckXPath06() throws XPathExpressionException { 174 xpath.compile(" "); 175 } 176 177 /** 178 * Test for XPath.compile(java.lang.String expression). The expression 179 * cannot be evaluated as this does not exist. 180 * 181 * @throws XPathExpressionException If the expression cannot be evaluated. 182 */ 183 @Test 184 public void testCheckXPath07() throws XPathExpressionException { 185 assertEquals(xpath.compile(EXPRESSION_NAME_B).evaluate(document, STRING), ""); 186 } 187 188 189 /** 190 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 191 * item, QName returnType). If String expression is null, should throw NPE. 192 * 193 * @throws XPathExpressionException If the expression cannot be evaluated. 194 */ 195 @Test(expectedExceptions = NullPointerException.class) 196 public void testCheckXPath08() throws XPathExpressionException { 197 xpath.evaluate(null, document, STRING); 198 } 199 200 /** 201 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 202 * item, QName returnType). If item is null, should throw NPE. 203 * 204 * @throws XPathExpressionException If the expression cannot be evaluated. 205 */ 206 @Test(expectedExceptions = NullPointerException.class) 207 public void testCheckXPath09() throws XPathExpressionException { 208 xpath.evaluate(EXPRESSION_NAME_A, null, STRING); 209 } 210 211 /** 212 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 213 * item, QName returnType). If returnType is null, should throw NPE. 214 * 215 * @throws XPathExpressionException If the expression cannot be evaluated. 216 */ 217 @Test(expectedExceptions = NullPointerException.class) 218 public void testCheckXPath10() throws XPathExpressionException { 219 xpath.evaluate(EXPRESSION_NAME_A, document, null); 220 } 221 222 /** 223 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 224 * item, QName returnType). If a request is made to evaluate the expression 225 * in the absence of a context item, simple expressions, such as "1+1", can 226 * be evaluated. 227 * 228 * @throws XPathExpressionException If the expression cannot be evaluated. 229 */ 230 @Test 231 public void testCheckXPath11() throws XPathExpressionException { 232 assertEquals(xpath.evaluate("1+1", document, STRING), "2"); 233 } 234 235 /** 236 * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName 237 * returnType) throws XPathExpressionException if expression is a empty 238 * string "". 239 * 240 * @throws XPathExpressionException If the expression cannot be evaluated. 241 */ 242 @Test(expectedExceptions = XPathExpressionException.class) 243 public void testCheckXPath12() throws XPathExpressionException { 244 xpath.evaluate("", document, STRING); 245 } 246 247 /** 248 * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName 249 * returnType) throws IllegalArgumentException if returnType is not one of 250 * the types defined in XPathConstants. 251 * 252 * @throws XPathExpressionException If the expression cannot be evaluated. 253 */ 254 @Test(expectedExceptions = IllegalArgumentException.class) 255 public void testCheckXPath13() throws XPathExpressionException { 256 xpath.evaluate(EXPRESSION_NAME_A, document, TEST_QNAME); 257 } 258 259 /** 260 * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName 261 * returnType) returns correct boolean value if returnType is Boolean. 262 * 263 * @throws XPathExpressionException If the expression cannot be evaluated. 264 */ 265 @Test 266 public void testCheckXPath14() throws XPathExpressionException { 267 assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, BOOLEAN), true); 268 } 269 270 /** 271 * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName 272 * returnType) returns false as expression is not successful in evaluating 273 * to any result if returnType is Boolean. 274 * 275 * @throws XPathExpressionException If the expression cannot be evaluated. 276 */ 277 @Test 278 public void testCheckXPath15() throws XPathExpressionException { 279 assertEquals(xpath.evaluate(EXPRESSION_NAME_B, document, BOOLEAN), false); 280 } 281 282 /** 283 * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName 284 * returnType) returns correct number value if return type is Number. 285 * 286 * @throws XPathExpressionException If the expression cannot be evaluated. 287 */ 288 @Test 289 public void testCheckXPath16() throws XPathExpressionException { 290 assertEquals(xpath.evaluate(EXPRESSION_NAME_A, document, NUMBER), 6d); 291 } 292 293 294 /** 295 * XPath.evaluate(java.lang.String expression, java.lang.Object item, QName 296 * returnType) returns correct string value if return type is Node. 297 * 298 * @throws XPathExpressionException If the expression cannot be evaluated. 299 */ 300 @Test 301 public void testCheckXPath17() throws XPathExpressionException { 302 assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A, document, NODE)).getValue(), "6"); 303 } 304 305 /** 306 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 307 * item, QName returnType). If return type is NodeList,the evaluated value 308 * equals to "6" as expected. 309 * 310 * @throws XPathExpressionException If the expression cannot be evaluated. 311 */ 312 @Test 313 public void testCheckXPath18() throws XPathExpressionException { 314 NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A, document, NODESET); 315 assertEquals(((Attr) nodeList.item(0)).getValue(), "6"); 316 } 317 318 /** 319 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 320 * item). If expression is null, should throw NPE. 321 * 322 * @throws XPathExpressionException If the expression cannot be evaluated. 323 */ 324 @Test(expectedExceptions = NullPointerException.class) 325 public void testCheckXPath19() throws XPathExpressionException { 326 xpath.evaluate(null, document); 327 } 328 329 /** 330 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 331 * item). If a request is made to evaluate the expression in the absence of 332 * a context item, simple expressions, such as "1+1", can be evaluated. 333 * 334 * @throws XPathExpressionException If the expression cannot be evaluated. 335 */ 336 @Test 337 public void testCheckXPath20() throws XPathExpressionException { 338 assertEquals(xpath.evaluate("1+1", document), "2"); 339 } 340 341 /** 342 * XPath.evaluate(java.lang.String expression, java.lang.Object item) throws 343 * NPE if InputSource is null. 344 * 345 * @throws XPathExpressionException If the expression cannot be evaluated. 346 */ 347 @Test(expectedExceptions = NullPointerException.class) 348 public void testCheckXPath21() throws XPathExpressionException { 349 xpath.evaluate(EXPRESSION_NAME_A, null); 350 } 351 352 /** 353 * Save system property for restoring. 354 */ 355 @BeforeGroups (groups = {"readLocalFiles"}) 356 public void setFilePermissions() { 357 setPermissions(new FilePermission(XML_PATH.toFile().toString(), "read")); 358 } 359 360 /** 361 * Restore the system property. 362 */ 363 @AfterGroups (groups = {"readLocalFiles"}) 364 public void restoreFilePermissions() { 365 setPermissions(); 366 } 367 368 /** 369 * XPath.evaluate(java.lang.String expression, InputSource source) return 370 * correct value by looking for Node. 371 * 372 * @throws XPathExpressionException If the expression cannot be evaluated. 373 * @throws IOException if the file exists but is a directory rather than 374 * a regular file, does not exist but cannot be created, or cannot 375 * be opened for any other reason. 376 */ 377 @Test (groups = {"readLocalFiles"}) 378 public void testCheckXPath22() throws XPathExpressionException, IOException { 379 try (InputStream is = Files.newInputStream(XML_PATH)) { 380 assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is)), "6"); 381 } 382 } 383 384 /** 385 * XPath.evaluate(java.lang.String expression, InputSource source) throws 386 * NPE if InputSource is null. 387 * 388 * @throws XPathExpressionException If the expression cannot be evaluated. 389 */ 390 @Test(expectedExceptions = NullPointerException.class) 391 public void testCheckXPath23() throws XPathExpressionException { 392 xpath.evaluate(EXPRESSION_NAME_A, null); 393 } 394 395 /** 396 * XPath.evaluate(java.lang.String expression, InputSource source) throws 397 * NPE if String expression is null. 398 * 399 * @throws XPathExpressionException If the expression cannot be evaluated. 400 * @throws IOException if the file exists but is a directory rather than 401 * a regular file, does not exist but cannot be created, or cannot 402 * be opened for any other reason. 403 */ 404 @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class) 405 public void testCheckXPath24() throws IOException, XPathExpressionException { 406 try (InputStream is = Files.newInputStream(XML_PATH)) { 407 xpath.evaluate(null, new InputSource(is)); 408 } 409 } 410 411 /** 412 * Test for XPath.evaluate(java.lang.String expression, InputSource source). 413 * If expression is junk characters, expression cannot be evaluated, should 414 * throw XPathExpressionException. 415 * 416 * @throws XPathExpressionException If the expression cannot be evaluated. 417 * @throws IOException if the file exists but is a directory rather than 418 * a regular file, does not exist but cannot be created, or cannot 419 * be opened for any other reason. 420 */ 421 @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class) 422 public void testCheckXPath25() throws XPathExpressionException, IOException { 423 try (InputStream is = Files.newInputStream(XML_PATH)) { 424 xpath.evaluate("-*&", new InputSource(is)); 425 } 426 } 427 428 /** 429 * XPath.evaluate(java.lang.String expression, InputSource source) throws 430 * XPathExpressionException if expression is blank " ". 431 * 432 * @throws XPathExpressionException If the expression cannot be evaluated. 433 * @throws IOException if the file exists but is a directory rather than 434 * a regular file, does not exist but cannot be created, or cannot 435 * be opened for any other reason. 436 */ 437 @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class) 438 public void testCheckXPath26() throws XPathExpressionException, IOException { 439 try (InputStream is = Files.newInputStream(XML_PATH)) { 440 xpath.evaluate(" ", new InputSource(is)); 441 } 442 } 443 444 /** 445 * XPath.evaluate(java.lang.String expression, InputSource source, QName 446 * returnType) returns correct string value which return type is String. 447 * 448 * @throws XPathExpressionException If the expression cannot be evaluated. 449 * @throws IOException if the file exists but is a directory rather than 450 * a regular file, does not exist but cannot be created, or cannot 451 * be opened for any other reason. 452 */ 453 @Test(groups = {"readLocalFiles"}) 454 public void testCheckXPath27() throws IOException, XPathExpressionException { 455 try (InputStream is = Files.newInputStream(XML_PATH)) { 456 assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), STRING), "6"); 457 } 458 } 459 460 /** 461 * XPath.evaluate(java.lang.String expression, InputSource source, QName 462 * returnType) throws NPE if source is null. 463 * 464 * @throws XPathExpressionException If the expression cannot be evaluated. 465 */ 466 @Test(expectedExceptions = NullPointerException.class) 467 public void testCheckXPath28() throws XPathExpressionException { 468 xpath.evaluate(EXPRESSION_NAME_A, null, STRING); 469 } 470 471 /** 472 * XPath.evaluate(java.lang.String expression, InputSource source, QName 473 * returnType) throws NPE if expression is null. 474 * 475 * @throws XPathExpressionException If the expression cannot be evaluated. 476 * @throws IOException if the file exists but is a directory rather than 477 * a regular file, does not exist but cannot be created, or cannot 478 * be opened for any other reason. 479 */ 480 @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class) 481 public void testCheckXPath29() throws IOException, XPathExpressionException { 482 try (InputStream is = Files.newInputStream(XML_PATH)) { 483 xpath.evaluate(null, new InputSource(is), STRING); 484 } 485 } 486 487 /** 488 * XPath.evaluate(java.lang.String expression, InputSource source, 489 * QName returnType) throws NPE if returnType is null. 490 * 491 * @throws XPathExpressionException If the expression cannot be evaluated. 492 * @throws IOException if the file exists but is a directory rather than 493 * a regular file, does not exist but cannot be created, or cannot 494 * be opened for any other reason. 495 */ 496 @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class) 497 public void testCheckXPath30() throws IOException, XPathExpressionException { 498 try (InputStream is = Files.newInputStream(XML_PATH)) { 499 xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), null); 500 } 501 } 502 503 /** 504 * XPath.evaluate(java.lang.String expression, InputSource source, QName 505 * returnType) throws XPathExpressionException if expression is junk characters. 506 * 507 * @throws XPathExpressionException If the expression cannot be evaluated. 508 * @throws IOException if the file exists but is a directory rather than 509 * a regular file, does not exist but cannot be created, or cannot 510 * be opened for any other reason. 511 */ 512 @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class) 513 public void testCheckXPath31() throws XPathExpressionException, IOException { 514 try (InputStream is = Files.newInputStream(XML_PATH)) { 515 xpath.evaluate("-*&", new InputSource(is), STRING); 516 } 517 } 518 519 /** 520 * XPath.evaluate(java.lang.String expression, InputSource source, QName 521 * returnType) throws XPathExpressionException if expression is blank " ". 522 * 523 * @throws XPathExpressionException If the expression cannot be evaluated. 524 * @throws IOException if the file exists but is a directory rather than 525 * a regular file, does not exist but cannot be created, or cannot 526 * be opened for any other reason. 527 */ 528 @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class) 529 public void testCheckXPath32() throws XPathExpressionException, IOException { 530 try (InputStream is = Files.newInputStream(XML_PATH)) { 531 xpath.evaluate(" ", new InputSource(is), STRING); 532 } 533 } 534 535 /** 536 * XPath.evaluate(java.lang.String expression, InputSource source, 537 * QName returnType) throws IllegalArgumentException if returnType is not 538 * one of the types defined in XPathConstants. 539 * 540 * @throws XPathExpressionException If the expression cannot be evaluated. 541 * @throws IOException if the file exists but is a directory rather than 542 * a regular file, does not exist but cannot be created, or cannot 543 * be opened for any other reason. 544 */ 545 @Test(groups = {"readLocalFiles"}, expectedExceptions = IllegalArgumentException.class) 546 public void testCheckXPath33() throws IOException, XPathExpressionException { 547 try (InputStream is = Files.newInputStream(XML_PATH)) { 548 xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), TEST_QNAME); 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 * @throws XPathExpressionException If the expression cannot be evaluated. 557 * @throws IOException if the file exists but is a directory rather than 558 * a regular file, does not exist but cannot be created, or cannot 559 * be opened for any other reason. 560 */ 561 @Test(groups = {"readLocalFiles"}) 562 public void testCheckXPath34() throws IOException, XPathExpressionException { 563 try (InputStream is = Files.newInputStream(XML_PATH)) { 564 assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), 565 BOOLEAN), true); 566 } 567 } 568 569 /** 570 * XPath.evaluate(java.lang.String expression, InputSource source, 571 * QName returnType) return correct boolean value if return type is Boolean. 572 * 573 * @throws XPathExpressionException If the expression cannot be evaluated. 574 * @throws IOException if the file exists but is a directory rather than 575 * a regular file, does not exist but cannot be created, or cannot 576 * be opened for any other reason. 577 */ 578 @Test(groups = {"readLocalFiles"}) 579 public void testCheckXPath35() throws IOException, XPathExpressionException { 580 try (InputStream is = Files.newInputStream(XML_PATH)) { 581 assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is), 582 BOOLEAN), false); 583 } 584 } 585 586 /** 587 * XPath.evaluate(java.lang.String expression, InputSource source, 588 * QName returnType) return correct number value if return type is Number. 589 * 590 * @throws XPathExpressionException If the expression cannot be evaluated. 591 * @throws IOException if the file exists but is a directory rather than 592 * a regular file, does not exist but cannot be created, or cannot 593 * be opened for any other reason. 594 */ 595 @Test(groups = {"readLocalFiles"}) 596 public void testCheckXPath36() throws IOException, XPathExpressionException { 597 try (InputStream is = Files.newInputStream(XML_PATH)) { 598 assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), 599 NUMBER), 6d); 600 } 601 } 602 603 /** 604 * XPath.evaluate(java.lang.String expression, InputSource source, 605 * QName returnType) return correct string value if return type is Node. 606 * 607 * @throws XPathExpressionException If the expression cannot be evaluated. 608 * @throws IOException if the file exists but is a directory rather than 609 * a regular file, does not exist but cannot be created, or cannot 610 * be opened for any other reason. 611 */ 612 @Test(groups = {"readLocalFiles"}) 613 public void testCheckXPath37() throws IOException, XPathExpressionException { 614 try (InputStream is = Files.newInputStream(XML_PATH)) { 615 assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A, 616 new InputSource(is), NODE)).getValue(), "6"); 617 } 618 } 619 620 /** 621 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 622 * QName returnType) which return type is NodeList. 623 * 624 * @throws XPathExpressionException If the expression cannot be evaluated. 625 * @throws IOException if the file exists but is a directory rather than 626 * a regular file, does not exist but cannot be created, or cannot 627 * be opened for any other reason. 628 */ 629 @Test(groups = {"readLocalFiles"}) 630 public void testCheckXPath38() throws IOException, XPathExpressionException { 631 try (InputStream is = Files.newInputStream(XML_PATH)) { 632 NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A, 633 new InputSource(is), NODESET); 634 assertEquals(((Attr) nodeList.item(0)).getValue(), "6"); 635 } 636 } 637 638 /** 639 * Test for XPath.evaluate(java.lang.String expression, InputSource iSource, 640 * QName returnType). If return type is Boolean, should return false as 641 * expression is not successful in evaluating to any result. 642 * 643 * @throws XPathExpressionException If the expression cannot be evaluated. 644 * @throws IOException if the file exists but is a directory rather than 645 * a regular file, does not exist but cannot be created, or cannot 646 * be opened for any other reason. 647 */ 648 @Test(groups = {"readLocalFiles"}) 649 public void testCheckXPath52() throws IOException, XPathExpressionException { 650 try (InputStream is = Files.newInputStream(XML_PATH)) { 651 assertEquals(xpath.evaluate(EXPRESSION_NAME_B, new InputSource(is), 652 BOOLEAN), false); 653 } 654 } 655 656 /** 657 * XPath.evaluate(java.lang.String expression, InputSource iSource, QName 658 * returnType) returns correct number value which return type is Number. 659 * 660 * @throws XPathExpressionException If the expression cannot be evaluated. 661 * @throws IOException if the file exists but is a directory rather than 662 * a regular file, does not exist but cannot be created, or cannot 663 * be opened for any other reason. 664 */ 665 @Test(groups = {"readLocalFiles"}) 666 public void testCheckXPath53() throws IOException, XPathExpressionException { 667 try (InputStream is = Files.newInputStream(XML_PATH)) { 668 assertEquals(xpath.evaluate(EXPRESSION_NAME_A, new InputSource(is), 669 NUMBER), 6d); 670 } 671 } 672 673 /** 674 * XPath.evaluate(java.lang.String expression, InputSource iSource, QName 675 * returnType) returns a node value if returnType is Node. 676 * 677 * @throws XPathExpressionException If the expression cannot be evaluated. 678 * @throws IOException if the file exists but is a directory rather than 679 * a regular file, does not exist but cannot be created, or cannot 680 * be opened for any other reason. 681 */ 682 @Test(groups = {"readLocalFiles"}) 683 public void testCheckXPath54() throws IOException, XPathExpressionException { 684 try (InputStream is = Files.newInputStream(XML_PATH)) { 685 assertEquals(((Attr)xpath.evaluate(EXPRESSION_NAME_A, 686 new InputSource(is), NODE)).getValue(), "6"); 687 } 688 } 689 690 /** 691 * XPath.evaluate(java.lang.String expression, InputSource iSource, QName 692 * returnType) returns a node list if returnType is NodeList. 693 * 694 * @throws XPathExpressionException If the expression cannot be evaluated. 695 * @throws IOException if the file exists but is a directory rather than 696 * a regular file, does not exist but cannot be created, or cannot 697 * be opened for any other reason. 698 */ 699 @Test(groups = {"readLocalFiles"}) 700 public void testCheckXPath55() throws IOException, XPathExpressionException { 701 try (InputStream is = Files.newInputStream(XML_PATH)) { 702 NodeList nodeList = (NodeList)xpath.evaluate(EXPRESSION_NAME_A, 703 new InputSource(is), NODESET); 704 assertEquals(((Attr) nodeList.item(0)).getValue(), "6"); 705 } 706 } 707 708 /** 709 * Test for XPath.getNamespaceContext() returns the current namespace 710 * context, null is returned if no namespace context is in effect. 711 */ 712 @Test 713 public void testCheckXPath56() { 714 // CR 6376058 says that an impl will be provided, but by 715 // default we still return null here 716 assertNull(xpath.getNamespaceContext()); 717 } 718 719 /** 720 * Test for XPath.setNamespaceContext(NamespaceContext nsContext) Establish 721 * a namespace context. Set a valid nsContext and retrieve it using 722 * getNamespaceContext(), should return the same. 723 */ 724 @Test 725 public void testCheckXPath57() { 726 MyNamespaceContext myNamespaceContext = new MyNamespaceContext(); 727 xpath.setNamespaceContext(myNamespaceContext); 728 assertEquals(xpath.getNamespaceContext(), myNamespaceContext); 729 } 730 731 /** 732 * Test for XPath.setNamespaceContext(NamespaceContext nsContext) Establish 733 * a namespace context. NullPointerException is thrown if nsContext is null. 734 */ 735 @Test(expectedExceptions = NullPointerException.class) 736 public void testCheckXPath58() { 737 xpath.setNamespaceContext(null); 738 } 739 740 /** 741 * Test for XPath.getXPathFunctionResolver() Return the current function 742 * resolver. Null is returned if no function resolver is in effect. 743 */ 744 @Test 745 public void testCheckXPath59() { 746 assertNull(xpath.getXPathFunctionResolver()); 747 } 748 749 /** 750 * Test for XPath.setXPathFunctionResolver(XPathFunctionResolver resolver). 751 * Set a valid resolver and retrieve it using getXPathFunctionResolver(), 752 * should return the same. 753 */ 754 @Test 755 public void testCheckXPath60() { 756 xpath.setXPathFunctionResolver((functionName, arity) -> null); 757 assertNotNull(xpath.getXPathFunctionResolver()); 758 } 759 760 /** 761 * Test for XPath.setXPathFunctionResolver(XPathFunctionResolver resolver). 762 * set resolver as null, should throw NPE. 763 */ 764 @Test(expectedExceptions = NullPointerException.class) 765 public void testCheckXPath61() { 766 xpath.setXPathFunctionResolver(null); 767 } 768 769 /** 770 * Test for XPath.getXPathVariableResolver() Return the current variable 771 * resolver. null is returned if no variable resolver is in effect. 772 */ 773 @Test 774 public void testCheckXPath62() { 775 assertNull(xpath.getXPathVariableResolver()); 776 } 777 778 /** 779 * Test for XPath.setXPathVariableResolver(XPathVariableResolver resolver). 780 * Set a valid resolver and retrieve it using getXPathVariableResolver(), 781 * should return the same. 782 */ 783 @Test 784 public void testCheckXPath63() { 785 xpath.setXPathVariableResolver(qname -> null); 786 assertNotNull(xpath.getXPathVariableResolver()); 787 } 788 789 /** 790 * Test for XPath.setXPathVariableResolver(XPathVariableResolver resolver). 791 * Set resolver as null, should throw NPE. 792 */ 793 @Test(expectedExceptions = NullPointerException.class) 794 public void testCheckXPath64() { 795 xpath.setXPathVariableResolver(null); 796 } 797 798 /** 799 * Customized NamespaceContext used for test 800 */ 801 private class MyNamespaceContext implements NamespaceContext { 802 /** 803 * et Namespace URI bound to a prefix in the current scope. 804 * @param prefix prefix to look up 805 * @return a Namespace URI identical to prefix 806 */ 807 @Override 808 public String getNamespaceURI(String prefix) { 809 return prefix; 810 } 811 812 /** 813 * Get prefix bound to Namespace URI in the current scope. 814 * @param namespaceURI URI of Namespace to lookup 815 * @return prefix identical to URI of Namespace 816 */ 817 @Override 818 public String getPrefix(String namespaceURI) { 819 return namespaceURI; 820 } 821 822 /** 823 * Get all prefixes bound to a Namespace URI in the current scope. 824 * @param namespaceURI URI of Namespace to lookup 825 * @return null 826 */ 827 @Override 828 public Iterator getPrefixes(String namespaceURI) { 829 return null; 830 } 831 } 832 }