Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
|
45 |
package org.openide.text; |
46 |
|
47 |
import java.beans.PropertyChangeListener; |
48 |
import java.beans.PropertyChangeSupport; |
49 |
import java.beans.VetoableChangeListener; |
50 |
import java.io.ByteArrayInputStream; |
51 |
import java.io.ByteArrayOutputStream; |
52 |
import java.io.IOException; |
53 |
import java.io.InputStream; |
54 |
import java.io.OutputStream; |
55 |
import java.io.Serializable; |
56 |
import java.util.Date; |
57 |
import javax.swing.event.UndoableEditEvent; |
58 |
import javax.swing.text.AttributeSet; |
59 |
import javax.swing.text.BadLocationException; |
60 |
import javax.swing.text.Document; |
61 |
import javax.swing.text.EditorKit; |
62 |
import javax.swing.text.Position; |
63 |
import javax.swing.text.StyledDocument; |
64 |
import javax.swing.undo.AbstractUndoableEdit; |
65 |
import javax.swing.undo.CannotRedoException; |
66 |
import javax.swing.undo.CannotUndoException; |
67 |
import javax.swing.undo.CompoundEdit; |
68 |
import javax.swing.undo.UndoableEdit; |
69 |
import org.netbeans.junit.NbTestCase; |
70 |
import org.openide.awt.UndoRedo; |
71 |
import org.openide.cookies.EditorCookie; |
72 |
import org.openide.text.CloneableEditorSupport.Env; |
73 |
import org.openide.util.Exceptions; |
74 |
import org.openide.util.Lookup; |
75 |
import org.openide.util.RequestProcessor; |
76 |
import org.openide.util.RequestProcessor.Task; |
77 |
import org.openide.windows.CloneableOpenSupport; |
78 |
import org.openide.windows.CloneableTopComponent; |
79 |
|
80 |
/** |
81 |
* Deadlock of a thread simulating reloading of document and another thread trying to close the file. |
82 |
* |
83 |
* @author Miloslav Metelka |
84 |
*/ |
85 |
public class EditToBeUndoneRedoneTest extends NbTestCase |
86 |
implements CloneableEditorSupport.Env { |
87 |
static { |
88 |
System.setProperty("org.openide.windows.DummyWindowManager.VISIBLE", "false"); |
89 |
} |
90 |
/** the support to work with */ |
91 |
private transient CES support; |
92 |
|
93 |
// Env variables |
94 |
private transient String content = ""; |
95 |
private transient boolean valid = true; |
96 |
private transient boolean modified = false; |
97 |
/** if not null contains message why this document cannot be modified */ |
98 |
private transient String cannotBeModified; |
99 |
private transient Date date = new Date (); |
100 |
private transient final PropertyChangeSupport pcl; |
101 |
private transient VetoableChangeListener vetoL; |
102 |
|
103 |
private transient volatile boolean inReloadBeforeSupportLock; |
104 |
private transient volatile boolean closing; |
105 |
|
106 |
private static EditToBeUndoneRedoneTest RUNNING; |
107 |
|
108 |
public EditToBeUndoneRedoneTest(String s) { |
109 |
super(s); |
110 |
pcl = new PropertyChangeSupport(this); |
111 |
} |
112 |
|
113 |
protected void setUp () { |
114 |
support = new CES (this, Lookup.EMPTY); |
115 |
RUNNING = this; |
116 |
} |
117 |
|
118 |
protected boolean runInEQ() { |
119 |
return false; |
120 |
} |
121 |
|
122 |
@Override |
123 |
protected int timeOut() { |
124 |
return 15000; |
125 |
} |
126 |
|
127 |
private Object writeReplace () { |
128 |
return new Replace (); |
129 |
} |
130 |
|
131 |
public void testUndoRedoEdits() throws Exception { |
132 |
Document doc = support.openDocument(); |
133 |
doc.insertString(0, "a", null); |
134 |
UndoRedo.Manager ur = support.getUndoRedo(); |
135 |
MyEdit myEdit = NbDocument.getEditToBeUndoneOfType(support, MyEdit.class); |
136 |
assertNotNull("Expected valid myEdit", myEdit); |
137 |
ur.undo(); |
138 |
myEdit = NbDocument.getEditToBeRedoneOfType(support, MyEdit.class); |
139 |
assertNotNull("Expected valid myEdit", myEdit); |
140 |
} |
141 |
|
142 |
// |
143 |
// Implementation of the CloneableEditorSupport.Env |
144 |
// |
145 |
|
146 |
public synchronized void addPropertyChangeListener(PropertyChangeListener l) { |
147 |
pcl.addPropertyChangeListener(l); |
148 |
} |
149 |
public synchronized void removePropertyChangeListener(PropertyChangeListener l) { |
150 |
pcl.removePropertyChangeListener(l); |
151 |
} |
152 |
|
153 |
public synchronized void addVetoableChangeListener(VetoableChangeListener l) { |
154 |
assertNull ("This is the first veto listener", vetoL); |
155 |
vetoL = l; |
156 |
} |
157 |
public void removeVetoableChangeListener(VetoableChangeListener l) { |
158 |
assertEquals ("Removing the right veto one", vetoL, l); |
159 |
vetoL = null; |
160 |
} |
161 |
|
162 |
public CloneableOpenSupport findCloneableOpenSupport() { |
163 |
return RUNNING.support; |
164 |
} |
165 |
|
166 |
public String getMimeType() { |
167 |
return "text/plain"; |
168 |
} |
169 |
|
170 |
public Date getTime() { |
171 |
return date; |
172 |
} |
173 |
|
174 |
public InputStream inputStream() throws IOException { |
175 |
return new ByteArrayInputStream (content.getBytes ()); |
176 |
} |
177 |
public OutputStream outputStream() throws IOException { |
178 |
class ContentStream extends ByteArrayOutputStream { |
179 |
public void close () throws IOException { |
180 |
super.close (); |
181 |
content = new String (toByteArray ()); |
182 |
} |
183 |
} |
184 |
|
185 |
return new ContentStream (); |
186 |
} |
187 |
|
188 |
public boolean isValid() { |
189 |
return valid; |
190 |
} |
191 |
|
192 |
public boolean isModified() { |
193 |
return modified; |
194 |
} |
195 |
|
196 |
public void markModified() throws IOException { |
197 |
if (cannotBeModified != null) { |
198 |
final String notify = cannotBeModified; |
199 |
IOException e = new IOException () { |
200 |
public String getLocalizedMessage () { |
201 |
return notify; |
202 |
} |
203 |
}; |
204 |
Exceptions.attachLocalizedMessage(e, cannotBeModified); |
205 |
throw e; |
206 |
} |
207 |
|
208 |
modified = true; |
209 |
} |
210 |
|
211 |
public void unmarkModified() { |
212 |
modified = false; |
213 |
} |
214 |
|
215 |
/** Implementation of the CES */ |
216 |
private static final class CES extends CloneableEditorSupport implements EditorCookie { |
217 |
public CES (Env env, Lookup l) { |
218 |
super (env, l); |
219 |
} |
220 |
|
221 |
@Override |
222 |
protected EditorKit createEditorKit () { |
223 |
// Important to use NbLikeEditorKit since otherwise FilterDocument |
224 |
// would be created with improper runAtomic() |
225 |
return new MyKit (); |
226 |
} |
227 |
public CloneableTopComponent.Ref getRef () { |
228 |
return allEditors; |
229 |
} |
230 |
|
231 |
protected String messageName() { |
232 |
return "Name"; |
233 |
} |
234 |
|
235 |
protected String messageOpened() { |
236 |
return "Opened"; |
237 |
} |
238 |
|
239 |
protected String messageOpening() { |
240 |
return "Opening"; |
241 |
} |
242 |
|
243 |
protected String messageSave() { |
244 |
return "Save"; |
245 |
} |
246 |
|
247 |
protected String messageToolTip() { |
248 |
return "ToolTip"; |
249 |
} |
250 |
|
251 |
} |
252 |
|
253 |
private static final class Replace implements Serializable { |
254 |
public Object readResolve () { |
255 |
return RUNNING; |
256 |
} |
257 |
} |
258 |
|
259 |
private static final class MyEdit extends CompoundEdit { // Marker custom undo edit |
260 |
|
261 |
} |
262 |
|
263 |
private static final class MyKit extends NbLikeEditorKit { |
264 |
|
265 |
@Override |
266 |
public Document createDefaultDocument() { |
267 |
return new Doc() { |
268 |
|
269 |
@Override |
270 |
protected void fireUndoableEditUpdate(UndoableEditEvent e) { |
271 |
UndoableEdit edit = e.getEdit(); |
272 |
MyEdit wrapEdit = new MyEdit(); |
273 |
wrapEdit.addEdit(edit); |
274 |
wrapEdit.end(); |
275 |
e = new UndoableEditEvent(e.getSource(), wrapEdit); |
276 |
|
277 |
super.fireUndoableEditUpdate(e); |
278 |
} |
279 |
|
280 |
}; |
281 |
} |
282 |
|
283 |
|
284 |
} |
285 |
} |