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 50 public class Deadlock56413Test extends NbTestCase implements CloneableEditorSupport.Env { 51 52 boolean inCreateKit = false; 53 boolean shouldWaitInCreate = true; 54 Object kitLock = new Object (); 55 56 57 private CES support; 58 59 private String content = "Hello"; 61 private boolean valid = true; 62 private boolean modified = false; 63 private Date date = new Date (); 64 private transient PropertyChangeSupport prop = new PropertyChangeSupport(this); 65 private transient VetoableChangeSupport veto = new VetoableChangeSupport(this); 66 private Exception exception; 67 68 boolean loaded = false; 69 70 71 public Deadlock56413Test(String s) { 72 super(s); 73 } 74 75 public static void main(String [] args) { 76 TestRunner.run(new NbTestSuite(Deadlock56413Test.class)); 77 } 78 79 protected void setUp () { 80 support = new CES (this, org.openide.util.Lookup.EMPTY); 81 } 82 83 public void testDeadlock56413() throws Exception { 84 javax.swing.SwingUtilities.invokeAndWait(new Runnable () {public void run() {}}); 86 87 Thread loading = new Thread (new Runnable () { public void run() { 90 try { 91 StyledDocument docu = support.openDocument(); 92 loaded = true; 93 } catch (IOException ioe) { 94 exception = ioe; 95 } 96 }}); 97 loading.start(); 98 99 synchronized(kitLock) { 101 while (!inCreateKit) kitLock.wait(); 102 103 setNewTime(new Date ()); 105 Thread.sleep(2000); 108 109 shouldWaitInCreate = false; 111 kitLock.notifyAll(); 112 } 113 114 loading.join(10000); 115 assertNull("No exception thrown", exception); 116 assertTrue("Loading finished", loaded); 117 } 118 119 120 124 public void addPropertyChangeListener(PropertyChangeListener l) { 125 prop.addPropertyChangeListener (l); 126 } 127 128 public void removePropertyChangeListener(PropertyChangeListener l) { 129 prop.removePropertyChangeListener (l); 130 } 131 132 public void addVetoableChangeListener(VetoableChangeListener l) { 133 veto.addVetoableChangeListener (l); 134 } 135 136 public void removeVetoableChangeListener(VetoableChangeListener l) { 137 veto.removeVetoableChangeListener (l); 138 } 139 140 void setNewTime(Date d) { 141 date = d; 142 prop.firePropertyChange (PROP_TIME, null, null); 143 } 144 145 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 146 return support; 147 } 148 149 public String getMimeType() { 150 return "text/plain"; 151 } 152 153 public java.util.Date getTime() { 154 return date; 155 } 156 157 public java.io.InputStream inputStream() throws java.io.IOException { 158 return new ByteArrayInputStream (content.getBytes()); 159 } 160 161 public java.io.OutputStream outputStream() throws java.io.IOException { 162 return new ByteArrayOutputStream (); 163 } 164 165 public boolean isValid() { 166 return valid; 167 } 168 169 public boolean isModified() { 170 return modified; 171 } 172 173 public void markModified() throws java.io.IOException { 174 modified = true; 175 } 176 177 public void unmarkModified() { 178 modified = false; 179 } 180 181 182 private final class CES extends CloneableEditorSupport { 183 184 public CES (Env env, org.openide.util.Lookup l) { 185 super (env, l); 186 } 187 188 protected String messageName() { 189 return "Name"; 190 } 191 192 protected String messageOpened() { 193 return "Opened"; 194 } 195 196 protected String messageOpening() { 197 return "Opening"; 198 } 199 200 protected String messageSave() { 201 return "Save"; 202 } 203 204 protected String messageToolTip() { 205 return "ToolTip"; 206 } 207 208 protected StyledDocument createStyledDocument (EditorKit kit) { 209 StyledDocument doc = super.createStyledDocument(kit); 210 211 try { 214 java.lang.reflect.Field f = CloneableEditorSupport.class.getDeclaredField("doc"); 215 f.setAccessible(true); 216 f.set(this, doc); 217 } catch (Exception e) { 218 exception = e; 219 } 220 221 synchronized(kitLock) { 222 inCreateKit = true; 223 kitLock.notifyAll(); 224 try { 225 while (shouldWaitInCreate) kitLock.wait(); 226 } catch (InterruptedException e) { 227 exception = e; 228 } 229 } 230 231 return doc; 232 } 233 234 } 236 } 237 | Popular Tags |