KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.textui.TestRunner;
28 import org.netbeans.junit.*;
29 import org.openide.util.Exceptions;
30
31 /**
32  * Simulates issue 46981. Editor locks the document, but somebody else closes it
33  * while it is working on it and a deadlock occurs.
34  * @author Petr Nejedly, Jaroslav Tulach
35  */

36 public class DocumentCannotBeClosedWhenReadLockedTest 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     
50     /** lock to use for communication between AWT & main thread */
51     private Object JavaDoc LOCK = new Object JavaDoc ();
52     
53     /** Creates new TextTest */
54     public DocumentCannotBeClosedWhenReadLockedTest(String JavaDoc s) {
55         super(s);
56     }
57     
58     public static void main(String JavaDoc[] args) {
59         TestRunner.run(new NbTestSuite(DocumentCannotBeClosedWhenReadLockedTest.class));
60     }
61
62     protected void setUp () {
63         support = new CES (this, org.openide.util.Lookup.EMPTY);
64     }
65     
66     public void testReadLockTheDocumentAndThenTryToCreateAPositionInItMeanWhileLetOtherThreadCloseAComponent () throws Exception JavaDoc {
67         StyledDocument doc = support.openDocument ();
68         final CloneableEditorSupport.Pane pane = support.openAt (support.createPositionRef (0, javax.swing.text.Position.Bias.Forward), 0);
69         assertNotNull (pane);
70         assertNotNull ("TopComponent is there", pane.getComponent ());
71         
72         class DoWork implements Runnable JavaDoc {
73             private boolean startedAWT;
74             private boolean finishedAWT;
75             private boolean startedWork;
76             private boolean finishedWork;
77             
78             public void run () {
79                 if (javax.swing.SwingUtilities.isEventDispatchThread ()) {
80                     doWorkInAWT ();
81                 } else {
82                     doWork ();
83                 }
84             }
85              
86             private void doWorkInAWT () {
87                 startedAWT = true;
88                 synchronized (LOCK) {
89                     // let the main thread know that it can do the rendering
90
LOCK.notify();
91                 }
92                 
93                 try {
94                     Thread.sleep (500);
95                 } catch (InterruptedException JavaDoc ex) {
96                     fail (ex.getMessage ());
97                 }
98                 
99                 // this will call into notifyUnmodified.
100
pane.getComponent ().close ();
101                 assertFalse ("The document should be marked unmodified now", modified);
102                 synchronized (this) {
103                     finishedAWT = true;
104                     notifyAll ();
105                 }
106             }
107             
108             private void doWork () {
109                 startedWork = true;
110                 synchronized (LOCK) {
111                     try {
112                         LOCK.wait ();
113                     } catch (InterruptedException JavaDoc ex) {
114                         throw new org.netbeans.junit.AssertionFailedErrorException (ex);
115                     }
116                 }
117                 
118                 // now the document is blocked in after close, try to ask for a position
119
support.createPositionRef (0, javax.swing.text.Position.Bias.Forward);
120                 finishedWork = true;
121             }
122             
123             public synchronized void waitFinishedAWT () throws InterruptedException JavaDoc {
124                 int cnt = 5;
125                 while (!finishedAWT && cnt-- > 0) {
126                     wait (500);
127                 }
128                 
129                 if (!finishedAWT) {
130                     fail ("AWT has not finsihed");
131                 }
132             }
133             
134         }
135         DoWork doWork = new DoWork ();
136         
137         
138         synchronized (LOCK) {
139             javax.swing.SwingUtilities.invokeLater (doWork);
140             LOCK.wait ();
141         }
142             
143         
144         doc.render (doWork);
145         
146         // wait for AWT work to finish
147
javax.swing.SwingUtilities.invokeAndWait (new Runnable JavaDoc () { public void run () {}});
148         
149         assertTrue ("AWT started", doWork.startedAWT);
150         assertTrue ("Work started", doWork.startedWork);
151
152         doWork.waitFinishedAWT ();
153         assertTrue ("Work done", doWork.finishedWork);
154     }
155
156     public void testReadLockTheDocumentAndThenTryToCreateAPositionInItMeanWhileLetOtherThreadCloseIt () throws Exception JavaDoc {
157         class DoWork implements Runnable JavaDoc {
158             private boolean finishedAWT;
159             private boolean finishedWork;
160             
161             public void run () {
162                 if (javax.swing.SwingUtilities.isEventDispatchThread ()) {
163                     doWorkInAWT ();
164                 } else {
165                     doWork ();
166                 }
167             }
168              
169             private void doWorkInAWT () {
170                 synchronized (LOCK) {
171                     // let the main thread know that it can do the rendering
172
LOCK.notify();
173                 }
174                 
175                 try {
176                     Thread.sleep (500);
177                 } catch (InterruptedException JavaDoc ex) {
178                     fail (ex.getMessage ());
179                 }
180                 
181                 // this will call into notifyUnmodified.
182
support.close ();
183                 assertFalse ("The document should be marked unmodified now", modified);
184                 synchronized (this) {
185                     finishedAWT = true;
186                     notifyAll ();
187                 }
188             }
189             
190             private void doWork () {
191                 synchronized (LOCK) {
192                     try {
193                         LOCK.wait ();
194                     } catch (InterruptedException JavaDoc ex) {
195                         throw new org.netbeans.junit.AssertionFailedErrorException (ex);
196                     }
197                 }
198                 
199                 // now the document is blocked in after close, try to ask for a position
200
support.createPositionRef (0, javax.swing.text.Position.Bias.Forward);
201                 finishedWork = true;
202             }
203             
204             public synchronized void waitFinishedAWT () throws InterruptedException JavaDoc {
205                 int cnt = 5;
206                 while (!finishedAWT && cnt-- > 0) {
207                     wait (500);
208                 }
209                 
210                 if (!finishedAWT) {
211                     fail ("AWT has not finsihed");
212                 }
213             }
214         }
215         DoWork doWork = new DoWork ();
216         StyledDocument doc = support.openDocument ();
217         
218         synchronized (LOCK) {
219             javax.swing.SwingUtilities.invokeLater (doWork);
220             LOCK.wait ();
221         }
222             
223         
224         doc.render (doWork);
225         
226         // maybe this needs to invokeAndWait something empty in AWT?
227
doWork.waitFinishedAWT ();
228         assertTrue ("Work done", doWork.finishedWork);
229     }
230     
231     
232     
233     
234     //
235
// Implementation of the CloneableEditorSupport.Env
236
//
237

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