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 static org.testng.Assert.assertEquals; 27 import static org.testng.Assert.assertFalse; 28 import static org.testng.Assert.assertNull; 29 import static org.testng.Assert.assertTrue; 30 31 import java.io.File; 32 import java.io.FileInputStream; 33 import java.io.IOException; 34 35 import javax.xml.XMLConstants; 36 import javax.xml.namespace.NamespaceContext; 37 import javax.xml.namespace.QName; 38 import javax.xml.parsers.DocumentBuilder; 39 import javax.xml.parsers.DocumentBuilderFactory; 40 import javax.xml.parsers.ParserConfigurationException; 41 import javax.xml.testutils.TestBase; 42 import javax.xml.xpath.XPath; 43 import javax.xml.xpath.XPathConstants; 44 import javax.xml.xpath.XPathExpression; 45 import javax.xml.xpath.XPathExpressionException; 46 import javax.xml.xpath.XPathFactory; 47 import javax.xml.xpath.XPathFunction; 48 import javax.xml.xpath.XPathFunctionResolver; 49 import javax.xml.xpath.XPathVariableResolver; 50 51 import org.testng.annotations.BeforeTest; 52 import org.testng.annotations.Test; 53 import org.w3c.dom.Attr; 54 import org.w3c.dom.Document; 55 import org.w3c.dom.Node; 56 import org.w3c.dom.NodeList; 57 import org.xml.sax.InputSource; 58 import org.xml.sax.SAXException; 59 60 /** 61 * Class containing the test cases for XPath API. 62 */ 63 public class XPath01 extends TestBase { 64 65 private Document document = null; 66 private static final javax.xml.namespace.QName qname = new QName(XMLConstants.XML_NS_URI, ""); 67 68 @BeforeTest 69 public void setup() throws ParserConfigurationException, SAXException, IOException { 70 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 71 DocumentBuilder builder = dbf.newDocumentBuilder(); 72 document = builder.parse(new File(TestUtils.XML_DIR + "widgets.xml")); 73 } 74 75 /** 76 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 77 * item, QName returnType) which return type is String. 78 */ 79 @Test 80 public void testCheckXPath01() { 81 try { 82 XPathFactory xpathFactory = XPathFactory.newInstance(); 83 XPath xpath = xpathFactory.newXPath(); 84 String expression = "/widgets/widget[@name='a']/@quantity"; 85 String quantity = (String) xpath.evaluate(expression, document, XPathConstants.STRING); 86 assertEquals(quantity, "6"); 87 } catch (XPathExpressionException e) { 88 fail(e); 89 } 90 } 91 92 /** 93 * Test for XPath.compile(java.lang.String expression) and then 94 * evaluate(java.lang.Object item, QName returnType). 95 */ 96 @Test 97 public void testCheckXPath02() { 98 try { 99 XPathFactory xpathFactory = XPathFactory.newInstance(); 100 XPath xpath = xpathFactory.newXPath(); 101 String expression = "/widgets/widget[@name='a']/@quantity"; 102 XPathExpression xpathExpression = xpath.compile(expression); 103 String quantity = (String) xpathExpression.evaluate(document, XPathConstants.STRING); 104 assertEquals(quantity, "6"); 105 } catch (XPathExpressionException e) { 106 fail(e); 107 } 108 } 109 110 /** 111 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 112 * item) when the third argument is left off of the XPath.evaluate method, 113 * all expressions are evaluated to a String value. 114 */ 115 @Test 116 public void testCheckXPath03() { 117 try { 118 XPathFactory xpathFactory = XPathFactory.newInstance(); 119 XPath xpath = xpathFactory.newXPath(); 120 String expression = "/widgets/widget[@name='a']/@quantity"; 121 String quantity = xpath.evaluate(expression, document); 122 assertEquals(quantity, "6"); 123 } catch (XPathExpressionException e) { 124 fail(e); 125 } 126 } 127 128 /** 129 * Test for XPath.compile(java.lang.String expression). If expression is 130 * null, should throw NPE. 131 */ 132 @Test(expectedExceptions = NullPointerException.class) 133 public void testCheckXPath04() { 134 try { 135 XPathFactory xpathFactory = XPathFactory.newInstance(); 136 XPath xpath = xpathFactory.newXPath(); 137 xpath.compile(null); 138 } catch (XPathExpressionException e) { 139 fail(e); 140 } 141 } 142 143 /** 144 * Test for XPath.compile(java.lang.String expression). If expression cannot 145 * be compiled junk characters, should throw XPathExpressionException. 146 * 147 * @throws XPathExpressionException 148 */ 149 @Test(expectedExceptions = XPathExpressionException.class) 150 public void testCheckXPath05() throws XPathExpressionException { 151 XPathFactory xpathFactory = XPathFactory.newInstance(); 152 XPath xpath = xpathFactory.newXPath(); 153 String expression = "-*&"; 154 xpath.compile(expression); 155 } 156 157 /** 158 * Test for XPath.compile(java.lang.String expression). If expression is 159 * blank, should throw XPathExpressionException 160 * 161 * @throws XPathExpressionException 162 */ 163 @Test(expectedExceptions = XPathExpressionException.class) 164 public void testCheckXPath06() throws XPathExpressionException { 165 XPathFactory xpathFactory = XPathFactory.newInstance(); 166 XPath xpath = xpathFactory.newXPath(); 167 xpath.compile(" "); 168 } 169 170 /** 171 * Test for XPath.compile(java.lang.String expression). The expression 172 * cannot be evaluated as this does not exist. 173 */ 174 @Test 175 public void testCheckXPath07() { 176 try { 177 XPathFactory xpathFactory = XPathFactory.newInstance(); 178 XPath xpath = xpathFactory.newXPath(); 179 String expression = "/widgets/widget[@name='b']/@quantity"; 180 XPathExpression xpathExpression = xpath.compile(expression); 181 String quantity = (String) xpathExpression.evaluate(document, XPathConstants.STRING); 182 assertEquals(quantity, ""); 183 } catch (XPathExpressionException e) { 184 fail(e); 185 } 186 } 187 188 /** 189 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 190 * item, QName returnType). If String expression is null, should throw NPE 191 */ 192 @Test(expectedExceptions = NullPointerException.class) 193 public void testCheckXPath08() { 194 try { 195 XPathFactory xpathFactory = XPathFactory.newInstance(); 196 XPath xpath = xpathFactory.newXPath(); 197 xpath.evaluate(null, document, XPathConstants.STRING); 198 } catch (XPathExpressionException e) { 199 fail(e); 200 } 201 } 202 203 /** 204 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 205 * item, QName returnType). If item is null, should throw NPE. 206 */ 207 @Test(expectedExceptions = NullPointerException.class) 208 public void testCheckXPath09() { 209 try { 210 XPathFactory xpathFactory = XPathFactory.newInstance(); 211 XPath xpath = xpathFactory.newXPath(); 212 String expression = "/widgets/widget[@name='a']/@quantity"; 213 xpath.evaluate(expression, null, XPathConstants.STRING); 214 } catch (XPathExpressionException e) { 215 fail(e); 216 } 217 } 218 219 /** 220 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 221 * item, QName returnType). If returnType is null, should throw NPE. 222 */ 223 @Test(expectedExceptions = NullPointerException.class) 224 public void testCheckXPath10() { 225 try { 226 XPathFactory xpathFactory = XPathFactory.newInstance(); 227 XPath xpath = xpathFactory.newXPath(); 228 String expression = "/widgets/widget[@name='a']/@quantity"; 229 xpath.evaluate(expression, document, null); 230 } catch (XPathExpressionException e) { 231 fail(e); 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 XPathFactory xpathFactory = XPathFactory.newInstance(); 245 XPath xpath = xpathFactory.newXPath(); 246 String expression = "1+1"; 247 String quantity = (String) xpath.evaluate(expression, document, XPathConstants.STRING); 248 assertEquals(quantity, "2"); 249 } catch (XPathExpressionException e) { 250 fail(e); 251 } 252 } 253 254 /** 255 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 256 * item, QName returnType). If expression is a blank string "", should throw 257 * XPathExpressionException. 258 * 259 * @throws XPathExpressionException 260 */ 261 @Test(expectedExceptions = XPathExpressionException.class) 262 public void testCheckXPath12() throws XPathExpressionException { 263 XPathFactory xpathFactory = XPathFactory.newInstance(); 264 XPath xpath = xpathFactory.newXPath(); 265 xpath.evaluate("", document, XPathConstants.STRING); 266 } 267 268 /** 269 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 270 * item, QName returnType). If returnType is not one of the types defined in 271 * XPathConstants, should throw IllegalArgumentException. 272 */ 273 @Test(expectedExceptions = IllegalArgumentException.class) 274 public void testCheckXPath13() { 275 try { 276 XPathFactory xpathFactory = XPathFactory.newInstance(); 277 XPath xpath = xpathFactory.newXPath(); 278 String expression = "/widgets/widget[@name='a']/@quantity"; 279 xpath.evaluate(expression, document, qname); 280 } catch (XPathExpressionException e) { 281 fail(e); 282 } 283 } 284 285 /** 286 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 287 * item, QName returnType). if returnType is Boolean, should return true. 288 */ 289 @Test 290 public void testCheckXPath14() { 291 try { 292 XPathFactory xpathFactory = XPathFactory.newInstance(); 293 XPath xpath = xpathFactory.newXPath(); 294 String expression = "/widgets/widget[@name='a']/@quantity"; 295 Boolean result = (Boolean) xpath.evaluate(expression, document, XPathConstants.BOOLEAN); 296 assertTrue(result); 297 } catch (XPathExpressionException e) { 298 fail(e); 299 } 300 } 301 302 /** 303 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 304 * item, QName returnType). if returnType is Boolean, should return false as 305 * expression is not successful in evaluating to any result. 306 */ 307 @Test 308 public void testCheckXPath15() { 309 try { 310 XPathFactory xpathFactory = XPathFactory.newInstance(); 311 XPath xpath = xpathFactory.newXPath(); 312 String expression = "/widgets/widget[@name='b']/@quantity"; 313 Boolean result = (Boolean) xpath.evaluate(expression, document, XPathConstants.BOOLEAN); 314 assertFalse(result); 315 } catch (XPathExpressionException e) { 316 fail(e); 317 } 318 } 319 320 /** 321 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 322 * item, QName returnType). If return type is Double, the evaluated value 323 * equals to 6.0 as expected. 324 */ 325 @Test 326 public void testCheckXPath16() { 327 try { 328 XPathFactory xpathFactory = XPathFactory.newInstance(); 329 XPath xpath = xpathFactory.newXPath(); 330 String expression = "/widgets/widget[@name='a']/@quantity"; 331 Double quantity = (Double) xpath.evaluate(expression, document, XPathConstants.NUMBER); 332 assertEquals(quantity, 6d); 333 } catch (XPathExpressionException e) { 334 fail(e); 335 } 336 } 337 338 /** 339 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 340 * item, QName returnType). If return type is Node, the evaluated value 341 * equals to "6" as expected. 342 */ 343 @Test 344 public void testCheckXPath17() { 345 try { 346 XPathFactory xpathFactory = XPathFactory.newInstance(); 347 XPath xpath = xpathFactory.newXPath(); 348 String expression = "/widgets/widget[@name='a']/@quantity"; 349 Node quantity = (Node) xpath.evaluate(expression, document, XPathConstants.NODE); 350 Attr attr = (Attr) quantity; 351 String result = attr.getValue(); 352 assertEquals(result, "6"); 353 } catch (XPathExpressionException e) { 354 fail(e); 355 } 356 } 357 358 /** 359 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 360 * item, QName returnType). If return type is NodeList,the evaluated value 361 * equals to "6" as expected. 362 */ 363 @Test 364 public void testCheckXPath18() { 365 try { 366 XPathFactory xpathFactory = XPathFactory.newInstance(); 367 XPath xpath = xpathFactory.newXPath(); 368 String expression = "/widgets/widget[@name='a']/@quantity"; 369 NodeList nodeList = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET); 370 Node quantity = nodeList.item(0); 371 Attr attr = (Attr) quantity; 372 String result = attr.getValue(); 373 assertEquals(result, "6"); 374 } catch (XPathExpressionException e) { 375 fail(e); 376 } 377 } 378 379 /** 380 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 381 * item). If expression is null, should throw NPE. 382 */ 383 @Test(expectedExceptions = NullPointerException.class) 384 public void testCheckXPath19() { 385 try { 386 XPathFactory xpathFactory = XPathFactory.newInstance(); 387 XPath xpath = xpathFactory.newXPath(); 388 xpath.evaluate(null, document); 389 } catch (XPathExpressionException e) { 390 fail(e); 391 } 392 } 393 394 /** 395 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 396 * item). If a request is made to evaluate the expression in the absence of 397 * a context item, simple expressions, such as "1+1", can be evaluated. 398 */ 399 @Test 400 public void testCheckXPath20() { 401 try { 402 XPathFactory xpathFactory = XPathFactory.newInstance(); 403 XPath xpath = xpathFactory.newXPath(); 404 String expression = "1+1"; 405 String quantity = xpath.evaluate(expression, document); 406 assertEquals(quantity, "2"); 407 } catch (XPathExpressionException e) { 408 fail(e); 409 } 410 } 411 412 /** 413 * Test for XPath.evaluate(java.lang.String expression, java.lang.Object 414 * item). If InputSource is null, should throw NPE. 415 */ 416 @Test(expectedExceptions = NullPointerException.class) 417 public void testCheckXPath21() { 418 try { 419 XPathFactory xpathFactory = XPathFactory.newInstance(); 420 XPath xpath = xpathFactory.newXPath(); 421 String expression = "/widgets/widget[@name='a']/@quantity"; 422 xpath.evaluate(expression, null); 423 } catch (XPathExpressionException e) { 424 fail(e); 425 } 426 } 427 428 /** 429 * Test for XPath.evaluate(java.lang.String expression, InputSource source) 430 */ 431 @Test 432 public void testCheckXPath22() { 433 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 434 InputSource iSource = new InputSource(fis); 435 XPathFactory xpathFactory = XPathFactory.newInstance(); 436 XPath xpath = xpathFactory.newXPath(); 437 String expression = "/widgets/widget[@name='a']/@quantity"; 438 String quantity = xpath.evaluate(expression, iSource); 439 assertEquals(quantity, "6"); 440 } catch (XPathExpressionException | IOException e) { 441 fail(e); 442 } 443 } 444 445 /** 446 * Test for XPath.evaluate(java.lang.String expression, InputSource source). 447 * If InputSource is null, should throw NPE. 448 */ 449 @Test(expectedExceptions = NullPointerException.class) 450 public void testCheckXPath23() { 451 try { 452 XPathFactory xpathFactory = XPathFactory.newInstance(); 453 XPath xpath = xpathFactory.newXPath(); 454 String expression = "/widgets/widget[@name='a']/@quantity"; 455 xpath.evaluate(expression, null); 456 } catch (XPathExpressionException e) { 457 fail(e); 458 } 459 } 460 461 /** 462 * Test for XPath.evaluate(java.lang.String expression, InputSource source). 463 * If String expression is null, should throw NPE. 464 */ 465 @Test(expectedExceptions = NullPointerException.class) 466 public void testCheckXPath24() { 467 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 468 InputSource iSource = new InputSource(fis); 469 XPathFactory xpathFactory = XPathFactory.newInstance(); 470 XPath xpath = xpathFactory.newXPath(); 471 xpath.evaluate(null, iSource); 472 } catch (XPathExpressionException | IOException e) { 473 fail(e); 474 } 475 } 476 477 /** 478 * Test for XPath.evaluate(java.lang.String expression, InputSource source). 479 * If expression is junk characters, expression cannot be evaluated, should 480 * throw XPathExpressionException. 481 * 482 * @throws XPathExpressionException 483 */ 484 @Test(expectedExceptions = XPathExpressionException.class) 485 public void testCheckXPath25() throws XPathExpressionException { 486 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 487 InputSource iSource = new InputSource(fis); 488 XPathFactory xpathFactory = XPathFactory.newInstance(); 489 XPath xpath = xpathFactory.newXPath(); 490 xpath.evaluate("-*&", iSource); 491 } catch (IOException e) { 492 fail(e); 493 } 494 } 495 496 /** 497 * Test for XPath.evaluate(java.lang.String expression, InputSource source). 498 * If expression is blank " ", should throw XPathExpressionException. 499 * 500 * @throws XPathExpressionException 501 */ 502 @Test(expectedExceptions = XPathExpressionException.class) 503 public void testCheckXPath26() throws XPathExpressionException { 504 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 505 InputSource iSource = new InputSource(fis); 506 XPathFactory xpathFactory = XPathFactory.newInstance(); 507 XPath xpath = xpathFactory.newXPath(); 508 xpath.evaluate(" ", iSource); 509 } catch (IOException e) { 510 fail(e); 511 } 512 } 513 514 /** 515 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 516 * QName returnType) which return type is String. The evaluated value equals 517 * to "6" as expected. 518 */ 519 @Test 520 public void testCheckXPath27() { 521 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 522 InputSource iSource = new InputSource(fis); 523 XPathFactory xpathFactory = XPathFactory.newInstance(); 524 XPath xpath = xpathFactory.newXPath(); 525 String expression = "/widgets/widget[@name='a']/@quantity"; 526 String quantity = (String) xpath.evaluate(expression, iSource, XPathConstants.STRING); 527 assertEquals(quantity, "6"); 528 } catch (XPathExpressionException | IOException e) { 529 fail(e); 530 } 531 } 532 533 /** 534 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 535 * QName returnType). If source is null, should throw NPE. 536 */ 537 @Test(expectedExceptions = NullPointerException.class) 538 public void testCheckXPath28() { 539 try { 540 XPathFactory xpathFactory = XPathFactory.newInstance(); 541 XPath xpath = xpathFactory.newXPath(); 542 String expression = "/widgets/widget[@name='a']/@quantity"; 543 xpath.evaluate(expression, null, XPathConstants.STRING); 544 } catch (XPathExpressionException e) { 545 fail(e); 546 } 547 } 548 549 /** 550 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 551 * QName returnType). If expression is null, should throw NPE. 552 */ 553 @Test(expectedExceptions = NullPointerException.class) 554 public void testCheckXPath29() { 555 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 556 InputSource iSource = new InputSource(fis); 557 XPathFactory xpathFactory = XPathFactory.newInstance(); 558 XPath xpath = xpathFactory.newXPath(); 559 xpath.evaluate(null, iSource, XPathConstants.STRING); 560 } catch (XPathExpressionException | IOException e) { 561 fail(e); 562 } 563 } 564 565 /** 566 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 567 * QName returnType). If returnType is null, should throw NPE. 568 */ 569 @Test(expectedExceptions = NullPointerException.class) 570 public void testCheckXPath30() { 571 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 572 InputSource iSource = new InputSource(fis); 573 XPathFactory xpathFactory = XPathFactory.newInstance(); 574 XPath xpath = xpathFactory.newXPath(); 575 String expression = "/widgets/widget[@name='a']/@quantity"; 576 xpath.evaluate(expression, iSource, null); 577 } catch (XPathExpressionException | IOException e) { 578 fail(e); 579 } 580 } 581 582 /** 583 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 584 * QName returnType). If expression is junk characters, should throw 585 * XPathExpressionException. 586 * 587 * @throws XPathExpressionException 588 */ 589 @Test(expectedExceptions = XPathExpressionException.class) 590 public void testCheckXPath31() throws XPathExpressionException { 591 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 592 InputSource iSource = new InputSource(fis); 593 XPathFactory xpathFactory = XPathFactory.newInstance(); 594 XPath xpath = xpathFactory.newXPath(); 595 xpath.evaluate("-*&", iSource, XPathConstants.STRING); 596 } catch (IOException e) { 597 fail(e); 598 } 599 } 600 601 /** 602 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 603 * QName returnType). If expression is blank " ", should throw 604 * XPathExpressionException. 605 * 606 * @throws XPathExpressionException 607 */ 608 @Test(expectedExceptions = XPathExpressionException.class) 609 public void testCheckXPath32() throws XPathExpressionException { 610 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 611 InputSource iSource = new InputSource(fis); 612 XPathFactory xpathFactory = XPathFactory.newInstance(); 613 XPath xpath = xpathFactory.newXPath(); 614 String expression = " "; 615 xpath.evaluate(expression, iSource, XPathConstants.STRING); 616 } catch (IOException e) { 617 fail(e); 618 } 619 } 620 621 /** 622 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 623 * QName returnType). If returnType is not one of the types defined in 624 * XPathConstants, should throw IllegalArgumentException. 625 */ 626 @Test(expectedExceptions = IllegalArgumentException.class) 627 public void testCheckXPath33() { 628 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 629 InputSource iSource = new InputSource(fis); 630 XPathFactory xpathFactory = XPathFactory.newInstance(); 631 XPath xpath = xpathFactory.newXPath(); 632 String expression = "/widgets/widget[@name='a']/@quantity"; 633 xpath.evaluate(expression, iSource, qname); 634 } catch (XPathExpressionException | IOException e) { 635 fail(e); 636 } 637 } 638 639 /** 640 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 641 * QName returnType). if return type is Boolean, should return true. 642 */ 643 @Test 644 public void testCheckXPath34() { 645 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 646 InputSource iSource = new InputSource(fis); 647 XPathFactory xpathFactory = XPathFactory.newInstance(); 648 XPath xpath = xpathFactory.newXPath(); 649 String expression = "/widgets/widget[@name='a']/@quantity"; 650 Boolean result = (Boolean) xpath.evaluate(expression, iSource, XPathConstants.BOOLEAN); 651 assertTrue(result); 652 } catch (XPathExpressionException | IOException e) { 653 fail(e); 654 } 655 } 656 657 /** 658 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 659 * QName returnType). If returnType is Boolean, should return false as 660 * expression is not successful in evaluating to any result. 661 */ 662 @Test 663 public void testCheckXPath35() { 664 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 665 InputSource iSource = new InputSource(fis); 666 XPathFactory xpathFactory = XPathFactory.newInstance(); 667 XPath xpath = xpathFactory.newXPath(); 668 String expression = "/widgets/widget[@name='b']/@quantity"; 669 Boolean result = (Boolean) xpath.evaluate(expression, iSource, XPathConstants.BOOLEAN); 670 assertFalse(result); 671 } catch (XPathExpressionException | IOException e) { 672 fail(e); 673 } 674 } 675 676 /** 677 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 678 * QName returnType). The evaluated value equals to 6.0 as expected. 679 */ 680 @Test 681 public void testCheckXPath36() { 682 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 683 InputSource iSource = new InputSource(fis); 684 XPathFactory xpathFactory = XPathFactory.newInstance(); 685 XPath xpath = xpathFactory.newXPath(); 686 String expression = "/widgets/widget[@name='a']/@quantity"; 687 Double quantity = (Double) xpath.evaluate(expression, iSource, XPathConstants.NUMBER); 688 assertEquals(quantity, 6d); 689 } catch (XPathExpressionException | IOException e) { 690 fail(e); 691 } 692 } 693 694 /** 695 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 696 * QName returnType) which returnType is Node. 697 */ 698 @Test 699 public void testCheckXPath37() { 700 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 701 InputSource iSource = new InputSource(fis); 702 XPathFactory xpathFactory = XPathFactory.newInstance(); 703 XPath xpath = xpathFactory.newXPath(); 704 String expression = "/widgets/widget[@name='a']/@quantity"; 705 Node quantity = (Node) xpath.evaluate(expression, iSource, XPathConstants.NODE); 706 Attr attr = (Attr) quantity; 707 String result = attr.getValue(); 708 assertEquals(result, "6"); 709 } catch (XPathExpressionException | IOException e) { 710 fail(e); 711 } 712 } 713 714 /** 715 * Test for XPath.evaluate(java.lang.String expression, InputSource source, 716 * QName returnType) which return type is NodeList. 717 */ 718 @Test 719 public void testCheckXPath38() { 720 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 721 InputSource iSource = new InputSource(fis); 722 XPathFactory xpathFactory = XPathFactory.newInstance(); 723 XPath xpath = xpathFactory.newXPath(); 724 String expression = "/widgets/widget[@name='a']/@quantity"; 725 NodeList nodeList = (NodeList) xpath.evaluate(expression, iSource, XPathConstants.NODESET); 726 Node quantity = nodeList.item(0); 727 Attr attr = (Attr) quantity; 728 String result = attr.getValue(); 729 assertEquals(result, "6"); 730 } catch (XPathExpressionException | IOException e) { 731 fail(e); 732 } 733 } 734 735 /** 736 * Test for XPath.evaluate(java.lang.String expression, InputSource iSource, 737 * QName returnType). If return type is Boolean, should return false as 738 * expression is not successful in evaluating to any result. 739 */ 740 @Test 741 public void testCheckXPath52() { 742 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 743 InputSource iSource = new InputSource(fis); 744 XPathFactory xpathFactory = XPathFactory.newInstance(); 745 XPath xpath = xpathFactory.newXPath(); 746 String expression = "/widgets/widget[@name='b']/@quantity"; 747 Boolean result = (Boolean) xpath.evaluate(expression, iSource, XPathConstants.BOOLEAN); 748 assertFalse(result); 749 } catch (XPathExpressionException | IOException e) { 750 fail(e); 751 } 752 } 753 754 /** 755 * Test for XPath.evaluate(java.lang.String expression, InputSource iSource, 756 * QName returnType) which return type is Double. 757 */ 758 @Test 759 public void testCheckXPath53() { 760 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 761 InputSource iSource = new InputSource(fis); 762 XPathFactory xpathFactory = XPathFactory.newInstance(); 763 XPath xpath = xpathFactory.newXPath(); 764 String expression = "/widgets/widget[@name='a']/@quantity"; 765 Double quantity = (Double) xpath.evaluate(expression, iSource, XPathConstants.NUMBER); 766 assertEquals(quantity, 6d); 767 } catch (XPathExpressionException | IOException e) { 768 fail(e); 769 } 770 } 771 772 /** 773 * Test for XPath.evaluate(java.lang.String expression, InputSource iSource, 774 * QName returnType) which returnType is Node. 775 */ 776 @Test 777 public void testCheckXPath54() { 778 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 779 InputSource iSource = new InputSource(fis); 780 XPathFactory xpathFactory = XPathFactory.newInstance(); 781 XPath xpath = xpathFactory.newXPath(); 782 String expression = "/widgets/widget[@name='a']/@quantity"; 783 Node quantity = (Node) xpath.evaluate(expression, iSource, XPathConstants.NODE); 784 Attr attr = (Attr) quantity; 785 String result = attr.getValue(); 786 assertEquals(result, "6"); 787 } catch (XPathExpressionException | IOException e) { 788 fail(e); 789 } 790 } 791 792 /** 793 * Test for XPath.evaluate(java.lang.String expression, InputSource iSource, 794 * QName returnType) which returnType is NodeList. 795 */ 796 @Test 797 public void testCheckXPath55() { 798 try (FileInputStream fis = new FileInputStream(TestUtils.XML_DIR + "widgets.xml")) { 799 InputSource iSource = new InputSource(fis); 800 XPathFactory xpathFactory = XPathFactory.newInstance(); 801 XPath xpath = xpathFactory.newXPath(); 802 String expression = "/widgets/widget[@name='a']/@quantity"; 803 NodeList nodeList = (NodeList) xpath.evaluate(expression, iSource, XPathConstants.NODESET); 804 Node quantity = nodeList.item(0); 805 Attr attr = (Attr) quantity; 806 String result = attr.getValue(); 807 assertEquals(result, "6"); 808 } catch (XPathExpressionException | IOException e) { 809 fail(e); 810 } 811 } 812 813 /** 814 * Test for XPath.getNamespaceContext() returns the current namespace 815 * context, null is returned if no namespace context is in effect. 816 */ 817 @Test 818 public void testCheckXPath56() { 819 XPathFactory xpathFactory = XPathFactory.newInstance(); 820 XPath xpath = xpathFactory.newXPath(); 821 NamespaceContext namespaceContext = xpath.getNamespaceContext(); 822 // CR 6376058 says that an impl will be provided, but by 823 // default we still return null here 824 assertNull(namespaceContext); 825 } 826 827 /** 828 * Test for XPath.setNamespaceContext(NamespaceContext nsContext) Establish 829 * a namespace context. Set a valid nsContext and retrieve it using 830 * getNamespaceContext(), should return the same. 831 */ 832 @Test 833 public void testCheckXPath57() { 834 XPathFactory xpathFactory = XPathFactory.newInstance(); 835 XPath xpath = xpathFactory.newXPath(); 836 MyNamespaceContext myNamespaceContext = new MyNamespaceContext(); 837 xpath.setNamespaceContext(myNamespaceContext); 838 NamespaceContext returned = xpath.getNamespaceContext(); 839 assertEquals(returned, myNamespaceContext); 840 } 841 842 /** 843 * Test for XPath.setNamespaceContext(NamespaceContext nsContext) Establish 844 * a namespace context. NullPointerException is thrown if nsContext is null. 845 */ 846 @Test(expectedExceptions = NullPointerException.class) 847 public void testCheckXPath58() { 848 XPathFactory xpathFactory = XPathFactory.newInstance(); 849 XPath xpath = xpathFactory.newXPath(); 850 xpath.setNamespaceContext(null); 851 } 852 853 /** 854 * Test for XPath.getXPathFunctionResolver() Return the current function 855 * resolver. Null is returned if no function resolver is in effect. 856 */ 857 @Test 858 public void testCheckXPath59() { 859 XPathFactory xpathFactory = XPathFactory.newInstance(); 860 XPath xpath = xpathFactory.newXPath(); 861 XPathFunctionResolver xpfResolver = xpath.getXPathFunctionResolver(); 862 assertNull(xpfResolver); 863 } 864 865 /** 866 * Test for XPath.setXPathFunctionResolver(XPathFunctionResolver resolver). 867 * Set a valid resolver and retrieve it using getXPathFunctionResolver(), 868 * should return the same. 869 */ 870 @Test 871 public void testCheckXPath60() { 872 XPathFactory xpathFactory = XPathFactory.newInstance(); 873 XPath xpath = xpathFactory.newXPath(); 874 MyXPathFunctionResolver myXPFResolver = new MyXPathFunctionResolver(); 875 xpath.setXPathFunctionResolver(myXPFResolver); 876 XPathFunctionResolver returned = xpath.getXPathFunctionResolver(); 877 assertEquals(returned, myXPFResolver); 878 } 879 880 /** 881 * Test for XPath.setXPathFunctionResolver(XPathFunctionResolver resolver). 882 * set resolver as null, should throw NPE. 883 */ 884 @Test(expectedExceptions = NullPointerException.class) 885 public void testCheckXPath61() { 886 XPathFactory xpathFactory = XPathFactory.newInstance(); 887 XPath xpath = xpathFactory.newXPath(); 888 xpath.setXPathFunctionResolver(null); 889 } 890 891 /** 892 * Test for XPath.getXPathVariableResolver() Return the current variable 893 * resolver. null is returned if no variable resolver is in effect. 894 */ 895 @Test 896 public void testCheckXPath62() { 897 XPathFactory xpathFactory = XPathFactory.newInstance(); 898 XPath xpath = xpathFactory.newXPath(); 899 XPathVariableResolver xpvResolver = xpath.getXPathVariableResolver(); 900 assertNull(xpvResolver); 901 } 902 903 /** 904 * Test for XPath.setXPathVariableResolver(XPathVariableResolver resolver). 905 * Set a valid resolver and retrieve it using getXPathVariableResolver(), 906 * should return the same. 907 */ 908 @Test 909 public void testCheckXPath63() { 910 XPathFactory xpathFactory = XPathFactory.newInstance(); 911 XPath xpath = xpathFactory.newXPath(); 912 MyXPathVariableResolver myXPVResolver = new MyXPathVariableResolver(); 913 xpath.setXPathVariableResolver(myXPVResolver); 914 XPathVariableResolver returned = xpath.getXPathVariableResolver(); 915 assertEquals(returned, myXPVResolver); 916 } 917 918 /** 919 * Test for XPath.setXPathVariableResolver(XPathVariableResolver resolver). 920 * Set resolver as null, should throw NPE. 921 */ 922 @Test(expectedExceptions = NullPointerException.class) 923 public void testCheckXPath64() { 924 XPathFactory xpathFactory = XPathFactory.newInstance(); 925 XPath xpath = xpathFactory.newXPath(); 926 xpath.setXPathVariableResolver(null); 927 } 928 929 /** 930 * Customized NamespaceContext used for test. 931 */ 932 private class MyNamespaceContext implements NamespaceContext { 933 public java.lang.String getNamespaceURI(java.lang.String prefix) { 934 System.out.println("XPath01 - in MyNamespaceContext - getNamespaceURI "); 935 return prefix; 936 } 937 938 public java.lang.String getPrefix(java.lang.String namespaceURI) { 939 System.out.println("XPath01 - in MyNamespaceContext - getPrefix "); 940 return namespaceURI; 941 } 942 943 public java.util.Iterator getPrefixes(java.lang.String namespaceURI) { 944 System.out.println("XPath01 - in MyNamespaceContext - getPrefixes "); 945 return null; 946 } 947 } 948 949 /** 950 * Customized XPathFunctionResolver used for test. 951 */ 952 private class MyXPathFunctionResolver implements XPathFunctionResolver { 953 public XPathFunction resolveFunction(QName functionName, int arity) { 954 System.out.println("XPath01 - in MyXPathFunctionResolver - resolveFunction "); 955 return null; 956 } 957 } 958 959 /** 960 * Customized XPathVariableResolver used for test. 961 */ 962 private class MyXPathVariableResolver implements XPathVariableResolver { 963 public java.lang.Object resolveVariable(QName variableName) { 964 System.out.println("XPath01 - in MyXPathVariableResolver - resolveVariable "); 965 return null; 966 } 967 } 968 }