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 31 36 public class DocumentCannotBeClosedWhenReadLockedTest extends NbTestCase implements CloneableEditorSupport.Env { 37 38 private CES support; 39 private String content = "Hello"; 41 private boolean valid = true; 42 private boolean modified = false; 43 44 private String cannotBeModified; 45 private java.util.Date date = new java.util.Date (); 46 private java.util.List propL = new java.util.ArrayList (); 47 private java.beans.VetoableChangeListener vetoL; 48 49 50 51 private Object LOCK = new Object (); 52 53 54 public DocumentCannotBeClosedWhenReadLockedTest(String s) { 55 super(s); 56 } 57 58 public static void main(String [] args) { 59 TestRunner.run(new NbTestSuite(DocumentCannotBeClosedWhenReadLockedTest.class)); 60 } 61 62 protected void setUp () { 63 support = new CES (this, org.openide.util.Lookup.EMPTY); 64 } 65 66 public void testReadLockTheDocumentAndThenTryToCreateAPositionInItMeanWhileLetOtherThreadCloseAComponent () throws Exception { 67 StyledDocument doc = support.openDocument (); 68 final CloneableEditorSupport.Pane pane = support.openAt (support.createPositionRef (0, javax.swing.text.Position.Bias.Forward), 0); 69 assertNotNull (pane); 70 assertNotNull ("TopComponent is there", pane.getComponent ()); 71 72 class DoWork implements Runnable { 73 private boolean startedAWT; 74 private boolean finishedAWT; 75 private boolean startedWork; 76 private boolean finishedWork; 77 78 public void run () { 79 if (javax.swing.SwingUtilities.isEventDispatchThread ()) { 80 doWorkInAWT (); 81 } else { 82 doWork (); 83 } 84 } 85 86 private void doWorkInAWT () { 87 startedAWT = true; 88 synchronized (LOCK) { 89 LOCK.notify(); 91 } 92 93 try { 94 Thread.sleep (500); 95 } catch (InterruptedException ex) { 96 fail (ex.getMessage ()); 97 } 98 99 pane.getComponent ().close (); 101 assertFalse ("The document should be marked unmodified now", modified); 102 synchronized (this) { 103 finishedAWT = true; 104 notifyAll (); 105 } 106 } 107 108 private void doWork () { 109 startedWork = true; 110 synchronized (LOCK) { 111 try { 112 LOCK.wait (); 113 } catch (InterruptedException ex) { 114 throw new org.netbeans.junit.AssertionFailedErrorException (ex); 115 } 116 } 117 118 support.createPositionRef (0, javax.swing.text.Position.Bias.Forward); 120 finishedWork = true; 121 } 122 123 public synchronized void waitFinishedAWT () throws InterruptedException { 124 int cnt = 5; 125 while (!finishedAWT && cnt-- > 0) { 126 wait (500); 127 } 128 129 if (!finishedAWT) { 130 fail ("AWT has not finsihed"); 131 } 132 } 133 134 } 135 DoWork doWork = new DoWork (); 136 137 138 synchronized (LOCK) { 139 javax.swing.SwingUtilities.invokeLater (doWork); 140 LOCK.wait (); 141 } 142 143 144 doc.render (doWork); 145 146 javax.swing.SwingUtilities.invokeAndWait (new Runnable () { public void run () {}}); 148 149 assertTrue ("AWT started", doWork.startedAWT); 150 assertTrue ("Work started", doWork.startedWork); 151 152 doWork.waitFinishedAWT (); 153 assertTrue ("Work done", doWork.finishedWork); 154 } 155 156 public void testReadLockTheDocumentAndThenTryToCreateAPositionInItMeanWhileLetOtherThreadCloseIt () throws Exception { 157 class DoWork implements Runnable { 158 private boolean finishedAWT; 159 private boolean finishedWork; 160 161 public void run () { 162 if (javax.swing.SwingUtilities.isEventDispatchThread ()) { 163 doWorkInAWT (); 164 } else { 165 doWork (); 166 } 167 } 168 169 private void doWorkInAWT () { 170 synchronized (LOCK) { 171 LOCK.notify(); 173 } 174 175 try { 176 Thread.sleep (500); 177 } catch (InterruptedException ex) { 178 fail (ex.getMessage ()); 179 } 180 181 support.close (); 183 assertFalse ("The document should be marked unmodified now", modified); 184 synchronized (this) { 185 finishedAWT = true; 186 notifyAll (); 187 } 188 } 189 190 private void doWork () { 191 synchronized (LOCK) { 192 try { 193 LOCK.wait (); 194 } catch (InterruptedException ex) { 195 throw new org.netbeans.junit.AssertionFailedErrorException (ex); 196 } 197 } 198 199 support.createPositionRef (0, javax.swing.text.Position.Bias.Forward); 201 finishedWork = true; 202 } 203 204 public synchronized void waitFinishedAWT () throws InterruptedException { 205 int cnt = 5; 206 while (!finishedAWT && cnt-- > 0) { 207 wait (500); 208 } 209 210 if (!finishedAWT) { 211 fail ("AWT has not finsihed"); 212 } 213 } 214 } 215 DoWork doWork = new DoWork (); 216 StyledDocument doc = support.openDocument (); 217 218 synchronized (LOCK) { 219 javax.swing.SwingUtilities.invokeLater (doWork); 220 LOCK.wait (); 221 } 222 223 224 doc.render (doWork); 225 226 doWork.waitFinishedAWT (); 228 assertTrue ("Work done", doWork.finishedWork); 229 } 230 231 232 233 234 238 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 239 propL.add (l); 240 } 241 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 242 propL.remove (l); 243 } 244 245 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 246 assertNull ("This is the first veto listener", vetoL); 247 vetoL = l; 248 } 249 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 250 assertEquals ("Removing the right veto one", vetoL, l); 251 vetoL = null; 252 } 253 254 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 255 return support; 256 } 257 258 public String getMimeType() { 259 return "text/plain"; 260 } 261 262 public java.util.Date getTime() { 263 return date; 264 } 265 266 public java.io.InputStream inputStream() throws java.io.IOException { 267 return new java.io.ByteArrayInputStream (content.getBytes ()); 268 } 269 public java.io.OutputStream outputStream() throws java.io.IOException { 270 class ContentStream extends java.io.ByteArrayOutputStream { 271 public void close () throws java.io.IOException { 272 super.close (); 273 content = new String (toByteArray ()); 274 } 275 } 276 277 return new ContentStream (); 278 } 279 280 public boolean isValid() { 281 return valid; 282 } 283 284 public boolean isModified() { 285 return modified; 286 } 287 288 public void markModified() throws java.io.IOException { 289 if (cannotBeModified != null) { 290 IOException e = new IOException (); 291 Exceptions.attachLocalizedMessage(e, cannotBeModified); 292 throw e; 293 } 294 295 modified = true; 296 } 297 298 public void unmarkModified() { 299 synchronized (LOCK) { 300 LOCK.notify (); 301 try { 302 LOCK.wait (500); 303 } catch (InterruptedException ex) { 304 ex.printStackTrace(); 305 } 306 } 307 modified = false; 308 } 309 310 311 private final class CES extends CloneableEditorSupport { 312 313 public CES (Env env, org.openide.util.Lookup l) { 314 super (env, l); 315 } 316 317 protected String messageName() { 318 return "Name"; 319 } 320 321 protected String messageOpened() { 322 return "Opened"; 323 } 324 325 protected String messageOpening() { 326 return "Opening"; 327 } 328 329 protected String messageSave() { 330 return "Save"; 331 } 332 333 protected String messageToolTip() { 334 return "ToolTip"; 335 } 336 337 protected EditorKit createEditorKit () { 338 return new NbLikeEditorKit (); 339 } 340 } } 342 | Popular Tags |