KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > undo > QuietUndoManagerTest


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  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui.undo;
21
22 import java.io.IOException JavaDoc;
23 import javax.swing.event.UndoableEditEvent JavaDoc;
24 import javax.swing.text.AbstractDocument JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.DefaultStyledDocument JavaDoc;
27 import javax.swing.undo.AbstractUndoableEdit JavaDoc;
28 import javax.swing.undo.CannotRedoException JavaDoc;
29 import javax.swing.undo.CannotUndoException JavaDoc;
30 import junit.framework.TestCase;
31 import org.netbeans.editor.BaseDocument;
32 import org.netbeans.modules.xml.schema.model.GlobalElement;
33 import org.netbeans.modules.xml.schema.model.Schema;
34 import org.netbeans.modules.xml.schema.model.SchemaModel;
35 import org.netbeans.modules.xml.xam.ui.TestCatalogModel;
36 import org.netbeans.modules.xml.xam.ui.Util;
37 import org.openide.awt.UndoRedo;
38
39 /**
40  * Tests QuietUndoManager class, both in isolation and with XAM model.
41  * Additional tests of QuietUndoManager can be found in schema/core.
42  *
43  * @author Nathan Fiedler
44  */

45 public class QuietUndoManagerTest extends TestCase {
46     private SchemaModel model;
47     private AbstractDocument JavaDoc document;
48     private QuietUndoManager manager;
49
50     public QuietUndoManagerTest(String JavaDoc testName) {
51         super(testName);
52     }
53
54     @Override JavaDoc
55     protected void tearDown() {
56         TestCatalogModel.getDefault().clearDocumentPool();
57     }
58
59     /**
60      * Delete the document text and discard all undoable edits.
61      */

62     private void cleanup() {
63         manager.endCompound();
64         document.removeUndoableEditListener(manager);
65         if (model != null) {
66             model.removeUndoableEditListener(manager);
67         }
68         manager.discardAllEdits();
69         manager.die();
70         manager = null;
71         try {
72             document.replace(0, document.getLength(), null, null);
73         } catch (BadLocationException JavaDoc ble) {
74             // ignore, it's irrelevant
75
}
76         document = null;
77         model = null;
78     }
79
80     /**
81      * Ends the compound mode of the undo manager and discards all edits.
82      */

83     private void endTestCase() {
84         manager.endCompound();
85         document.removeUndoableEditListener(manager);
86         if (model != null) {
87             model.removeUndoableEditListener(manager);
88         } else {
89             // Without a backing model, the document can be scrapped.
90
try {
91                 document.replace(0, document.getLength(), null, null);
92             } catch (BadLocationException JavaDoc ble) {
93                 // ignore, it's irrelevant
94
}
95         }
96         manager.discardAllEdits();
97         // Clear out the cached document instances to start fresh.
98
TestCatalogModel.getDefault().clearDocumentPool();
99     }
100
101     /**
102      * Tests the QuietUndoManager with a simple DefaultStyledDocument and
103      * no XAM-based model, to ensure manager has correct logic.
104      */

105     public void testWithoutModel() {
106         document = new DefaultStyledDocument JavaDoc();
107         manager = new QuietUndoManager(new UndoRedo.Manager());
108         manager.setDocument(document);
109
110         // The following test cases are documented in the undo/redo
111
// specification (xml/www/specs/undo/index.html).
112
// Each of these test cases should succeed individually.
113
// Comment out one or more to test a particular case.
114

115         // Case A: Initially the actions should be disabled.
116
viewModel();
117         assertFalse(manager.canUndo());
118         assertFalse(manager.canRedo());
119         viewSource();
120         assertFalse(manager.canUndo());
121         assertFalse(manager.canRedo());
122         viewModel();
123         assertFalse(manager.canUndo());
124         assertFalse(manager.canRedo());
125         endTestCase();
126
127         // Case B: Undo/redo of a model edit.
128
viewModel();
129         manager.undoableEditHappened(new UndoableEditEvent JavaDoc(this,
130                 new AbstractUndoableEdit JavaDoc()));
131         assertTrue(manager.canUndo());
132         assertFalse(manager.canRedo());
133         manager.undo();
134         assertFalse(manager.canUndo());
135         assertTrue(manager.canRedo());
136         manager.redo();
137         assertTrue(manager.canUndo());
138         assertFalse(manager.canRedo());
139         viewSource();
140         assertTrue(manager.canUndo());
141         assertFalse(manager.canRedo());
142         manager.undo();
143         assertFalse(manager.canUndo());
144         assertTrue(manager.canRedo());
145         manager.redo();
146         assertTrue(manager.canUndo());
147         assertFalse(manager.canRedo());
148         endTestCase();
149
150         // Case C: Undo/redo of a source edit.
151
viewSource();
152         try {
153             document.insertString(0, ": case C1", null);
154             document.insertString(0, ": case C2", null);
155         } catch (BadLocationException JavaDoc ble) {
156             fail(ble.toString());
157         }
158         assertTrue(manager.canUndo());
159         assertFalse(manager.canRedo());
160         manager.undo();
161         assertTrue(manager.canUndo());
162         assertTrue(manager.canRedo());
163         manager.undo();
164         assertFalse(manager.canUndo());
165         assertTrue(manager.canRedo());
166         manager.redo();
167         assertTrue(manager.canUndo());
168         assertTrue(manager.canRedo());
169         manager.redo();
170         assertTrue(manager.canUndo());
171         assertFalse(manager.canRedo());
172         viewModel();
173         assertTrue(manager.canUndo());
174         assertFalse(manager.canRedo());
175         manager.undo();
176         assertFalse(manager.canUndo());
177         assertTrue(manager.canRedo());
178         manager.redo();
179         assertTrue(manager.canUndo());
180         assertFalse(manager.canRedo());
181         try {
182             assertEquals(": case C2: case C1",
183                     document.getText(0, document.getLength()));
184         } catch (BadLocationException JavaDoc ble) {
185             fail(ble.toString());
186         }
187         endTestCase();
188
189         // Case D, cannot test this in unit tests.
190

191         // Case E, cannot test this in unit tests.
192

193         // Case F: Undo/redo with a mix of document and model edits.
194
viewSource();
195         try {
196             document.insertString(0, ": case F1", null);
197             document.insertString(0, ": case F2", null);
198         } catch (BadLocationException JavaDoc ble) {
199             fail(ble.toString());
200         }
201         viewModel();
202         manager.undoableEditHappened(new UndoableEditEvent JavaDoc(this,
203                 new AbstractUndoableEdit JavaDoc()));
204         viewSource();
205         assertTrue(manager.canUndo());
206         assertFalse(manager.canRedo());
207         manager.undo();
208         assertTrue(manager.canUndo());
209         assertTrue(manager.canRedo());
210         manager.undo();
211         assertTrue(manager.canUndo());
212         assertTrue(manager.canRedo());
213         manager.undo();
214         assertFalse(manager.canUndo());
215         assertTrue(manager.canRedo());
216         manager.redo();
217         assertTrue(manager.canUndo());
218         assertTrue(manager.canRedo());
219         manager.redo();
220         assertTrue(manager.canUndo());
221         assertTrue(manager.canRedo());
222         manager.redo();
223         assertTrue(manager.canUndo());
224         assertFalse(manager.canRedo());
225         viewModel();
226         assertTrue(manager.canUndo());
227         assertFalse(manager.canRedo());
228         manager.undo();
229         assertTrue(manager.canUndo());
230         assertTrue(manager.canRedo());
231         manager.undo();
232         assertFalse(manager.canUndo());
233         assertTrue(manager.canRedo());
234         manager.redo();
235         assertTrue(manager.canUndo());
236         assertTrue(manager.canRedo());
237         manager.redo();
238         assertTrue(manager.canUndo());
239         assertFalse(manager.canRedo());
240         try {
241             assertEquals(": case F2: case F1",
242                     document.getText(0, document.getLength()));
243         } catch (BadLocationException JavaDoc ble) {
244             fail(ble.toString());
245         }
246         endTestCase();
247
248         // Case G, cannot test this in unit tests.
249

250         // Case AA: Single document edit.
251
viewSource();
252         try {
253             document.insertString(0, "a", null);
254         } catch (BadLocationException JavaDoc ble) {
255             fail(ble.toString());
256         }
257         viewModel();
258         assertTrue(manager.canUndo());
259         assertFalse(manager.canRedo());
260         manager.undo();
261         assertFalse(manager.canUndo());
262         assertTrue(manager.canRedo());
263         manager.redo();
264         assertTrue(manager.canUndo());
265         assertFalse(manager.canRedo());
266         viewSource();
267         assertTrue(manager.canUndo());
268         assertFalse(manager.canRedo());
269         manager.undo();
270         assertFalse(manager.canUndo());
271         assertTrue(manager.canRedo());
272         manager.redo();
273         assertTrue(manager.canUndo());
274         assertFalse(manager.canRedo());
275         try {
276             assertEquals("a", document.getText(0, document.getLength()));
277         } catch (BadLocationException JavaDoc ble) {
278             fail(ble.toString());
279         }
280         endTestCase();
281
282         // Case AC: An open compound edit is properly closed.
283
viewSource();
284         try {
285             document.insertString(0, ": case AC1", null);
286             document.insertString(0, ": case AC2", null);
287         } catch (BadLocationException JavaDoc ble) {
288             fail(ble.toString());
289         }
290         viewModel();
291         viewSource();
292         assertTrue(manager.canUndo());
293         assertFalse(manager.canRedo());
294         manager.undo();
295         assertTrue(manager.canUndo());
296         assertTrue(manager.canRedo());
297         try {
298             document.insertString(0, ": case AC3", null);
299         } catch (BadLocationException JavaDoc ble) {
300             fail(ble.toString());
301         }
302         viewModel();
303         assertTrue(manager.canUndo());
304         assertFalse(manager.canRedo());
305         manager.undo();
306         assertFalse(manager.canUndo());
307         assertTrue(manager.canRedo());
308         manager.redo();
309         assertTrue(manager.canUndo());
310         assertFalse(manager.canRedo());
311         try {
312             assertEquals(": case AC3: case AC1",
313                     document.getText(0, document.getLength()));
314         } catch (BadLocationException JavaDoc ble) {
315             fail(ble.toString());
316         }
317         endTestCase();
318
319         // Case AD: Switching views leaves undo history intact.
320
viewSource();
321         try {
322             document.insertString(0, ": case AD1", null);
323             document.insertString(0, ": case AD2", null);
324         } catch (BadLocationException JavaDoc ble) {
325             fail(ble.toString());
326         }
327         assertTrue(manager.canUndo());
328         assertFalse(manager.canRedo());
329         manager.undo();
330         assertTrue(manager.canUndo());
331         assertTrue(manager.canRedo());
332         viewModel();
333         viewSource();
334         assertTrue(manager.canUndo());
335         assertTrue(manager.canRedo());
336         manager.undo();
337         assertFalse(manager.canUndo());
338         assertTrue(manager.canRedo());
339         manager.redo();
340         assertTrue(manager.canUndo());
341         assertTrue(manager.canRedo());
342         manager.redo();
343         assertTrue(manager.canUndo());
344         assertFalse(manager.canRedo());
345         try {
346             assertEquals(": case AD2: case AD1",
347                     document.getText(0, document.getLength()));
348         } catch (BadLocationException JavaDoc ble) {
349             fail(ble.toString());
350         }
351         endTestCase();
352
353         // Case AE: Open compound edit and switching views should close compound set.
354
viewSource();
355         try {
356             document.insertString(0, ": case AE1", null);
357             document.insertString(0, ": case AE2", null);
358         } catch (BadLocationException JavaDoc ble) {
359             fail(ble.toString());
360         }
361         viewModel();
362         viewSource();
363         assertTrue(manager.canUndo());
364         assertFalse(manager.canRedo());
365         manager.undo();
366         assertTrue(manager.canUndo());
367         assertTrue(manager.canRedo());
368         viewModel();
369         assertTrue(manager.canUndo());
370         assertTrue(manager.canRedo());
371         manager.undo();
372         assertFalse(manager.canUndo());
373         assertTrue(manager.canRedo());
374         manager.redo();
375         assertTrue(manager.canUndo());
376         assertFalse(manager.canRedo());
377         try {
378             assertEquals(": case AE2: case AE1",
379                     document.getText(0, document.getLength()));
380         } catch (BadLocationException JavaDoc ble) {
381             fail(ble.toString());
382         }
383         endTestCase();
384
385         // Case AF: Document edit is undone, then replaced by model edit.
386
viewSource();
387         try {
388             document.insertString(0, ": case AF1", null);
389             document.insertString(0, ": case AF2", null);
390         } catch (BadLocationException JavaDoc ble) {
391             fail(ble.toString());
392         }
393         assertTrue(manager.canUndo());
394         assertFalse(manager.canRedo());
395         manager.undo();
396         assertTrue(manager.canUndo());
397         assertTrue(manager.canRedo());
398         viewModel();
399         manager.undoableEditHappened(new UndoableEditEvent JavaDoc(this,
400                 new AbstractUndoableEdit JavaDoc() {
401             private static final long serialVersionUID = 1L;
402
403             public void redo() throws CannotRedoException JavaDoc {
404                 super.redo();
405                 try {
406                     document.insertString(0, ": case AF3", null);
407                 } catch (BadLocationException JavaDoc ble) {
408                     fail(ble.toString());
409                 }
410             }
411
412             public void undo() throws CannotUndoException JavaDoc {
413                 super.undo();
414                 try {
415                     document.replace(0, 10, null, null);
416                 } catch (BadLocationException JavaDoc ble) {
417                     fail(ble.toString());
418                 }
419             }
420         }));
421         try {
422             document.insertString(0, ": case AF3", null);
423         } catch (BadLocationException JavaDoc ble) {
424             fail(ble.toString());
425         }
426         assertTrue(manager.canUndo());
427         assertFalse(manager.canRedo());
428         manager.undo();
429         assertTrue(manager.canUndo());
430         assertTrue(manager.canRedo());
431         manager.undo();
432         assertFalse(manager.canUndo());
433         assertTrue(manager.canRedo());
434         manager.redo();
435         assertTrue(manager.canUndo());
436         assertTrue(manager.canRedo());
437         manager.redo();
438         assertTrue(manager.canUndo());
439         assertFalse(manager.canRedo());
440         try {
441             assertEquals(": case AF3: case AF1",
442                     document.getText(0, document.getLength()));
443         } catch (BadLocationException JavaDoc ble) {
444             fail(ble.toString());
445         }
446         endTestCase();
447
448         cleanup();
449     }
450
451     /**
452      * Tests the QuietUndoManager with a schema model and NetBeans editor
453      * document, to ensure manager operates correctly in near-native
454      * environment.
455      */

456     public void testWithModel() {
457         try {
458             model = Util.loadSchemaModel(Util.PO_XSD);
459         } catch (Exception JavaDoc e) {
460             fail(e.toString());
461         }
462         assertNotNull("failed to load SchemaModel", model);
463         document = (AbstractDocument JavaDoc) model.getModelSource().getLookup().
464                 lookup(AbstractDocument JavaDoc.class);
465         assertNotNull("ModelSource did not contain AbstractDocument", document);
466         // We are most interested in the issues that BaseDocument creates.
467
assertTrue(document instanceof BaseDocument);
468         manager = new QuietUndoManager(new UndoRedo.Manager());
469         manager.setDocument(document);
470
471         // The following test cases are documented in the undo/redo
472
// specification (xml/www/specs/undo/index.html).
473
// Each of these test cases should succeed individually.
474
// Comment out one or more to test a particular case.
475

476         // Case A: Initially the actions should be disabled.
477
assertFalse(manager.canUndo());
478         assertFalse(manager.canRedo());
479         viewSource();
480         assertFalse(manager.canUndo());
481         assertFalse(manager.canRedo());
482         viewModel();
483         assertFalse(manager.canUndo());
484         assertFalse(manager.canRedo());
485         endTestCase();
486
487         // Case B: Undo/redo of a model edit.
488
viewModel();
489         model.startTransaction();
490         Util.createGlobalElement(model, "elementB");
491         model.endTransaction();
492         assertTrue(manager.canUndo());
493         assertFalse(manager.canRedo());
494         manager.undo();
495         assertFalse(manager.canUndo());
496         assertTrue(manager.canRedo());
497         manager.redo();
498         assertTrue(manager.canUndo());
499         assertFalse(manager.canRedo());
500         viewSource();
501         assertTrue(manager.canUndo());
502         assertFalse(manager.canRedo());
503         manager.undo();
504         assertFalse(manager.canUndo());
505         assertTrue(manager.canRedo());
506         manager.redo();
507         assertTrue(manager.canUndo());
508         assertFalse(manager.canRedo());
509         endTestCase();
510
511         // Case C: Undo/redo of a source edit.
512
viewSource();
513         try {
514             document.insertString(0, "<!-- C1 -->", null);
515             document.insertString(0, "<!-- C2 -->", null);
516         } catch (BadLocationException JavaDoc ble) {
517             fail(ble.toString());
518         }
519         assertTrue(manager.canUndo());
520         assertFalse(manager.canRedo());
521         manager.undo();
522         assertTrue(manager.canUndo());
523         assertTrue(manager.canRedo());
524         manager.undo();
525         assertFalse(manager.canUndo());
526         assertTrue(manager.canRedo());
527         manager.redo();
528         assertTrue(manager.canUndo());
529         assertTrue(manager.canRedo());
530         manager.redo();
531         assertTrue(manager.canUndo());
532         assertFalse(manager.canRedo());
533         viewModel();
534         assertTrue(manager.canUndo());
535         assertFalse(manager.canRedo());
536         manager.undo();
537         assertFalse(manager.canUndo());
538         assertTrue(manager.canRedo());
539         manager.redo();
540         assertTrue(manager.canUndo());
541         assertFalse(manager.canRedo());
542         endTestCase();
543
544         // Case D, cannot test this in unit tests.
545

546         // Case E, cannot test this in unit tests.
547

548         // Case F: Undo/redo with a mix of document and model edits.
549
viewSource();
550         try {
551             document.insertString(0, "<!-- F1 -->", null);
552             document.insertString(0, "<!-- F2 -->", null);
553         } catch (BadLocationException JavaDoc ble) {
554             fail(ble.toString());
555         }
556         viewModel();
557         model.startTransaction();
558         Util.createGlobalElement(model, "elementF1");
559         model.endTransaction();
560         viewSource();
561         assertTrue(manager.canUndo());
562         assertFalse(manager.canRedo());
563         manager.undo();
564         assertTrue(manager.canUndo());
565         assertTrue(manager.canRedo());
566         manager.undo();
567         assertTrue(manager.canUndo());
568         assertTrue(manager.canRedo());
569         manager.undo();
570         assertFalse(manager.canUndo());
571         assertTrue(manager.canRedo());
572         manager.redo();
573         assertTrue(manager.canUndo());
574         assertTrue(manager.canRedo());
575         manager.redo();
576         assertTrue(manager.canUndo());
577         assertTrue(manager.canRedo());
578         manager.redo();
579         assertTrue(manager.canUndo());
580         assertFalse(manager.canRedo());
581         viewModel();
582         assertTrue(manager.canUndo());
583         assertFalse(manager.canRedo());
584         manager.undo();
585         assertTrue(manager.canUndo());
586         assertTrue(manager.canRedo());
587         manager.undo();
588         assertFalse(manager.canUndo());
589         assertTrue(manager.canRedo());
590         manager.redo();
591         assertTrue(manager.canUndo());
592         assertTrue(manager.canRedo());
593         manager.redo();
594         assertTrue(manager.canUndo());
595         assertFalse(manager.canRedo());
596         endTestCase();
597
598         // Case G, cannot test this in unit tests.
599

600         // Case AA: Single document edit.
601
viewSource();
602         try {
603             document.insertString(0, "\n", null);
604         } catch (BadLocationException JavaDoc ble) {
605             fail(ble.toString());
606         }
607         viewModel();
608         assertTrue(manager.canUndo());
609         assertFalse(manager.canRedo());
610         manager.undo();
611         assertFalse(manager.canUndo());
612         assertTrue(manager.canRedo());
613         manager.redo();
614         assertTrue(manager.canUndo());
615         assertFalse(manager.canRedo());
616         viewSource();
617         assertTrue(manager.canUndo());
618         assertFalse(manager.canRedo());
619         manager.undo();
620         assertFalse(manager.canUndo());
621         assertTrue(manager.canRedo());
622         manager.redo();
623         assertTrue(manager.canUndo());
624         assertFalse(manager.canRedo());
625         endTestCase();
626
627         // Case AC: An open compound edit is properly closed.
628
viewSource();
629         try {
630             document.insertString(0, "<!-- AC1 -->", null);
631             document.insertString(0, "<!-- AC2 -->", null);
632         } catch (BadLocationException JavaDoc ble) {
633             fail(ble.toString());
634         }
635         viewModel();
636         viewSource();
637         assertTrue(manager.canUndo());
638         assertFalse(manager.canRedo());
639         manager.undo();
640         assertTrue(manager.canUndo());
641         assertTrue(manager.canRedo());
642         try {
643             document.insertString(0, "<!-- AC3 -->", null);
644         } catch (BadLocationException JavaDoc ble) {
645             fail(ble.toString());
646         }
647         viewModel();
648         assertTrue(manager.canUndo());
649         assertFalse(manager.canRedo());
650         manager.undo();
651         assertFalse(manager.canUndo());
652         assertTrue(manager.canRedo());
653         manager.redo();
654         assertTrue(manager.canUndo());
655         assertFalse(manager.canRedo());
656         endTestCase();
657
658         // Case AD: Switching views leaves undo history intact.
659
viewSource();
660         try {
661             document.insertString(0, "<!-- AD1 -->", null);
662             document.insertString(0, "<!-- AD2 -->", null);
663         } catch (BadLocationException JavaDoc ble) {
664             fail(ble.toString());
665         }
666         assertTrue(manager.canUndo());
667         assertFalse(manager.canRedo());
668         manager.undo();
669         assertTrue(manager.canUndo());
670         assertTrue(manager.canRedo());
671         viewModel();
672         viewSource();
673         assertTrue(manager.canUndo());
674         assertTrue(manager.canRedo());
675         manager.undo();
676         assertFalse(manager.canUndo());
677         assertTrue(manager.canRedo());
678         manager.redo();
679         assertTrue(manager.canUndo());
680         assertTrue(manager.canRedo());
681         manager.redo();
682         assertTrue(manager.canUndo());
683         assertFalse(manager.canRedo());
684         endTestCase();
685
686         // Case AE: Open compound edit and switching views should close compound set.
687
viewSource();
688         try {
689             document.insertString(0, "<!-- AE1 -->", null);
690             document.insertString(0, "<!-- AE2 -->", null);
691         } catch (BadLocationException JavaDoc ble) {
692             fail(ble.toString());
693         }
694         viewModel();
695         viewSource();
696         assertTrue(manager.canUndo());
697         assertFalse(manager.canRedo());
698         manager.undo();
699         assertTrue(manager.canUndo());
700         assertTrue(manager.canRedo());
701         viewModel();
702         assertTrue(manager.canUndo());
703         assertTrue(manager.canRedo());
704         manager.undo();
705         assertFalse(manager.canUndo());
706         assertTrue(manager.canRedo());
707         manager.redo();
708         assertTrue(manager.canUndo());
709         assertFalse(manager.canRedo());
710         endTestCase();
711
712         // Case AF: Document edit is undone, then replaced by model edit.
713
viewSource();
714         try {
715             document.insertString(0, "<!-- AF1 -->", null);
716             document.insertString(0, "<!-- AF2 -->", null);
717         } catch (BadLocationException JavaDoc ble) {
718             fail(ble.toString());
719         }
720         assertTrue(manager.canUndo());
721         assertFalse(manager.canRedo());
722         manager.undo();
723         assertTrue(manager.canUndo());
724         assertTrue(manager.canRedo());
725         viewModel();
726         model.startTransaction();
727         Util.createGlobalElement(model, "elementAF");
728         model.endTransaction();
729         assertTrue(manager.canUndo());
730         assertFalse(manager.canRedo());
731         manager.undo();
732         assertTrue(manager.canUndo());
733         assertTrue(manager.canRedo());
734         manager.undo();
735         assertFalse(manager.canUndo());
736         assertTrue(manager.canRedo());
737         manager.redo();
738         assertTrue(manager.canUndo());
739         assertTrue(manager.canRedo());
740         manager.redo();
741         assertTrue(manager.canUndo());
742         assertFalse(manager.canRedo());
743         endTestCase();
744
745         // Case AG: Model edit followed by document edit
746
viewModel();
747         assertFalse(manager.canUndo());
748         assertFalse(manager.canRedo());
749         model.startTransaction();
750         Util.createGlobalElement(model, "elementAG1");
751         model.endTransaction();
752         assertTrue(manager.canUndo());
753         assertFalse(manager.canRedo());
754         viewSource();
755         assertTrue(manager.canUndo());
756         assertFalse(manager.canRedo());
757         try {
758             document.insertString(0, "<!-- AG1 -->", null);
759         } catch (BadLocationException JavaDoc ble) {
760             fail(ble.toString());
761         }
762         assertTrue(manager.canUndo());
763         assertFalse(manager.canRedo());
764         manager.undo();
765         assertTrue(manager.canUndo());
766         assertTrue(manager.canRedo());
767         manager.undo();
768         assertFalse(manager.canUndo());
769         assertTrue(manager.canRedo());
770         manager.redo();
771         assertTrue(manager.canUndo());
772         assertTrue(manager.canRedo());
773         manager.redo();
774         assertTrue(manager.canUndo());
775         assertFalse(manager.canRedo());
776         viewModel();
777         assertTrue(manager.canUndo());
778         assertFalse(manager.canRedo());
779         manager.undo();
780         assertTrue(manager.canUndo());
781         assertTrue(manager.canRedo());
782         manager.undo();
783         assertFalse(manager.canUndo());
784         assertTrue(manager.canRedo());
785         manager.redo();
786         assertTrue(manager.canUndo());
787         assertTrue(manager.canRedo());
788         manager.redo();
789         assertTrue(manager.canUndo());
790         assertFalse(manager.canRedo());
791         endTestCase();
792
793         cleanup();
794     }
795
796     /**
797      * Tests the conditions under which issue 83963 occurs.
798      */

799     public void testIssue83963() {
800         try {
801             model = Util.loadSchemaModel(Util.PO_XSD);
802         } catch (Exception JavaDoc e) {
803             fail(e.toString());
804         }
805         assertNotNull("failed to load SchemaModel", model);
806         document = (AbstractDocument JavaDoc) model.getModelSource().getLookup().
807                 lookup(AbstractDocument JavaDoc.class);
808         assertNotNull("ModelSource did not contain AbstractDocument", document);
809         // We are most interested in the issues that BaseDocument creates.
810
assertTrue(document instanceof BaseDocument);
811         manager = new QuietUndoManager(new UndoRedo.Manager());
812         manager.setDocument(document);
813
814         String JavaDoc original = null;
815         try {
816             original = document.getText(0, document.getLength());
817         } catch (BadLocationException JavaDoc ble) {
818             fail(ble.toString());
819         }
820         viewModel();
821         assertFalse(manager.canUndo());
822         assertFalse(manager.canRedo());
823         model.startTransaction();
824         Util.createGlobalElement(model, "element83963");
825         model.endTransaction();
826         Schema schema = model.getSchema();
827         boolean found = false;
828         for (GlobalElement ge : schema.getElements()) {
829             if (ge.getName().equals("element83963")) {
830                 found = true;
831                 break;
832             }
833         }
834         assertTrue("failed to add global element", found);
835         assertTrue(manager.canUndo());
836         assertFalse(manager.canRedo());
837         viewSource();
838         assertTrue(manager.canUndo());
839         assertFalse(manager.canRedo());
840         try {
841             int length = document.getLength();
842             String JavaDoc text = document.getText(0, length);
843             assertTrue(text.contains("element83963"));
844             int mark = text.lastIndexOf("element83963");
845             assertTrue(mark > 0 && mark < length);
846             mark += "element83963\"/>".length();
847             // Must create a document edit that overlaps the model edit,
848
// and it must introduce a new DOM element (e.g. comment).
849
// For example, the following two lines will not cause the error.
850
//document.insertString(mark, "\n \n", null);
851
//assertEquals(length + 4, document.getLength());
852
document.insertString(mark, "\n<!-- -->\n", null);
853             assertEquals(length + 10, document.getLength());
854         } catch (BadLocationException JavaDoc ble) {
855             fail(ble.toString());
856         }
857         assertTrue(manager.canUndo());
858         assertFalse(manager.canRedo());
859         manager.undo();
860         assertTrue(manager.canUndo());
861         assertTrue(manager.canRedo());
862         manager.undo();
863         assertFalse(manager.canUndo());
864         assertTrue(manager.canRedo());
865         String JavaDoc actual = null;
866         try {
867             actual = document.getText(0, document.getLength());
868         } catch (BadLocationException JavaDoc ble) {
869             fail(ble.toString());
870         }
871         assertTrue(actual.equals(original));
872         manager.redo();
873         assertTrue(manager.canUndo());
874         assertTrue(manager.canRedo());
875 // XXX: uncomment the redo() to get the error seen in issue 83963
876
// manager.redo();
877
assertTrue(manager.canUndo());
878 // XXX: Once the above redo() is working again, uncomment this code.
879
// assertFalse(manager.canRedo());
880
// Note that the 'xsd:' namespace is not used in this schema.
881
String JavaDoc expected =
882                 " <!-- etc. -->\n" +
883                 " <element name=\"element83963\"/>\n" +
884 // XXX: Once the above redo() is working again, uncomment this code.
885
// "<!-- -->\n" +
886
// "\n" +
887
"</schema>";
888         try {
889             actual = document.getText(0, document.getLength());
890             assertTrue(actual.endsWith(expected));
891         } catch (BadLocationException JavaDoc ble) {
892             fail(ble.toString());
893         }
894         endTestCase();
895
896         cleanup();
897     }
898
899     /**
900      * Simulate switching to the model view.
901      */

902     private void viewModel() {
903         manager.endCompound();
904         document.removeUndoableEditListener(manager);
905         if (model != null) {
906             try {
907                 // Sync any changes made to the document.
908
model.sync();
909             } catch (IOException JavaDoc ioe) {
910                 fail(ioe.toString());
911             }
912             // Ensure manager is not registered twice.
913
model.removeUndoableEditListener(manager);
914             model.addUndoableEditListener(manager);
915         }
916     }
917
918     /**
919      * Simulate switching to the source view.
920      */

921     private void viewSource() {
922         if (model != null) {
923             model.removeUndoableEditListener(manager);
924         }
925         // Ensure manager is not registered twice.
926
document.removeUndoableEditListener(manager);
927         document.addUndoableEditListener(manager);
928         manager.beginCompound();
929     }
930 }
931
Popular Tags