1 19 20 21 package org.openide.text; 22 23 import java.util.Date ; 24 import java.beans.*; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ByteArrayInputStream ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import javax.swing.text.*; 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.text.Document ; 32 import javax.swing.text.Position ; 33 import javax.swing.text.StyledDocument ; 34 35 import junit.textui.TestRunner; 36 37 import org.netbeans.junit.NbTestCase; 38 import org.netbeans.junit.NbTestSuite; 39 40 import org.openide.text.CloneableEditorSupport; 41 import org.openide.text.FilterDocument; 42 import org.openide.text.NbDocument; 43 import org.openide.util.RequestProcessor; 44 45 53 public class Deadlock49178Test extends NbTestCase implements CloneableEditorSupport.Env { 54 55 boolean inWait = false; 56 boolean shouldWait = true; 57 Object waitLock = new Object (); 58 59 60 private CES support; 61 62 private String content = "Hello"; 64 private boolean valid = true; 65 private boolean modified = false; 66 private Date date = new Date (); 67 private transient PropertyChangeSupport prop = new PropertyChangeSupport(this); 68 private transient VetoableChangeSupport veto = new VetoableChangeSupport(this); 69 private Exception exception; 70 71 boolean processingDone = false; 72 boolean closingDone = false; 73 74 75 public Deadlock49178Test(String s) { 76 super(s); 77 } 78 79 public static void main(String [] args) { 80 TestRunner.run(new NbTestSuite(Deadlock49178Test.class)); 81 } 82 83 protected void setUp () { 84 support = new CES (this, org.openide.util.Lookup.EMPTY); 85 } 86 87 public void testDeadlock49178() throws Exception { 88 final StyledDocument docu = support.openDocument(); 90 91 92 Thread closing = new Thread (new Runnable () { public void run() { 94 support.close(false); closingDone = true; 96 }}); 97 closing.start(); 98 99 Thread processing = new Thread (new Runnable () { 100 boolean second = false; 101 102 public void run() { 103 if (!second) { 104 second = true; 105 docu.render(this); 106 } else { support.createPositionRef(0, Position.Bias.Forward); 109 processingDone = true; 110 } 111 } 112 }); 113 114 115 synchronized(waitLock) { 116 while (!inWait) waitLock.wait(); 117 } 118 119 processing.start(); 120 121 Thread.sleep(1000); 122 synchronized(waitLock) { 123 shouldWait = false; 124 waitLock.notifyAll(); 125 } 126 127 closing.join(10000); 128 processing.join(10000); 129 assertNull("No exception thrown", exception); 130 assertTrue("Closing thread finished", closingDone); 131 assertTrue("Processing thread finished", processingDone); 132 } 133 134 135 139 public void addPropertyChangeListener(PropertyChangeListener l) { 140 prop.addPropertyChangeListener (l); 141 } 142 143 public void removePropertyChangeListener(PropertyChangeListener l) { 144 prop.removePropertyChangeListener (l); 145 } 146 147 public void addVetoableChangeListener(VetoableChangeListener l) { 148 veto.addVetoableChangeListener (l); 149 } 150 151 public void removeVetoableChangeListener(VetoableChangeListener l) { 152 veto.removeVetoableChangeListener (l); 153 } 154 155 156 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 157 return support; 158 } 159 160 public String getMimeType() { 161 return "text/plain"; 162 } 163 164 public java.util.Date getTime() { 165 return date; 166 } 167 168 public java.io.InputStream inputStream() throws java.io.IOException { 169 return new ByteArrayInputStream (content.getBytes()); 170 } 171 172 public java.io.OutputStream outputStream() throws java.io.IOException { 173 return new ByteArrayOutputStream (); 174 } 175 176 public boolean isValid() { 177 return valid; 178 } 179 180 public boolean isModified() { 181 return modified; 182 } 183 184 public void markModified() throws java.io.IOException { 185 modified = true; 186 } 187 188 public void unmarkModified() { 189 modified = false; 190 } 191 192 193 private final class CES extends CloneableEditorSupport { 194 195 public CES (Env env, org.openide.util.Lookup l) { 196 super (env, l); 197 } 198 199 protected String messageName() { 200 return "Name"; 201 } 202 203 protected String messageOpened() { 204 return "Opened"; 205 } 206 207 protected String messageOpening() { 208 return "Opening"; 209 } 210 211 protected String messageSave() { 212 return "Save"; 213 } 214 215 protected String messageToolTip() { 216 return "ToolTip"; 217 } 218 219 protected void notifyUnmodified () { 220 super.notifyUnmodified(); 221 222 synchronized(waitLock) { 223 inWait = true; 224 waitLock.notifyAll(); 225 try { 226 while (shouldWait) waitLock.wait(); 227 } catch (InterruptedException e) { 228 exception = e; 229 } 230 } 231 } 232 233 234 protected StyledDocument createStyledDocument (EditorKit kit) { 235 return new Doc(); } 237 238 class Doc extends DefaultStyledDocument implements NbDocument.WriteLockable { 239 public void runAtomic (Runnable r) { 240 writeLock(); 241 try { 242 r.run(); 243 } finally { 244 writeUnlock(); 245 } 246 } 247 248 public void runAtomicAsUser (Runnable r) throws BadLocationException { 249 runAtomic(r); 250 } 251 } 252 253 } 255 } 256 | Popular Tags |