KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Date JavaDoc;
24 import java.beans.*;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import javax.swing.text.*;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import javax.swing.text.Document JavaDoc;
32 import javax.swing.text.Position JavaDoc;
33 import javax.swing.text.StyledDocument JavaDoc;
34
35 import junit.textui.TestRunner;
36
37 import org.netbeans.junit.NbTestCase;
38 import org.netbeans.junit.NbTestSuite;
39
40 import org.openide.text.CloneableEditorSupport;
41 import org.openide.text.FilterDocument;
42 import org.openide.text.NbDocument;
43 import org.openide.util.RequestProcessor;
44
45 /**
46  * Tries closing a document while holding a read lock and adding
47  * a position reference from other thread.
48  *
49  * This test fails if switched to writeLock instead of readLock
50  *
51  * @author Petr Nejedly
52  */

53 public class Deadlock49178Test extends NbTestCase implements CloneableEditorSupport.Env {
54
55     boolean inWait = false;
56     boolean shouldWait = true;
57     Object JavaDoc waitLock = new Object JavaDoc();
58     
59     /** the support to work with */
60     private CES support;
61
62     // Env variables
63
private String JavaDoc content = "Hello";
64     private boolean valid = true;
65     private boolean modified = false;
66     private Date JavaDoc date = new Date JavaDoc ();
67     private transient PropertyChangeSupport prop = new PropertyChangeSupport(this);
68     private transient VetoableChangeSupport veto = new VetoableChangeSupport(this);
69     private Exception JavaDoc exception;
70     
71     boolean processingDone = false;
72     boolean closingDone = false;
73     
74     
75     public Deadlock49178Test(String JavaDoc s) {
76         super(s);
77     }
78     
79     public static void main(String JavaDoc[] args) {
80         TestRunner.run(new NbTestSuite(Deadlock49178Test.class));
81     }
82
83     protected void setUp () {
84         support = new CES (this, org.openide.util.Lookup.EMPTY);
85     }
86         
87     public void testDeadlock49178() throws Exception JavaDoc {
88     // open the document
89
final StyledDocument JavaDoc docu = support.openDocument();
90
91
92     // start closing it
93
Thread JavaDoc closing = new Thread JavaDoc(new Runnable JavaDoc() { public void run() {
94             support.close(false); // will block in notifyUnmodified()
95
closingDone = true;
96     }});
97     closing.start();
98
99         Thread JavaDoc processing = new Thread JavaDoc(new Runnable JavaDoc() {
100             boolean second = false;
101             
102             public void run() {
103                 if (!second) {
104                     second = true;
105                     docu.render(this);
106 // NbDocument.runAtomic(docu, this);
107
} else { // inside readLock
108
support.createPositionRef(0, Position.Bias.Forward);
109                     processingDone = true;
110                 }
111             }
112         });
113
114         
115         synchronized(waitLock) {
116         while (!inWait) waitLock.wait();
117         }
118
119         processing.start();
120         
121         Thread.sleep(1000);
122         synchronized(waitLock) {
123             shouldWait = false;
124             waitLock.notifyAll();
125         }
126     
127     closing.join(10000);
128         processing.join(10000);
129     assertNull("No exception thrown", exception);
130     assertTrue("Closing thread finished", closingDone);
131     assertTrue("Processing thread finished", processingDone);
132     }
133     
134
135     //
136
// Implementation of the CloneableEditorSupport.Env
137
//
138

139     public void addPropertyChangeListener(PropertyChangeListener l) {
140         prop.addPropertyChangeListener (l);
141     }
142
143     public void removePropertyChangeListener(PropertyChangeListener l) {
144         prop.removePropertyChangeListener (l);
145     }
146
147     public void addVetoableChangeListener(VetoableChangeListener l) {
148         veto.addVetoableChangeListener (l);
149     }
150
151     public void removeVetoableChangeListener(VetoableChangeListener l) {
152         veto.removeVetoableChangeListener (l);
153     }
154     
155     
156     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
157         return support;
158     }
159     
160     public String JavaDoc getMimeType() {
161         return "text/plain";
162     }
163     
164     public java.util.Date JavaDoc getTime() {
165         return date;
166     }
167     
168     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
169     return new ByteArrayInputStream JavaDoc(content.getBytes());
170     }
171     
172     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
173         return new ByteArrayOutputStream JavaDoc();
174     }
175     
176     public boolean isValid() {
177         return valid;
178     }
179     
180     public boolean isModified() {
181         return modified;
182     }
183
184     public void markModified() throws java.io.IOException JavaDoc {
185         modified = true;
186     }
187     
188     public void unmarkModified() {
189         modified = false;
190     }
191     
192     /** Implementation of the CES */
193     private final class CES extends CloneableEditorSupport {
194         
195         public CES (Env env, org.openide.util.Lookup l) {
196             super (env, l);
197         }
198         
199         protected String JavaDoc messageName() {
200             return "Name";
201         }
202         
203         protected String JavaDoc messageOpened() {
204             return "Opened";
205         }
206         
207         protected String JavaDoc messageOpening() {
208             return "Opening";
209         }
210         
211         protected String JavaDoc messageSave() {
212             return "Save";
213         }
214         
215         protected String JavaDoc messageToolTip() {
216             return "ToolTip";
217         }
218
219         protected void notifyUnmodified () {
220             super.notifyUnmodified();
221             
222             synchronized(waitLock) {
223         inWait = true;
224         waitLock.notifyAll();
225             try {
226                 while (shouldWait) waitLock.wait();
227         } catch (InterruptedException JavaDoc e) {
228            exception = e;
229         }
230         }
231         }
232         
233         
234         protected StyledDocument JavaDoc createStyledDocument (EditorKit kit) {
235             return new Doc(); // Why the FilterDocument doesn't support WriteLockable?
236
}
237
238         class Doc extends DefaultStyledDocument implements NbDocument.WriteLockable {
239             public void runAtomic (Runnable JavaDoc r) {
240                 writeLock();
241                 try {
242                     r.run();
243                 } finally {
244                     writeUnlock();
245                 }
246             }
247             
248             public void runAtomicAsUser (Runnable JavaDoc r) throws BadLocationException JavaDoc {
249                 runAtomic(r);
250             }
251         }
252
253     } // end of CES
254

255 }
256
Popular Tags