Lines 47-52
Link Here
|
47 |
import javax.swing.undo.UndoManager; |
47 |
import javax.swing.undo.UndoManager; |
48 |
import org.netbeans.junit.NbTestCase; |
48 |
import org.netbeans.junit.NbTestCase; |
49 |
import org.netbeans.lib.editor.util.swing.DocumentUtilities; |
49 |
import org.netbeans.lib.editor.util.swing.DocumentUtilities; |
|
|
50 |
import org.openide.util.Exceptions; |
51 |
import org.openide.util.RequestProcessor; |
50 |
|
52 |
|
51 |
/** |
53 |
/** |
52 |
* Test functionality of BaseDocument. |
54 |
* Test functionality of BaseDocument. |
Lines 58-64
Link Here
|
58 |
public BaseDocumentTest(String testName) { |
60 |
public BaseDocumentTest(String testName) { |
59 |
super(testName); |
61 |
super(testName); |
60 |
} |
62 |
} |
|
|
63 |
|
64 |
public void testRunExclusive() throws Exception { |
65 |
final BaseDocument doc = new BaseDocument(false, "text/plain"); // NOI18N |
66 |
doc.insertString(0, "Nazdar", null); |
67 |
|
68 |
doc.runExclusive(new Runnable() { |
69 |
@Override |
70 |
public void run() { |
71 |
try { |
72 |
doc.getText(0, doc.getLength()); |
73 |
} catch (Exception ex) { |
74 |
fail("Unexpected exception ex=" + ex); |
75 |
} |
76 |
} |
77 |
}); |
78 |
|
79 |
doc.runExclusive(new Runnable() { |
80 |
@Override |
81 |
public void run() { |
82 |
try { |
83 |
doc.insertString(0, "a", null); |
84 |
fail("Exception expected upon insertString()"); |
85 |
} catch (IllegalStateException ex) { |
86 |
// Expected |
87 |
} catch (Exception ex) { |
88 |
fail("Unexpected exception ex=" + ex); |
89 |
} |
90 |
} |
91 |
}); |
92 |
|
93 |
doc.runExclusive(new Runnable() { |
94 |
@Override |
95 |
public void run() { |
96 |
try { |
97 |
doc.runAtomic(new Runnable() { |
98 |
@Override |
99 |
public void run() { |
100 |
fail("Should never run"); |
101 |
} |
102 |
}); |
103 |
fail("Exception expected upon runAtomic()"); |
104 |
} catch (IllegalStateException ex) { |
105 |
// Expected |
106 |
} catch (Exception ex) { |
107 |
fail("Unexpected exception ex=" + ex); |
108 |
} |
109 |
} |
110 |
}); |
111 |
|
112 |
doc.runAtomic(new Runnable() { |
113 |
@Override |
114 |
public void run() { |
115 |
doc.runExclusive(new Runnable() { |
116 |
@Override |
117 |
public void run() { |
118 |
try { |
119 |
doc.getText(0, doc.getLength()); |
120 |
} catch (BadLocationException ex) { |
121 |
fail("Unexpected exception ex=" + ex); |
122 |
} |
123 |
} |
124 |
}); |
125 |
} |
126 |
}); |
127 |
|
128 |
doc.runAtomic(new Runnable() { |
129 |
@Override |
130 |
public void run() { |
131 |
doc.runExclusive(new Runnable() { |
132 |
@Override |
133 |
public void run() { |
134 |
try { |
135 |
doc.insertString(0, "a", null); |
136 |
} catch (IllegalStateException ex) { |
137 |
// Expected |
138 |
} catch (BadLocationException ex) { |
139 |
fail("Unexpected exception ex=" + ex); |
140 |
} |
141 |
} |
142 |
}); |
143 |
} |
144 |
}); |
145 |
|
146 |
// doc.render() in runExclusive() |
147 |
doc.runExclusive(new Runnable() { |
148 |
@Override |
149 |
public void run() { |
150 |
doc.render(new Runnable() { |
151 |
@Override |
152 |
public void run() { |
153 |
try { |
154 |
doc.getText(0, doc.getLength()); |
155 |
} catch (BadLocationException ex) { |
156 |
fail("Unexpected exception ex=" + ex); |
157 |
} |
158 |
} |
159 |
}); |
160 |
} |
161 |
}); |
162 |
|
163 |
// Nested runExclusive() |
164 |
doc.runExclusive(new Runnable() { |
165 |
@Override |
166 |
public void run() { |
167 |
doc.runExclusive(new Runnable() { |
168 |
@Override |
169 |
public void run() { |
170 |
try { |
171 |
doc.getText(0, doc.getLength()); |
172 |
} catch (BadLocationException ex) { |
173 |
fail("Unexpected exception ex=" + ex); |
174 |
} |
175 |
} |
176 |
}); |
177 |
} |
178 |
}); |
179 |
} |
61 |
|
180 |
|
|
|
181 |
|
182 |
public void testRunExclusiveThreading() throws Exception { |
183 |
final BaseDocument doc = new BaseDocument(false, "text/plain"); // NOI18N |
184 |
doc.insertString(0, "Nazdar", null); |
185 |
|
186 |
// Test thread access (runExclusive() and attempt read lock. |
187 |
final boolean t2Started[] = new boolean[1]; |
188 |
final boolean t2DocAccess[] = new boolean[1]; |
189 |
doc.runExclusive(new Runnable() { |
190 |
@Override |
191 |
public void run() { |
192 |
RequestProcessor.getDefault().post(new Runnable() { |
193 |
@Override |
194 |
public void run() { |
195 |
t2Started[0] = true; |
196 |
doc.render(new Runnable() { |
197 |
@Override |
198 |
public void run() { |
199 |
t2DocAccess[0] = true; |
200 |
} |
201 |
}); |
202 |
} |
203 |
}); |
204 |
while (!t2Started[0]) { |
205 |
tSleep(1); |
206 |
} |
207 |
tSleep(5); |
208 |
assertFalse("Read lock access granted when in runExclusive", t2DocAccess[0]); |
209 |
} |
210 |
}); |
211 |
tSleep(2); |
212 |
assertTrue("Read lock access not granted in T2", t2DocAccess[0]); |
213 |
|
214 |
// Reversed test (read lock and attempt runExclusive()). |
215 |
t2Started[0] = false; |
216 |
t2DocAccess[0] = false; |
217 |
doc.render(new Runnable() { |
218 |
@Override |
219 |
public void run() { |
220 |
RequestProcessor.getDefault().post(new Runnable() { |
221 |
@Override |
222 |
public void run() { |
223 |
t2Started[0] = true; |
224 |
doc.runExclusive(new Runnable() { |
225 |
@Override |
226 |
public void run() { |
227 |
t2DocAccess[0] = true; |
228 |
} |
229 |
}); |
230 |
} |
231 |
}); |
232 |
while (!t2Started[0]) { |
233 |
tSleep(1); |
234 |
} |
235 |
tSleep(5); |
236 |
assertFalse("runExclusive doc access granted when in render()", t2DocAccess[0]); |
237 |
} |
238 |
}); |
239 |
tSleep(2); |
240 |
assertTrue("runExclusive() access not granted in T2", t2DocAccess[0]); |
241 |
} |
242 |
|
243 |
private static final void tSleep(long millis) { |
244 |
try { |
245 |
Thread.sleep(millis); |
246 |
} catch (InterruptedException ex) { |
247 |
fail("Failed sleep"); |
248 |
} |
249 |
} |
250 |
|
62 |
public void testBackwardBiasPosition() throws Exception { |
251 |
public void testBackwardBiasPosition() throws Exception { |
63 |
BaseDocument doc = new BaseDocument(false, "text/plain"); // NOI18N |
252 |
BaseDocument doc = new BaseDocument(false, "text/plain"); // NOI18N |
64 |
UndoManager undoManager = new UndoManager(); |
253 |
UndoManager undoManager = new UndoManager(); |