1 19 35 36 import java.io.*; 37 import java.util.*; 38 import org.w3c.dom.*; 39 40 import org.netbeans.modules.schema2beans.*; 41 import book.*; 42 43 44 public class TestBook extends BaseTest { 45 public static void main(String [] argv) { 46 TestBook o = new TestBook(); 47 if (argv.length > 0) 48 o.setDocumentDir(argv[0]); 49 try { 50 o.run(); 51 } catch (Exception e) { 52 e.printStackTrace(); 53 System.exit(1); 54 } 55 System.exit(0); 56 } 57 58 public void run() 59 throws Exception { 60 Book book; 61 62 this.readDocument(); 63 64 out("creating the bean graph"); 65 book = Book.createGraph(doc); 66 67 out("bean graph created"); 69 70 74 78 String s1, s2; 79 80 { 82 setTest("single String - get/set"); 83 s1 = book.getSummary(); 84 s1 += "Some more dynamic notes on the summary."; 85 book.setSummary(s1); 86 s2 = book.getSummary(); 87 check(s1.equals(s2)); 88 } 89 90 { 92 setTest("single String - remove"); 93 book.setSummary(null); 94 s2 = book.getSummary(); 95 check(s2==null); 96 out("should not have a 'summary' element:", 97 book.dumpDomNode("summary", 1)); 98 } 99 100 { 102 setTest("single String - set new"); 103 book.setSummary(s1); 104 s2 = book.getSummary(); 105 check(s1.equals(s2)); 106 out("should have a 'summary' element:", 107 book.dumpDomNode("summary", 1)); 108 } 109 110 114 { 115 boolean b1, b2; 116 setTest("boolean - get value (from true)"); 117 b1 = book.isGood(); 118 check(b1==true); 119 out("should have a 'good' element:", 120 book.dumpDomNode("good", 1)); 121 122 setTest("boolean - change to same value (true)"); 123 book.setGood(b1); 124 check(b1==book.isGood()); 125 out("should still have a 'good' element:", 126 book.dumpDomNode("good", 1)); 127 128 setTest("boolean - change to false"); 129 book.setGood(!b1); 130 check(b1!=book.isGood()); 131 out("should not have a 'good' element:", 132 book.dumpDomNode("good", 1)); 133 134 setTest("boolean - change back to true"); 135 book.setGood(b1); 136 check(b1==book.isGood()); 137 out("should have a 'good' element:", 138 book.dumpDomNode("good", 1)); 139 } 140 141 { 145 boolean b1, b2; 146 setTest("boolean - get value (from false)"); 147 b1 = book.isAvailable(); 148 check(b1==false); 149 out("should not have an 'available' element:", 150 book.dumpDomNode("available", 1)); 151 152 setTest("boolean - change to same value (false)"); 153 book.setAvailable(b1); 154 check(b1==book.isAvailable()); 155 out("should not have an 'available' element:", 156 book.dumpDomNode("available", 1)); 157 158 setTest("boolean - change to true"); 159 book.setAvailable(!b1); 160 check(b1!=book.isAvailable()); 161 out("should have now an 'available' element:", 162 book.dumpDomNode("available", 1)); 163 164 setTest("boolean - change back to false"); 165 book.setAvailable(b1); 166 check(b1==book.isAvailable()); 167 out("should not have an 'available' element:", 168 book.dumpDomNode("available", 1)); 169 } 170 171 172 184 { 185 out("String[] - search for the string arrays"); 186 out("found " + book.sizeChapter() + " chapter in book"); 187 for (int i=0; i<book.sizeChapter(); i++) { 188 Chapter c = book.getChapter(i); 189 out("chapter " + (i+1) + " has " + c.sizeParagraph() + 190 " paragraphs:", c.dumpDomNode("paragraph", 1)); 191 } 192 } 193 194 { 196 Chapter c = null; 197 198 setTest("String[0] - search for empty array"); 199 for (int i=0; i<book.sizeChapter(); i++) { 200 Chapter c2 = book.getChapter(i); 201 if (c2.sizeParagraph() == 0) { 202 c = c2; 203 break; 204 } 205 } 206 check(c != null); 207 208 int tmp[] = {-1, 0, 1, 999}; 209 210 for (int i=0; i<tmp.length; i++) { 212 setTest("String[0] - out of bounds access [" + tmp[i] + 213 "]"); 214 s1 = "This is a new paragraph"; 215 try { 216 c.setParagraph(tmp[i], s1); 217 check(false, "didn't get an exception"); 218 } 219 catch(IndexOutOfBoundsException e) { 220 check(true, "(IndexOutOfBoundsException)"); 221 } 222 catch(Exception ee) { 223 check(false, ee.getMessage()); 224 } 225 } 226 227 setTest("String[0] - add a string to the array"); 229 c.addParagraph(s1); 230 if (c.sizeParagraph() != 1) 231 err("array size != 1"); 232 s2 = c.getParagraph(0); 233 check(s1.equals(s2)); 234 out("should contain one paragraph:", 235 c.dumpDomNode("paragraph", 1)); 236 237 setTest("String[0] - add another string to the array"); 238 c.addParagraph(s1); 239 if (c.sizeParagraph() != 2) 240 err("array size != 2"); 241 s2 = c.getParagraph(1); 242 check(s1.equals(s2)); 243 out("should contain two paragraphs:", 244 c.dumpDomNode("paragraph", 1)); 245 246 setTest("String[0] - compare paragraphs"); 247 s1 = c.getParagraph(0); 248 s2 = c.getParagraph(1); 249 check(s1.equals(s2), "(same content)"); 250 check(s1 != s2, "(different instances)"); 251 252 setTest("String[0] - remove element"); 253 c.setParagraph(0, null); 254 check(c.sizeParagraph() == 2); 255 out("should contain one paragraph:", 256 c.dumpDomNode("paragraph", 1)); 257 258 setTest("String[0] - access elements"); 259 s2 = c.getParagraph(0); 260 check(s2 == null, "(first, should be null)"); 261 s2 = c.getParagraph(1); 262 check(s2 != null, "(second, should be !null)"); 263 264 s1 = "This is another new paragraph"; 265 setTest("String[0] - set again first element"); 266 c.setParagraph(0, s1); 267 check(c.sizeParagraph() == 2); 268 out("should contain two paragraphs:", 269 c.dumpDomNode("paragraph", 1)); 270 271 setTest("String[0] - removing all elements"); 272 s1 = c.getParagraph(1); 273 int i1 = c.removeParagraph(s1); 274 check(i1 == 1, "(removed correct index)"); 275 check(c.sizeParagraph() == 1, "(size is now 1)"); 276 s1 = c.getParagraph(0); 277 int i2 = c.removeParagraph(s1); 278 check(i2 == 0, "(removed correct index)"); 279 check(c.sizeParagraph() == 0, "(size is now 0)"); 280 out("should contain no paragraph:", 281 c.dumpDomNode("paragraph", 1)); 282 283 } 284 285 { 287 Chapter c = null; 288 s1 = "This is again another new paragraph"; 289 290 setTest("String[0] - search for array of size 1"); 291 for (int i=0; i<book.sizeChapter(); i++) { 292 Chapter c2 = book.getChapter(i); 293 if (c2.sizeParagraph() == 1) { 294 c = c2; 295 break; 296 } 297 } 298 check(c != null); 299 300 int tmp[] = {-1, 1, 999}; 301 302 for (int i=0; i<tmp.length; i++) { 304 setTest("String[0] - out of bounds access [" + tmp[i] + 305 "]"); 306 try { 307 c.setParagraph(tmp[i], s1); 308 check(false, "didn't get an exception"); 309 } 310 catch(IndexOutOfBoundsException e) { 311 check(true, "(IndexOutOfBoundsException)"); 312 } 313 catch(Exception ee) { 314 check(false, ee.getMessage()); 315 } 316 } 317 318 setTest("String[0] - add a string to the array"); 320 c.addParagraph(s1); 321 if (c.sizeParagraph() != 2) 322 err("array size != 2"); 323 s2 = c.getParagraph(1); 324 check(s1.equals(s2)); 325 out("should contain two paragraphs:", 326 c.dumpDomNode("paragraph", 1)); 327 328 setTest("String[0] - compare paragraphs"); 329 s1 = c.getParagraph(0); 330 s2 = c.getParagraph(1); 331 check(!s1.equals(s2), "(different content)"); 332 333 setTest("String[0] - add more paragraphs"); 334 s1 = "I always wanted to be a kind of a big paragraph " + 335 " as I always dreamed to fill an XML paragraph element " + 336 " only to make sure it could be large enough stuffed with " + 337 " useless characters."; 338 int size = 100; 339 for (int i=0; i<size; i++) 340 c.addParagraph(s1); 341 check(c.sizeParagraph() == size+2 , 342 "(" + size + " new paragraphs)"); 343 344 setTest("String[0] - removing paragraphs (leaving 10)"); 345 for (int i=0; i<size-8; i++) 346 c.removeParagraph(s1); 347 check(c.sizeParagraph() == 10); 348 out("should contain 10 paragraphs:", 349 c.dumpDomNode("paragraph", 1)); 350 } 351 352 353 { 373 Index idx; 374 375 int size = book.sizeIndex(); 376 out("book has " + size + " Index beans", 377 book.dumpDomNode("index", 1)); 378 379 setTest("remove a bean w/ remove()"); 380 idx = book.getIndex(0); 381 int i1 = book.removeIndex(idx); 382 check(i1==0, "(correct element removed)"); 383 check(book.sizeIndex()==size-1, "(correct Index array size)"); 384 out("book should have " + (size - 1) + " Index beans", 385 book.dumpDomNode("index", 1)); 386 387 setTest("remove another bean w/ set(null)"); 388 idx = book.getIndex(0); 389 book.setIndex(0, null); 390 check(book.sizeIndex()==size-1, "(correct Index array size)"); 391 out("book should have " + (size - 2) + " Index beans", 392 book.dumpDomNode("index", 1)); 393 394 setTest("add an empty bean to the graph"); 395 idx = new Index(); 396 book.addIndex(idx); 397 check(book.sizeIndex()==size, "(correct Index array size)"); 398 out("book should have " + (size - 1) + " Index beans", 399 book.dumpDomNode("index", 1)); 400 401 setTest("add a subtree bean"); 402 Ref r = new Ref(); 403 r.setPage("122"); 404 r.setLine("32"); 405 idx = new Index(); 406 idx.addRef(r); 407 r = new Ref(); 408 r.setPage("1"); 409 r.setLine("3"); 410 idx.addRef(r); 411 idx.setWord("who"); 412 book.setIndex(0, idx); 413 out("book should have " + size + " Index beans", 414 book.dumpDomNode("index", 1)); 415 out("idx should have 2 refs", 416 idx.dumpDomNode(3)); 417 check(book.sizeIndex() == size); 418 419 setTest("add another subtree bean"); 420 r = new Ref(); 421 r.setPage("22"); 422 r.setLine("2"); 423 idx = new Index(); 424 idx.setWord("what"); 425 idx.addRef(r); 426 book.addIndex(idx); 427 out("book should have " + (size+1) + " Index beans", 428 book.dumpDomNode("index", 1)); 429 out("idx should have 1 ref", 430 idx.dumpDomNode(3)); 431 check(book.sizeIndex() == size+1); 432 433 setTest("should failed adding the same instance subtree"); 434 try { 435 book.addIndex(idx); 436 check(false, "didn't get an exception"); 437 } 438 catch(IllegalArgumentException e) { 439 check(true, "\n >> caught: " + e); 440 } 441 catch(Exception ee) { 442 ee.printStackTrace(); 443 check(false, "\n >> got wrong type of exception: " + ee); 444 } 445 446 setTest("add the same cloned tree"); 447 int i2 = book.addIndex((Index)book.getIndex(0).clone()); 448 out("book should have " + (i2+1) + " Index beans", 449 book.dumpDomNode("index", 1)); 450 451 out("Initial Index is:", 452 book.getIndex(0).dumpDomNode(3)); 453 out("New Index should be identical:", 454 book.getIndex(i2).dumpDomNode(3)); 455 456 check(book.sizeIndex() == i2+1); 457 } 458 459 { 466 Index idx = new Index(); 467 idx.setWord("oops"); 468 setTest("add beans"); 469 Ref r = new Ref(); 470 r.setPage("999"); 471 r.setLine("9"); 472 idx.addRef(r); 473 r = new Ref(); 474 r.setPage("888"); 475 r.setLine("8"); 476 idx.addRef(r); 477 check(idx.sizeRef()==2, "(correct size of array)"); 478 out("should have nothing to print: ", idx.dumpDomNode(1)); 479 setTest("remove an element"); 480 idx.removeRef(r); 481 check(idx.sizeRef()==1, "(correct size of array)"); 482 setTest("remove last element"); 483 idx.setRef(0, null); 484 check(idx.sizeRef()==1, "(correct size)"); 485 r = new Ref(); 486 r.setPage("77"); 487 r.setLine("7"); 488 idx.setRef(0, r); 489 out("add an element and test getValues:", idx.dumpBeanNode()); 490 } 491 492 setTest("creating the root for a brand new graph"); 497 book = Book.createGraph(); 498 check(book != null); 499 500 setTest("populating the graph"); 501 book.setGood(false); 502 book.setAvailable(true); 503 book.setSummary("This book is about avoiding summaries at the end of books"); 504 Chapter c = new Chapter(); 505 c.setComment("What's a good summary in chapter 1"); 506 c.setComment2("Additional comment"); 507 c.addParagraph("This is the first paragraph"); 508 c.addParagraph("This is a second paragraph"); 509 book.addChapter(c); 510 511 c = new Chapter(); 512 book.addChapter(c); 513 c.setComment("Yet another comment for chapter 2"); 514 c.addParagraph("only one paragraph in this second chapter"); 515 516 c = new Chapter(); 517 book.addChapter(c); 518 519 Index i = new Index(); 520 i.setWord("summary"); 521 522 Ref r = new Ref(); 523 r.setPage("22"); 524 r.setLine("12"); 525 i.addRef(r); 526 527 r = new Ref(); 528 i.addRef(r); 529 r.setPage("4"); 530 r.setLine("5"); 531 532 book.addIndex(i); 533 534 check(true); 535 536 out("print the new graph DOM nodes:", 537 book.dumpDomNode(9)); 538 539 out("Re-read the original XML file for array getter/setter testing"); 540 this.readDocument(); 541 book = Book.createGraph(doc); 542 out("bean graph created"); 543 544 { 545 setTest("check Index[] getter() method"); 546 Index []a1 = book.getIndex(); 547 check(a1.length == book.sizeIndex(), "(correct array size)"); 548 check(a1[0] == book.getIndex(0), "(same first element)"); 549 check(a1[1] == book.getIndex(1), "(same second element)"); 550 check(a1[0] != book.getIndex(1), "([0] != [1])"); 551 setTest("check Chapter[] getter() method"); 552 Chapter []a2 = book.getChapter(); 553 check(a2.length == book.sizeChapter(), "(correct array size)"); 554 check(a2[0] == book.getChapter(0), "(same first element)"); 555 check(a2[1] == book.getChapter(1), "(same second element)"); 556 check(a2[2] == book.getChapter(2), "(same third element)"); 557 check(a2[0] != book.getChapter(2), "([0] != [2])"); 558 setTest("check Chapter.Paragraph getter() method"); 559 String []p = a2[0].getParagraph(); 560 check(p.length == book.getChapter(0).sizeParagraph(), 561 "(correct array size)"); 562 p = a2[1].getParagraph(); 563 check(p.length == book.getChapter(1).sizeParagraph(), 564 "(correct array size)"); 565 p = a2[2].getParagraph(); 566 check(p.length == book.getChapter(2).sizeParagraph(), 567 "(correct array size)"); 568 setTest("change the Chapter[] order"); 569 int []s = new int[3]; 570 s[0] = a2[0].sizeParagraph(); 571 s[1] = a2[1].sizeParagraph(); 572 s[2] = a2[2].sizeParagraph(); 573 Chapter []a3 = new Chapter[3]; 574 a3[2] = (Chapter)a2[0]; 575 a3[1] = (Chapter)a2[1]; 576 a3[0] = (Chapter)a2[2]; 577 book.setChapter(a3); 578 check(book.getChapter(2).sizeParagraph()==s[0], 579 "(reversed first element)"); 580 check(book.getChapter(1).sizeParagraph()==s[1], 581 "(same second element)"); 582 check(book.getChapter(0).sizeParagraph()==s[2], 583 "(reversed third element)"); 584 585 setTest("Chapter going in and out of Book"); 586 out("Make sure the contents of a Chapter object remain intact after removing that Chapter from the Book"); 587 Chapter undecidedChapter = new Chapter(); 588 undecidedChapter.setComment("This chapter may not make it into the book."); 589 out(undecidedChapter.getComment()); 590 book.addChapter(undecidedChapter); 591 out(book.sizeChapter()); 592 check(book.getChapter(book.sizeChapter()-1) == undecidedChapter, "undecided chapter is in there"); 593 book.removeChapter(undecidedChapter); 594 out("make sure undecidedChapter is intact"); 595 out(undecidedChapter.getComment()); 596 out("make sure book is intact"); 597 out(book.sizeChapter()); 598 out("Put the chapter back in."); 599 book.addChapter(undecidedChapter); 600 out(book); 601 } 602 603 this.readDocument(); 605 out("creating the bean graph"); 606 book = Book.createGraph(doc); 607 608 setTest("check if the root has any choice prop"); 609 Iterator it = book.listChoiceProperties(); 610 check(!it.hasNext(), "(none found)"); 611 check(book.listChoiceProperties("Chapter") == null, "(none found on Chapter prop)"); 612 check(book.listChoiceProperties("index") == null, "(none found on index prop)"); 613 setTest("test extra prop"); 614 615 BaseProperty[] bps; 616 Extra e = book.getExtra(); 617 it = e.listChoiceProperties(); 618 check(it.hasNext(), "(at least one)"); 619 List<BaseProperty[]> propertiesList = new ArrayList<BaseProperty[]>(); 622 while (it.hasNext()) { 623 propertiesList.add((BaseProperty[])it.next()); 624 } 625 Collections.sort(propertiesList, new Comparator<BaseProperty[]>(){ 626 public int compare(BaseProperty[] o1, BaseProperty []o2) { 627 return o1[0].getName().compareTo(o2[0].getName()); 628 }}); 629 630 for (BaseProperty[] each : propertiesList){ 631 out("Getting a set of choice properties:"); 632 this.printChoiceProperties(each); 633 } 634 635 out("Getting the same list 3 times:"); 636 bps = e.listChoiceProperties("Size"); 637 this.printChoiceProperties(bps); 638 bps = e.listChoiceProperties("size-cm"); 639 this.printChoiceProperties(bps); 640 bps = e.listChoiceProperties("size-inches"); 641 this.printChoiceProperties(bps); 642 643 out("Getting twice the same list:"); 644 bps = e.listChoiceProperties("WeightKg"); 645 this.printChoiceProperties(bps); 646 bps = e.listChoiceProperties("weight-lb"); 647 this.printChoiceProperties(bps); 648 649 check(!e.isChoiceProperty("color"), " color not choice prop"); 650 651 656 this.parse(book, "/Book/Good"); 657 this.parse(book, "/Good"); 658 this.parse(book, "Summary/"); 659 book.setSummary(null); 660 this.parse(book, "Summary"); 661 this.parse(book, "Available"); 662 this.parse(book, "Extra"); 663 this.parse(book, "Chapter"); 664 this.parse(book, "Chapter/Comment"); 665 this.parse(book, "Chapter/Paragraph"); 666 this.parse(book, "index/ref/line"); 667 668 out(book.dumpBeanNode()); 669 this.parse(book, "/Book/Index.Word=E-Tools/Ref/Page"); 670 this.parse(book, "/Book/Index.Word=E-Tool/Ref/Page"); 671 this.parse(book, "/Book/Index.Word=E-Tool/Ref.Line=15/Page"); 672 this.parse(book, "/Book/Index.Word=E-Tool/Ref.Line=15/Page=22"); 673 this.parse(book, "/Book/Index.Word=E-Tool/Ref.Line=15/Page=5"); 674 675 book = new Book(); 676 out(book.dumpBeanNode()); 677 this.parse(book, "/Book/Index.Word=E-Tools/Ref.Line=15/Page!"); 678 out(book.dumpBeanNode()); 679 this.parse(book, "/Book/Chapter.Comment=My Comment"); 680 out(book.dumpBeanNode()); 681 this.parse(book, "/Book/Chapter.Comment=My Comment!"); 682 out(book.dumpBeanNode()); 683 this.parse(book, "/Book/Index.Word=the word/Ref.Line=10/Page=20!"); 684 out(book.dumpBeanNode()); 685 686 setTest("simple merge"); 689 this.readDocument(); 690 out("creating the bean graph"); 691 book = Book.createGraph(doc); 692 Book b2 = (Book)book.clone(); 693 s1 = "Some more dynamic notes on the summary."; 694 b2.setSummary(s1); 695 b2.getChapter(0).setComment("New Comment"); 696 book.merge(b2); 697 check(book.isEqualTo(b2), "identical graphs"); 698 699 Chapter c1 = new Chapter(); 701 Chapter c2 = new Chapter(); 702 c1.merge(c2); 703 704 } 705 706 707 void parse(BaseBean bean, String parse) { 708 out("Parsing " + parse); 709 DDParser p = new DDParser(bean, parse); 710 while (p.hasNext()) { 711 Object o = p.next(); 712 if (o != null) { 713 if (o instanceof BaseBean) 714 this.out(((BaseBean)o).dumpBeanNode()); 715 else 716 this.out(o.toString()); 717 } 718 else 719 this.out("null"); 720 } 721 } 722 723 void printChoiceProperties(BaseProperty[] bps) { 724 if (bps == null) 725 err("got null instead a BaseProperty[] instance"); 726 else { 727 for (int l=0; l<bps.length; l++) 728 check(bps[l].isChoiceProperty(), bps[l].getDtdName()); 729 } 730 } 731 } 732 733 | Popular Tags |