KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestMergeExtendBaseBean


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  * TestMergeExtendBaseBean - test the basic features.
21  *
22  */

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 JavaDoc[] argv) {
37         BaseTest o = new TestMergeExtendBaseBean();
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
50
51     //
52
// This is the reference number (in KB) for memory consumption for 50 clones
53
// (simply ran the test to get the number). We check against this number
54
// to make sure that any change in schema2beans does not introduce a
55
// significant increase of mem usage.
56
//
57
static float memUsageReference = 236;
58
59     
60     public class MyListener implements PropertyChangeListener {
61         String JavaDoc listenerName;
62         Object JavaDoc oldValue;
63         Object JavaDoc newValue;
64         String JavaDoc propertyName;
65         Object JavaDoc 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 JavaDoc k;
92             this.oldValue = e.getOldValue();
93             this.newValue = e.getNewValue();
94             this.propertyName = e.getPropertyName();
95             this.source = e.getSource();
96             String JavaDoc n = this.propertyName;
97             //System.out.println("propertyName="+n);
98
this.index = GraphManager.getPropertyIndex(rootBean, n);
99         
100             //new Exception().printStackTrace(System.out);
101
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 JavaDoc oldValue() {
134             return this.oldValue;
135         }
136     
137         public String JavaDoc stringOldValue() {
138             if (this.oldValue == null)
139                 return "<null>";
140             else
141                 return this.oldValue.toString();
142         }
143     
144         public Object JavaDoc newValue() {
145             return this.newValue;
146         }
147     
148         public String JavaDoc stringNewValue() {
149             if (this.newValue == null)
150                 return "<null>";
151             else
152                 return this.newValue.toString();
153         }
154     
155         public String JavaDoc name() {
156             return this.propertyName;
157         }
158     
159         public void mute(boolean mute) {
160             this.mute = mute;
161         }
162     
163         public String JavaDoc 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 JavaDoc {
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         // b2 should never change, so we should never receive anything
196
// on this listener
197
l2 = new MyListener(b2);
198         b2.addPropertyChangeListener(l2);
199
200         //
201
// Merge two identical graphs
202
//
203
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         // One mixed up document, but should be the same
210
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         //
221
// Change only one element of the second graph
222
//
223
setTest("Merge(Update) - changing one property");
224         String JavaDoc s1 = "This is the new summary";
225         String JavaDoc s2 = b2.getSummary();
226     
227         // g1/g2
228
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         // g1/g3
238
this.mute(true);
239         b1.setSummary(s2);
240         this.mute(false);
241         out("should not get any event");
242         b1.merge(b3);
243         // All of the same contents, but in a different order is still different.
244
check(b3.equals(b1), " - equals");
245
246         this.mute(true);
247         b2.setSummary(s2);
248     
249         //
250
// Mixing array of strings
251
//
252
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 JavaDoc [] ss1 = b2.getAuthor();
258         String JavaDoc [] ss2 = new String JavaDoc[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         // All of the same contents, but in a different order is still different.
269
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         //
277
// Mixing array of beans
278
//
279
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         // All of the same contents, but in a different order is still different.
296
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         //
304
// Removing a String elt from an array
305
//
306
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         //
325
// Adding a String elt from an array
326
//
327
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         //out("Here is b1:");
334
//b1.writeNoReindent(System.out);
335
//out("Here is b2:");
336
//b2.writeNoReindent(System.out);
337
b1.merge(b2);
338         //out("Here is b1:");
339
//b1.writeNoReindent(System.out);
340
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         //
347
// Removing a Bean elt from an array
348
//
349
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         //
368
// Adding a Bean elt from an array
369
//
370
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         //
385
// Compare two graphs with missing nodes and elements
386
//
387
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         // bean[] full / bean null
401
check(b4.equals(b5));
402     
403         setTest("comparing graphs with null indexed elts");
404         // g1.bean[] has 1 null / g2.bean[] has 1 null / bean null
405
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         // g1.bean non null / g2.bean is null
416
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         // Clone an element which is not part of a graph
428
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         // Add both elements to two identical graphs - should get two
435
// identical graphs
436
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         //
445
// Test the attributes. When we merge graphs, we need to make
446
// sure that the attributes are also merged.
447
//
448
this.readDocument("tm01_g2.xml");
449         out("creating the bean graph1");
450         b1 = Book.createGraph(doc);
451     
452         // g3 and g2 elements are identicals, g3 has attributes,
453
// g2 has not.
454
this.readDocument("tm01_g3.xml");
455         out("creating the bean graph2");
456         b2 = Book.createGraph(doc);
457
458         // Make sure that we can clone a part of the graph without loosing
459
// the attributes.
460
BaseBean bb = b2.getChapter(0);
461         // Should see the chapter attribute
462
out(bb.toString());
463
464         b3 = (Book)b1.clone();
465         String JavaDoc chapterPropertyName = b3.CHAPTER;
466         chapterPropertyName = new String JavaDoc(chapterPropertyName+""); // see if we can get a different String, to test that intern is called properly.
467
int index = b3.addValue(chapterPropertyName, bb.clone());
468         bb = b3.getChapter(index);
469         // Should see the chapter attribute on this cloned element
470
out(bb.toString());
471
472         l1 = new MyListener(b1);
473         b1.addPropertyChangeListener(l1);
474     
475         // b2 should never change, so we should never receive anything
476
// on this listener
477
l2 = new MyListener(b2);
478         b2.addPropertyChangeListener(l2);
479
480         // The only events we should have when we merge are the
481
// attributes events, because the graphs only differ by attr.
482
setTest("Merging attributes");
483         check(!b1.equals(b2), "shouldn't be equals (diff an attr)");
484         /*
485         out("b1:");
486         b1.write(out);
487         out("b2:");
488         b2.write(out);
489         //org.netbeans.modules.schema2beans.DDLogFlags.setDebug(true);
490         */

491         b1.merge(b2);
492         /*
493         out("b1:");
494         b1.write(out);
495         */

496         // All of the same contents, but in a different order is still different.
497
check(b1.equals(b2), "should be equals");
498     
499         // Make sure that b1 has the attributes
500
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         //
531
// Make sure that we do not consume too much memory
532
//
533

534         // Ignore the first one
535
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             // We consume more memory than excepted
557
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             // We consume less memory than expected
564
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