KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.StringWriter JavaDoc;
23 import java.util.logging.Handler JavaDoc;
24 import java.util.logging.LogManager JavaDoc;
25 import java.util.logging.LogRecord JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import javax.swing.text.*;
28 import javax.swing.text.StyledDocument JavaDoc;
29
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.util.Lookup;
32
33 /**
34  * Simulates issue 46981. Editor locks the document, but somebody else closes it
35  * while it is working on it and a deadlock occurs.
36  * @author Petr Nejedly, Jaroslav Tulach
37  */

38 public class DocumentCannotBeClosedWhenAWTBlockedTest extends NbTestCase implements CloneableEditorSupport.Env {
39     /** the support to work with */
40     private CES support;
41     // Env variables
42
private String JavaDoc content = "Hello";
43     private boolean valid = true;
44     private boolean modified = false;
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     static {
50         Logger JavaDoc l = Logger.getLogger("");
51         Handler JavaDoc[] arr = l.getHandlers();
52         for (int i = 0; i < arr.length; i++) {
53             l.removeHandler(arr[i]);
54         }
55         l.addHandler(new ErrManager());
56     }
57     
58     
59     /** lock to use for communication between AWT & main thread */
60     private Object JavaDoc LOCK = new Object JavaDoc ();
61     
62     /** Creates new TextTest */
63     public DocumentCannotBeClosedWhenAWTBlockedTest(String JavaDoc s) {
64         super(s);
65     }
66     
67     protected void setUp () throws Exception JavaDoc {
68         System.setProperty("org.openide.util.Lookup", DocumentCannotBeClosedWhenAWTBlockedTest.class.getName() + "$Lkp");
69         
70         super.setUp();
71         
72         Lookup l = Lookup.getDefault();
73         if (!(l instanceof Lkp)) {
74             fail("Wrong lookup: " + l);
75         }
76         
77         clearWorkDir();
78         
79         support = new CES(this, org.openide.util.Lookup.EMPTY);
80         
81         ErrManager.messages.setLength(0);
82     }
83     
84     public void testModifyAndBlockAWTAndTryToClose () throws Exception JavaDoc {
85         StyledDocument JavaDoc doc = support.openDocument();
86         doc.insertString(0, "Ble", null);
87         
88         assertTrue("Modified", support.isModified());
89         
90         class Block implements Runnable JavaDoc {
91             public synchronized void run() {
92                 try {
93                     wait();
94                 } catch (InterruptedException JavaDoc ex) {
95                     ex.printStackTrace();
96                 }
97             }
98         }
99         
100         Block b = new Block();
101         javax.swing.SwingUtilities.invokeLater(b);
102         
103         boolean success = support.canClose();
104         
105         synchronized (b) {
106             b.notifyAll();
107         }
108         
109         assertFalse("Support cannot close as we cannot ask the user", success);
110         
111         if (ErrManager.messages.indexOf("InterruptedException") == -1) {
112             fail("InterruptedException exception should be reported: " + ErrManager.messages);
113         }
114     }
115
116     
117     public void testBlockingAWTForFiveSecIsOk() throws Exception JavaDoc {
118         StyledDocument JavaDoc doc = support.openDocument();
119         doc.insertString(0, "Ble", null);
120         
121         assertTrue("Modified", support.isModified());
122         
123         class Block implements Runnable JavaDoc {
124             public synchronized void run() {
125                 try {
126                     wait(5000);
127                 } catch (InterruptedException JavaDoc ex) {
128                     ex.printStackTrace();
129                 }
130             }
131         }
132         
133         Block b = new Block();
134         javax.swing.SwingUtilities.invokeLater(b);
135         
136         boolean success = support.canClose();
137         
138         synchronized (b) {
139             b.notifyAll();
140         }
141         
142         assertTrue("Ok, we managed to ask the question", success);
143         
144         if (ErrManager.messages.length() > 0) {
145             fail("No messages should be reported: " + ErrManager.messages);
146         }
147     }
148
149     public void testCallingFromAWTIsOk() throws Exception JavaDoc {
150         StyledDocument JavaDoc doc = support.openDocument();
151         doc.insertString(0, "Ble", null);
152         
153         assertTrue("Modified", support.isModified());
154         
155         class AWT implements Runnable JavaDoc {
156             boolean success;
157             
158             public synchronized void run() {
159                 success = support.canClose();
160             }
161         }
162         
163         AWT b = new AWT();
164         javax.swing.SwingUtilities.invokeAndWait(b);
165         
166         assertTrue("Ok, we managed to ask the question", b.success);
167         
168         if (ErrManager.messages.length() > 0) {
169             fail("No messages should be reported: " + ErrManager.messages);
170         }
171     }
172     
173     //
174
// Implementation of the CloneableEditorSupport.Env
175
//
176

177     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
178         propL.add (l);
179     }
180     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
181         propL.remove (l);
182     }
183     
184     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
185         assertNull ("This is the first veto listener", vetoL);
186         vetoL = l;
187     }
188     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
189         assertEquals ("Removing the right veto one", vetoL, l);
190         vetoL = null;
191     }
192     
193     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
194         return support;
195     }
196     
197     public String JavaDoc getMimeType() {
198         return "text/plain";
199     }
200     
201     public java.util.Date JavaDoc getTime() {
202         return date;
203     }
204     
205     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
206         return new java.io.ByteArrayInputStream JavaDoc (content.getBytes ());
207     }
208     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
209         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
210             public void close () throws java.io.IOException JavaDoc {
211                 super.close ();
212                 content = new String JavaDoc (toByteArray ());
213             }
214         }
215         
216         return new ContentStream ();
217     }
218     
219     public boolean isValid() {
220         return valid;
221     }
222     
223     public boolean isModified() {
224         return modified;
225     }
226
227     public void markModified() throws java.io.IOException JavaDoc {
228         modified = true;
229     }
230     
231     public void unmarkModified() {
232         modified = false;
233     }
234
235     /** Implementation of the CES */
236     private final class CES extends CloneableEditorSupport {
237         
238         public CES (Env env, org.openide.util.Lookup l) {
239             super (env, l);
240         }
241         
242         protected String JavaDoc messageName() {
243             return "Name";
244         }
245         
246         protected String JavaDoc messageOpened() {
247             return "Opened";
248         }
249         
250         protected String JavaDoc messageOpening() {
251             return "Opening";
252         }
253         
254         protected String JavaDoc messageSave() {
255             return "Save";
256         }
257         
258         protected String JavaDoc messageToolTip() {
259             return "ToolTip";
260         }
261         
262         protected EditorKit createEditorKit () {
263             return new NbLikeEditorKit ();
264         }
265     } // end of CES
266
public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
267         public Lkp() {
268             this(new org.openide.util.lookup.InstanceContent());
269         }
270         
271         private Lkp(org.openide.util.lookup.InstanceContent ic) {
272             super(ic);
273             ic.add(new DD());
274             ic.add(new ErrManager());
275         }
276     }
277     
278     /** Our own dialog displayer.
279      */

