KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestVeto


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  * TestVeto - test the vetoable events
21  *
22  */

23
24 import java.io.*;
25 import java.util.*;
26 import org.w3c.dom.*;
27
28 import java.beans.*;
29
30 import org.netbeans.modules.schema2beans.*;
31 import book.*;
32
33
34 public class TestVeto extends BaseTest
35 {
36     public static void main(String JavaDoc[] argv) {
37         BaseTest o = new TestVeto();
38         if (argv.length > 0)
39             o.setDocumentDir(argv[0]);
40         try {
41             o.run();
42         } catch (Exception JavaDoc e) {
43             e.printStackTrace();
44             System.exit(1);
45         }
46         System.exit(0);
47     }
48     
49     public class MyListener implements VetoableChangeListener
50     {
51         GraphManager gm;
52         String JavaDoc listenerName;
53         String JavaDoc title;
54         Object JavaDoc oldValue;
55         Object JavaDoc newValue;
56         String JavaDoc propertyName;
57         Object JavaDoc source;
58         int index;
59
60         boolean remove;
61         
62         // Used to check that the event is triggered once the changed are done
63
Chapter tracePara;
64         
65         boolean raise = false;;
66         boolean raised = false;
67         boolean printStringArray = false;
68
69         public MyListener(String JavaDoc title, BaseBean bean)
70         {
71             this.listenerName = bean.name();
72             gm = bean.graphManager();
73             this.remove = false;
74             this.title = title;
75             out("new listener for " + this.title + " / " + this.listenerName);
76         }
77
78         public void printStringValues(String JavaDoc[] ss, String JavaDoc str)
79         {
80             if (ss == null)
81                 out(str + " is null");
82             else
83             {
84                 if (ss.length == 0)
85                     out(str + ".length = 0");
86                 else
87                     for(int i=0; i<ss.length; i++)
88                         out(str + "[" + i + "]=" + ss[i]);
89             }
90         }
91
92         public void reset()
93         {
94             this.oldValue = null;
95             this.newValue = null;
96             this.propertyName = null;
97             this.source = null;
98             this.index = -1;
99             this.raise = false;
100             this.raised = false;
101             this.printStringArray = false;
102         }
103
104         public void printStringArray()
105         {
106             this.printStringArray = true;
107         }
108
109         public void traceParagraphs(Chapter c)
110         {
111             this.tracePara = c;
112         }
113
114         public void veto()
115         {
116             this.raise = true;
117         }
118         
119         public void vetoableChange(PropertyChangeEvent e)
120             throws PropertyVetoException
121         {
122             if (this.raised)
123                 out(this.title+ " received an undo event:");
124             else
125                 out(this.title + " received veto event:");
126
127             this.oldValue = e.getOldValue();
128             this.newValue = e.getNewValue();
129             this.propertyName = e.getPropertyName();
130             this.source = e.getSource();
131             String JavaDoc n = this.propertyName;
132             this.index = gm.getPropertyIndex(n);
133
134             out("<Lnr:" + this.listenerName + " Evt:" + n +
135                 " Src:" + this.source.getClass().getName() + ">");
136             if (remove)
137             {
138                 out("<" + gm.getPropertyName(n) + "[" + this.index +
139                     "]" + " - Parent: " + gm.getPropertyParentName(n) + ">");
140             }
141             else
142             {
143                 out("<" + gm.getPropertyName(n) + "[" + this.index +
144                     "]" + " - Parent: " + gm.getPropertyParentName(n) +
145                     "/" + gm.getPropertyParent(n).getClass().getName() + ">");
146             }
147
148             if (this.tracePara != null)
149             {
150                 String JavaDoc[] p = this.tracePara.getParagraph();
151                 for (int i=0; i<p.length; i++)
152                     out("From event listener: " + p[i]);
153             }
154
155             if (this.printStringArray)
156             {
157                 this.printStringValues((String JavaDoc[])this.oldValue, "oldValues");
158                 this.printStringValues((String JavaDoc[])this.newValue, "newValues");
159             }
160
161             if (this.raise && !this.raised)
162             {
163                 out("Listener: raising PropertyVetoException");
164                 this.raised = true;
165                 throw new PropertyVetoException("value rejected", e);
166             }
167             //out("received " + this.oldValue + "/" + this.newValue + "/" +
168
// this.propertyName);
169
}
170
171         public void removeMode()
172         {
173             this.remove = true;
174         }
175         
176         public Object JavaDoc oldValue()
177         {
178             return this.oldValue;
179         }
180
181         public String JavaDoc stringOldValue()
182         {
183             if (this.oldValue == null)
184                 return "<null>";
185             else
186                 return this.oldValue.toString();
187         }
188
189         public Object JavaDoc newValue()
190         {
191             return this.newValue;
192         }
193
194         public String JavaDoc stringNewValue()
195         {
196             if (this.newValue == null)
197                 return "<null>";
198             else
199                 return this.newValue.toString();
200         }
201         
202         public String JavaDoc name()
203         {
204             return this.propertyName;
205         }
206
207         public String JavaDoc toString()
208         {
209             return this.name() + " raised from source " +
210                 this.source.getClass().getName();
211         }
212     }
213     
214     public void run()
215         throws Exception JavaDoc
216     {
217         Book book;
218
219         this.readDocument();
220         
221         out("creating the bean graph");
222         book = Book.createGraph(this.doc);
223         GraphManager gm = book.graphManager();
224
225         out("bean graph created");
226         //out(book.toString());
227

228         /*
229          * Book
230          * Index[1,n]
231          * Word - String
232          * Ref[1,n]
233          * Page - String
234          * Line - String
235          * Chapter[1,n]
236          * Comment? - String
237          * Paragraph[0,n] - String
238          * Summary? - String
239          * Author[1,n] - String
240          * Good - Boolean
241          * Available - Boolean
242          */

243
244         //
245
// Set a listener on the root
246
//
247
MyListener l = new MyListener("Book listener", book);
248         book.addVetoableChangeListener(l);
249         //GraphManager.debug(true);
250
setTest("simple change event on the root - no veto");
251         l.reset();
252         String JavaDoc s = "This book is about how to veto changes";
253         // Change a property on the root - this should raises an event
254
book.setSummary(s);
255         // Check the received event
256
check(l.oldValue() == null, "(old value)");
257         check(l.newValue().equals(s), "(new value)");
258         check(book.getSummary().equals(s), "(new value)");
259
260         boolean gotException = false;
261         setTest("simple change event on the root - veto");
262         l.reset();
263         l.veto();
264         String JavaDoc s2 = "this is the new value";
265         s = book.getSummary();
266         try {
267             book.setSummary(s2);
268         } catch(PropertyVetoException ve) {
269             check(book.getSummary().equals(s), "(got exception & same value)");
270             gotException = true;
271         }
272
273         if (!gotException)
274             check(false, " didn't get the veto exception (1)!");
275
276         setTest("Try to listen for a non vetoable property");
277         gotException = false;
278         try {
279             book.addVetoableChangeListener("Reviews", l);
280         } catch(Exception JavaDoc e) {
281             check(true, "got exception:\n" + e.getMessage());
282             gotException = true;
283         }
284
285         if (!gotException)
286             check(false, " didn't get the runtime exception (2)!");
287
288         setTest("Indexed final property");
289         l.reset();
290         String JavaDoc [] ss = {"Author1", "Author2", "Author3"};
291         l.printStringArray();
292         book.setAuthor(ss);
293
294         l.reset();
295         l.veto();
296         l.printStringArray();
297         String JavaDoc [] ss2 = {"Author1_new", "Author2_new"};
298         try {
299             book.setAuthor(ss2);
300         } catch(PropertyVetoException ve) {
301             check(true, "(got exception)");
302             gotException = true;
303         }
304
305         if (!gotException)
306             check(false, " didn't get the veto exception (3)!");
307
308         l.printStringValues(book.getAuthor(), "getAuthor()");
309         
310         setTest("Set a second listener to get two events");
311         MyListener l2 = new MyListener("Author listener", book);
312         l.reset();
313         l2.reset();
314         gotException = false;
315         try {
316             book.addVetoableChangeListener("author", l2);
317         } catch(Exception JavaDoc e) {
318             check(false, "got exception:\n" + e.getMessage());
319             gotException = true;
320         }
321
322         String JavaDoc [] ss3 = {"re-Author1_new", "re-Author2_new"};
323         book.setAuthor(ss3);
324         l.printStringValues(book.getAuthor(), "getAuthor()");
325
326         l.reset();
327         l2.reset();
328         l.veto();
329         String JavaDoc [] ss4 = {"only_one_author"};
330         try {
331             book.setAuthor(ss4);
332         } catch(PropertyVetoException ve) {
333             check(true, "(got exception)");
334             gotException = true;
335         }
336
337         if (!gotException)
338             check(false, " didn't get the veto exception (4)!");
339
340     }
341
342 }
343
344
345
346
Popular Tags