1 19 20 21 package org.openide.text; 22 23 24 25 import java.io.IOException ; 26 import javax.swing.text.*; 27 import junit.textui.TestRunner; 28 import org.netbeans.junit.*; 29 import org.openide.util.Exceptions; 30 import org.openide.util.RequestProcessor; 31 32 38 public class Deadlock40766Test extends NbTestCase implements CloneableEditorSupport.Env { 39 40 private CES support; 41 private String content = "Hello"; 43 private boolean valid = true; 44 private boolean modified = false; 45 46 private String cannotBeModified; 47 private java.util.Date date = new java.util.Date (); 48 private java.util.List propL = new java.util.ArrayList (); 49 private java.beans.VetoableChangeListener vetoL; 50 51 52 public Deadlock40766Test(String s) { 53 super(s); 54 } 55 56 public static void main(String [] args) { 57 TestRunner.run(new NbTestSuite(Deadlock40766Test.class)); 58 } 59 60 protected void setUp () { 61 support = new CES (this, org.openide.util.Lookup.EMPTY); 62 } 63 64 RequestProcessor my = new RequestProcessor("my"); 65 66 public void testDeadlock40766() throws Exception { 67 org.openide.util.Task task; 68 69 synchronized (support.helperLock) { 70 my.post (support); 71 support.helperLock.wait (); 73 } 74 75 78 StyledDocument doc = support.openDocument(); 79 80 synchronized (support.helperLock) { 81 support.helperLock.notifyAll(); 83 support.helperLock.wait (1000); 84 } 85 86 88 NbDocument.runAtomic(doc, new Runnable () { 89 public void run() { 90 synchronized (support.helperLock) { 91 support.helperLock.notifyAll(); 92 } 93 94 try { 95 support.openDocument(); 96 } catch (IOException ioe) { 97 fail(ioe.getMessage()); 98 } 99 100 } 101 }); 102 103 } 104 105 119 public void testDeadlock38013() throws Exception { 120 org.openide.util.Task task; 121 122 124 synchronized (support.helperLock) { 125 my.post (support); support.helperLock.wait (); 127 support.helperLock.notifyAll(); 130 support.helperLock.wait (); 131 } 132 135 StyledDocument doc = support.openDocument(); 136 137 } 138 139 public void testCreatePositionCanBeCalledFromWriteLockOnDocument () throws Exception { 140 final StyledDocument doc = support.openDocument (); 141 142 class R implements Runnable { 143 boolean inAtomic; 144 PositionRef ref; 145 146 public void run () { 147 if (!inAtomic) { 148 inAtomic = true; 149 NbDocument.runAtomic (doc, this); 150 return; 151 } 152 153 synchronized (this) { 154 notifyAll (); 155 try { 156 wait (1000); 157 } catch (InterruptedException ex) { 158 fail (ex.getMessage ()); 159 } 160 } 161 ref = support.createPositionRef (0, Position.Bias.Backward); 162 } 163 } 164 165 RequestProcessor.Task task; 166 R r = new R (); 167 synchronized (r) { 168 task = RequestProcessor.getDefault ().post (r); 169 r.wait (); 170 } 171 172 PositionRef ref = support.createPositionRef (1, Position.Bias.Backward); 175 176 assertNotNull ("Ref created", ref); 177 task.waitFinished (); 178 assertNotNull ("Ref1 crated", r.ref); 179 180 } 182 186 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 187 propL.add (l); 188 } 189 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 190 propL.remove (l); 191 } 192 193 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 194 assertNull ("This is the first veto listener", vetoL); 195 vetoL = l; 196 } 197 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 198 assertEquals ("Removing the right veto one", vetoL, l); 199 vetoL = null; 200 } 201 202 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 203 return support; 204 } 205 206 public String getMimeType() { 207 return "text/plain"; 208 } 209 210 public java.util.Date getTime() { 211 return date; 212 } 213 214 public java.io.InputStream inputStream() throws java.io.IOException { 215 return new java.io.ByteArrayInputStream (content.getBytes ()); 216 } 217 public java.io.OutputStream outputStream() throws java.io.IOException { 218 class ContentStream extends java.io.ByteArrayOutputStream { 219 public void close () throws java.io.IOException { 220 super.close (); 221 content = new String (toByteArray ()); 222 } 223 } 224 225 return new ContentStream (); 226 } 227 228 public boolean isValid() { 229 return valid; 230 } 231 232 public boolean isModified() { 233 return modified; 234 } 235 236 public void markModified() throws java.io.IOException { 237 if (cannotBeModified != null) { 238 IOException e = new IOException (); 239 Exceptions.attachLocalizedMessage(e, cannotBeModified); 240 throw e; 241 } 242 243 modified = true; 244 } 245 246 public void unmarkModified() { 247 modified = false; 248 } 249 250 251 private final class CES extends CloneableEditorSupport implements Runnable { 252 253 public CES (Env env, org.openide.util.Lookup l) { 254 super (env, l); 255 } 256 257 protected String messageName() { 258 return "Name"; 259 } 260 261 protected String messageOpened() { 262 return "Opened"; 263 } 264 265 protected String messageOpening() { 266 return "Opening"; 267 } 268 269 protected String messageSave() { 270 return "Save"; 271 } 272 273 protected String messageToolTip() { 274 return "ToolTip"; 275 } 276 277 278 protected StyledDocument createStyledDocument (EditorKit kit) { 279 class Doc extends DefaultStyledDocument implements NbDocument.WriteLockable { 280 public void runAtomic (Runnable r) { 281 writeLock(); 282 try { 283 r.run(); 284 } finally { 285 writeUnlock(); 286 } 287 } 288 public void runAtomicAsUser (Runnable r) { 289 runAtomic(r); 290 } 291 } 292 StyledDocument sd = new Doc(); 293 return sd; 294 } 295 296 Object helperLock = new Object (); 297 298 void howToReproduceDeadlock40766(boolean beforeLock) { 299 if (my.isRequestProcessorThread()) { 300 synchronized(helperLock) { 301 try { 302 helperLock.notifyAll(); 303 helperLock.wait(1000); 304 } catch (InterruptedException ie) { 305 fail (ie.getMessage ()); 306 } 307 } 308 } 309 } 310 311 public void run () { 312 createPositionRef(0, Position.Bias.Forward); 313 } 314 315 } } 317 | Popular Tags |