KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > UndoRedoCooperationTest


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
21 package org.openide.text;
22
23
24
25 import java.io.IOException JavaDoc;
26 import javax.swing.text.*;
27 import org.netbeans.junit.*;
28 import org.openide.util.Exceptions;
29 import org.openide.util.RequestProcessor;
30
31 /**
32  * Emulating old UndoRedo manager deadlock.
33  *
34  * @author Jaroslav Tulach
35  */

36 public class UndoRedoCooperationTest extends NbTestCase implements CloneableEditorSupport.Env {
37     /** the support to work with */
38     private CES support;
39     // Env variables
40
private String JavaDoc content = "Hello";
41     private boolean valid = true;
42     private boolean modified = false;
43     /** if not null contains message why this document cannot be modified */
44     private String JavaDoc cannotBeModified;
45     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
46     private java.util.List JavaDoc/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList JavaDoc ();
47     private java.beans.VetoableChangeListener JavaDoc vetoL;
48     
49     /** Creates new TextTest */
50     public UndoRedoCooperationTest (String JavaDoc s) {
51         super(s);
52     }
53     
54     protected javax.swing.text.EditorKit JavaDoc createEditorKit() {
55         return new NbLikeEditorKit();
56     }
57
58     protected void setUp () {
59         support = new CES (this, org.openide.util.Lookup.EMPTY);
60     }
61     
62     public void testOneThreadDoingEditsOneThreadDoingReverts () throws Exception JavaDoc {
63         final StyledDocument d = support.openDocument ();
64         d.insertString (0, "Ahoj\n", null);
65         assertTrue ("We can do undo now", support.getUndoRedo ().canUndo ());
66         
67         class Blocker implements javax.swing.event.DocumentListener JavaDoc {
68             public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
69                 synchronized (UndoRedoCooperationTest.this) {
70                     UndoRedoCooperationTest.this.notify ();
71                     try {
72                         UndoRedoCooperationTest.this.wait ();
73                     } catch (InterruptedException JavaDoc ex) {
74                         ex.printStackTrace();
75                     }
76                 }
77             }
78
79             public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
80             }
81
82             public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
83             }
84         }
85         d.addDocumentListener (new Blocker ());
86         
87         
88         class Run implements Runnable JavaDoc {
89             public void run () {
90                 try {
91                     d.insertString (2, "Kuk", null);
92                 } catch (BadLocationException ex) {
93                     ex.printStackTrace();
94                 }
95             }
96         }
97         class Undo implements Runnable JavaDoc {
98             public void run () {
99                 support.getUndoRedo ().undo ();
100                 support.getUndoRedo ().undo ();
101                 assertFalse (support.getUndoRedo ().canUndo ());
102                 assertTrue (support.getUndoRedo ().canRedo ());
103             }
104         }
105         
106         RequestProcessor.Task t1, t2;
107         synchronized (this) {
108             t1 = new RequestProcessor ("Inserting into document").post (new Run ());
109             wait ();
110             // now the inserting thread is blocked in EditThatCanBlockInAddEditMethod
111
t2 = new RequestProcessor ("Doing undo").post (new Undo ());
112             
113             // wait a while till one of the undos is called
114
Thread.sleep (100);
115             // let the insert into document continue
116
notify ();
117         }
118         
119         // there should be a deadlock
120
t1.waitFinished ();
121         t2.waitFinished ();
122     }
123     
124     public void testDeadlock8692 () throws Exception JavaDoc {
125         doTest (0);
126     }
127     
128     public void testUndoRedo () throws Exception JavaDoc {
129         doTest (1000);
130     }
131     
132     private void doTest (int sleep) throws Exception JavaDoc {
133         final StyledDocument d = support.openDocument ();
134         d.insertString (0, "Ahoj\n", null);
135         support.saveDocument ();
136         assertFalse ("Previous save make it non-modified", support.isModified ());
137         
138         cannotBeModified = "My reason";
139         
140         class R implements Runnable JavaDoc {
141             private Exception JavaDoc ex;
142             
143             public void run () {
144                 try {
145                     d.remove (0, 2);
146                 } catch (BadLocationException ex) {
147                     this.ex = ex;
148                 }
149             }
150         }
151
152         R r = new R ();
153         NbDocument.runAtomic (d, r);
154
155         if (sleep > 0) {
156             Thread.sleep (sleep);
157         }
158         
159         //
160
// anyway we need to wait till all posted AWT tasks are finished
161
javax.swing.SwingUtilities.invokeAndWait (new Runnable JavaDoc () {
162             public void run () {
163                 // just wait
164
}
165         });
166         
167         assertEquals ("Text contains orignal version", "Aho", d.getText (0, 3));
168     }
169     
170     public void testUndoMustBePossibleWithPlainDocument () throws Exception JavaDoc {
171         support.plain = true;
172         
173         final StyledDocument d = support.openDocument ();
174
175         assertTrue ("Document is not empty", d.getLength () > 0);
176         
177         d.remove (0, d.getLength ());
178         String JavaDoc s = d.getText (0, d.getLength ());
179         assertEquals ("The document is empty", "", s);
180         
181         assertTrue ("There is something to undo", support.getUndoRedo ().canUndo ());
182         support.getUndoRedo ().undo ();
183
184         s = d.getText (0, d.getLength ());
185         assertEquals ("Contains the original content", content, s);
186     }
187     
188     public void testClearTheMap() throws Exception JavaDoc {
189         support.plain = false;
190         
191         final StyledDocument d = support.openDocument ();
192         
193         support.getUndoRedo().discardAllEdits();
194
195         assertTrue ("Document is not empty", d.getLength () > 0);
196         
197         cannotBeModified = "Cannot";
198         try {
199             d.remove (0, d.getLength ());
200         } catch (BadLocationException be) { /* expected */ }
201         
202         String JavaDoc s = d.getText (0, d.getLength ());
203         assertEquals ("The document is the same", "Hello", s);
204     }
205     
206     public void testEmptyRunAtomic() throws Exception JavaDoc {
207         content = "";
208         final StyledDocument d = support.openDocument ();
209         d.insertString(0, "a", null);
210         assertTrue(support.isModified());
211         // Run empty runnable which should call notifyModify() followed by
212
// notifyUnmodified()
213
NbDocument.runAtomic(d, new Runnable JavaDoc() {
214             public void run() {
215                 // Do nothing
216
}
217         });
218         assertTrue("Empty runAtomic() must not reset the modified flag", support.isModified());
219     }
220
221     public void testCanUndoDoesNotMarkDocumentUnmodified() throws Exception JavaDoc {
222         content = "";
223         final StyledDocument d = support.openDocument ();
224         d.insertString(0, "a", null);
225         assertTrue(support.isModified());
226         assertTrue(support.getUndoRedo().canUndo());
227         assertTrue("canUndo() must not reset the modified flag", support.isModified());
228     }
229
230     
231     //
232
// Implementation of the CloneableEditorSupport.Env
233
//
234

