1 17 18 19 package org.apache.commons.digester; 20 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.ArrayList ; 25 26 import junit.framework.Test; 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 import org.xml.sax.SAXException ; 31 32 40 41 public class RuleTestCase extends TestCase { 42 43 44 46 47 50 protected Digester digester = null; 51 52 53 55 56 61 public RuleTestCase(String name) { 62 63 super(name); 64 65 } 66 67 68 70 71 74 public void setUp() { 75 76 digester = new Digester(); 77 78 } 79 80 81 84 public static Test suite() { 85 86 return (new TestSuite(RuleTestCase.class)); 87 88 } 89 90 91 94 public void tearDown() { 95 96 digester = null; 97 98 } 99 100 101 102 104 105 110 public void testObjectCreate1() throws SAXException , IOException { 111 112 digester.addObjectCreate("employee", 114 "org.apache.commons.digester.Employee"); 115 digester.addSetProperties("employee"); 116 117 Object root = null; 119 root = digester.parse(getInputStream("Test1.xml")); 120 121 assertNotNull("Digester returned an object", root); 122 assertTrue("Digester returned an Employee", 123 root instanceof Employee); 124 Employee employee = (Employee) root; 125 assertEquals("First name is correct", 126 "First Name", 127 employee.getFirstName()); 128 assertEquals("Last name is correct", 129 "Last Name", 130 employee.getLastName()); 131 132 133 } 134 135 136 142 public void testObjectCreate2() throws SAXException , IOException { 143 144 digester.addObjectCreate("employee", Employee.class); 146 digester.addSetProperties("employee"); 147 digester.addObjectCreate("employee/address", 148 "org.apache.commons.digester.Address"); 149 digester.addSetProperties("employee/address"); 150 151 Object root = null; 153 root = digester.parse(getInputStream("Test1.xml")); 154 155 assertNotNull("Digester returned an object", root); 156 assertTrue("Digester returned an Employee", 157 root instanceof Employee); 158 Employee employee = (Employee) root; 159 assertEquals("First name is correct", 160 "First Name", 161 employee.getFirstName()); 162 assertEquals("Last name is correct", 163 "Last Name", 164 employee.getLastName()); 165 166 167 } 168 169 170 176 public void testObjectCreate3() throws SAXException , IOException { 177 178 digester.addObjectCreate("employee", Employee.class); 180 digester.addSetProperties("employee"); 181 digester.addObjectCreate("employee/address", 182 "org.apache.commons.digester.Address"); 183 digester.addSetProperties("employee/address"); 184 digester.addSetNext("employee/address", 185 "addAddress"); 186 187 Object root = null; 189 root = digester.parse(getInputStream("Test1.xml")); 190 191 validateObjectCreate3(root); 192 193 try { 195 root = digester.parse(getInputStream("Test1.xml")); 196 } catch (Throwable t) { 197 fail("Digester threw IOException: " + t); 198 } 199 validateObjectCreate3(root); 200 201 } 202 203 204 208 public void testObjectCreate4() throws SAXException , IOException { 209 210 digester.addObjectCreate("employee", Employee.class); 212 digester.addCallMethod("employee", 213 "setFirstName", 1); 214 digester.addCallParam("employee", 0, "firstName"); 215 digester.addCallMethod("employee", 216 "setLastName", 1); 217 digester.addCallParam("employee", 0, "lastName"); 218 219 220 Object root = null; 222 root = digester.parse(getInputStream("Test1.xml")); 223 224 assertNotNull("Digester returned an object", root); 225 assertTrue("Digester returned an Employee", 226 root instanceof Employee); 227 Employee employee = (Employee) root; 228 assertEquals("First name is correct", 229 "First Name", 230 employee.getFirstName()); 231 assertEquals("Last name is correct", 232 "Last Name", 233 employee.getLastName()); 234 235 } 236 237 238 245 public void testObjectCreate5() throws SAXException , IOException { 246 247 digester.addObjectCreate("employee", Employee.class); 249 digester.addCallMethod("employee/firstName", "setFirstName", 0); 250 digester.addCallMethod("employee/lastName", "setLastName", 0); 251 252 253 Object root = null; 255 root = digester.parse(getInputStream("Test5.xml")); 256 257 assertNotNull("Digester returned an object", root); 258 assertTrue("Digester returned an Employee", 259 root instanceof Employee); 260 Employee employee = (Employee) root; 261 assertEquals("First name is correct", 262 "First Name", 263 employee.getFirstName()); 264 assertEquals("Last name is correct", 265 "Last Name", 266 employee.getLastName()); 267 268 } 269 270 271 275 public void testRepeatedParse() throws SAXException , IOException { 276 277 digester.addObjectCreate("employee", Employee.class); 279 digester.addSetProperties("employee"); 280 digester.addObjectCreate("employee/address", 281 "org.apache.commons.digester.Address"); 282 digester.addSetProperties("employee/address"); 283 digester.addSetNext("employee/address", 284 "addAddress"); 285 286 Object root1 = null; 288 root1 = digester.parse(getInputStream("Test1.xml")); 289 290 validateObjectCreate3(root1); 291 292 Object root2 = null; 294 root2 = digester.parse(getInputStream("Test1.xml")); 295 296 validateObjectCreate3(root2); 297 298 assertTrue("Different tree instances were returned", 300 root1 != root2); 301 302 } 303 304 305 311 public void testRuleSet1() throws SAXException , IOException { 312 313 RuleSet rs = new TestRuleSet(); 315 digester.addRuleSet(rs); 316 317 Object root = null; 319 root = digester.parse(getInputStream("Test1.xml")); 320 321 assertNotNull("Digester returned an object", root); 322 assertTrue("Digester returned an Employee", 323 root instanceof Employee); 324 Employee employee = (Employee) root; 325 assertEquals("First name is correct", 326 "First Name", 327 employee.getFirstName()); 328 assertEquals("Last name is correct", 329 "Last Name", 330 employee.getLastName()); 331 assertNotNull("Can retrieve home address", 332 employee.getAddress("home")); 333 assertNotNull("Can retrieve office address", 334 employee.getAddress("office")); 335 336 } 337 338 339 342 public void testRuleSet2() throws SAXException , IOException { 343 344 digester.setNamespaceAware(true); 346 RuleSet rs = new TestRuleSet(null, 347 "http://jakarta.apache.org/digester/Foo"); 348 digester.addRuleSet(rs); 349 350 Object root = null; 352 root = digester.parse(getInputStream("Test2.xml")); 353 354 assertNotNull("Digester returned an object", root); 355 assertTrue("Digester returned an Employee", 356 root instanceof Employee); 357 Employee employee = (Employee) root; 358 assertEquals("First name is correct", 359 "First Name", 360 employee.getFirstName()); 361 assertEquals("Last name is correct", 362 "Last Name", 363 employee.getLastName()); 364 assertNotNull("Can retrieve home address", 365 employee.getAddress("home")); 366 assertNotNull("Can retrieve office address", 367 employee.getAddress("office")); 368 369 } 370 371 372 377 public void testRuleSet3() throws SAXException , IOException { 378 379 digester.setNamespaceAware(true); 381 RuleSet rs = new TestRuleSet(null, 382 "http://jakarta.apache.org/digester/Foo"); 383 digester.addRuleSet(rs); 384 385 Object root = null; 387 root = digester.parse(getInputStream("Test3.xml")); 388 389 assertNotNull("Digester returned an object", root); 390 assertTrue("Digester returned an Employee", 391 root instanceof Employee); 392 Employee employee = (Employee) root; 393 assertEquals("First name is correct", 394 "First Name", 395 employee.getFirstName()); 396 assertEquals("Last name is correct", 397 "Last Name", 398 employee.getLastName()); 399 assertNull("Can not retrieve home address", 400 employee.getAddress("home")); 401 assertNull("Can not retrieve office address", 402 employee.getAddress("office")); 403 404 } 405 406 407 417 public void testSetTopRule1() throws SAXException , IOException { 418 419 digester.addObjectCreate("employee", 421 "org.apache.commons.digester.Employee"); 422 digester.addSetProperties("employee"); 423 digester.addObjectCreate("employee/address", 424 "org.apache.commons.digester.Address"); 425 digester.addSetProperties("employee/address"); 426 digester.addSetTop("employee/address", "setEmployee"); 427 428 Object root = null; 430 root = digester.parse(getInputStream("Test1.xml")); 431 validateObjectCreate3(root); 432 433 } 434 435 436 440 public void testSetTopRule2() throws SAXException , IOException { 441 442 digester.addObjectCreate("employee", 444 "org.apache.commons.digester.Employee"); 445 digester.addSetProperties("employee"); 446 digester.addObjectCreate("employee/address", 447 "org.apache.commons.digester.Address"); 448 digester.addSetProperties("employee/address"); 449 digester.addSetTop("employee/address", "setEmployee", 450 "org.apache.commons.digester.Employee"); 451 452 Object root = null; 454 root = digester.parse(getInputStream("Test1.xml")); 455 456 validateObjectCreate3(root); 457 458 } 459 460 464 public void testAddRule() { 465 Digester digester = new Digester(); 466 TestRule rule = new TestRule("Test"); 467 digester.addRule("/root", rule); 468 469 assertEquals("Digester is not properly on rule addition.", 470 digester, rule.getDigester()); 471 472 } 473 474 475 public void testSetNext() throws SAXException , IOException { 476 Digester digester = new Digester(); 477 digester.setRules(new ExtendedBaseRules()); 478 digester.setValidating(false); 479 480 481 digester.addObjectCreate("!*/b", BetaBean.class); 482 digester.addObjectCreate("!*/a", AlphaBean.class); 483 digester.addObjectCreate("root", ArrayList .class); 484 digester.addSetProperties("!*"); 485 digester.addSetNext("!*/b/?", "setChild"); 486 digester.addSetNext("!*/a/?", "setChild"); 487 digester.addSetNext("!root/?", "add"); 488 ArrayList root = 489 (ArrayList ) digester.parse(getInputStream("Test4.xml")); 490 491 assertEquals("Wrong array size", 2, root.size()); 492 AlphaBean one = (AlphaBean) root.get(0); 493 assertTrue(one.getChild() instanceof BetaBean); 494 BetaBean two = (BetaBean) one.getChild(); 495 assertEquals("Wrong name (1)", two.getName() , "TWO"); 496 assertTrue(two.getChild() instanceof AlphaBean); 497 AlphaBean three = (AlphaBean) two.getChild(); 498 assertEquals("Wrong name (2)", three.getName() , "THREE"); 499 BetaBean four = (BetaBean) root.get(1); 500 assertEquals("Wrong name (3)", four.getName() , "FOUR"); 501 assertTrue(four.getChild() instanceof BetaBean); 502 BetaBean five = (BetaBean) four.getChild(); 503 assertEquals("Wrong name (4)", five.getName() , "FIVE"); 504 505 } 506 507 508 public void testSetTop() throws SAXException , IOException { 509 Digester digester = new Digester(); 510 digester.setRules(new ExtendedBaseRules()); 511 digester.setValidating(false); 512 513 514 digester.addObjectCreate("!*/b", BetaBean.class); 515 digester.addObjectCreate("!*/a", AlphaBean.class); 516 digester.addObjectCreate("root", ArrayList .class); 517 digester.addSetProperties("!*"); 518 digester.addSetTop("!*/b/?", "setParent"); 519 digester.addSetTop("!*/a/?", "setParent"); 520 digester.addSetRoot("!*/a", "add"); 521 digester.addSetRoot("!*/b", "add"); 522 ArrayList root = 523 (ArrayList ) digester.parse(getInputStream("Test4.xml")); 524 525 assertEquals("Wrong array size", 5, root.size()); 526 527 529 Object obj = root.get(1); 530 assertTrue("TWO should be a BetaBean", obj instanceof BetaBean); 531 BetaBean two = (BetaBean) obj; 532 assertNotNull("Two's parent should not be null", two.getParent()); 533 assertEquals("Wrong name (1)", "TWO", two.getName()); 534 assertEquals("Wrong name (2)", "ONE", two.getParent().getName() ); 535 536 obj = root.get(0); 537 assertTrue("THREE should be an AlphaBean", obj instanceof AlphaBean); 538 AlphaBean three = (AlphaBean) obj; 539 assertNotNull("Three's parent should not be null", three.getParent()); 540 assertEquals("Wrong name (3)", "THREE", three.getName()); 541 assertEquals("Wrong name (4)", "TWO", three.getParent().getName()); 542 543 obj = root.get(3); 544 assertTrue("FIVE should be a BetaBean", obj instanceof BetaBean); 545 BetaBean five = (BetaBean) obj; 546 assertNotNull("Five's parent should not be null", five.getParent()); 547 assertEquals("Wrong name (5)", "FIVE", five.getName()); 548 assertEquals("Wrong name (6)", "FOUR", five.getParent().getName()); 549 550 } 551 552 553 555 public void testSetCustomProperties() throws SAXException , IOException { 556 557 Digester digester = new Digester(); 558 559 digester.setValidating(false); 560 561 digester.addObjectCreate("toplevel", ArrayList .class); 562 digester.addObjectCreate("toplevel/one", Address.class); 563 digester.addSetNext("toplevel/one", "add"); 564 digester.addObjectCreate("toplevel/two", Address.class); 565 digester.addSetNext("toplevel/two", "add"); 566 digester.addObjectCreate("toplevel/three", Address.class); 567 digester.addSetNext("toplevel/three", "add"); 568 digester.addObjectCreate("toplevel/four", Address.class); 569 digester.addSetNext("toplevel/four", "add"); 570 digester.addSetProperties("toplevel/one"); 571 digester.addSetProperties( 572 "toplevel/two", 573 new String [] {"alt-street", "alt-city", "alt-state"}, 574 new String [] {"street", "city", "state"}); 575 digester.addSetProperties( 576 "toplevel/three", 577 new String [] {"aCity", "state"}, 578 new String [] {"city"}); 579 digester.addSetProperties("toplevel/four", "alt-city", "city"); 580 581 582 ArrayList root = 583 (ArrayList ) digester.parse(getInputStream("Test7.xml")); 584 585 assertEquals("Wrong array size", 4, root.size()); 586 587 589 Object 590 obj = root.get(0); 591 assertTrue("(1) Should be an Address ", obj instanceof Address); 592 Address addressOne = (Address) obj; 593 assertEquals("(1) Street attribute", "New Street", 594 addressOne.getStreet()); 595 assertEquals("(1) City attribute", "Las Vegas", addressOne.getCity()); 596 assertEquals("(1) State attribute", "Nevada", addressOne.getState()); 597 598 obj = root.get(1); 599 assertTrue("(2) Should be an Address ", obj instanceof Address); 600 Address addressTwo = (Address) obj; 601 assertEquals("(2) Street attribute", "Old Street", 602 addressTwo.getStreet()); 603 assertEquals("(2) City attribute", "Portland", addressTwo.getCity()); 604 assertEquals("(2) State attribute", "Oregon", addressTwo.getState()); 605 606 obj = root.get(2); 607 assertTrue("(3) Should be an Address ", obj instanceof Address); 608 Address addressThree = (Address) obj; 609 assertEquals("(3) Street attribute", "4th Street", 610 addressThree.getStreet()); 611 assertEquals("(3) City attribute", "Dayton", addressThree.getCity()); 612 assertEquals("(3) State attribute", "US" , addressThree.getState()); 613 614 obj = root.get(3); 615 assertTrue("(4) Should be an Address ", obj instanceof Address); 616 Address addressFour = (Address) obj; 617 assertEquals("(4) Street attribute", "6th Street", 618 addressFour.getStreet()); 619 assertEquals("(4) City attribute", "Cleveland", addressFour.getCity()); 620 assertEquals("(4) State attribute", "Ohio", addressFour.getState()); 621 622 } 623 624 626 627 635 protected InputStream getInputStream(String name) throws IOException { 636 637 return (this.getClass().getResourceAsStream 638 ("/org/apache/commons/digester/" + name)); 639 640 } 641 642 643 648 protected void validateObjectCreate3(Object root) { 649 650 assertNotNull("Digester returned an object", root); 652 assertTrue("Digester returned an Employee", 653 root instanceof Employee); 654 Employee employee = (Employee) root; 655 assertEquals("First name is correct", 656 "First Name", 657 employee.getFirstName()); 658 assertEquals("Last name is correct", 659 "Last Name", 660 employee.getLastName()); 661 662 Address home = employee.getAddress("home"); 664 assertNotNull("Retrieved home address", home); 665 assertEquals("Home street", "Home Street", 666 home.getStreet()); 667 assertEquals("Home city", "Home City", 668 home.getCity()); 669 assertEquals("Home state", "HS", 670 home.getState()); 671 assertEquals("Home zip", "HmZip", 672 home.getZipCode()); 673 674 Address office = employee.getAddress("office"); 676 assertNotNull("Retrieved office address", office); 677 assertEquals("Office street", "Office Street", 678 office.getStreet()); 679 assertEquals("Office city", "Office City", 680 office.getCity()); 681 assertEquals("Office state", "OS", 682 office.getState()); 683 assertEquals("Office zip", "OfZip", 684 office.getZipCode()); 685 686 } 687 688 689 } 690 | Popular Tags |