KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.*;
25 import javax.swing.text.StyledDocument JavaDoc;
26
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29
30 import org.openide.awt.UndoRedo;
31 import org.openide.util.RequestProcessor;
32
33
34 /** Checks that the default impl of Documents UndoRedo really locks
35  * the document first on all of its methods.
36  *
37  * @author Jarda Tulach
38  */

39 public class UndoRedoTest extends NbTestCase implements CloneableEditorSupport.Env {
40     /** the method of manager that we are testing */
41     private Method method;
42     
43     private CES support;
44     // Env variables
45
private String JavaDoc content = "";
46     private boolean valid = true;
47     private boolean modified = false;
48     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
49     private java.util.List JavaDoc/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList JavaDoc ();
50     private java.beans.VetoableChangeListener JavaDoc vetoL;
51     
52     
53     /** Creates new UndoRedoTest */
54     public UndoRedoTest(Method m) {
55         super(m.getName ());
56         method = m;
57     }
58   
59     public static junit.framework.TestSuite suite () throws Exception JavaDoc {
60         Method[] arr = UndoRedo.Manager.class.getMethods ();
61         
62         NbTestSuite suite = new NbTestSuite ();
63         for (int i = 0; i < arr.length; i++) {
64             if (arr[i].getDeclaringClass () == javax.swing.undo.UndoManager JavaDoc.class) {
65                 suite.addTest (new UndoRedoTest (arr[i]));
66             }
67         }
68         return suite;
69     }
70     
71     protected void runTest () throws Exception JavaDoc {
72         support = new CES (this, org.openide.util.Lookup.EMPTY);
73
74         assertNull ("The document is closed", support.getDocument ());
75         
76         UndoRedo ur = support.getUndoRedo ();
77         
78         Object JavaDoc[] args = new Object JavaDoc[0];
79         
80         if (method.getParameterTypes ().length == 1) {
81             if (method.getParameterTypes ()[0] == javax.swing.undo.UndoableEdit JavaDoc.class) {
82                 args = new Object JavaDoc[] { new FakeEdit () };
83             }
84             if (method.getParameterTypes ()[0] == Integer.TYPE) {
85                 args = new Object JavaDoc[] { new Integer JavaDoc (30) };
86             }
87         }
88         
89         if (! "end".equals (method.getName ())) {
90             try {
91                 // invoking of the method work
92
method.invoke (ur, args);
93             } catch (InvocationTargetException ex) {
94                 if (
95                     ex.getTargetException () instanceof javax.swing.undo.CannotUndoException JavaDoc ||
96                     ex.getTargetException () instanceof javax.swing.undo.CannotRedoException JavaDoc
97                 ) {
98                     // ok
99
} else {
100                     throw ex;
101                 }
102             }
103         }
104         
105         
106         final StyledDocument JavaDoc doc = support.openDocument ();
107         doc.insertString (0, "First edit\n", null);
108         doc.insertString (1, "Second", null);
109         doc.remove (0, 5);
110
111         // this might improve the situation for #47022
112
javax.swing.SwingUtilities.invokeAndWait (new Runnable JavaDoc () {
113             public void run () {
114                 // just wait
115
}
116         });
117
118         assertTrue ("We did edits, we need to be able to do undo", ur.canUndo ());
119         
120         ur.undo ();
121         
122         assertTrue ("There is something to undo", ur.canUndo ());
123         assertTrue ("There is something to redo", ur.canRedo ());
124
125         
126         class Blocker implements javax.swing.event.DocumentListener JavaDoc, Runnable JavaDoc {
127             public long time;
128             
129             public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
130                 synchronized (this) {
131                     this.notify ();
132                     try {
133                         long time = System.currentTimeMillis ();
134                         this.wait (100);
135                         time = System.currentTimeMillis () - time;
136                     } catch (InterruptedException JavaDoc ex) {
137                         ex.printStackTrace();
138                     }
139                 }
140             }
141
142             public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
143             }
144
145             public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
146             }
147             
148             public void run () {
149                 // the thread that modifies the document to block it
150
try {
151                     doc.insertString (0, "Kuk", null);
152                 } catch (javax.swing.text.BadLocationException JavaDoc ex) {
153                     ex.printStackTrace();
154                 }
155             }
156         }
157         Blocker blocker = new Blocker ();
158         doc.addDocumentListener (blocker);
159         synchronized (blocker) {
160             RequestProcessor.getDefault ().post (blocker);
161             blocker.wait ();
162         }
163     
164         try {
165             method.invoke (ur, args);
166         } catch (InvocationTargetException ex) {
167             if (
168                 ex.getTargetException () instanceof javax.swing.undo.CannotRedoException JavaDoc
169             ) {
170                 // well, sometimes redo is not possible, so skip it
171
} else {
172                 throw ex;
173             }
174         }
175         
176         synchronized (blocker) {
177             blocker.notify ();
178         }
179         if (blocker.time > 50) {
180             fail ("The method " + method + " should finish sooner than in " + blocker.time + " ms");
181         }
182     }
183     
184     
185     //
186
// Implementation of the CloneableEditorSupport.Env
187
//
188

