1 19 24 25 import java.io.*; 26 import java.util.*; 27 import org.w3c.dom.*; 28 29 import org.netbeans.modules.schema2beans.*; 30 import java.beans.*; 31 import book.*; 32 33 34 public class TestAttr extends BaseTest 35 { 36 public static void main(String [] argv) { 37 BaseTest o = new TestAttr(); 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 public class MyListener implements PropertyChangeListener 50 { 51 GraphManager gm; 52 String listenerName; 53 Object oldValue; 54 Object newValue; 55 String propertyName; 56 Object source; 57 boolean mute; 58 boolean remove; 59 60 public MyListener(BaseBean bean) 61 { 62 this.listenerName = bean.name(); 63 gm = bean.graphManager(); 64 this.remove = false; 65 this.mute = false; 66 out("new listener for " + this.listenerName); 67 } 68 69 public void reset() 70 { 71 this.oldValue = null; 72 this.newValue = null; 73 this.propertyName = null; 74 this.source = null; 75 } 76 77 public void propertyChange(PropertyChangeEvent e) 78 { 79 if (this.mute) 80 return; 81 82 String k; 83 this.oldValue = e.getOldValue(); 84 this.newValue = e.getNewValue(); 85 this.propertyName = e.getPropertyName(); 86 this.source = e.getSource(); 87 88 String n = this.propertyName; 89 int i = gm.getPropertyIndex(n); 90 String pn; 91 92 if (gm.isAttribute(n)) 93 { 94 pn = "Attr:" + gm.getPropertyName(n); 95 if (i != -1) 96 pn += "[" + i + "]"; 97 pn += "." + gm.getAttributeName(n); 98 } 99 else 100 { 101 pn = "Prop:" + gm.getPropertyName(n); 102 if (i != -1) 103 pn += "[" + i + "]"; 104 } 105 106 if (this.newValue == null) 107 k = "Rmv"; 108 else 109 if (this.oldValue == null) 110 k = "Add"; 111 else 112 k = "Chg"; 113 114 out("<" + k + " Lnr:" + this.listenerName + " Evt:" + n + 115 " Src:" + this.source.getClass().getName() + ">"); 116 117 if (remove) 118 { 119 out("<" + pn + " - ParentName: " + 120 gm.getPropertyParentName(n) + ">"); 121 } 122 else 123 { 124 BaseBean bb = gm.getPropertyParent(n); 125 String nm = "<no class>"; 126 127 if (bb != null) 128 nm = bb.getClass().getName(); 129 130 out("<" + pn + " - ParentName: " + 131 gm.getPropertyParentName(n) + 132 " - ParentClass:" + nm + ">"); 133 } 134 135 } 138 139 public void removeMode() 140 { 141 this.remove = true; 142 } 143 144 public Object oldValue() 145 { 146 return this.oldValue; 147 } 148 149 public String stringOldValue() 150 { 151 if (this.oldValue == null) 152 return "<null>"; 153 else 154 return this.oldValue.toString(); 155 } 156 157 public Object newValue() 158 { 159 return this.newValue; 160 } 161 162 public String stringNewValue() 163 { 164 if (this.newValue == null) 165 return "<null>"; 166 else 167 return this.newValue.toString(); 168 } 169 170 public String name() 171 { 172 return this.propertyName; 173 } 174 175 public void mute(boolean mute) 176 { 177 this.mute = mute; 178 } 179 180 public String toString() 181 { 182 return this.name() + " raised from source " + 183 this.source.getClass().getName(); 184 } 185 } 186 187 private MyListener l1; 188 189 public void run() 190 throws Exception  191 { 192 Book book; 193 194 this.readDocument(); 195 out("creating the bean graph"); 196 book = Book.createGraph(doc); 198 out("bean graph created"); 199 200 l1 = new MyListener(book); 201 book.addPropertyChangeListener(l1); 202 l1.mute(true); 203 204 208 { 210 String s1, s2; 211 212 setTest("get enum attribute from root"); 213 s1 = book.getAttributeValue("Good"); 214 check(s1.equals("no")); 215 out("Changing to another value - should get an event"); 216 s2 = "yes"; 217 l1.mute(false); 218 book.setAttributeValue("good", s2); 219 l1.mute(false); 220 s1 = book.getAttributeValue("good"); 221 check(s1.equals(s2)); 222 out("Book DOM content should be yes", book.dumpDomNode(0)); 223 out("Changing to a non-enum value (should get an exception)"); 224 boolean gotException = false; 225 try 226 { 227 book.setAttributeValue("good", "maybe"); 228 } 229 catch(IllegalArgumentException e) 230 { 231 check(true, "got the proper exception"); 232 gotException = true; 233 } 234 catch(Exception e) 235 { 236 check(false, "got the wrong exception type: " + 237 e.getClass().getName() + ", it should be " + 238 "IllegalArgumentException"); 239 gotException = true; 240 } 241 if (!gotException) 242 check(false, "didnt' get any exception"); 243 } 244 245 { 247 String s1, s2; 248 249 setTest("get #REQUIRED attribute"); 250 s1 = book.getAttributeValue("Summary", "length"); 251 check(s1.equals("132")); 252 out("Changing to another value"); 253 s2 = "133"; 254 book.setAttributeValue("Summary", "length", s2); 255 s1 = book.getAttributeValue("Summary", "length"); 256 check(s1.equals(s2)); 257 258 setTest("get #IMPLIED attribute"); 259 s1 = book.getAttributeValue("Summary", "lang"); 260 check(s1.equals("en")); 261 out("Setting a new value"); 262 s2 = "fr"; 263 book.setAttributeValue("Summary", "lang", s2); 264 s1 = book.getAttributeValue("Summary", "lang"); 265 check(s1.equals(s2)); 266 s2 = ""; 267 book.setAttributeValue("Summary", "lang", s2); 268 s1 = book.getAttributeValue("Summary", "lang"); 269 check(s1.equals(s2)); 270 book.setAttributeValue("Summary", "lang", null); 271 s1 = book.getAttributeValue("Summary", "lang"); 272 check(s1 == null); 273 s2 = "fr"; 274 book.setAttributeValue("Summary", "lang", s2); 275 276 277 setTest("get #FIXED attribute"); 278 s1 = book.getAttributeValue("Summary", "size"); 279 check(s1.equals("12")); 280 out("Summary DOM content should be 133/fr/12", 281 book.dumpDomNode(1)); 282 out("Setting a new value (should get an exception)"); 283 s2 = "15"; 284 boolean gotException = false; 285 try 286 { 287 book.setAttributeValue("Summary", "size", s2); 288 } 289 catch(IllegalStateException e) 290 { 291 check(true, "got the proper exception"); 292 gotException = true; 293 } 294 catch(Exception e) 295 { 296 check(false, "got the wrong exception type: " + 297 e.getClass().getName() + ", it should be " + 298 "IllegalStateException"); 299 gotException = true; 300 } 301 if (!gotException) 302 check(false, "didnt' get any exception"); 303 } 304 305 { 307 String s1, s2, s3, s4; 308 309 setTest("get/set non set #IMPLIED attribute"); 310 Chapter c = book.getChapter(0); 311 out("Chapter DOM should have no attribute", 312 c.dumpDomNode(0)); 313 s1 = c.getAttributeValue("title"); 314 check(s1 == null); 315 s2 = "My chapter"; 316 c.setAttributeValue("title", s2); 317 s1 = c.getAttributeValue("title"); 318 check(s1.equals(s2)); 319 out("Chapter DOM should have one title attribute", 320 c.dumpDomNode(0)); 321 322 setTest("access from parent & current bean"); 325 s1 = book.getAttributeValue("Chapter", 0, "title"); 326 s2 = c.getAttributeValue("title"); 327 check(s1.equals(s2)); 328 329 setTest("attribute stick with elt when mixing"); 331 s1 = book.getAttributeValue("Chapter", 0, "title"); 332 s2 = book.getAttributeValue("Chapter", 1, "title"); 333 check(s1 != null); 334 check(s2 == null); 335 Chapter[] ac = book.getChapter(); 336 c = ac[1]; 337 ac[1] = ac[0]; 338 ac[0] = c; 339 book.setChapter(ac); 340 s3 = book.getAttributeValue("Chapter", 0, "title"); 342 s4 = book.getAttributeValue("Chapter", 1, "title"); 343 check(s3 == null); 344 check(s4.equals(s1)); 345 346 setTest("get/set non set #IMPLIED attribute (idx != 0)"); 347 c = book.getChapter(2); 348 out("Chapter DOM should have no attribute", 349 c.dumpDomNode(0)); 350 s1 = c.getAttributeValue("title"); 351 check(s1 == null); 352 s2 = "My chapter2"; 353 c.setAttributeValue("title", s2); 354 s1 = c.getAttributeValue("title"); 355 check(s1.equals(s2)); 356 out("Chapter DOM should have one title attribute", 357 c.dumpDomNode(0)); 358 } 359 360 { 362 String s1; 363 364 setTest("get unknown attribute"); 365 boolean gotException = false; 366 try 367 { 368 s1 = book.getAttributeValue("Summary", "Splash"); 369 } 370 catch(IllegalArgumentException e) 371 { 372 check(true, "got the proper exception"); 373 gotException = true; 374 } 375 catch(Exception e) 376 { 377 check(false, "got the wrong exception type: " + 378 e.getClass().getName() + ", it should be " + 379 "IllegalArgumentException"); 380 gotException = true; 381 } 382 if (!gotException) 383 check(false, "didnt' get any exception"); 384 } 385 386 { 388 l1.mute(true); 389 String s1, s2; 390 setTest("add a brand new element with default attributes"); 391 Index idx = new Index(); 392 int i = book.addIndex(idx); 393 s1 = idx.getAttributeValue("cross-ref"); 394 s2 = book.getAttributeValue("Index", i, "CrossRef"); 395 check(s1.equals(s2)); 396 out("should have created: cross-ref & glossary, and not color", 397 book.dumpDomNode(1)); 398 399 setTest("add a brand new element, setting attributes"); 401 idx = new Index(); 402 idx.setAttributeValue("cross-ref", "yes"); 403 idx.setAttributeValue("color", "blue"); 404 idx.setWord("my word"); 405 idx.setAttributeValue("word", "freq", "123"); 406 book.setIndex(i, idx); 407 out("should have created: cross-ref (yes), glossary (nope) " + 408 "and color (blue)", book.dumpDomNode(3)); 409 } 410 411 { 413 BaseBean root = book.graphManager().getBeanRoot(); 414 this.parseGraph(root, "\t"); 415 } 416 417 out("Make sure that default attributes get set."); 418 Book anotherBook = Book.createGraph(); 419 anotherBook.setSummary("This is my summary."); 420 out(anotherBook); 421 422 setTest("attributes as properties"); 423 book.setGood("no"); 424 check("no".equals(book.getGood())); 425 out("Checking chapter title"); 426 Chapter chap0 = book.getChapter(0); 427 chap0.setTitle("My title"); 428 out(chap0.getTitle()); 429 430 445 446 Chapter tca = new Chapter(); 448 tca.setTitle("Dolly: A Good Clone & A Day"); 449 out("Title before cloning:"); 450 out(tca.getTitle()); 451 Chapter theClone = (Chapter) tca.clone(); 452 out("Title after cloning:"); 453 out(theClone.getTitle()); 454 Book fullGraph = Book.createGraph(); 455 fullGraph.addChapter(theClone); 456 out("And here is the clone in it's own graph"); 457 out(fullGraph); 458 } 459 460 void parseGraph(BaseBean bean, String indent) 461 { 462 if (bean == null) 463 return; 464 465 out(indent + "[" + bean.name() + "]"); 466 467 BaseProperty[] props = bean.listProperties(); 468 for (int i=0; i<props.length; i++) 469 { 470 BaseProperty p = props[i]; 471 String name = p.getName(); 472 473 String str = "<" + name; 475 if (p.isIndexed()) 476 str += "[" + p.size() + "]"; 477 str += "> - " + p.getPropertyClass(); 478 out(indent + str); 479 480 String [] attrs = p.getAttributeNames(); 482 for (int j=0; j<attrs.length; j++) 483 out(indent + " a:" + attrs[j]); 484 485 if (p.isBean() && p.isIndexed()) 487 { 488 BaseBean[] ba = (BaseBean[])bean.getValues(name); 489 for (int k=0; k<ba.length; k++) 490 this.parseGraph(ba[k], indent + "\t"); 491 } 492 else 493 if (p.isBean()) 494 { 495 BaseBean b = (BaseBean)bean.getValue(name); 496 this.parseGraph(b, indent + "\t"); 497 } 498 } 499 } 500 } 501 502 503 504
| Popular Tags
|