| 1 61 62 63 package org.jaxen; 64 65 import junit.framework.TestCase; 66 import org.jaxen.function.StringFunction; 67 import org.jaxen.saxpath.helpers.XPathReaderFactory; 68 import org.jaxen.pattern.Pattern; 69 70 import java.util.ArrayList ; 71 import java.util.Iterator ; 72 import java.util.List ; 73 74 public abstract class XPathTestBase extends TestCase 75 { 76 protected static String VAR_URI = "http://jaxen.org/test-harness/var"; 77 protected static String TESTS_XML = "xml/test/tests.xml"; 78 79 protected static boolean verbose = false; 80 protected static boolean debug = false; 81 private ContextSupport contextSupport; 82 83 public XPathTestBase(String name) 84 { 85 super(name); 86 } 87 88 public void setUp() 89 { 90 this.contextSupport = null; 91 System.setProperty(XPathReaderFactory.DRIVER_PROPERTY, 92 ""); 93 log("-----------------------------"); 94 } 95 96 public void log(String text) 97 { 98 log(verbose, 99 text); 100 } 101 102 public void log(boolean actualVerbose, 103 String text) 104 { 105 if (actualVerbose) System.out.println(text); 106 } 107 108 protected void assertCountXPath(int expectedSize, Object context, String xpathStr) throws JaxenException 109 { 110 assertCountXPath2(expectedSize, context, xpathStr); 111 } 112 113 protected Object assertCountXPath2(int expectedSize, Object context, String xpathStr) throws JaxenException 114 { 115 log(debug, 116 " Select :: " + xpathStr); 117 BaseXPath xpath = new BaseXPath(xpathStr); 118 List results = xpath.selectNodes(getContext(context)); 119 log(debug, 120 " Expected Size :: " + expectedSize); 121 log(debug, 122 " Result Size :: " + results.size()); 123 if (expectedSize != results.size()) 124 { 125 log(debug, 126 " ## FAILED"); 127 log(debug, 128 " ## xpath: " + xpath + " = " + xpath.debug()); 129 Iterator resultIter = results.iterator(); 130 while (resultIter.hasNext()) 131 { 132 log(debug, 133 " --> " + resultIter.next()); 134 } 135 } 136 assertEquals(xpathStr, 137 expectedSize, 138 results.size()); 139 if (expectedSize > 0) 140 { 141 return results.get(0); 142 } 143 return null; 144 } 145 146 protected void assertInvalidXPath(Object context, String xpathStr) 147 { 148 try 149 { 150 log(debug, 151 " Select :: " + xpathStr); 152 BaseXPath xpath = new BaseXPath(xpathStr); 153 List results = xpath.selectNodes(getContext(context)); 154 log(debug, 155 " Result Size :: " + results.size()); 156 fail("An exception was expected."); 157 } 158 catch (UnsupportedAxisException e) 159 { 160 log(debug, 161 " ## SKIPPED -- Unsupported Axis"); 162 } 163 catch (JaxenException e) 164 { 165 log(debug, " Caught expected exception " + e.getMessage()); 166 } 167 } 168 169 protected void assertValueOfXPath(String expected, Object context, String xpathStr) throws JaxenException 170 { 171 try 172 { 173 BaseXPath xpath = new BaseXPath(xpathStr); 174 Object node = xpath.evaluate(getContext(context)); 175 String result = StringFunction.evaluate(node, 176 getNavigator()); 177 log(debug, 178 " Select :: " + xpathStr); 179 log(debug, 180 " Expected :: " + expected); 181 log(debug, 182 " Result :: " + result); 183 if (!expected.equals(result)) 184 { 185 log(debug, 186 " ## FAILED"); 187 log(debug, 188 " ## xpath: " + xpath + " = " + xpath.debug()); 189 } 190 assertEquals(xpathStr, 191 expected, 192 result); 193 } 194 catch (UnsupportedAxisException e) 195 { 196 log(debug, 197 " ## SKIPPED -- Unsupported Axis "); 198 } 199 } 200 201 protected Context getContext(Object contextNode) 202 { 203 Context context = new Context(getContextSupport()); 204 List list = new ArrayList (1); 205 list.add(contextNode); 206 context.setNodeSet(list); 207 return context; 208 } 209 210 public ContextSupport getContextSupport() 211 { 212 if (this.contextSupport == null) 213 { 214 this.contextSupport = new ContextSupport(new SimpleNamespaceContext(), 215 XPathFunctionContext.getInstance(), 216 new SimpleVariableContext(), 217 getNavigator()); 218 } 219 return this.contextSupport; 220 } 221 222 public abstract Navigator getNavigator(); 223 224 public abstract Object getDocument(String url) throws Exception ; 225 226 public void testGetNodeType() throws FunctionCallException, UnsupportedAxisException 227 { 228 Navigator nav = getNavigator(); 229 Object document = nav.getDocument("xml/testNamespaces.xml"); 230 int count = 0; 231 Iterator descendantOrSelfAxisIterator = nav.getDescendantOrSelfAxisIterator(document); 232 while (descendantOrSelfAxisIterator.hasNext()) 233 { 234 Object node = descendantOrSelfAxisIterator.next(); 235 Iterator namespaceAxisIterator = nav.getNamespaceAxisIterator(node); 236 while (namespaceAxisIterator.hasNext()) 237 { 238 count++; 239 assertEquals("Node type mismatch", Pattern.NAMESPACE_NODE, nav.getNodeType(namespaceAxisIterator.next())); 240 } 241 } 242 assertEquals(25, count); 243 } 244 245 246 248 public void testid53371() throws JaxenException 249 { 250 Navigator nav = getNavigator(); 251 String url = "xml/jaxen24.xml"; 252 log("Document [" + url + "]"); 253 Object document = nav.getDocument(url); 254 XPath contextpath = new BaseXPath("/body/div", nav); 255 log("Initial Context :: " + contextpath); 256 List list = contextpath.selectNodes(document); 257 Iterator iter = list.iterator(); 258 while (iter.hasNext()) 259 { 260 Object context = iter.next(); 261 assertCountXPath(1, context, "preceding::*[1]"); 262 assertValueOfXPath("span", context, "local-name(preceding::*[1])"); 263 } 264 } 265 266 268 public void testid53391() throws JaxenException 269 { 270 Navigator nav = getNavigator(); 271 String url = "xml/jaxen24.xml"; 272 log("Document [" + url + "]"); 273 Object document = nav.getDocument(url); 274 XPath contextpath = new BaseXPath("/", nav); 275 log("Initial Context :: " + contextpath); 276 List list = contextpath.selectNodes(document); 277 Iterator iter = list.iterator(); 278 while (iter.hasNext()) 279 { 280 Object context = iter.next(); 281 assertCountXPath(0, context, "//preceding::x"); 282 assertCountXPath(0, context, "//following::x"); 283 assertCountXPath(0, context, "/descendant::*/preceding::x"); 284 assertCountXPath(0, context, "/descendant::node()/preceding::x"); 285 } 286 } 287 288 290 public void testid53430() throws JaxenException 291 { 292 Navigator nav = getNavigator(); 293 String url = "xml/simple.xml"; 294 log("Document [" + url + "]"); 295 Object document = nav.getDocument(url); 296 XPath contextpath = new BaseXPath("/", nav); 297 log("Initial Context :: " + contextpath); 298 List list = contextpath.selectNodes(document); 299 Iterator iter = list.iterator(); 300 while (iter.hasNext()) 301 { 302 Object context = iter.next(); 303 assertValueOfXPath("abd", context, "string()"); 304 } 305 } 306 307 public void testid53441() throws JaxenException 308 { 309 Navigator nav = getNavigator(); 310 String url = "xml/simple.xml"; 311 log("Document [" + url + "]"); 312 Object document = nav.getDocument(url); 313 XPath contextpath = new BaseXPath("/root", nav); 314 log("Initial Context :: " + contextpath); 315 List list = contextpath.selectNodes(document); 316 Iterator iter = list.iterator(); 317 while (iter.hasNext()) 318 { 319 Object context = iter.next(); 320 assertValueOfXPath("abd", context, "string()"); 321 } 322 } 323 324 public void testid53452() throws JaxenException 325 { 326 Navigator nav = getNavigator(); 327 String url = "xml/simple.xml"; 328 log("Document [" + url + "]"); 329 Object document = nav.getDocument(url); 330 XPath contextpath = new BaseXPath("/root/a", nav); 331 log("Initial Context :: " + contextpath); 332 List list = contextpath.selectNodes(document); 333 Iterator iter = list.iterator(); 334 while (iter.hasNext()) 335 { 336 Object context = iter.next(); 337 assertValueOfXPath("a", context, "string()"); 338 } 339 } 340 341 public void testid53463() throws JaxenException 342 { 343 Navigator nav = getNavigator(); 344 String url = "xml/simple.xml"; 345 log("Document [" + url + "]"); 346 Object document = nav.getDocument(url); 347 XPath contextpath = new BaseXPath("/root/c", nav); 348 log("Initial Context :: " + contextpath); 349 List list = contextpath.selectNodes(document); 350 Iterator iter = list.iterator(); 351 while (iter.hasNext()) 352 { 353 Object context = iter.next(); 354 assertValueOfXPath("d", context, "string()"); 355 } 356 } 357 358 360 public void testid53482() throws JaxenException 361 { 362 Navigator nav = getNavigator(); 363 String url = "xml/jaxen3.xml"; 364 log("Document [" + url + "]"); 365 Object document = nav.getDocument(url); 366 XPath contextpath = new BaseXPath("/", nav); 367 log("Initial Context :: " + contextpath); 368 List list = contextpath.selectNodes(document); 369 Iterator iter = list.iterator(); 370 while (iter.hasNext()) 371 { 372 Object context = iter.next(); 373 assertCountXPath(1, context, "/Configuration/hostname/attrlist/hostname[. = 'CE-A'] "); 374 } 375 } 376 377 379 public void testid53502() throws JaxenException 380 { 381 Navigator nav = getNavigator(); 382 String url = "xml/numbers.xml"; 383 log("Document [" + url + "]"); 384 Object document = nav.getDocument(url); 385 XPath contextpath = new BaseXPath("/", nav); 386 log("Initial Context :: " + contextpath); 387 List list = contextpath.selectNodes(document); 388 Iterator iter = list.iterator(); 389 while (iter.hasNext()) 390 { 391 Object context = iter.next(); 392 394 assertInvalidXPath(context, "/numbers numbers"); 395 397 assertInvalidXPath(context, "/a/b[c > d]efg"); 398 400 assertInvalidXPath(context, "/inv/child::"); 401 403 assertInvalidXPath(context, "/invoice/@test[abcd"); 404 assertInvalidXPath(context, "/invoice/@test[abcd > x"); 405 407 assertInvalidXPath(context, "string-length('a"); 408 410 assertInvalidXPath(context, "/descendant::()"); 411 assertInvalidXPath(context, "(1 + 1"); 412 } 413 } 414 415 417 public void testid53602() throws JaxenException 418 { 419 Navigator nav = getNavigator(); 420 String url = "xml/underscore.xml"; 421 log("Document [" + url + "]"); 422 Object document = nav.getDocument(url); 423 XPath contextpath = new BaseXPath("/", nav); 424 log("Initial Context :: " + contextpath); 425 List list = contextpath.selectNodes(document); 426 Iterator iter = list.iterator(); 427 while (iter.hasNext()) 428 { 429 Object context = iter.next(); 430 assertCountXPath(1, context, "/root/@a"); 431 assertCountXPath(1, context, "/root/@_a"); 432 assertCountXPath(1, context, "/root/b"); 433 assertCountXPath(1, context, "/root/_b"); 434 assertValueOfXPath("1", context, "/root/@a"); 435 assertValueOfXPath("2", context, "/root/@_a"); 436 assertValueOfXPath("1", context, "/root/b"); 437 assertValueOfXPath("2", context, "/root/_b"); 438 } 439 } 440 441 443 public void testid53662() throws JaxenException 444 { 445 Navigator nav = getNavigator(); 446 String url = "xml/web.xml"; 447 log("Document [" + url + "]"); 448 Object document = nav.getDocument(url); 449 XPath contextpath = new BaseXPath("/", nav); 450 log("Initial Context :: " + contextpath); 451 List list = contextpath.selectNodes(document); 452 Iterator iter = list.iterator(); 453 while (iter.hasNext()) 454 { 455 Object context = iter.next(); 456 assertValueOfXPath("true", context, "/web-app/servlet/servlet-name = 'file'"); 457 assertValueOfXPath("true", context, "/web-app/servlet/servlet-name = 'snoop'"); 458 } 459 } 460 461 public void testid53685() throws JaxenException 462 { 463 Navigator nav = getNavigator(); 464 String url = "xml/numbers.xml"; 465 log("Document [" + url + "]"); 466 Object document = nav.getDocument(url); 467 XPath contextpath = new BaseXPath("/", nav); 468 log("Initial Context :: " + contextpath); 469 List list = contextpath.selectNodes(document); 470 Iterator iter = list.iterator(); 471 while (iter.hasNext()) 472 { 473 Object context = iter.next(); 474 assertValueOfXPath("true", context, "/numbers/set/nr = '-3'"); 475 assertValueOfXPath("true", context, "/numbers/set/nr = -3"); 476 assertValueOfXPath("true", context, "/numbers/set/nr = 24"); 477 assertValueOfXPath("true", context, "/numbers/set/nr/@value = '9999'"); 478 assertValueOfXPath("true", context, "/numbers/set/nr/@value = 9999.0"); 479 assertValueOfXPath("true", context, "/numbers/set/nr/@value = 66"); 480 } 481 } 482 483 485 public void testid53733() throws JaxenException 486 { 487 Navigator nav = getNavigator(); 488 String url = "xml/numbers.xml"; 489 log("Document [" + url + "]"); 490 Object document = nav.getDocument(url); 491 XPath contextpath = new BaseXPath("/", nav); 492 log("Initial Context :: " + contextpath); 493 List list = contextpath.selectNodes(document); 494 Iterator iter = list.iterator(); 495 while (iter.hasNext()) 496 { 497 Object context = iter.next(); 498 assertValueOfXPath("true", context, "(8 * 2 + 1) = 17"); 499 assertValueOfXPath("true", context, "(1 + 8 * 2) = 17"); 500 assertValueOfXPath("true", context, "(7 - 3 + 1) = 5"); 501 assertValueOfXPath("true", context, "(8 - 4 + 5 - 6) = 3"); 502 504 506 assertValueOfXPath("0", context, "3 - 2 - 1"); 507 509 assertValueOfXPath("1", context, "8 div 4 div 2"); 510 512 assertValueOfXPath("3", context, "3 mod 7 mod 5"); 513 515 assertValueOfXPath("false", context, "1 = 2 = 2"); 516 518 assertValueOfXPath("false", context, "2 != 3 != 1"); 519 521 assertValueOfXPath("false", context, "3 > 2 > 1"); 522 524 assertValueOfXPath("false", context, "3 >= 2 >= 2"); 525 527 assertValueOfXPath("true", context, "1 < 2 < 3"); 528 530 assertValueOfXPath("true", context, "2 <= 2 <= 3"); 531 } 532 } 533 534 536 public void testid53850() throws JaxenException 537 { 538 Navigator nav = getNavigator(); 539 String url = "xml/pi2.xml"; 540 log("Document [" + url + "]"); 541 Object document = nav.getDocument(url); 542 XPath contextpath = new BaseXPath("/a/c", nav); 543 log("Initial Context :: " + contextpath); 544 List list = contextpath.selectNodes(document); 545 Iterator iter = list.iterator(); 546 while (iter.hasNext()) 547 { 548 Object context = iter.next(); 549 assertCountXPath(1, context, "//processing-instruction()"); 550 assertCountXPath(1, context, "preceding-sibling::*"); 551 assertCountXPath(5, context, "preceding-sibling::node()"); 552 assertCountXPath(1, context, "preceding-sibling::*[1]"); 553 assertCountXPath(1, context, "preceding-sibling::processing-instruction()"); 554 assertValueOfXPath("order-by=\"x\"", context, "preceding-sibling::processing-instruction()"); 555 assertValueOfXPath("foo", context, "preceding-sibling::*[1]"); 556 assertValueOfXPath("order-by=\"x\"", context, "preceding-sibling::node()[2]"); 557 } 558 } 559 560 public void testid53911() throws JaxenException 561 { 562 Navigator nav = getNavigator(); 563 String url = "xml/id.xml"; 564 log("Document [" + url + "]"); 565 Object document = nav.getDocument(url); 566 XPath contextpath = new BaseXPath("/", nav); 567 log("Initial Context :: " + contextpath); 568 List list = contextpath.selectNodes(document); 569 SimpleVariableContext varContext = new SimpleVariableContext(); 570 varContext.setVariableValue(null, "foobar", "foobar"); 571 varContext.setVariableValue(null, "foo", "foo"); 572 getContextSupport().setVariableContext(varContext); 573 Iterator iter = list.iterator(); 574 while (iter.hasNext()) 575 { 576 Object context = iter.next(); 577 assertValueOfXPath("foobar", context, "$foobar"); 578 assertCountXPath(1, context, "/foo[@id=$foobar]"); 579 assertCountXPath(0, context, "/foo[@id='$foobar']"); 580 assertCountXPath(1, context, "/foo[concat($foo, 'bar')=@id]"); 581 assertCountXPath(0, context, "CD_Library/artist[@name=$artist]"); 582 } 583 } 584 585 public void testid53957() throws JaxenException 586 { 587 Navigator nav = getNavigator(); 588 String url = "xml/id.xml"; 589 log("Document [" + url + "]"); 590 Object document = nav.getDocument(url); 591 XPath contextpath = new BaseXPath("/", nav); 592 log("Initial Context :: " + contextpath); 593 List list = contextpath.selectNodes(document); 594 Iterator iter = list.iterator(); 595 while (iter.hasNext()) 596 { 597 Object context = iter.next(); 598 600 assertCountXPath(1, context, "/foo/@id/parent::foo"); 601 } 602 } 603 604 606 public void testid53975() throws JaxenException 607 { 608 Navigator nav = getNavigator(); 609 String url = "xml/id.xml"; 610 log("Document [" + url + "]"); 611 Object document = nav.getDocument(url); 612 XPath contextpath = new BaseXPath("/foo/@id", nav); 613 log("Initial Context :: " + contextpath); 614 List list = contextpath.selectNodes(document); 615 Iterator iter = list.iterator(); 616 while (iter.hasNext()) 617 { 618 Object context = iter.next(); 619 assertCountXPath(1, context, "parent::foo"); 620 } 621 } 622 623 public void testid53992() throws JaxenException 624 { 625 Navigator nav = getNavigator(); 626 String url = "xml/pi.xml"; 627 log("Document [" + url + "]"); 628 Object document = nav.getDocument(url); 629 XPath contextpath = new BaseXPath("/", nav); 630 log("Initial Context :: " + contextpath); 631 List list = contextpath.selectNodes(document); 632 Iterator iter = list.iterator(); 633 while (iter.hasNext()) 634 { 635 Object context = iter.next(); 636 assertCountXPath(3, context, "//processing-instruction()"); 637 assertCountXPath(2, context, "//processing-instruction('cheese')"); 638 try 639 { 640 Object result = assertCountXPath2(1, context, "//processing-instruction('toast')"); 641 assertValueOfXPath("is tasty", result, "string()"); 642 } 643 catch (UnsupportedAxisException e) 644 { 645 log(debug, " ## SKIPPED -- Unsupported Axis"); 646 } 647 } 648 } 649 650 652 public void testid54032() throws JaxenException 653 { 654 Navigator nav = getNavigator(); 655 String url = "xml/evaluate.xml"; 656 log("Document [" + url + "]"); 657 Object document = nav.getDocument(url); 658 XPath contextpath = new BaseXPath("/", nav); 659 log("Initial Context :: " + contextpath); 660 List list = contextpath.selectNodes(document); 661 Iterator iter = list.iterator(); 662 while (iter.hasNext()) 663 { 664 Object context = iter.next(); 665 assertCountXPath(3, context, "evaluate('//jumps/*')"); 666 assertCountXPath(1, context, "evaluate('//jumps/object/dog')"); 667 assertCountXPath(0, context, "evaluate('//jumps/object')/evaluate"); 668 assertCountXPath(1, context, "evaluate('//jumps/object')/dog"); 669 assertCountXPath(1, context, "evaluate('//jumps/*')/dog"); 670 assertCountXPath(1, context, "//metatest[ evaluate(@select) = . ]"); 671 } 672 } 673 674 public void testid54082() throws JaxenException 675 { 676 Navigator nav = getNavigator(); 677 String url = "xml/numbers.xml"; 678 log("Document [" + url + "]"); 679 Object document = nav.getDocument(url); 680 XPath contextpath = new BaseXPath("/numbers/set[1]", nav); 681 log("Initial Context :: " + contextpath); 682 List list = contextpath.selectNodes(document); 683 Iterator iter = list.iterator(); 684 while (iter.hasNext()) 685 { 686 Object context = iter.next(); 687 assertCountXPath(1, context, "*[-3 = .]"); 688 assertValueOfXPath("true", context, "54 < *"); 689 assertValueOfXPath("true", context, "55 <= *"); 690 assertValueOfXPath("false", context, "69 < *"); 691 assertValueOfXPath("true", context, "-2 > *"); 692 assertValueOfXPath("true", context, "-3 >= *"); 693 assertValueOfXPath("false", context, "-4 >= *"); 694 } 695 } 696 697 708 710 public void testid54145() throws JaxenException 711 { 712 Navigator nav = getNavigator(); 713 String url = "xml/axis.xml"; 714 log("Document [" + url + "]"); 715 Object document = nav.getDocument(url); 716 XPath contextpath = new BaseXPath("/root", nav); 717 log("Initial Context :: " + contextpath); 718 List list = contextpath.selectNodes(document); 719 Iterator iter = list.iterator(); 720 while (iter.hasNext()) 721 { 722 Object context = iter.next(); 723 assertCountXPath(0, context, "preced
|