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