KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > DocumentUndoTest


1
2 package org.netbeans.modules.editor;
3
4 import javax.swing.undo.UndoManager JavaDoc;
5 import org.netbeans.editor.BaseDocument;
6
7 /**
8  * Test the annotations attached to the editor.
9  *
10  * @author Miloslav Metelka
11  */

12 public class DocumentUndoTest extends BaseDocumentUnitTestCase {
13     
14     private UndoManager JavaDoc undoManager;
15     
16     public DocumentUndoTest(String JavaDoc testMethodName) {
17         super(testMethodName);
18         
19     }
20     
21     protected void setUp() throws Exception JavaDoc {
22         super.setUp();
23         
24         undoManager = new UndoManager JavaDoc();
25         getDocument().addUndoableEditListener(undoManager);
26     }
27
28     public void testUndoWordAtOnce() throws Exception JavaDoc {
29         insertByAtomicChars(0, "abc");
30         undoManager.undo();
31         assertDocumentText("Expected empty document", "");
32     }
33     
34     public void testUndoSecondWordFromTwo() throws Exception JavaDoc {
35         insertByAtomicChars(0, "abc def");
36         undoManager.undo();
37         assertDocumentText("Expected second word undone", "abc ");
38     }
39     
40     public void testUndoAtomicThenNonAtomic() throws Exception JavaDoc {
41         insertByAtomicChars(0, "a b");
42         getDocument().insertString(3, "c", null);
43         undoManager.undo();
44         assertDocumentText("Expected second word undone", "a ");
45     }
46     
47     public void testUndoNonAtomicThenAtomic() throws Exception JavaDoc {
48         getDocument().insertString(0, "a", null);
49         insertByAtomicChars(1, "bc");
50         undoManager.undo();
51         assertDocumentText("Expected empty document", "");
52     }
53     
54     public void testUndoTwoNonAtomic() throws Exception JavaDoc {
55         getDocument().insertString(0, "a", null);
56         getDocument().insertString(1, "b", null);
57         undoManager.undo();
58         assertDocumentText("Expected empty document", "");
59     }
60     
61     private void insertByAtomicChars(int offset, String JavaDoc text) throws Exception JavaDoc {
62         BaseDocument doc = getDocument();
63         for (int i = 0; i < text.length(); i++) {
64             doc.atomicLock();
65             try {
66                 doc.insertString(offset + i, text.substring(i, i + 1), null);
67             } finally {
68                 doc.atomicUnlock();
69             }
70         }
71     }
72
73 }
74
Popular Tags