235     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
236         propL.add (l);
237     }
238     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
239         propL.remove (l);
240     }
241     
242     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
243         assertNull ("This is the first veto listener", vetoL);
244         vetoL = l;
245     }
246     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
247         assertEquals ("Removing the right veto one", vetoL, l);
248         vetoL = null;
249     }
250     
251     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
252         return support;
253     }
254     
255     public String JavaDoc getMimeType() {
256         return "text/plain";
257     }
258     
259     public java.util.Date JavaDoc getTime() {
260         return date;
261     }
262     
263     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
264         return new java.io.ByteArrayInputStream JavaDoc (content.getBytes ());
265     }
266     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
267         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
268             public void close () throws java.io.IOException JavaDoc {
269                 super.close ();
270                 content = new String JavaDoc (toByteArray ());
271             }
272         }
273         
274         return new ContentStream ();
275     }
276     
277     public boolean isValid() {
278         return valid;
279     }
280     
281     public boolean isModified() {
282         return modified;
283     }
284
285     public void markModified() throws java.io.IOException JavaDoc {
286         if (cannotBeModified != null) {
287             IOException JavaDoc e = new IOException JavaDoc ();
288             Exceptions.attachLocalizedMessage(e, cannotBeModified);
289             throw e;
290         }
291         
292         modified = true;
293     }
294     
295     public void unmarkModified() {
296         modified = false;
297     }
298
299     /** Implementation of the CES */
300     private final class CES extends CloneableEditorSupport {
301         public boolean plain;
302         
303         
304         public CES (Env env, org.openide.util.Lookup l) {
305             super (env, l);
306         }
307         
308         protected String JavaDoc messageName() {
309             return "Name";
310         }
311         
312         protected String JavaDoc messageOpened() {
313             return "Opened";
314         }
315         
316         protected String JavaDoc messageOpening() {
317             return "Opening";
318         }
319         
320         protected String JavaDoc messageSave() {
321             return "Save";
322         }
323         
324         protected String JavaDoc messageToolTip() {
325             return "ToolTip";
326         }
327
328         protected javax.swing.text.EditorKit JavaDoc createEditorKit() {
329             if (plain) {
330                 return super.createEditorKit ();
331             } else {
332                 return UndoRedoCooperationTest.this.createEditorKit ();
333             }
334         }
335     } // end of CES
336

337 }
338
Popular Tags