Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
|
15 |
package org.openide.text; |
16 |
|
17 |
|
18 |
import java.io.File; |
19 |
import java.io.IOException; |
20 |
import javax.swing.text.*; |
21 |
import javax.swing.text.BadLocationException; |
22 |
import javax.swing.text.Document; |
23 |
import javax.swing.text.Position; |
24 |
import javax.swing.text.StyledDocument; |
25 |
|
26 |
import junit.textui.TestRunner; |
27 |
|
28 |
import org.netbeans.junit.NbTestCase; |
29 |
import org.netbeans.junit.NbTestSuite; |
30 |
|
31 |
import org.openide.filesystems.FileObject; |
32 |
import org.openide.filesystems.LocalFileSystem; |
33 |
import org.openide.text.CloneableEditorSupport; |
34 |
import org.openide.text.FilterDocument; |
35 |
import org.openide.text.NbDocument; |
36 |
import org.openide.util.RequestProcessor; |
37 |
|
38 |
/** |
39 |
* Emulating old UndoRedo manager deadlock. |
40 |
* |
41 |
* @author Jaroslav Tulach |
42 |
*/ |
43 |
public class UndoRedoCooperationTest extends NbTestCase implements CloneableEditorSupport.Env { |
44 |
/** the support to work with */ |
45 |
private CES support; |
46 |
// Env variables |
47 |
private String content = "Hello"; |
48 |
private boolean valid = true; |
49 |
private boolean modified = false; |
50 |
/** if not null contains message why this document cannot be modified */ |
51 |
private String cannotBeModified; |
52 |
private java.util.Date date = new java.util.Date (); |
53 |
private java.util.List/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList (); |
54 |
private java.beans.VetoableChangeListener vetoL; |
55 |
|
56 |
/** Creates new TextTest */ |
57 |
public UndoRedoCooperationTest (String s) { |
58 |
super(s); |
59 |
} |
60 |
|
61 |
public static void main(String[] args) { |
62 |
TestRunner.run(new NbTestSuite(UndoRedoCooperationTest.class)); |
63 |
} |
64 |
|
65 |
protected void setUp () { |
66 |
support = new CES (this, org.openide.util.Lookup.EMPTY); |
67 |
} |
68 |
|
69 |
|
70 |
public void testDeadlock8692 () throws Exception { |
71 |
doTest (0); |
72 |
} |
73 |
|
74 |
public void testUndoRedo () throws Exception { |
75 |
doTest (1000); |
76 |
} |
77 |
|
78 |
private void doTest (int sleep) throws Exception { |
79 |
final StyledDocument d = support.openDocument (); |
80 |
d.insertString (0, "Ahoj\n", null); |
81 |
|
82 |
cannotBeModified = "My reason"; |
83 |
|
84 |
class R implements Runnable { |
85 |
private Exception ex; |
86 |
|
87 |
public void run () { |
88 |
try { |
89 |
d.remove (0, 2); |
90 |
} catch (BadLocationException ex) { |
91 |
this.ex = ex; |
92 |
} |
93 |
} |
94 |
} |
95 |
|
96 |
R r = new R (); |
97 |
NbDocument.runAtomic (d, r); |
98 |
|
99 |
if (sleep > 0) { |
100 |
Thread.sleep (sleep); |
101 |
} |
102 |
|
103 |
assertEquals ("Text contains orignal version", "Aho", d.getText (0, 3)); |
104 |
} |
105 |
|
106 |
|
107 |
// |
108 |
// Implementation of the CloneableEditorSupport.Env |
109 |
// |
110 |
|
111 |
public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { |
112 |
propL.add (l); |
113 |
} |
114 |
public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { |
115 |
propL.remove (l); |
116 |
} |
117 |
|
118 |
public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { |
119 |
assertNull ("This is the first veto listener", vetoL); |
120 |
vetoL = l; |
121 |
} |
122 |
public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { |
123 |
assertEquals ("Removing the right veto one", vetoL, l); |
124 |
vetoL = null; |
125 |
} |
126 |
|
127 |
public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { |
128 |
return support; |
129 |
} |
130 |
|
131 |
public String getMimeType() { |
132 |
return "text/plain"; |
133 |
} |
134 |
|
135 |
public java.util.Date getTime() { |
136 |
return date; |
137 |
} |
138 |
|
139 |
public java.io.InputStream inputStream() throws java.io.IOException { |
140 |
return new java.io.ByteArrayInputStream (content.getBytes ()); |
141 |
} |
142 |
public java.io.OutputStream outputStream() throws java.io.IOException { |
143 |
class ContentStream extends java.io.ByteArrayOutputStream { |
144 |
public void close () throws java.io.IOException { |
145 |
super.close (); |
146 |
content = new String (toByteArray ()); |
147 |
} |
148 |
} |
149 |
|
150 |
return new ContentStream (); |
151 |
} |
152 |
|
153 |
public boolean isValid() { |
154 |
return valid; |
155 |
} |
156 |
|
157 |
public boolean isModified() { |
158 |
return modified; |
159 |
} |
160 |
|
161 |
public void markModified() throws java.io.IOException { |
162 |
if (cannotBeModified != null) { |
163 |
IOException e = new IOException (); |
164 |
org.openide.ErrorManager.getDefault ().annotate (e, cannotBeModified); |
165 |
throw e; |
166 |
} |
167 |
|
168 |
modified = true; |
169 |
} |
170 |
|
171 |
public void unmarkModified() { |
172 |
modified = false; |
173 |
} |
174 |
|
175 |
/** Implementation of the CES */ |
176 |
private final class CES extends CloneableEditorSupport { |
177 |
|
178 |
public CES (Env env, org.openide.util.Lookup l) { |
179 |
super (env, l); |
180 |
} |
181 |
|
182 |
protected String messageName() { |
183 |
return "Name"; |
184 |
} |
185 |
|
186 |
protected String messageOpened() { |
187 |
return "Opened"; |
188 |
} |
189 |
|
190 |
protected String messageOpening() { |
191 |
return "Opening"; |
192 |
} |
193 |
|
194 |
protected String messageSave() { |
195 |
return "Save"; |
196 |
} |
197 |
|
198 |
protected String messageToolTip() { |
199 |
return "ToolTip"; |
200 |
} |
201 |
|
202 |
} // end of CES |
203 |
} |