280     private static final class DD extends org.openide.DialogDisplayer {
281         public static Object JavaDoc[] options;
282         public static Object JavaDoc toReturn;
283         public static boolean disableTest;
284         
285         public java.awt.Dialog JavaDoc createDialog(org.openide.DialogDescriptor descriptor) {
286             throw new IllegalStateException JavaDoc ("Not implemented");
287         }
288         
289         public Object JavaDoc notify(org.openide.NotifyDescriptor descriptor) {
290             return descriptor.getOptions()[0];
291         }
292         
293     } // end of DD
294
private static final class ErrManager extends Handler JavaDoc {
295         static final StringBuffer JavaDoc messages = new StringBuffer JavaDoc();
296         static int nOfMessages;
297         static final String JavaDoc DELIMITER = ": ";
298
299         /** setup in setUp */
300         static java.io.PrintStream JavaDoc log = System.err;
301         
302         private String JavaDoc prefix;
303         
304         public ErrManager () {
305             prefix = "";
306         }
307         
308         private ErrManager (String JavaDoc pr) {
309             this.prefix = pr;
310         }
311         
312         static void resetMessages() {
313             messages.delete(0, ErrManager.messages.length());
314             nOfMessages = 0;
315         }
316         
317         private void logImpl(String JavaDoc s) {
318             synchronized (ErrManager.messages) {
319                 nOfMessages++;
320                 messages.append('['); log.print ('[');
321                 messages.append(prefix); log.print (prefix);
322                 messages.append("] - "); log.print ("] - ");
323                 messages.append(s); log.println (s);
324                 messages.append('\n');
325             }
326         }
327         
328         public void publish(LogRecord JavaDoc record) {
329             logImpl(record.getMessage());
330             if (record.getThrown() != null) {
331                 StringWriter JavaDoc w = new StringWriter JavaDoc ();
332                 record.getThrown().printStackTrace (new java.io.PrintWriter JavaDoc (w));
333                 logImpl (w.toString ());
334             }
335         }
336
337         public void flush() {
338         }
339
340         public void close() {
341         }
342     } // end of ErrManager
343
}
344
Popular Tags