KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestMerge


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

23
24 import org.netbeans.modules.schema2beans.*;
25
26 import java.beans.*;
27
28 import book.*;
29
30
31 public class TestMerge extends BaseTest {
32     public static void main(String JavaDoc[] argv) {
33         BaseTest o = new TestMerge();
34         if (argv.length > 0)
35             o.setDocumentDir(argv[0]);
36         try {
37             o.run();
38         } catch (Exception JavaDoc e) {
39             e.printStackTrace();
40             System.exit(1);
41         }
42         System.exit(0);
43     }
44     
45
46
47     //
48
// This is the reference number for memory consumption for 50 clones
49
// (simply ran the test to get the number). We check against this number
50
// to make sure that any change in schema2beans does not introduce a
51
// significant increase of mem usage. (Note that sometimes JDK changes
52
// can change this number too.)
53
//
54
static float memUsageReference = 4436;
55
56     
57     public class MyListener implements PropertyChangeListener {
58         GraphManager gm;
59         String JavaDoc listenerName;
60         Object JavaDoc oldValue;
61         Object JavaDoc newValue;
62         String JavaDoc propertyName;
63         Object JavaDoc source;
64         int index;
65         boolean mute;
66         boolean remove;
67     
68         public MyListener(BaseBean bean) {
69             this.listenerName = bean.name();
70             gm = bean.graphManager();
71             this.remove = false;
72             this.mute = false;
73             out("new listener for " + this.listenerName);
74         }
75     
76         public void reset() {
77             this.oldValue = null;
78             this.newValue = null;
79             this.propertyName = null;
80             this.source = null;
81             this.index = -1;
82         }
83     
84         public void propertyChange(PropertyChangeEvent e) {
85             if (this.mute)
86                 return;
87         
88             String JavaDoc k;
89             this.oldValue = e.getOldValue();
90             this.newValue = e.getNewValue();
91             this.propertyName = e.getPropertyName();
92             this.source = e.getSource();
93             String JavaDoc n = this.propertyName;
94             this.index = gm.getPropertyIndex(n);
95         
96             if (this.newValue == null)
97                 k = "Rmv";
98             else
99                 if (this.oldValue == null)
100                     k = "Add";
101                 else
102                     k = "Chg";
103         
104             out("<" + k + " Lnr:" + this.listenerName + " Evt:" + n +
105                 " Src:" + this.source.getClass().getName() + ">");
106             if (remove) {
107                 out("<" + gm.getPropertyName(n) + "[" + this.index +
108                     "]" + " - Parent: " + gm.getPropertyParentName(n) + ">");
109             } else {
110                 BaseBean propertyParent = gm.getPropertyParent(n);
111                 String JavaDoc parentClassName = propertyParent == null ? "<no class>" : propertyParent.getClass().getName();
112                 out("<" + gm.getPropertyName(n) + "[" + this.index +
113                     "]" + " - Parent: " + gm.getPropertyParentName(n) +
114                     "/" + parentClassName + ">");
115             }
116         
117             //out("received " + this.oldValue + "/" + this.newValue + "/" +
118
// this.propertyName);
119
}
120     
121         public void removeMode() {
122             this.remove = true;
123         }
124     
125         public Object JavaDoc oldValue() {
126             return this.oldValue;
127         }
128     
129         public String JavaDoc stringOldValue() {
130             if (this.oldValue == null)
131                 return "<null>";
132             else
133                 return this.oldValue.toString();
134         }
135     
136         public Object JavaDoc newValue() {
137             return this.newValue;
138         }
139     
140         public String JavaDoc stringNewValue() {
141             if (this.newValue == null)
142                 return "<null>";
143             else
144                 return this.newValue.toString();
145         }
146     
147         public String JavaDoc name() {
148             return this.propertyName;
149         }
150     
151         public void mute(boolean mute) {
152             this.mute = mute;
153         }
154     
155         public String JavaDoc toString() {
156             return this.name() + " raised from source " +
157                 this.source.getClass().getName();
158         }
159     }
160     
161     private MyListener l1, l2, l3;
162     
163     void mute(boolean mute) {
164         if (l1 != null)
165             l1.mute(mute);
166         if (l2 != null)
167             l2.mute(mute);
168         if (l3 != null)
169             l3.mute(mute);
170     }
171     
172     public void run()
173         throws Exception JavaDoc {
174         Book b1, b2, b3;
175     
176         this.readDocument("tm01_g1.xml");
177         out("creating the bean graph1");
178         b1 = Book.createGraph(doc);
179     
180         this.readDocument("tm01_g1.xml");
181         out("creating the bean graph2");
182         b2 = Book.createGraph(doc);
183     
184         l1 = new MyListener(b1);
185         b1.addPropertyChangeListener(l1);
186     
187         // b2 should never change, so we should never receive anything
188
// on this listener
189
l2 = new MyListener(b2);
190         b2.addPropertyChangeListener(l2);
191     
192         //
193
// Merge two identical graphs
194
//
195
setTest("Merge(Update) - two identical xml files");
196         out("should not get any event");
197         b1.merge(b2);
198         check(b1.toString().equals(b2.toString()), " - no diff");
199         check(b1.isEqualTo(b2), " - equals");
200     
201         // One mixed up document, but should be the same
202
setTest("Merge(Update) - two identical xml files (one mixed up)");
203         this.readDocument("tm01_g2.xml");
204         out("creating the bean graph3");
205         b3 = Book.createGraph(doc);
206     
207         l3 = new MyListener(b3);
208         b3.addPropertyChangeListener(l3);
209     
210         b3.merge(b1);
211         check(b3.isEqualTo(b1), " - equals");
212     
213         //
214
// Change only one element of the second graph
215
//
216
setTest("Merge(Update) - changing one property");
217         String JavaDoc s1 = "This is the new summary";
218         String JavaDoc s2 = b2.getSummary();
219     
220         // g1/g2
221
this.mute(true);
222         b2.setSummary(s1);
223         out("should get one event for Summary");
224         this.mute(false);
225         b1.merge(b2);
226     
227         b1.write(System.out);
228     
229         check(b2.isEqualTo(b1), " - equals");
230         // g1/g3
231
this.mute(true);
232         b1.setSummary(s2);
233         this.mute(false);
234         out("should not get any event");
235         b1.merge(b3);
236         check(b3.isEqualTo(b1), " - equals");
237         this.mute(true);
238         b2.setSummary(s2);
239     
240         //
241
// Mixing array of strings
242
//
243
setTest("Merge(Update) - mixing array of strings");
244         check(b1.getAuthor(0).equals(b2.getAuthor(0)), "initial match");
245         check(b1.getAuthor(1).equals(b2.getAuthor(1)), "initial match");
246         check(b1.getAuthor(2).equals(b2.getAuthor(2)), "initial match");
247         this.mute(false);
248         String JavaDoc [] ss1 = b2.getAuthor();
249         String JavaDoc [] ss2 = new String JavaDoc[3];
250         ss2[0] = ss1[2];
251         ss2[1] = ss1[1];
252         ss2[2] = ss1[0];
253         b2.setAuthor(ss2);
254         check(b1.getAuthor(0).equals(b2.getAuthor(2)), "mixed");
255         check(b1.getAuthor(1).equals(b2.getAuthor(1)), "mixed");
256         check(b1.getAuthor(2).equals(b2.getAuthor(0)), "mixed");
257         out("should not get any event");
258         b1.merge(b2);
259         check(b2.isEqualTo(b1), " - equals");
260         this.mute(true);
261         b2.setAuthor(ss1);
262         check(b1.getAuthor(0).equals(b2.getAuthor(0)), "reset match");
263         check(b1.getAuthor(1).equals(b2.getAuthor(1)), "reset match");
264         check(b1.getAuthor(2).equals(b2.getAuthor(2)), "reset match");
265     
266         //
267
// Mixing array of beans
268
//
269
setTest("Merge(Update) - mixing array of beans");
270         check(b1.getChapter(0).isEqualTo(b2.getChapter(0)), "initial match");
271         check(b1.getChapter(1).isEqualTo(b2.getChapter(1)), "initial match");
272         check(b1.getChapter(2).isEqualTo(b2.getChapter(2)), "initial match");
273         this.mute(false);
274         Chapter [] cc1 = b2.getChapter();
275         Chapter [] cc2 = new Chapter[3];
276         cc2[0] = cc1[2];
277         cc2[1] = cc1[1];
278         cc2[2] = cc1[0];
279         b2.setChapter(cc2);
280         check(b1.getChapter(0).isEqualTo(b2.getChapter(2)), "mixed");
281         check(b1.getChapter(1).isEqualTo(b2.getChapter(1)), "mixed");
282         check(b1.getChapter(2).isEqualTo(b2.getChapter(0)), "mixed");
283         out("should not get any event");
284         b1.merge(b2);
285         check(b2.isEqualTo(b1), " - equals");
286         this.mute(true);
287         b2.setChapter(cc1);
288         check(b1.getChapter(0).isEqualTo(b2.getChapter(0)), "reset match");
289         check(b1.getChapter(1).isEqualTo(b2.getChapter(1)), "reset match");
290         check(b1.getChapter(2).isEqualTo(b2.getChapter(2)), "reset match");
291     
292         //
293
// Removing a String elt from an array
294
//
295
setTest("Merge(Update) - remove elt from array of strings");
296         check(b1.getAuthor(0).equals(b2.getAuthor(0)), "initial match");
297         check(b1.getAuthor(1).equals(b2.getAuthor(1)), "initial match");
298         check(b1.getAuthor(2).equals(b2.getAuthor(2)), "initial match");
299         this.mute(false);
300         s1 = b2.getAuthor(1);
301         out("should get one remove event from deletion");
302         b2.removeAuthor(s1);
303         check(b1.getAuthor(0).equals(b2.getAuthor(0)), "match after rem");
304         check(b1.getAuthor(2).equals(b2.getAuthor(1)), "match after rem");
305         out("should get one remove event from merge");
306         b1.merge(b2);
307         check(b1.getAuthor(0).equals(b2.getAuthor(0)), "match");
308         check(b1.getAuthor(1).equals(b2.getAuthor(1)), "match");
309         check(b1.sizeAuthor() == b2.sizeAuthor(), "correct size");
310         check(b2.isEqualTo(b1), " - equals");
311     
312         //
313
// Adding a String elt from an array
314
//
315
setTest("Merge(Update) - add elt from array of strings");
316         this.mute(false);
317         out("should get one event for elt added");
318         b2.addAuthor(s1);
319         check(b2.getAuthor(2).equals(s1), "added");
320         out("should get one event for elt added from merge");
321         //out("Here is b1:");
322
//b1.writeNoReindent(System.out);
323
//out("Here is b2:");
324
//b2.writeNoReindent(System.out);
325
b1.merge(b2);
326         //out("Here is b1:");
327
//b1.writeNoReindent(System.out);
328
check(b1.getAuthor(0).equals(b2.getAuthor(0)), "match");
329         check(b1.getAuthor(1).equals(b2.getAuthor(1)), "match");
330         check(b1.getAuthor(2).equals(b2.getAuthor(2)), "match");
331         check(b1.sizeAuthor() == b2.sizeAuthor(), "correct size");
332         check(b2.isEqualTo(b1), " - equals");
333     
334         //
335
// Removing a Bean elt from an array
336
//
337
setTest("Merge(Update) - remove elt from array of beans");
338         check(b1.getChapter(0).isEqualTo(b2.getChapter(0)), "initial match");
339         check(b1.getChapter(1).isEqualTo(b2.getChapter(1)), "initial match");
340         check(b1.getChapter(2).isEqualTo(b2.getChapter(2)), "initial match");
341         this.mute(false);
342         Chapter c1 = b2.getChapter(1);
343         Chapter c2 = (Chapter)c1.clone();
344         out("should get one remove event from deletion");
345         b2.removeChapter(c1);
346         check(b1.getChapter(0).isEqualTo(b2.getChapter(0)), "match after rem");
347         check(b1.getChapter(2).isEqualTo(b2.getChapter(1)), "match after rem");
348         out("should get one remove event from merge");
349         b1.merge(b2);
350         check(b1.getChapter(0).isEqualTo(b2.getChapter(0)), "match");
351         check(b1.getChapter(1).isEqualTo(b2.getChapter(1)), "match");
352         check(b1.sizeChapter() == b2.sizeChapter(), "correct size");
353         check(b2.isEqualTo(b1), " - equals");
354     
355         //
356
// Adding a Bean elt from an array
357
//
358
setTest("Merge(Update) - add elt from array of strings");
359         this.mute(false);
360         out("should get one event for elt added");
361         b2.addChapter(c2);
362         check(b2.getChapter(2).isEqualTo(c2), "added");
363         out("should get one event for elt added from merge");
364         b1.merge(b2);
365         check(b1.getChapter(0).isEqualTo(b2.getChapter(0)), "match");
366         check(b1.getChapter(1).isEqualTo(b2.getChapter(1)), "match");
367         check(b1.getChapter(2).isEqualTo(b2.getChapter(2)), "match");
368         check(b1.sizeChapter() == b2.sizeChapter(), "correct size");
369         check(b2.isEqualTo(b1), " - equals");
370     
371     
372         //
373
// Compare two graphs with missing nodes and elements
374
//
375
Book b4, b5, b6;
376     
377         this.readDocument("tm01_g1.xml");
378         out("creating the bean graph1");
379         b4 = Book.createGraph(doc);
380     
381         b5 = (Book)b4.clone();
382         b6 = (Book)b4.clone();
383     
384         setTest("comparing graphs with 1 null elts");
385         check(b4.sizeChapter() == 3);
386         check(b5.sizeChapter() == 3);
387     
388         // bean[] full / bean null
389
check(b4.isEqualTo(b5));
390     
391         setTest("comparing graphs with null indexed elts");
392         // g1.bean[] has 1 null / g2.bean[] has 1 null / bean null
393
b4.setChapter(1, null);
394         b5.setChapter(2, null);
395         check(!b4.isEqualTo(b5));
396         b4.merge(b5);
397         check(b4.sizeChapter() == 3, "correct new array sise");
398         check(b4.getChapter(0).isEqualTo(b5.getChapter(0)), "elt ok");
399         check(b4.getChapter(1) == null, "elt ok");
400         check(b4.getChapter(2).isEqualTo(b5.getChapter(1)), "elt ok");
401     
402         setTest("comparing graphs with null single bean elt");
403         // g1.bean non null / g2.bean is null
404
b4 = (Book)b6.clone();
405         b5 = (Book)b6.clone();
406         Content ct = new Content();
407         ct.setTitle("This is a title");
408         ct.setComment("And this is a comment");
409         check(b4.isEqualTo(b5));
410         b5.setContent(ct);
411         check(!b4.isEqualTo(b5));
412         b4.merge(b5);
413         check(b4.getContent().isEqualTo(b5.getContent()));
414     
415         // Clone an element which is not part of a graph
416
Chapter c3 = new Chapter();
417         c3.setComment("This is a comment");
418         c3.setNumber("123");
419         c3.addParagraph("This is a new paragraph");
420         Chapter c4 = (Chapter)c3.clone();
421     
422         // Add both elements to two identical graphs - should get two
423
// identical graphs
424
setTest("cloning a new bean");
425         Book b7 = (Book)b2.clone();
426         Book b8 = (Book)b2.clone();
427         b7.addChapter(c3);
428         b8.addChapter(c4);
429         check(c3.isEqualTo(c4), "objects equal");
430         check(b7.isEqualTo(b8), "same graph once added");
431     
432         //
433
// Test the attributes. When we merge graphs, we need to make
434
// sure that the attributes are also merged.
435
//
436
this.readDocument("tm01_g2.xml");
437         out("creating the bean graph1");
438         b1 = Book.createGraph(doc);
439     
440         // g3 and g2 elements are identicals, g3 has attributes,
441
// g2 has not.
442
this.readDocument("tm01_g3.xml");
443         out("creating the bean graph2");
444         b2 = Book.createGraph(doc);
445
446         // Make sure that we can clone a part of the graph without loosing
447
// the attributes.
448
BaseBean bb = b2.getChapter(0);
449         // Should see the chapter attribute
450
out(bb.dumpDomNode());
451
452         b3 = (Book)b1.clone();
453         int index = b3.addValue("Chapter", bb.clone());
454         bb = b3.getChapter(index);
455         // Should see the chapter attribute on this cloned element
456
out(bb.dumpDomNode());
457
458         l1 = new MyListener(b1);
459         l1.mute(true);
460         b1.addPropertyChangeListener(l1);
461     
462         // b2 should never change, so we should never receive anything
463
// on this listener
464
l2 = new MyListener(b2);
465         b2.addPropertyChangeListener(l2);
466
467         // The only events we should have when we merge are the
468
// attributes events, because the graphs only differ by attr.
469
setTest("Merging attributes");
470         check(!b1.isEqualTo(b2), "shouldn't be equals (diff an attr)");
471         //GraphManager.debug(true);
472
b1.merge(b2);
473         check(b1.isEqualTo(b2), "should be equals");
474     
475         // Make sure that b1 has the attributes
476
s1 = b1.getAttributeValue("good");
477         check(s1 != null, "attr on root != null");
478         if (s1 != null) {
479             check(s1.equals("no"), "attr on root");
480         }
481         s1 = b1.getAttributeValue("summary", "length");
482         check(s1 != null, "attr on summary != null");
483         if (s1 != null) {
484             check(s1.equals("123"), "attr on summary");
485         }
486         s1 = b1.getAttributeValue("summary", "lang");
487         check(s1 != null, "attr on summary != null");
488         if (s1 != null) {
489             check(s1.equals("us"), "attr on summary");
490         }
491
492         s1 = b1.getAttributeValue("chapter", 1, "title");
493         out(s1);
494         check(s1 != null, "attr on chapter != null");
495         if (s1 != null) {
496             check(s1.equals("First"), "attr on chapter");
497         }
498
499         s1 = b1.getAttributeValue("chapter", 2, "title");
500         out(s1);
501         check(s1 != null, "attr on chapter != null");
502         if (s1 != null) {
503             check(s1.equals("Second"), "attr on chapter");
504         }
505     
506         //
507
// Make sure that we do not consume too much memory
508
//
509

510         // Ignore the first one
511
this.getKMemUsage();
512
513         int k1 = this.getKMemUsage();
514         
515         Book newBook;
516
517         this.readDocument("tm01_g3.xml");
518         out("creating the bean graph for memory test");
519         newBook = Book.createGraph(doc);
520         
521         int maxLoop = 50;
522         BaseBean[] aBook = new BaseBean[maxLoop];
523         for(int loop=0; loop<maxLoop; loop++) {
524             aBook[loop] = (BaseBean)newBook.clone();
525         }
526
527         int k2 = this.getKMemUsage() - k1;
528
529         float diff = (float)(k2 - memUsageReference);
530
531         if (diff > 0) {
532             // We consume more memory than excepted
533
float p = diff/memUsageReference*100;
534             if (p > 20.0) {
535                 out("It seems that the last schema2beans code changes have increased the memory consumption by " + p + "%");
536                 out("If this is expected and acceptable, change the memUsageReference value in TestMerge.java, to be " + k2);
537             }
538         } else {
539             // We consume less memory than expected
540
float p = Math.abs(diff)/memUsageReference*100;
541             if (p > 25.0) {
542                 out("It seems that the last schema2beans code changes have decreased the memory consumption by " + p + "% !!!");
543                 out("Please, change the memUsageReference value in TestMerge.java, to be " + k2);
544             }
545         }
546         out("memory test done");
547
548         readDocument("tm01_g1.xml");
549         b1 = Book.createGraph(doc);
550         readDocument("tm01_g4.xml");
551         out("creating the bean graph for the comment merge test");
552         Book commentedGraph = Book.createGraph(doc);
553         b1.merge(commentedGraph);
554         out(b1);
555     }
556 }
557
558
559
Popular Tags