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