1 17 18 19 package org.apache.commons.digester; 20 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.StringReader ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 33 import org.xml.sax.SAXException ; 34 35 37 43 public class CallMethodRuleTestCase extends TestCase { 44 45 46 48 49 52 protected Digester digester = null; 53 54 55 57 58 63 public CallMethodRuleTestCase(String name) { 64 65 super(name); 66 67 } 68 69 70 72 73 76 public void setUp() { 77 78 digester = new Digester(); 79 80 } 81 82 83 86 public static Test suite() { 87 88 return (new TestSuite(CallMethodRuleTestCase.class)); 89 90 } 91 92 93 96 public void tearDown() { 97 98 digester = null; 99 100 } 101 102 103 104 106 107 111 public void testBasic() throws SAXException , IOException { 112 113 digester.addObjectCreate("employee", Employee.class); 115 digester.addCallMethod("employee", "toString", 0, (Class [])null); 117 digester.addCallMethod("employee", "toString", 0, (String [])null); 118 digester.addCallMethod("employee", "toString", 0, new Class [] {}); 119 digester.addCallMethod("employee", "toString", 0, new String [] {}); 120 digester.addCallMethod("employee", "toString"); 121 122 Object root1 = null; 124 root1 = digester.parse(getInputStream("Test5.xml")); 126 127 } 128 129 130 134 public void testCallMethodOnly() throws Exception { 135 136 digester.addObjectCreate("employee", Employee.class); 138 digester.addCallMethod("employee/firstName", "setFirstName", 0); 139 digester.addCallMethod("employee/lastName", "setLastName", 0); 140 141 Employee employee = (Employee) 143 digester.parse(getInputStream("Test9.xml")); 144 assertNotNull("parsed an employee", employee); 145 146 assertEquals("Set first name", "First Name", employee.getFirstName()); 148 assertEquals("Set last name", "Last Name", employee.getLastName()); 149 } 150 151 152 157 public void testSettingProperties() throws SAXException , IOException { 158 159 digester.addObjectCreate("employee", Employee.class); 161 digester.addCallMethod("employee", "setLastName", 1, 163 new String [] {"java.lang.String"}); 164 digester.addCallParam("employee/lastName", 0); 165 166 Object root1 = null; 168 169 root1 = digester.parse(getInputStream("Test5.xml")); 171 Employee employee = (Employee) root1; 172 assertEquals("Failed to call Employee.setLastName", 173 "Last Name", employee.getLastName()); 174 175 176 digester = new Digester(); 177 digester.addObjectCreate("employee", Employee.class); 179 digester.addCallMethod("employee", "setAge", 1, 181 new Class [] {int.class}); 182 digester.addCallParam("employee/age", 0); 183 184 root1 = null; 186 187 root1 = digester.parse(getInputStream("Test5.xml")); 189 employee = (Employee) root1; 190 assertEquals("Failed to call Employee.setAge", 21, employee.getAge()); 191 192 digester = new Digester(); 193 digester.addObjectCreate("employee", Employee.class); 195 digester.addCallMethod("employee", "setActive", 1, 196 new Class [] {boolean.class}); 197 digester.addCallParam("employee/active", 0); 198 199 root1 = null; 201 202 root1 = digester.parse(getInputStream("Test5.xml")); 204 employee = (Employee) root1; 205 assertEquals("Failed to call Employee.setActive", 206 true, employee.isActive()); 207 208 digester = new Digester(); 209 digester.addObjectCreate("employee", Employee.class); 211 digester.addCallMethod("employee", "setSalary", 1, 212 new Class [] {float.class}); 213 digester.addCallParam("employee/salary", 0); 214 215 root1 = null; 217 root1 = digester.parse(getInputStream("Test5.xml")); 219 employee = (Employee) root1; 220 assertEquals("Failed to call Employee.setSalary", 221 1000000.0f, employee.getSalary(), 0.1f); 222 } 223 224 225 229 public void testParamsFromStack() throws SAXException , IOException { 230 231 StringBuffer xml = new StringBuffer (). 232 append("<?xml version='1.0'?>"). 233 append("<map>"). 234 append(" <key name='The key'/>"). 235 append(" <value name='The value'/>"). 236 append("</map>"); 237 238 digester.addObjectCreate("map", HashMap .class); 239 digester.addCallMethod("map", "put", 2); 240 digester.addObjectCreate("map/key", AlphaBean.class); 241 digester.addSetProperties("map/key"); 242 digester.addCallParam("map/key", 0, true); 243 digester.addObjectCreate("map/value", BetaBean.class); 244 digester.addSetProperties("map/value"); 245 digester.addCallParam("map/value", 1, true); 246 247 Map map = (Map ) digester.parse(new StringReader (xml.toString())); 248 249 assertNotNull(map); 250 assertEquals(1, map.size()); 251 assertEquals("The key", 252 ((AlphaBean)map.keySet().iterator().next()).getName()); 253 assertEquals("The value", 254 ((BetaBean)map.values().iterator().next()).getName()); 255 } 256 257 258 266 public void testOrderNestedPartA() throws Exception { 267 268 270 digester.addObjectCreate("toplevel/element", NamedBean.class); 276 digester.addCallMethod("toplevel/element", "setName", 1); 277 digester.addCallParam("toplevel/element/element/element", 0, "name"); 278 279 digester.addObjectCreate("toplevel/element/element", NamedBean.class); 280 281 NamedBean root1 = null; 283 try { 284 root1 = (NamedBean) digester.parse(getInputStream("Test8.xml")); 286 287 } catch (Throwable t) { 288 fail("Digester threw Exception: " + t); 290 } 291 292 assertEquals("Wrong method call order", "C", root1.getName()); 299 } 300 301 311 public void testOrderNestedPartB() throws Exception { 312 313 StringBuffer word = new StringBuffer (); 315 digester.push(word); 316 digester.addCallMethod("*/element", "append", 1); 317 digester.addCallParam("*/element", 0, "name"); 318 319 Object root1 = null; 321 try { 322 root1 = digester.parse(getInputStream("Test8.xml")); 324 325 } catch (Throwable t) { 326 fail("Digester threw Exception: " + t); 328 } 329 330 assertEquals("Wrong method call order", "CBA", word.toString()); 331 } 332 333 public void testPrimitiveReading() throws Exception { 334 StringReader reader = new StringReader ( 335 "<?xml version='1.0' ?><root><bean good='true'/><bean good='false'/><bean/>" 336 + "<beanie bad='Fee Fie Foe Fum' good='true'/><beanie bad='Fee Fie Foe Fum' good='false'/>" 337 + "<beanie bad='Fee Fie Foe Fum'/></root>"); 338 339 Digester digester = new Digester(); 340 341 345 digester.addObjectCreate("root/bean", PrimitiveBean.class); 346 digester.addSetNext("root/bean", "add"); 347 Class [] params = { Boolean.TYPE }; 348 digester.addCallMethod("root/bean", "setBoolean", 1, params); 349 digester.addCallParam("root/bean", 0, "good"); 350 351 digester.addObjectCreate("root/beanie", PrimitiveBean.class); 352 digester.addSetNext("root/beanie", "add"); 353 Class [] beanieParams = { String .class, Boolean.TYPE }; 354 digester.addCallMethod("root/beanie", "testSetBoolean", 2, beanieParams); 355 digester.addCallParam("root/beanie", 0, "bad"); 356 digester.addCallParam("root/beanie", 1, "good"); 357 358 ArrayList list = new ArrayList (); 359 digester.push(list); 360 digester.parse(reader); 361 362 assertEquals("Wrong number of beans in list", 6, list.size()); 363 PrimitiveBean bean = (PrimitiveBean) list.get(0); 364 assertTrue("Bean 0 property not called", bean.getSetBooleanCalled()); 365 assertEquals("Bean 0 property incorrect", true, bean.getBoolean()); 366 bean = (PrimitiveBean) list.get(1); 367 assertTrue("Bean 1 property not called", bean.getSetBooleanCalled()); 368 assertEquals("Bean 1 property incorrect", false, bean.getBoolean()); 369 bean = (PrimitiveBean) list.get(2); 370 assertTrue("Bean 2 property called", !bean.getSetBooleanCalled()); 372 bean = (PrimitiveBean) list.get(3); 373 assertTrue("Bean 3 property not called", bean.getSetBooleanCalled()); 374 assertEquals("Bean 3 property incorrect", true, bean.getBoolean()); 375 bean = (PrimitiveBean) list.get(4); 376 assertTrue("Bean 4 property not called", bean.getSetBooleanCalled()); 377 assertEquals("Bean 4 property incorrect", false, bean.getBoolean()); 378 bean = (PrimitiveBean) list.get(5); 379 assertTrue("Bean 5 property not called", bean.getSetBooleanCalled()); 380 assertEquals("Bean 5 property incorrect", false, bean.getBoolean()); 381 } 382 383 public void testFromStack() throws Exception { 384 385 StringReader reader = new StringReader ( 386 "<?xml version='1.0' ?><root><one/><two/><three/><four/><five/></root>"); 387 388 Digester digester = new Digester(); 389 390 Class [] params = { String .class }; 391 392 digester.addObjectCreate("root/one", NamedBean.class); 393 digester.addSetNext("root/one", "add"); 394 digester.addCallMethod("root/one", "setName", 1, params); 395 digester.addCallParam("root/one", 0, 2); 396 397 digester.addObjectCreate("root/two", NamedBean.class); 398 digester.addSetNext("root/two", "add"); 399 digester.addCallMethod("root/two", "setName", 1, params); 400 digester.addCallParam("root/two", 0, 3); 401 402 digester.addObjectCreate("root/three", NamedBean.class); 403 digester.addSetNext("root/three", "add"); 404 digester.addCallMethod("root/three", "setName", 1, params); 405 digester.addCallParam("root/three", 0, 4); 406 407 digester.addObjectCreate("root/four", NamedBean.class); 408 digester.addSetNext("root/four", "add"); 409 digester.addCallMethod("root/four", "setName", 1, params); 410 digester.addCallParam("root/four", 0, 5); 411 412 digester.addObjectCreate("root/five", NamedBean.class); 413 digester.addSetNext("root/five", "add"); 414 Class [] newParams = { String .class, String .class }; 415 digester.addCallMethod("root/five", "test", 2, newParams); 416 digester.addCallParam("root/five", 0, 10); 417 digester.addCallParam("root/five", 1, 3); 418 419 digester.push("That lamb was sure to go."); 421 digester.push("And everywhere that Mary went,"); 422 digester.push("It's fleece was white as snow."); 423 digester.push("Mary had a little lamb,"); 424 425 ArrayList list = new ArrayList (); 426 digester.push(list); 427 digester.parse(reader); 428 429 assertEquals("Wrong number of beans in list", 5, list.size()); 430 NamedBean bean = (NamedBean) list.get(0); 431 assertEquals("Parameter not set from stack (1)", "Mary had a little lamb,", bean.getName()); 432 bean = (NamedBean) list.get(1); 433 assertEquals("Parameter not set from stack (2)", "It's fleece was white as snow.", bean.getName()); 434 bean = (NamedBean) list.get(2); 435 assertEquals("Parameter not set from stack (3)", "And everywhere that Mary went,", bean.getName()); 436 bean = (NamedBean) list.get(3); 437 assertEquals("Parameter not set from stack (4)", "That lamb was sure to go.", bean.getName()); 438 bean = (NamedBean) list.get(4); 439 assertEquals("Out of stack not set to null", null , bean.getName()); 440 } 441 442 public void testTwoCalls() throws Exception { 443 444 445 StringReader reader = new StringReader ( 446 "<?xml version='1.0' ?><root>" 447 + "<param class='int' coolness='true'>25</param>" 448 + "<param class='long'>50</param>" 449 + "<param class='float' coolness='false'>90</param></root>"); 450 451 Digester digester = new Digester(); 452 456 digester.addObjectCreate( "root/param", ParamBean.class ); 457 digester.addSetNext( "root/param", "add" ); 458 digester.addCallMethod( "root/param", "setThisAndThat", 2 ); 459 digester.addCallParam( "root/param", 0, "class" ); 460 digester.addCallParam( "root/param", 1 ); 461 digester.addCallMethod( "root/param", "setCool", 1, new Class [] {boolean.class } ); 462 digester.addCallParam( "root/param", 0, "coolness" ); 463 464 ArrayList list = new ArrayList (); 465 digester.push(list); 466 digester.parse(reader); 467 468 assertEquals("Wrong number of objects created", 3, list.size()); 469 ParamBean bean = (ParamBean) list.get(0); 470 assertEquals("Coolness wrong (1)", true, bean.isCool()); 471 assertEquals("This wrong (1)", "int", bean.getThis()); 472 assertEquals("That wrong (1)", "25", bean.getThat()); 473 bean = (ParamBean) list.get(1); 474 assertEquals("Coolness wrong (2)", false, bean.isCool()); 475 assertEquals("This wrong (2)", "long", bean.getThis()); 476 assertEquals("That wrong (2)", "50", bean.getThat()); 477 bean = (ParamBean) list.get(2); 478 assertEquals("Coolness wrong (3)", false, bean.isCool()); 479 assertEquals("This wrong (3)", "float", bean.getThis()); 480 assertEquals("That wrong (3)", "90", bean.getThat()); 481 } 482 483 public void testNestedBody() throws Exception { 484 485 StringReader reader = new StringReader ( 486 "<?xml version='1.0' ?><root>" 487 + "<spam>Simple</spam>" 488 + "<spam>Complex<spam>Deep<spam>Deeper<spam>Deepest</spam></spam></spam></spam>" 489 + "</root>"); 490 491 Digester digester = new Digester(); 492 493 497 498 digester.addObjectCreate("root/spam", NamedBean.class); 499 digester.addSetRoot("root/spam", "add"); 500 digester.addCallMethod( "root/spam", "setName", 1 ); 501 digester.addCallParam( "root/spam", 0); 502 503 digester.addObjectCreate("root/spam/spam", NamedBean.class); 504 digester.addSetRoot("root/spam/spam", "add"); 505 digester.addCallMethod( "root/spam/spam", "setName", 1 ); 506 digester.addCallParam( "root/spam/spam", 0); 507 508 digester.addObjectCreate("root/spam/spam/spam", NamedBean.class); 509 digester.addSetRoot("root/spam/spam/spam", "add"); 510 digester.addCallMethod( "root/spam/spam/spam", "setName", 1 ); 511 digester.addCallParam( "root/spam/spam/spam", 0); 512 513 514 digester.addObjectCreate("root/spam/spam/spam/spam", NamedBean.class); 515 digester.addSetRoot("root/spam/spam/spam/spam", "add"); 516 digester.addCallMethod( "root/spam/spam/spam/spam", "setName", 1 ); 517 digester.addCallParam( "root/spam/spam/spam/spam", 0); 518 519 ArrayList list = new ArrayList (); 520 digester.push(list); 521 digester.parse(reader); 522 523 NamedBean bean = (NamedBean) list.get(0); 524 assertEquals("Wrong name (1)", "Simple", bean.getName()); 525 bean = (NamedBean) list.get(4); 527 assertEquals("Wrong name (2)", "Complex", bean.getName()); 528 bean = (NamedBean) list.get(3); 529 assertEquals("Wrong name (3)", "Deep", bean.getName()); 530 bean = (NamedBean) list.get(2); 531 assertEquals("Wrong name (4)", "Deeper", bean.getName()); 532 bean = (NamedBean) list.get(1); 533 assertEquals("Wrong name (5)", "Deepest", bean.getName()); 534 } 535 536 public void testProcessingHook() throws Exception { 537 538 class TestCallMethodRule extends CallMethodRule { 539 Object result; 540 TestCallMethodRule(String methodName, int paramCount) 541 { 542 super(methodName, paramCount); 543 } 544 protected void processMethodCallResult(Object result) { 545 this.result = result; 546 } 547 } 548 549 StringReader reader = new StringReader ( 550 "<?xml version='1.0' ?><root>" 551 + "<param class='float' coolness='false'>90</param></root>"); 552 553 554 Digester digester = new Digester(); 555 559 digester.addObjectCreate( "root/param", ParamBean.class ); 560 digester.addSetNext( "root/param", "add" ); 561 TestCallMethodRule rule = new TestCallMethodRule( "setThisAndThat" , 2 ); 562 digester.addRule( "root/param", rule ); 563 digester.addCallParam( "root/param", 0, "class" ); 564 digester.addCallParam( "root/param", 1, "coolness" ); 565 566 ArrayList list = new ArrayList (); 567 digester.push(list); 568 digester.parse(reader); 569 570 assertEquals("Wrong number of objects created", 1, list.size()); 571 assertEquals("Result not passed into hook", "The Other", rule.result); 572 } 573 574 575 public void testPathCallParam() throws Exception { 576 String xml = "<?xml version='1.0'?><main>" 577 + "<alpha><beta>Ignore this</beta></alpha>" 578 + "<beta><epsilon><gamma>Ignore that</gamma></epsilon></beta>" 579 + "</main>"; 580 581 SimpleTestBean bean = new SimpleTestBean(); 582 bean.setAlphaBeta("[UNSET]", "[UNSET]"); 583 584 StringReader in = new StringReader (xml); 585 Digester digester = new Digester(); 586 digester.setRules(new ExtendedBaseRules()); 587 digester.addCallParamPath("*/alpha/?", 0); 588 digester.addCallParamPath("*/epsilon/?", 1); 589 digester.addCallMethod("main", "setAlphaBeta", 2); 590 591 digester.push(bean); 592 593 digester.parse(in); 594 595 assertEquals("Test alpha property setting", "main/alpha/beta" , bean.getAlpha()); 596 assertEquals("Test beta property setting", "main/beta/epsilon/gamma" , bean.getBeta()); 597 } 598 599 600 603 public void testCallInvalidTarget() throws Exception { 604 605 Digester digester = new Digester(); 606 digester.addObjectCreate("employee", HashMap .class); 607 608 CallMethodRule r = new CallMethodRule(1, "put", 0); 612 digester.addRule("employee", r); 613 614 try { 615 digester.parse(getInputStream("Test5.xml")); 616 fail("Exception should be thrown for invalid target offset"); 617 } 618 catch(SAXException e) { 619 } 621 } 622 623 627 public void testCallNext() throws Exception { 628 629 Digester digester = new Digester(); 630 digester.addObjectCreate("employee", HashMap .class); 631 632 digester.addObjectCreate("employee/address", Address.class); 633 digester.addSetNestedProperties("employee/address"); 634 CallMethodRule r = new CallMethodRule(1, "put", 2); 635 digester.addRule("employee/address", r); 636 digester.addCallParam("employee/address/type", 0); 637 digester.addCallParam("employee/address", 1, 0); 638 639 HashMap map = (HashMap ) digester.parse(getInputStream("Test5.xml")); 640 641 assertNotNull(map); 642 java.util.Set keys = map.keySet(); 643 assertEquals(2, keys.size()); 644 Address home = (Address) map.get("home"); 645 assertNotNull(home); 646 assertEquals("HmZip", home.getZipCode()); 647 Address office = (Address) map.get("office"); 648 assertNotNull(office); 649 assertEquals("OfZip", office.getZipCode()); 650 } 651 652 656 public void testCallRoot() throws Exception { 657 658 Digester digester = new Digester(); 659 digester.addObjectCreate("employee", HashMap .class); 660 661 digester.addObjectCreate("employee/address", Address.class); 662 digester.addSetNestedProperties("employee/address"); 663 CallMethodRule r = new CallMethodRule(-1, "put", 2); 664 digester.addRule("employee/address", r); 665 digester.addCallParam("employee/address/type", 0); 666 digester.addCallParam("employee/address", 1, 0); 667 668 HashMap map = (HashMap ) digester.parse(getInputStream("Test5.xml")); 669 670 assertNotNull(map); 671 java.util.Set keys = map.keySet(); 672 assertEquals(2, keys.size()); 673 Address home = (Address) map.get("home"); 674 assertNotNull(home); 675 assertEquals("HmZip", home.getZipCode()); 676 Address office = (Address) map.get("office"); 677 assertNotNull(office); 678 assertEquals("OfZip", office.getZipCode()); 679 } 680 681 683 684 692 protected InputStream getInputStream(String name) throws IOException { 693 694 return (this.getClass().getResourceAsStream 695 ("/org/apache/commons/digester/" + name)); 696 697 } 698 699 700 } 701 702 | Popular Tags |