1 19 23 24 import java.io.*; 25 import java.util.*; 26 import org.w3c.dom.*; 27 28 import org.netbeans.modules.schema2beans.*; 29 30 import java.beans.*; 31 32 import book.*; 33 34 35 public class TestMergeExtendBaseBean extends BaseTest { 36 public static void main(String [] argv) { 37 BaseTest o = new TestMergeExtendBaseBean(); 38 if (argv.length > 0) 39 o.setDocumentDir(argv[0]); 40 try { 41 o.run(); 42 } catch (Exception e) { 43 e.printStackTrace(); 44 System.exit(1); 45 } 46 System.exit(0); 47 } 48 49 50 51 static float memUsageReference = 236; 58 59 60 public class MyListener implements PropertyChangeListener { 61 String listenerName; 62 Object oldValue; 63 Object newValue; 64 String propertyName; 65 Object source; 66 int index; 67 boolean mute; 68 boolean remove; 69 Bean rootBean; 70 71 public MyListener(Bean bean) { 72 this.listenerName = bean.name(); 73 this.remove = false; 74 this.mute = false; 75 rootBean = bean._getRoot(); 76 out("new listener for " + this.listenerName); 77 } 78 79 public void reset() { 80 this.oldValue = null; 81 this.newValue = null; 82 this.propertyName = null; 83 this.source = null; 84 this.index = -1; 85 } 86 87 public void propertyChange(PropertyChangeEvent e) { 88 if (this.mute) 89 return; 90 91 String k; 92 this.oldValue = e.getOldValue(); 93 this.newValue = e.getNewValue(); 94 this.propertyName = e.getPropertyName(); 95 this.source = e.getSource(); 96 String n = this.propertyName; 97 this.index = GraphManager.getPropertyIndex(rootBean, n); 99 100 if (this.newValue == null) 102 k = "Rmv"; 103 else 104 if (this.oldValue == null) 105 k = "Add"; 106 else { 107 k = "Chg"; 108 } 109 110 out("<" + k + " Lnr:" + this.listenerName + " Evt:" + n + 111 " Src:" + this.source.getClass().getName() + ">"); 112 if (remove) { 113 out("<" + GraphManager.getPropertyName(n) + "[" + this.index + 114 "]" + " - Parent: " + GraphManager.getPropertyParentName(n) + ">"); 115 } else { 116 out("<" + GraphManager.getPropertyName(n) + "[" + this.index + 117 "]" + " - Parent: " + GraphManager.getPropertyParentName(n) + 118 "/" + GraphManager.getPropertyParent(rootBean, n).getClass().getName() + ">"); 119 } 120 121 print("oldValue="); 122 print(this.oldValue); 123 println(""); 124 print("newValue="); 125 print(this.newValue); 126 println(""); 127 } 128 129 public void removeMode() { 130 this.remove = true; 131 } 132 133 public Object oldValue() { 134 return this.oldValue; 135 } 136 137 public String stringOldValue() { 138 if (this.oldValue == null) 139 return "<null>"; 140 else 141 return this.oldValue.toString(); 142 } 143 144 public Object newValue() { 145 return this.newValue; 146 } 147 148 public String stringNewValue() { 149 if (this.newValue == null) 150 return "<null>"; 151 else 152 return this.newValue.toString(); 153 } 154 155 public String name() { 156 return this.propertyName; 157 } 158 159 public void mute(boolean mute) { 160 this.mute = mute; 161 } 162 163 public String toString() { 164 return this.name() + " raised from source " + 165 this.source.getClass().getName(); 166 } 167 } 168 169 private MyListener l1, l2, l3; 170 171 void mute(boolean mute) { 172 if (l1 != null) 173 l1.mute(mute); 174 if (l2 != null) 175 l2.mute(mute); 176 if (l3 != null) 177 l3.mute(mute); 178 } 179 180 public void run() 181 throws Exception { 182 Book b1, b2, b3; 183 184 this.readDocument("tm01_g1.xml"); 185 out("creating the bean graph1"); 186 b1 = Book.createGraph(doc); 187 188 this.readDocument("tm01_g1.xml"); 189 out("creating the bean graph2"); 190 b2 = Book.createGraph(doc); 191 192 l1 = new MyListener(b1); 193 b1.addPropertyChangeListener(l1); 194 195 l2 = new MyListener(b2); 198 b2.addPropertyChangeListener(l2); 199 200 setTest("Merge(Update) - two identical xml files"); 204 out("should not get any event"); 205 b1.merge(b2); 206 check(b1.toString().equals(b2.toString()), " - no diff"); 207 check(b1.equals(b2), " - equals"); 208 209 setTest("Merge(Update) - two identical xml files (one mixed up)"); 211 this.readDocument("tm01_g2.xml"); 212 out("creating the bean graph3"); 213 b3 = Book.createGraph(doc); 214 215 l3 = new MyListener(b3); 216 b3.addPropertyChangeListener(l3); 217 218 b3.merge(b1); 219 220 setTest("Merge(Update) - changing one property"); 224 String s1 = "This is the new summary"; 225 String s2 = b2.getSummary(); 226 227 this.mute(true); 229 b2.setSummary(s1); 230 out("should get one event for Summary"); 231 this.mute(false); 232 b1.merge(b2); 233 234 b1.write(System.out); 235 236 check(b2.equals(b1), " - equals"); 237 this.mute(true); 239 b1.setSummary(s2); 240 this.mute(false); 241 out("should not get any event"); 242 b1.merge(b3); 243 check(b3.equals(b1), " - equals"); 245 246 this.mute(true); 247 b2.setSummary(s2); 248 249 setTest("Merge(Update) - mixing array of strings"); 253 check(b1.getAuthor(0).equals(b2.getAuthor(0)), "initial match"); 254 check(b1.getAuthor(1).equals(b2.getAuthor(1)), "initial match"); 255 check(b1.getAuthor(2).equals(b2.getAuthor(2)), "initial match"); 256 this.mute(false); 257 String [] ss1 = b2.getAuthor(); 258 String [] ss2 = new String [3]; 259 ss2[0] = ss1[2]; 260 ss2[1] = ss1[1]; 261 ss2[2] = ss1[0]; 262 b2.setAuthor(ss2); 263 check(b1.getAuthor(0).equals(b2.getAuthor(2)), "mixed"); 264 check(b1.getAuthor(1).equals(b2.getAuthor(1)), "mixed"); 265 check(b1.getAuthor(2).equals(b2.getAuthor(0)), "mixed"); 266 267 b1.merge(b2); 268 check(b2.equals(b1), " - equals"); 270 this.mute(true); 271 b2.setAuthor(ss1); 272 check(b1.getAuthor(0).equals(b2.getAuthor(2)), "reset match"); 273 check(b1.getAuthor(1).equals(b2.getAuthor(1)), "reset match"); 274 check(b1.getAuthor(2).equals(b2.getAuthor(0)), "reset match"); 275 276 setTest("Merge(Update) - mixing array of beans"); 280 check(b1.getChapter(0).equals(b2.getChapter(0)), "initial match"); 281 check(b1.getChapter(1).equals(b2.getChapter(1)), "initial match"); 282 check(b1.getChapter(2).equals(b2.getChapter(2)), "initial match"); 283 this.mute(false); 284 Chapter [] cc1 = b2.getChapter(); 285 Chapter [] cc2 = new Chapter[3]; 286 cc2[0] = cc1[2]; 287 cc2[1] = cc1[1]; 288 cc2[2] = cc1[0]; 289 b2.setChapter(cc2); 290 check(b1.getChapter(0).equals(b2.getChapter(2)), "mixed"); 291 check(b1.getChapter(1).equals(b2.getChapter(1)), "mixed"); 292 check(b1.getChapter(2).equals(b2.getChapter(0)), "mixed"); 293 294 b1.merge(b2); 295 check(b2.equals(b1), " - equals"); 297 this.mute(true); 298 b2.setChapter(cc1); 299 check(b1.getChapter(0).equals(b2.getChapter(2)), "reset match"); 300 check(b1.getChapter(1).equals(b2.getChapter(1)), "reset match"); 301 check(b1.getChapter(2).equals(b2.getChapter(0)), "reset match"); 302 303 setTest("Merge(Update) - remove elt from array of strings"); 307 check(b1.getAuthor(0).equals(b2.getAuthor(0)), "initial match"); 308 check(b1.getAuthor(1).equals(b2.getAuthor(1)), "initial match"); 309 check(b1.getAuthor(2).equals(b2.getAuthor(2)), "initial match"); 310 this.mute(false); 311 b2 = (Book) b1.clone(); 312 s1 = b2.getAuthor(1); 313 out("should get one remove event from deletion"); 314 b2.removeAuthor(s1); 315 check(b1.getAuthor(0).equals(b2.getAuthor(0)), "match after rem"); 316 check(b1.getAuthor(2).equals(b2.getAuthor(1)), "match after rem"); 317 out("should get one Book remove event from merge"); 318 b1.merge(b2); 319 check(b1.getAuthor(0).equals(b2.getAuthor(0)), "match"); 320 check(b1.getAuthor(1).equals(b2.getAuthor(1)), "match"); 321 check(b1.sizeAuthor() == b2.sizeAuthor(), "correct size"); 322 check(b2.equals(b1), " - equals"); 323 324 setTest("Merge(Update) - add elt from array of strings"); 328 this.mute(false); 329 out("should get one event for elt added"); 330 b2.addAuthor(s1); 331 check(b2.getAuthor(2).equals(s1), "added"); 332 out("should get one event for elt added from merge"); 333 b1.merge(b2); 338 check(b1.getAuthor(0).equals(b2.getAuthor(0)), "match"); 341 check(b1.getAuthor(1).equals(b2.getAuthor(1)), "match"); 342 check(b1.getAuthor(2).equals(b2.getAuthor(2)), "match"); 343 check(b1.sizeAuthor() == b2.sizeAuthor(), "correct size"); 344 check(b2.equals(b1), " - equals"); 345 346 setTest("Merge(Update) - remove elt from array of beans"); 350 check(b1.getChapter(0).equals(b2.getChapter(0)), "initial match"); 351 check(b1.getChapter(1).equals(b2.getChapter(1)), "initial match"); 352 check(b1.getChapter(2).equals(b2.getChapter(2)), "initial match"); 353 this.mute(false); 354 Chapter c1 = b2.getChapter(1); 355 Chapter c2 = (Chapter)c1.clone(); 356 out("should get one remove event from deletion"); 357 b2.removeChapter(c1); 358 check(b1.getChapter(0).equals(b2.getChapter(0)), "match after rem"); 359 check(b1.getChapter(2).equals(b2.getChapter(1)), "match after rem"); 360 out("should get one Chapter remove event from merge"); 361 b1.merge(b2); 362 check(b1.getChapter(0).equals(b2.getChapter(0)), "match"); 363 check(b1.getChapter(1).equals(b2.getChapter(1)), "match"); 364 check(b1.sizeChapter() == b2.sizeChapter(), "correct size"); 365 check(b2.equals(b1), " - equals"); 366 367 setTest("Merge(Update) - add elt from array of strings"); 371 this.mute(false); 372 out("should get one event for elt added"); 373 b2.addChapter(c2); 374 check(b2.getChapter(2).equals(c2), "added"); 375 out("should get one event for elt added from merge"); 376 b1.merge(b2); 377 check(b1.getChapter(0).equals(b2.getChapter(0)), "match"); 378 check(b1.getChapter(1).equals(b2.getChapter(1)), "match"); 379 check(b1.getChapter(2).equals(b2.getChapter(2)), "match"); 380 check(b1.sizeChapter() == b2.sizeChapter(), "correct size"); 381 check(b2.equals(b1), " - equals"); 382 383 384 Book b4, b5, b6; 388 389 this.readDocument("tm01_g1.xml"); 390 out("creating the bean graph1"); 391 b4 = Book.createGraph(doc); 392 393 b5 = (Book)b4.clone(); 394 b6 = (Book)b4.clone(); 395 396 setTest("comparing graphs with 1 null elts"); 397 check(b4.sizeChapter() == 3); 398 check(b5.sizeChapter() == 3); 399 400 check(b4.equals(b5)); 402 403 setTest("comparing graphs with null indexed elts"); 404 b4.setChapter(1, null); 406 b5.setChapter(2, null); 407 check(!b4.equals(b5)); 408 b4.merge(b5); 409 check(b4.sizeChapter() == 3, "correct new array sise"); 410 check(b4.getChapter(0).equals(b5.getChapter(0)), "elt ok"); 411 check(b4.getChapter(1).equals(b5.getChapter(1)), "elt ok"); 412 check(b4.getChapter(2) == null, "elt ok"); 413 414 setTest("comparing graphs with null single bean elt"); 415 b4 = (Book)b6.clone(); 417 b5 = (Book)b6.clone(); 418 Content ct = new Content(); 419 ct.setTitle("This is a title"); 420 ct.setComment("And this is a comment"); 421 check(b4.equals(b5)); 422 b5.setContent(ct); 423 check(!b4.equals(b5)); 424 b4.merge(b5); 425 check(b4.getContent().equals(b5.getContent())); 426 427 Chapter c3 = new Chapter(); 429 c3.setComment("This is a comment"); 430 c3.setNumber("123"); 431 c3.addParagraph("This is a new paragraph"); 432 Chapter c4 = (Chapter)c3.clone(); 433 434 setTest("cloning a new bean"); 437 Book b7 = (Book)b2.clone(); 438 Book b8 = (Book)b2.clone(); 439 b7.addChapter(c3); 440 b8.addChapter(c4); 441 check(c3.equals(c4), "objects equal"); 442 check(b7.equals(b8), "same graph once added"); 443 444 this.readDocument("tm01_g2.xml"); 449 out("creating the bean graph1"); 450 b1 = Book.createGraph(doc); 451 452 this.readDocument("tm01_g3.xml"); 455 out("creating the bean graph2"); 456 b2 = Book.createGraph(doc); 457 458 BaseBean bb = b2.getChapter(0); 461 out(bb.toString()); 463 464 b3 = (Book)b1.clone(); 465 String chapterPropertyName = b3.CHAPTER; 466 chapterPropertyName = new String (chapterPropertyName+""); int index = b3.addValue(chapterPropertyName, bb.clone()); 468 bb = b3.getChapter(index); 469 out(bb.toString()); 471 472 l1 = new MyListener(b1); 473 b1.addPropertyChangeListener(l1); 474 475 l2 = new MyListener(b2); 478 b2.addPropertyChangeListener(l2); 479 480 setTest("Merging attributes"); 483 check(!b1.equals(b2), "shouldn't be equals (diff an attr)"); 484 491 b1.merge(b2); 492 496 check(b1.equals(b2), "should be equals"); 498 499 s1 = b1.getGood(); 501 check(s1 != null, "attr on root != null"); 502 if (s1 != null) { 503 check(s1.equals("no"), "attr on root"); 504 } 505 s1 = b1.getSummaryLength(); 506 check(s1 != null, "attr on summary != null"); 507 if (s1 != null) { 508 check(s1.equals("123"), "attr on summary"); 509 } 510 s1 = b1.getSummaryLang(); 511 check(s1 != null, "attr on summary != null"); 512 if (s1 != null) { 513 check(s1.equals("us"), "attr on summary"); 514 } 515 516 s1 = b1.getChapter(0).getTitle(); 517 out(s1); 518 check(s1 != null, "attr on chapter != null"); 519 if (s1 != null) { 520 check(s1.equals("First"), "attr on chapter"); 521 } 522 523 s1 = b1.getChapter(1).getTitle(); 524 out(s1); 525 check(s1 != null, "attr on chapter != null"); 526 if (s1 != null) { 527 check(s1.equals("Second"), "attr on chapter"); 528 } 529 530 534 this.getKMemUsage(); 536 537 int k1 = this.getKMemUsage(); 538 539 Book newBook; 540 541 this.readDocument("tm01_g3.xml"); 542 out("creating the bean graph for memory test"); 543 newBook = Book.createGraph(doc); 544 545 int maxLoop = 50; 546 BaseBean[] aBook = new BaseBean[maxLoop]; 547 for(int loop=0; loop<maxLoop; loop++) { 548 aBook[loop] = (BaseBean)newBook.clone(); 549 } 550 551 int k2 = this.getKMemUsage() - k1; 552 553 float diff = (float)(k2 - memUsageReference); 554 555 if (diff > 0) { 556 float p = diff/memUsageReference*100; 558 if (p > 20.0) { 559 out("It seems that the last schema2beans code changes have increased the memory consumption by " + p + "%"); 560 out("If this is expected and acceptable, change the memUsageReference value in TestMergeExtendBaseBean.java, to be " + k2); 561 } 562 } else { 563 float p = Math.abs(diff)/memUsageReference*100; 565 if (p > 25.0) { 566 out("It seems that the last schema2beans code changes have decreased the memory consumption by " + p + "% !!!"); 567 out("Please, change the memUsageReference value in TestMergeExtendBaseBean.java, to be " + k2); 568 } 569 } 570 out("memory test done"); 571 572 readDocument("tm01_g1.xml"); 573 b1 = Book.createGraph(doc); 574 readDocument("tm01_g4.xml"); 575 out("creating the bean graph for the comment merge test"); 576 Book commentedGraph = Book.createGraph(doc); 577 b1.merge(commentedGraph); 578 out(b1); 579 } 580 } 581 582 583
| Popular Tags
|