KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestEvents


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * TestEvents - test the events
21  *
22  * The following test assumes that we know the content of the
23  * graph as we get elements, add and change them. Therefore, the TestBook.xml
24  * file and this java test should be kept in sync.
25  *
26  * Test the following:
27  *
28  * listener on the root - change event for add/change/remove on the same root bean
29  * listener on the root - change event for add/change/remove
30  * on a sub node bean (propagation)
31  * listener on the root and on a subnode: test no reception, both reception and
32  * only one reception
33  * listener on a specific property
34  * remove listeners
35  * indexed and non indexed property events
36  * check utility methods to get the index, name and parent bean
37  * from an event name (graphManager methods)
38  * remove a subtree - get event within the subtree (no propagation)
39  * and within the original tree (propagation)
40  *
41  */

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 JavaDoc[] argv) {
56         BaseTest o = new TestEvents();
57         if (argv.length > 0)
58             o.setDocumentDir(argv[0]);
59         try {
60             o.run();
61         } catch (Exception JavaDoc 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 JavaDoc listenerName;
72         Object JavaDoc oldValue;
73         Object JavaDoc newValue;
74         String JavaDoc propertyName;
75         Object JavaDoc source;
76         int index;
77
78         boolean remove;
79         
80         // Used to check that the event is triggered once the changed are done
81
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 JavaDoc 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 JavaDoc[] p = this.tracePara.getParagraph();
132                 for (int i=0; i<p.length; i++)
133                     out("From event listener: " + p[i]);
134             }
135             //out("received " + this.oldValue + "/" + this.newValue + "/" +
136
// this.propertyName);
137
}
138
139         public void removeMode()
140         {
141             this.remove = true;
142         }
143         
144         public Object JavaDoc oldValue()
145         {
146             return this.oldValue;
147         }
148
149         public String JavaDoc stringOldValue()
150         {
151             if (this.oldValue == null)
152                 return "<null>";
153             else
154                 return this.oldValue.toString();
155         }
156
157         public Object JavaDoc newValue()
158         {
159             return this.newValue;
160         }
161
162         public String JavaDoc stringNewValue()
163         {
164             if (this.newValue == null)
165                 return "<null>";
166             else
167                 return this.newValue.toString();
168         }
169         
170         public String JavaDoc name()
171         {
172             return this.propertyName;
173         }
174
175         public String JavaDoc toString()
176         {
177             return this.name() + " raised from source " +
178                 this.source.getClass().getName();
179         }
180     }
181     
182     public void run()
183         throws Exception JavaDoc
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         // Check that we can read the graph an it is complete
194
out("bean graph created");
195         //out(book.toString());
196

197         /*
198          * Book
199          * Index[1,n]
200          * Word - String
201          * Ref[1,n]
202          * Page - String
203          * Line - String
204          * Chapter[1,n]
205          * Comment? - String
206          * Paragraph[0,n] - String
207          * Summary? - String
208          * Author[1,n] - String
209          * Good - Boolean
210          * Available - Boolean
211          */

212
213         //
214
// Set a change event on the root
215
//
216
MyListener l = new MyListener(book);
217         
218         book.addPropertyChangeListener(l);
219         
220         setTest("simple change event on the root");
221         l.reset();
222         String JavaDoc s = "This book is about how to raise the event familly";
223         // Change a property on the root - this should raises an event
224
book.setSummary(s);
225         // Check the received event
226
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 JavaDoc s2 = "This book is about nothing at all";
232         // Change a property on the root - this should raises an event
233
book.setSummary(s2);
234         // Check the received event
235
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         // Change a property on the root - this should raises an event
241
book.setSummary(null);
242         // Check the received event
243
check(l.oldValue().equals(s2), "(old value)");
244         check(l.newValue() == null, "(new value)");
245
246
247         //
248
// Keep the same event on the root, but change a property of another
249
// property (not a direct property of the root)
250
//
251
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         // Add three paragraphs
261

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         // Remove the first added
287
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         //
302
// Add another listener on an intermediate node and on a leaf
303
//
304

305         Chapter c2 = book.getChapter(1);
306         
307         MyListener l2 = new MyListener(c2);
308         
309         // Get the events only for Paragraph changes
310
c2.addPropertyChangeListener("Paragraph", l2);
311
312         // Check that we receive the event twice
313
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         // Check that modifying the comment won't notify the Paragraph listener
326
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         // Remove the book listener
336
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         // Register again the listeners
363
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         //
373
// Make sure that the event is triggered after the property
374
// has changed.
375
//
376
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 JavaDoc[] pp = c.getParagraph();
385         String JavaDoc[] pp2 = new String JavaDoc[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