KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.junit.*;
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 Starvation37045SecondTest extends NbTestCase implements CloneableEditorSupport.Env {
37     /** the support to work with */
38     private CES support;
39     // Env variables
40
private String JavaDoc content = "";
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     /** Creates new TextTest */
50     public Starvation37045SecondTest (String JavaDoc s) {
51         super(s);
52     }
53     
54     protected void setUp () {
55         support = new CES (this, org.openide.util.Lookup.EMPTY);
56     }
57     
58     
59     public void testTheStarvation37045 () throws Exception JavaDoc {
60         org.openide.util.Task task;
61         
62         synchronized (this) {
63             support.prepareDocument ().waitFinished ();
64             
65             task = org.openide.util.RequestProcessor.getDefault ().post (support);
66             // wait for the support (another thread) to try to open and block
67
// wait ();
68
/*
69             // now post there another task
70             task = org.openide.util.RequestProcessor.getDefault ().post (support);
71             // wait for it to block, any amount of time is likely to do it
72             Thread.sleep (500);
73 */

74             // notify the first edit(), to continue (and throw exception)
75
notify ();
76         }
77
78         // check for deadlock
79
for (int i = 0; i < 5; i++) {
80             if (task.isFinished ()) break;
81             Thread.sleep (500);
82         }
83         
84         // uncomment the next line if you want to see real starvation threaddump
85
task.waitFinished ();
86         assertTrue ("Should be finished, but there is a starvation", task.isFinished ());
87     }
88
89     //
90
// Implementation of the CloneableEditorSupport.Env
91
//
92

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