189     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
190         propL.add (l);
191     }
192     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
193         propL.remove (l);
194     }
195     
196     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
197         assertNull ("This is the first veto listener", vetoL);
198         vetoL = l;
199     }
200     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
201         assertEquals ("Removing the right veto one", vetoL, l);
202         vetoL = null;
203     }
204     
205     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
206         return support;
207     }
208     
209     public String JavaDoc getMimeType() {
210         return "text/plain";
211     }
212     
213     public java.util.Date JavaDoc getTime() {
214         return date;
215     }
216     
217     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
218         return new java.io.ByteArrayInputStream JavaDoc (content.getBytes ());
219     }
220     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
221         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
222             public void close () throws java.io.IOException JavaDoc {
223                 super.close ();
224                 content = new String JavaDoc (toByteArray ());
225             }
226         }
227         
228         return new ContentStream ();
229     }
230     
231     public boolean isValid() {
232         return valid;
233     }
234     
235     public boolean isModified() {
236         return modified;
237     }
238
239     public void markModified() throws java.io.IOException JavaDoc {
240         modified = true;
241     }
242     
243     public void unmarkModified() {
244         modified = false;
245     }
246
247     /** Implementation of the CES */
248     private final class CES extends CloneableEditorSupport {
249         public boolean plain;
250         
251         
252         public CES (Env env, org.openide.util.Lookup l) {
253             super (env, l);
254         }
255         
256         protected String JavaDoc messageName() {
257             return "Name";
258         }
259         
260         protected String JavaDoc messageOpened() {
261             return "Opened";
262         }
263         
264         protected String JavaDoc messageOpening() {
265             return "Opening";
266         }
267         
268         protected String JavaDoc messageSave() {
269             return "Save";
270         }
271         
272         protected String JavaDoc messageToolTip() {
273             return "ToolTip";
274         }
275
276         protected javax.swing.text.EditorKit JavaDoc createEditorKit() {
277             if (plain) {
278                 return super.createEditorKit ();
279             } else {
280                 return new NbLikeEditorKit ();
281             }
282         }
283     } // end of CES
284

285     private static final class FakeEdit implements javax.swing.undo.UndoableEdit JavaDoc {
286         public boolean addEdit(javax.swing.undo.UndoableEdit JavaDoc anEdit) {
287             return false;
288         }
289
290         public boolean canRedo() {
291             return true;
292         }
293
294         public boolean canUndo() {
295             return true;
296         }
297
298         public void die() {
299         }
300
301         public java.lang.String JavaDoc getPresentationName() {
302             return "";
303         }
304
305         public java.lang.String JavaDoc getRedoPresentationName() {
306             return "";
307         }
308
309         public java.lang.String JavaDoc getUndoPresentationName() {
310             return "";
311         }
312
313         public boolean isSignificant() {
314             return false;
315         }
316
317         public void redo() throws javax.swing.undo.CannotRedoException JavaDoc {
318         }
319
320         public boolean replaceEdit(javax.swing.undo.UndoableEdit JavaDoc anEdit) {
321             return true;
322         }
323
324         public void undo() throws javax.swing.undo.CannotUndoException JavaDoc {
325         }
326         
327     } // end of UndoableEdit
328
}
329
Popular Tags