1 19 42 43 import java.io.*; 44 import java.util.*; 45 import org.w3c.dom.*; 46 47 import java.beans.*; 48 49 import org.netbeans.modules.schema2beans.*; 50 import book.*; 51 52 53 public class TestEvents extends BaseTest 54 { 55 public static void main(String [] argv) { 56 BaseTest o = new TestEvents(); 57 if (argv.length > 0) 58 o.setDocumentDir(argv[0]); 59 try { 60 o.run(); 61 } catch (Exception e) { 62 e.printStackTrace(); 63 System.exit(1); 64 } 65 System.exit(0); 66 } 67 68 public class MyListener implements PropertyChangeListener 69 { 70 GraphManager gm; 71 String listenerName; 72 Object oldValue; 73 Object newValue; 74 String propertyName; 75 Object source; 76 int index; 77 78 boolean remove; 79 80 Chapter tracePara; 82 83 public MyListener(BaseBean bean) 84 { 85 this.listenerName = bean.name(); 86 gm = bean.graphManager(); 87 this.remove = false; 88 out("new listener for " + this.listenerName); 89 } 90 91 public void reset() 92 { 93 this.oldValue = null; 94 this.newValue = null; 95 this.propertyName = null; 96 this.source = null; 97 this.index = -1; 98 } 99 100 public void traceParagraphs(Chapter c) 101 { 102 this.tracePara = c; 103 } 104 105 106 public void propertyChange(PropertyChangeEvent e) 107 { 108 this.oldValue = e.getOldValue(); 109 this.newValue = e.getNewValue(); 110 this.propertyName = e.getPropertyName(); 111 this.source = e.getSource(); 112 String n = this.propertyName; 113 this.index = gm.getPropertyIndex(n); 114 115 out("<Lnr:" + this.listenerName + " Evt:" + n + 116 " Src:" + this.source.getClass().getName() + ">"); 117 if (remove) 118 { 119 out("<" + gm.getPropertyName(n) + "[" + this.index + 120 "]" + " - Parent: " + gm.getPropertyParentName(n) + ">"); 121 } 122 else 123 { 124 out("<" + gm.getPropertyName(n) + "[" + this.index + 125 "]" + " - Parent: " + gm.getPropertyParentName(n) + 126 "/" + gm.getPropertyParent(n).getClass().getName() + ">"); 127 } 128 129 if (this.tracePara != null) 130 { 131 String [] p = this.tracePara.getParagraph(); 132 for (int i=0; i<p.length; i++) 133 out("From event listener: " + p[i]); 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 String toString() 176 { 177 return this.name() + " raised from source " + 178 this.source.getClass().getName(); 179 } 180 } 181 182 public void run() 183 throws Exception 184 { 185 Book book; 186 187 this.readDocument(); 188 189 out("creating the bean graph"); 190 book = Book.createGraph(this.doc); 191 GraphManager gm = book.graphManager(); 192 193 out("bean graph created"); 195 197 212 213 MyListener l = new MyListener(book); 217 218 book.addPropertyChangeListener(l); 219 220 setTest("simple change event on the root"); 221 l.reset(); 222 String s = "This book is about how to raise the event familly"; 223 book.setSummary(s); 225 check(l.oldValue() == null, "(old value)"); 227 check(l.newValue().equals(s), "(new value)"); 228 229 setTest("change the same property on the root"); 230 l.reset(); 231 String s2 = "This book is about nothing at all"; 232 book.setSummary(s2); 234 check(l.oldValue().equals(s), "(old value)"); 236 check(l.newValue().equals(s2), "(new value)"); 237 238 setTest("remove this same property"); 239 l.reset(); 240 book.setSummary(null); 242 check(l.oldValue().equals(s2), "(old value)"); 244 check(l.newValue() == null, "(new value)"); 245 246 247 setTest("propagation of the event"); 252 Chapter c = book.getChapter(0); 253 l.reset(); 254 s = c.getComment(); 255 s2 = "Comment on the first chapter"; 256 c.setComment(s2); 257 check(l.oldValue.equals(s), "(oldvalue)"); 258 check(l.newValue.equals(s2), "(newvalue)"); 259 260 262 setTest("event on indexed property - add new"); 263 l.reset(); 264 s = "This is a new paragraph"; 265 int i = c.addParagraph(s); 266 check(l.oldValue == null, "(no old value - new element)"); 267 check(l.newValue.equals(s), "(new value)"); 268 check(l.index == i, "(correct index)"); 269 270 setTest("event on indexed property - add new"); 271 l.reset(); 272 s2 = "This is another paragraph"; 273 int j = c.addParagraph(s2); 274 check(l.oldValue == null, "(no old value - new element)"); 275 check(l.newValue.equals(s2), "(new value)"); 276 check(l.index == j, "(correct index)"); 277 278 setTest("event on indexed property - add new"); 279 l.reset(); 280 s2 = "This is yet another paragraph"; 281 j = c.addParagraph(s2); 282 check(l.oldValue == null, "(no old element - new element)"); 283 check(l.newValue.equals(s2), "(new value)"); 284 check(l.index == j, "(correct index)"); 285 286 setTest("event on indexed property - remove index"); 288 l.reset(); 289 c.setParagraph(i, null); 290 check(l.oldValue.equals(s), "(old value)"); 291 check(l.newValue == null, "(no new value)"); 292 check(l.index == i, "(correct index)"); 293 294 setTest("event on indexed property - reset new"); 295 l.reset(); 296 c.setParagraph(i, s); 297 check(l.oldValue == null, "(old value)"); 298 check(l.newValue.equals(s), "(no new value)"); 299 check(l.index == i, "(correct index)"); 300 301 305 Chapter c2 = book.getChapter(1); 306 307 MyListener l2 = new MyListener(c2); 308 309 c2.addPropertyChangeListener("Paragraph", l2); 311 312 setTest("two listeners - both receiving"); 314 l2.reset(); 315 l.reset(); 316 s = "This is a brand new one"; 317 c2.addParagraph(s); 318 check(l.oldValue == null, "(no old value)"); 319 check(l.newValue.equals(s), "(new value)"); 320 check(l.oldValue == l2.oldValue, "(same old value - both listeners)"); 321 check(l.newValue.equals(l2.newValue), "(same new value - both listeners)"); 322 check(l.index == l2.index, "(same index - both listeners)"); 323 check(l.source == l2.source, "(same source - both listeners)"); 324 325 setTest("two listeners - one receiving"); 327 l2.reset(); 328 l.reset(); 329 s = "That's a new comment value"; 330 c2.setComment(s); 331 check(l.newValue.equals(s), "(root listener: yep)"); 332 check(l2.newValue == null, "(chapter listener: noop)"); 333 334 335 book.removePropertyChangeListener(l); 337 setTest("one listener - no receiving"); 338 l2.reset(); 339 l.reset(); 340 s = "That's another new comment value"; 341 c2.setComment(s); 342 check(l.newValue == null, "(root listener: noop)"); 343 check(l2.newValue == null, "(chapter listener: noop)"); 344 345 setTest("one listener - one receiving"); 346 l2.reset(); 347 l.reset(); 348 s = "That's a brand new paragraph"; 349 c2.addParagraph(s); 350 check(l.newValue == null, "(root listener: noop)"); 351 check(l2.newValue.equals(s), "(chapter listener: yep)"); 352 353 setTest("no listener - no receiving"); 354 c2.removePropertyChangeListener("Paragraph", l2); 355 l2.reset(); 356 l.reset(); 357 s = "That's yet another brand new paragraph"; 358 c2.addParagraph(s); 359 check(l.newValue == null, "(root listener: noop)"); 360 check(l2.newValue == null, "(chapter listener: noop)"); 361 362 book.addPropertyChangeListener(l); 364 c2.addPropertyChangeListener("Paragraph", l2); 365 l.removeMode(); 366 l2.removeMode(); 367 l2.reset(); 368 l.reset(); 369 book.removeChapter(c2); 370 out("should have received paragraph events on Chapter and Chapter event on Book"); 371 372 c = new Chapter(); 377 c.addParagraph("1. this is a paragraph"); 378 c.addParagraph("2. this is a paragraph"); 379 c.addParagraph("3. this is a paragraph"); 380 c.addParagraph("4. this is a paragraph"); 381 out("should receive Chapter event and get 4 strings from the event"); 382 l.traceParagraphs(c); 383 book.addChapter(c); 384 String [] pp = c.getParagraph(); 385 String [] pp2 = new String [3]; 386 pp2[0] = pp[0]; 387 pp2[1] = pp[3]; 388 pp2[2] = pp[2]; 389 out("should receive Chapter event and get 3 strings from the event (1, 4, 3)"); 390 c.setParagraph(pp2); 391 l.traceParagraphs(null); 392 } 393 394 } 395 396 397 398 | Popular Tags |