1 19 20 21 package org.openide.text; 22 import java.io.StringWriter ; 23 import java.util.logging.Handler ; 24 import java.util.logging.LogManager ; 25 import java.util.logging.LogRecord ; 26 import java.util.logging.Logger ; 27 import javax.swing.text.*; 28 import javax.swing.text.StyledDocument ; 29 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.util.Lookup; 32 33 38 public class DocumentCannotBeClosedWhenAWTBlockedTest 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 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 static { 50 Logger l = Logger.getLogger(""); 51 Handler [] arr = l.getHandlers(); 52 for (int i = 0; i < arr.length; i++) { 53 l.removeHandler(arr[i]); 54 } 55 l.addHandler(new ErrManager()); 56 } 57 58 59 60 private Object LOCK = new Object (); 61 62 63 public DocumentCannotBeClosedWhenAWTBlockedTest(String s) { 64 super(s); 65 } 66 67 protected void setUp () throws Exception { 68 System.setProperty("org.openide.util.Lookup", DocumentCannotBeClosedWhenAWTBlockedTest.class.getName() + "$Lkp"); 69 70 super.setUp(); 71 72 Lookup l = Lookup.getDefault(); 73 if (!(l instanceof Lkp)) { 74 fail("Wrong lookup: " + l); 75 } 76 77 clearWorkDir(); 78 79 support = new CES(this, org.openide.util.Lookup.EMPTY); 80 81 ErrManager.messages.setLength(0); 82 } 83 84 public void testModifyAndBlockAWTAndTryToClose () throws Exception { 85 StyledDocument doc = support.openDocument(); 86 doc.insertString(0, "Ble", null); 87 88 assertTrue("Modified", support.isModified()); 89 90 class Block implements Runnable { 91 public synchronized void run() { 92 try { 93 wait(); 94 } catch (InterruptedException ex) { 95 ex.printStackTrace(); 96 } 97 } 98 } 99 100 Block b = new Block(); 101 javax.swing.SwingUtilities.invokeLater(b); 102 103 boolean success = support.canClose(); 104 105 synchronized (b) { 106 b.notifyAll(); 107 } 108 109 assertFalse("Support cannot close as we cannot ask the user", success); 110 111 if (ErrManager.messages.indexOf("InterruptedException") == -1) { 112 fail("InterruptedException exception should be reported: " + ErrManager.messages); 113 } 114 } 115 116 117 public void testBlockingAWTForFiveSecIsOk() throws Exception { 118 StyledDocument doc = support.openDocument(); 119 doc.insertString(0, "Ble", null); 120 121 assertTrue("Modified", support.isModified()); 122 123 class Block implements Runnable { 124 public synchronized void run() { 125 try { 126 wait(5000); 127 } catch (InterruptedException ex) { 128 ex.printStackTrace(); 129 } 130 } 131 } 132 133 Block b = new Block(); 134 javax.swing.SwingUtilities.invokeLater(b); 135 136 boolean success = support.canClose(); 137 138 synchronized (b) { 139 b.notifyAll(); 140 } 141 142 assertTrue("Ok, we managed to ask the question", success); 143 144 if (ErrManager.messages.length() > 0) { 145 fail("No messages should be reported: " + ErrManager.messages); 146 } 147 } 148 149 public void testCallingFromAWTIsOk() throws Exception { 150 StyledDocument doc = support.openDocument(); 151 doc.insertString(0, "Ble", null); 152 153 assertTrue("Modified", support.isModified()); 154 155 class AWT implements Runnable { 156 boolean success; 157 158 public synchronized void run() { 159 success = support.canClose(); 160 } 161 } 162 163 AWT b = new AWT(); 164 javax.swing.SwingUtilities.invokeAndWait(b); 165 166 assertTrue("Ok, we managed to ask the question", b.success); 167 168 if (ErrManager.messages.length() > 0) { 169 fail("No messages should be reported: " + ErrManager.messages); 170 } 171 } 172 173 177 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 178 propL.add (l); 179 } 180 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 181 propL.remove (l); 182 } 183 184 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 185 assertNull ("This is the first veto listener", vetoL); 186 vetoL = l; 187 } 188 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 189 assertEquals ("Removing the right veto one", vetoL, l); 190 vetoL = null; 191 } 192 193 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 194 return support; 195 } 196 197 public String getMimeType() { 198 return "text/plain"; 199 } 200 201 public java.util.Date getTime() { 202 return date; 203 } 204 205 public java.io.InputStream inputStream() throws java.io.IOException { 206 return new java.io.ByteArrayInputStream (content.getBytes ()); 207 } 208 public java.io.OutputStream outputStream() throws java.io.IOException { 209 class ContentStream extends java.io.ByteArrayOutputStream { 210 public void close () throws java.io.IOException { 211 super.close (); 212 content = new String (toByteArray ()); 213 } 214 } 215 216 return new ContentStream (); 217 } 218 219 public boolean isValid() { 220 return valid; 221 } 222 223 public boolean isModified() { 224 return modified; 225 } 226 227 public void markModified() throws java.io.IOException { 228 modified = true; 229 } 230 231 public void unmarkModified() { 232 modified = false; 233 } 234 235 236 private final class CES extends CloneableEditorSupport { 237 238 public CES (Env env, org.openide.util.Lookup l) { 239 super (env, l); 240 } 241 242 protected String messageName() { 243 return "Name"; 244 } 245 246 protected String messageOpened() { 247 return "Opened"; 248 } 249 250 protected String messageOpening() { 251 return "Opening"; 252 } 253 254 protected String messageSave() { 255 return "Save"; 256 } 257 258 protected String messageToolTip() { 259 return "ToolTip"; 260 } 261 262 protected EditorKit createEditorKit () { 263 return new NbLikeEditorKit (); 264 } 265 } public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 267 public Lkp() { 268 this(new org.openide.util.lookup.InstanceContent()); 269 } 270 271 private Lkp(org.openide.util.lookup.InstanceContent ic) { 272 super(ic); 273 ic.add(new DD()); 274 ic.add(new ErrManager()); 275 } 276 } 277 278 280 private static final class DD extends org.openide.DialogDisplayer { 281 public static Object [] options; 282 public static Object toReturn; 283 public static boolean disableTest; 284 285 public java.awt.Dialog createDialog(org.openide.DialogDescriptor descriptor) { 286 throw new IllegalStateException ("Not implemented"); 287 } 288 289 public Object notify(org.openide.NotifyDescriptor descriptor) { 290 return descriptor.getOptions()[0]; 291 } 292 293 } private static final class ErrManager extends Handler { 295 static final StringBuffer messages = new StringBuffer (); 296 static int nOfMessages; 297 static final String DELIMITER = ": "; 298 299 300 static java.io.PrintStream log = System.err; 301 302 private String prefix; 303 304 public ErrManager () { 305 prefix = ""; 306 } 307 308 private ErrManager (String pr) { 309 this.prefix = pr; 310 } 311 312 static void resetMessages() { 313 messages.delete(0, ErrManager.messages.length()); 314 nOfMessages = 0; 315 } 316 317 private void logImpl(String s) { 318 synchronized (ErrManager.messages) { 319 nOfMessages++; 320 messages.append('['); log.print ('['); 321 messages.append(prefix); log.print (prefix); 322 messages.append("] - "); log.print ("] - "); 323 messages.append(s); log.println (s); 324 messages.append('\n'); 325 } 326 } 327 328 public void publish(LogRecord record) { 329 logImpl(record.getMessage()); 330 if (record.getThrown() != null) { 331 StringWriter w = new StringWriter (); 332 record.getThrown().printStackTrace (new java.io.PrintWriter (w)); 333 logImpl (w.toString ()); 334 } 335 } 336 337 public void flush() { 338 } 339 340 public void close() { 341 } 342 } } 344 | Popular Tags |