KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
25
26 import org.netbeans.junit.NbTestCase;
27 import org.openide.util.Exceptions;
28
29
30 /**
31  * Exception during load of the document can cause starvation
32  * in the thread that waits for that to happen.
33  *
34  * @author Jaroslav Tulach
35  */

36 public class Starvation37045Test extends NbTestCase
37 implements CloneableEditorSupport.Env {
38     /** the support to work with */
39     private CES support;
40     // Env variables
41
private String JavaDoc content = "";
42     private boolean valid = true;
43     private boolean modified = false;
44     /** if not null contains message why this document cannot be modified */
45     private String JavaDoc cannotBeModified;
46     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
47     private java.util.List JavaDoc/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList JavaDoc ();
48     private java.beans.VetoableChangeListener JavaDoc vetoL;
49     
50     /** Creates new TextTest */
51     public Starvation37045Test (String JavaDoc s) {
52         super(s);
53     }
54     
55     protected void setUp () {
56         support = new CES (this, org.openide.util.Lookup.EMPTY);
57     }
58     
59     
60     public void testTheStarvation37045 () throws Exception JavaDoc {
61         org.openide.util.Task task;
62         
63         synchronized (this) {
64             org.openide.util.RequestProcessor.getDefault ().post (support);
65             // wait for the support (another thread) to try to open and block
66
wait ();
67
68             // now post there another task
69
task = org.openide.util.RequestProcessor.getDefault ().post (support);
70             // wait for it to block, any amount of time is likely to do it
71
Thread.sleep (500);
72             
73             // notify the first edit(), to continue (and throw exception)
74
notify ();
75         }
76
77         // check for deadlock
78
for (int i = 0; i < 5; i++) {
79             if (task.isFinished ()) break;
80             Thread.sleep (500);
81         }
82         
83         // uncomment the next line if you want to see real starvation threaddump
84
// task.waitFinished ();
85
assertTrue ("Should be finished, but there is a starvation", task.isFinished ());
86     }
87
88     //
89
// Implementation of the CloneableEditorSupport.Env
90
//
91

92     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
93         propL.add (l);
94     }
95     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
96         propL.remove (l);
97     }
98     
99     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
100         assertNull ("This is the first veto listener", vetoL);
101         vetoL = l;
102     }
103     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
104         assertEquals ("Removing the right veto one", vetoL, l);
105         vetoL = null;
106     }
107     
108     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
109         return support;
110     }
111     
112     public String JavaDoc getMimeType() {
113         return "text/plain";
114     }
115     
116     public java.util.Date JavaDoc getTime() {
117         return date;
118     }
119     
120     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
121         throw new OutOfMemoryError JavaDoc("Ha ha ha");
122         // return new java.io.ByteArrayInputStream (content.getBytes ());
123
}
124     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
125         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
126             public void close () throws java.io.IOException JavaDoc {
127                 super.close ();
128                 content = new String JavaDoc (toByteArray ());
129             }
130         }
131         
132         return new ContentStream ();
133     }
134     
135     public boolean isValid() {
136         return valid;
137     }
138     
139     public boolean isModified() {
140         return modified;
141     }
142
143     public void markModified() throws java.io.IOException JavaDoc {
144         if (cannotBeModified != null) {
145             IOException JavaDoc e = new IOException JavaDoc ();
146             Exceptions.attachLocalizedMessage(e, cannotBeModified);
147             throw e;
148         }
149         
150         modified = true;
151     }
152     
153     public void unmarkModified() {
154         modified = false;
155     }
156
157     /** Implementation of the CES */
158     private final class CES extends CloneableEditorSupport
159     implements Runnable JavaDoc {
160         private boolean wait = true;
161         
162         public CES (Env env, org.openide.util.Lookup l) {
163             super (env, l);
164         }
165         
166         protected String JavaDoc messageName() {
167             return "Name";
168         }
169         
170         protected String JavaDoc messageOpened() {
171             return "Opened";
172         }
173         
174         protected String JavaDoc messageOpening() {
175             return "Opening";
176         }
177         
178         protected String JavaDoc messageSave() {
179             return "Save";
180         }
181         
182         protected String JavaDoc messageToolTip() {
183             return "ToolTip";
184         }
185
186         protected javax.swing.text.EditorKit JavaDoc createEditorKit () {
187             if (wait) {
188                 synchronized (Starvation37045Test.this) {
189                     wait = false;
190                     try {
191                         Starvation37045Test.this.notifyAll ();
192                         Starvation37045Test.this.wait ();
193                     } catch (InterruptedException JavaDoc ex) {
194                         ex.printStackTrace();
195                         fail (ex.getMessage ());
196                     }
197                 }
198                 throw new IllegalStateException JavaDoc ("Let's pretend that I am broken!!!");
199             }
200             return super.createEditorKit ();
201         }
202         
203         public void run () {
204             boolean firstTime = wait;
205             try {
206                 edit ();
207                 if (firstTime) {
208                     fail ("It should throw an exception");
209                 }
210             } catch (IllegalStateException JavaDoc ex) {
211                 if (!firstTime) throw ex;
212                 assertEquals ("Name of exception is correct", "Let's pretend that I am broken!!!", ex.getMessage ());
213             }
214         }
215         
216     } // end of CES
217
}
218
Popular Tags