KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Try firing a PROP_TIME while the document is just being loaded.
47  *
48  * @author Petr Nejedly
49  */

50 public class Deadlock56413Test extends NbTestCase implements CloneableEditorSupport.Env {
51
52     boolean inCreateKit = false;
53     boolean shouldWaitInCreate = true;
54     Object JavaDoc kitLock = new Object JavaDoc();
55     
56     /** the support to work with */
57     private CES support;
58
59     // Env variables
60
private String JavaDoc content = "Hello";
61     private boolean valid = true;
62     private boolean modified = false;
63     private Date JavaDoc date = new Date JavaDoc ();
64     private transient PropertyChangeSupport prop = new PropertyChangeSupport(this);
65     private transient VetoableChangeSupport veto = new VetoableChangeSupport(this);
66     private Exception JavaDoc exception;
67     
68     boolean loaded = false;
69     
70     
71     public Deadlock56413Test(String JavaDoc s) {
72         super(s);
73     }
74     
75     public static void main(String JavaDoc[] args) {
76         TestRunner.run(new NbTestSuite(Deadlock56413Test.class));
77     }
78
79     protected void setUp () {
80         support = new CES (this, org.openide.util.Lookup.EMPTY);
81     }
82         
83     public void testDeadlock56413() throws Exception JavaDoc {
84         // prime the event queue
85
javax.swing.SwingUtilities.invokeAndWait(new Runnable JavaDoc() {public void run() {}});
86         
87         // start loading, will register a listener for PROP_TIME
88
// and start a thread fetching the bits
89
Thread JavaDoc loading = new Thread JavaDoc(new Runnable JavaDoc() { public void run() {
90         try {
91             StyledDocument JavaDoc docu = support.openDocument();
92                 loaded = true;
93         } catch (IOException JavaDoc ioe) {
94         exception = ioe;
95         }
96     }});
97     loading.start();
98     
99     // as soon as it gets to createEditorKit, fire PROP_TIME
100
synchronized(kitLock) {
101         while (!inCreateKit) kitLock.wait();
102         
103         // XXX: fire
104
setNewTime(new Date JavaDoc());
105         // let the reload thread lock the doc and block on COS$L
106
// no possible hook there
107
Thread.sleep(2000);
108         
109         // allow kit to continue
110
shouldWaitInCreate = false;
111         kitLock.notifyAll();
112     }
113     
114     loading.join(10000);
115     assertNull("No exception thrown", exception);
116     assertTrue("Loading finished", loaded);
117     }
118     
119
120     //
121
// Implementation of the CloneableEditorSupport.Env
122
//
123

124     public void addPropertyChangeListener(PropertyChangeListener l) {
125         prop.addPropertyChangeListener (l);
126     }
127
128     public void removePropertyChangeListener(PropertyChangeListener l) {
129         prop.removePropertyChangeListener (l);
130     }
131
132     public void addVetoableChangeListener(VetoableChangeListener l) {
133         veto.addVetoableChangeListener (l);
134     }
135
136     public void removeVetoableChangeListener(VetoableChangeListener l) {
137         veto.removeVetoableChangeListener (l);
138     }
139     
140     void setNewTime(Date JavaDoc d) {
141     date = d;
142         prop.firePropertyChange (PROP_TIME, null, null);
143     }
144     
145     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
146         return support;
147     }
148     
149     public String JavaDoc getMimeType() {
150         return "text/plain";
151     }
152     
153     public java.util.Date JavaDoc getTime() {
154         return date;
155     }
156     
157     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
158     return new ByteArrayInputStream JavaDoc(content.getBytes());
159     }
160     
161     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
162         return new ByteArrayOutputStream JavaDoc();
163     }
164     
165     public boolean isValid() {
166         return valid;
167     }
168     
169     public boolean isModified() {
170         return modified;
171     }
172
173     public void markModified() throws java.io.IOException JavaDoc {
174         modified = true;
175     }
176     
177     public void unmarkModified() {
178         modified = false;
179     }
180
181     /** Implementation of the CES */
182     private final class CES extends CloneableEditorSupport {
183         
184         public CES (Env env, org.openide.util.Lookup l) {
185             super (env, l);
186         }
187         
188         protected String JavaDoc messageName() {
189             return "Name";
190         }
191         
192         protected String JavaDoc messageOpened() {
193             return "Opened";
194         }
195         
196         protected String JavaDoc messageOpening() {
197             return "Opening";
198         }
199         
200         protected String JavaDoc messageSave() {
201             return "Save";
202         }
203         
204         protected String JavaDoc messageToolTip() {
205             return "ToolTip";
206         }
207     
208         protected StyledDocument JavaDoc createStyledDocument (EditorKit kit) {
209             StyledDocument JavaDoc doc = super.createStyledDocument(kit);
210             
211             // have to store the field before unfusing the other thread
212
// in normal conditions, the store would happen just on return.
213
try {
214                 java.lang.reflect.Field JavaDoc f = CloneableEditorSupport.class.getDeclaredField("doc");
215                 f.setAccessible(true);
216                 f.set(this, doc);
217             } catch (Exception JavaDoc e) {
218                 exception = e;
219             }
220             
221         synchronized(kitLock) {
222         inCreateKit = true;
223         kitLock.notifyAll();
224             try {
225                 while (shouldWaitInCreate) kitLock.wait();
226         } catch (InterruptedException JavaDoc e) {
227            exception = e;
228         }
229         }
230
231             return doc;
232         }
233
234     } // end of CES
235

236 }
237
Popular Tags