1 19 20 21 package org.openide.text; 22 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.lang.reflect.Method ; 28 import java.util.Collections ; 29 import java.util.HashSet ; 30 import java.util.Set ; 31 import javax.swing.SwingUtilities ; 32 import javax.swing.JEditorPane ; 33 import javax.swing.text.BadLocationException ; 34 import javax.swing.text.Position ; 35 import javax.swing.text.StyledDocument ; 36 37 import junit.textui.TestRunner; 38 39 import org.netbeans.junit.NbTestCase; 40 import org.netbeans.junit.NbTestSuite; 41 import org.openide.actions.*; 42 import org.openide.cookies.CloseCookie; 43 import org.openide.cookies.EditCookie; 44 45 import org.openide.cookies.EditorCookie; 46 import org.openide.cookies.OpenCookie; 47 import org.openide.cookies.PrintCookie; 48 import org.openide.cookies.SaveCookie; 49 import org.openide.filesystems.FileLock; 50 import org.openide.filesystems.FileObject; 51 import org.openide.filesystems.FileStateInvalidException; 52 import org.openide.filesystems.FileSystem; 53 import org.openide.filesystems.Repository; 54 import org.openide.loaders.DataFolder; 55 import org.openide.loaders.DataNode; 56 import org.openide.loaders.DataObject; 57 import org.openide.loaders.DataObjectExistsException; 58 import org.openide.loaders.ExtensionList; 59 import org.openide.loaders.MultiDataObject; 60 import org.openide.loaders.MultiFileLoader; 61 import org.openide.loaders.UniFileLoader; 62 import org.openide.nodes.Children; 63 import org.openide.nodes.CookieSet; 64 import org.openide.nodes.Node; 65 import org.openide.text.CloneableEditorSupport; 66 import org.openide.util.HelpCtx; 67 import org.openide.util.Mutex; 68 import org.openide.util.io.NbMarshalledObject; 69 import org.openide.util.Lookup; 70 import org.openide.util.LookupListener; 71 import org.openide.util.NbBundle; 72 import org.openide.util.actions.SystemAction; 73 import org.openide.windows.CloneableOpenSupport; 74 import org.openide.windows.WindowManager; 75 76 77 79 public class DataEditorSupportTest extends NbTestCase { 80 String content = ""; 82 long expectedSize = -1; 83 java.util.Date date = new java.util.Date (); 84 85 MyFileObject fileObject; 86 org.openide.filesystems.FileSystem fs; 87 static DataEditorSupportTest RUNNING; 88 static { 89 System.setProperty ("org.openide.util.Lookup", "org.openide.text.DataEditorSupportTest$Lkp"); 90 } 91 92 public DataEditorSupportTest(String s) { 93 super(s); 94 } 95 96 protected void setUp () throws Exception { 97 RUNNING = this; 98 99 fs = org.openide.filesystems.FileUtil.createMemoryFileSystem (); 100 org.openide.filesystems.Repository.getDefault ().addFileSystem (fs); 101 org.openide.filesystems.FileObject root = fs.getRoot (); 102 fileObject = new MyFileObject (org.openide.filesystems.FileUtil.createData (root, "my.obj")); 103 } 104 105 protected void tearDown () throws Exception { 106 waitEQ (); 107 108 RUNNING = null; 109 org.openide.filesystems.Repository.getDefault ().removeFileSystem (fs); 110 } 111 112 protected boolean runInEQ() { 113 return false; 114 } 115 116 private void waitEQ () throws Exception { 117 javax.swing.SwingUtilities.invokeAndWait (new Runnable () { public void run () { } }); 118 } 119 120 DES support () throws Exception { 121 DataObject obj = DataObject.find (fileObject); 122 123 assertEquals ("My object was created", MyDataObject.class, obj.getClass ()); 124 Object cookie = obj.getCookie (org.openide.cookies.OpenCookie.class); 125 assertNotNull ("Our object has this cookie", cookie); 126 assertEquals ("It is my cookie", DES.class, cookie.getClass ()); 127 128 return (DES)cookie; 129 } 130 131 132 private DataObject obj; 133 public void testItCanBeGCedIssue57565 () throws Exception { 134 DES sup = support (); 135 assertFalse ("It is closed now", support ().isDocumentLoaded ()); 136 137 Lookup lkp = sup.getLookup (); 138 obj = (DataObject)lkp.lookup (DataObject.class); 139 assertNotNull ("DataObject found", obj); 140 141 sup.openDocument (); 142 assertTrue ("It is open now", support ().isDocumentLoaded ()); 143 144 assertTrue ("Closed ok", sup.close ()); 145 146 java.lang.ref.WeakReference refLkp = new java.lang.ref.WeakReference (lkp); 147 lkp = null; 148 149 java.lang.ref.WeakReference ref = new java.lang.ref.WeakReference (sup); 150 sup = null; 151 152 assertGC ("Can disappear", ref); 153 assertGC ("And its lookup as well", refLkp); 154 155 156 157 } 158 159 public void testGetOpenedPanesWorksAfterDeserialization () throws Exception { 160 doGetOpenedPanesWorksAfterDeserialization (-1); 161 } 162 public void testGetOpenedPanesWorksAfterDeserializationIfTheFileGetsBig () throws Exception { 163 doGetOpenedPanesWorksAfterDeserialization (1024 * 1024 * 10); 164 } 165 166 public void test68015 () throws Exception { 167 DES edSupport = support(); 168 edSupport.open(); 169 170 waitEQ(); 171 172 edSupport.desEnv().markModified(); 173 174 assertTrue(edSupport.messageName().indexOf('*') != -1); 175 assertTrue(edSupport.messageHtmlName().indexOf('*') != -1); 176 } 177 178 private void doGetOpenedPanesWorksAfterDeserialization (int size) throws Exception { 179 support().open (); 180 181 waitEQ (); 182 183 CloneableEditor ed = (CloneableEditor)support().getRef ().getAnyComponent (); 184 185 JEditorPane [] panes = getPanes(); 186 assertNotNull (panes); 187 assertEquals ("One is there", 1, panes.length); 188 189 NbMarshalledObject obj = new NbMarshalledObject (ed); 190 ed.close (); 191 192 panes = getPanes(); 193 assertNull ("No panes anymore", panes); 194 195 DataObject oldObj = DataObject.find (fileObject); 196 oldObj.setValid (false); 197 198 expectedSize = size; 199 200 ed = (CloneableEditor)obj.get (); 201 202 DataObject newObj = DataObject.find (fileObject); 203 204 if (oldObj == newObj) { 205 fail ("Object should not be the same, new one shall be created after marking the old invalid"); 206 } 207 208 panes = getPanes (); 209 assertNotNull ("One again", panes); 210 assertEquals ("One is there again", 1, panes.length); 211 } 212 213 private JEditorPane [] getPanes() throws Exception { 214 return Mutex.EVENT.readAccess(new Mutex.ExceptionAction<JEditorPane []>() { 215 public JEditorPane [] run() throws Exception { 216 return support().getOpenedPanes (); 217 } 218 }); 219 } 220 221 public void testEnvOutputStreamTakesLock() throws Exception { 222 DataEditorSupport.Env env = (DataEditorSupport.Env)support().desEnv(); 223 assertNull(env.fileLock); 224 OutputStream stream = env.outputStream(); 225 assertNotNull(stream); 226 stream.close(); 227 assertNotNull(env.fileLock); 228 env.fileLock.releaseLock(); 229 } 230 231 234 private static final class MyFileObject extends org.openide.filesystems.FileObject { 235 private org.openide.filesystems.FileObject delegate; 236 237 public MyFileObject (org.openide.filesystems.FileObject del) { 238 delegate = del; 239 } 240 241 public java.io.OutputStream getOutputStream (FileLock lock) throws IOException { 242 class ContentStream extends java.io.ByteArrayOutputStream { 243 public void close () throws java.io.IOException { 244 super.close (); 245 RUNNING.content = new String (toByteArray ()); 246 } 247 } 248 249 return new ContentStream (); 250 } 251 252 public void delete (FileLock lock) throws IOException { 253 delegate.delete (lock); 254 } 255 256 public void setImportant (boolean b) { 257 delegate.setImportant (b); 258 } 259 260 public void addFileChangeListener (org.openide.filesystems.FileChangeListener fcl) { 261 delegate.addFileChangeListener (fcl); 262 } 263 264 public void removeFileChangeListener (org.openide.filesystems.FileChangeListener fcl) { 265 delegate.removeFileChangeListener (fcl); 266 } 267 268 public Object getAttribute (String attrName) { 269 return delegate.getAttribute (attrName); 270 } 271 272 public FileObject createFolder (String name) throws IOException { 273 throw new IOException ("Not supported"); 274 } 275 276 public void rename (FileLock lock, String name, String ext) throws IOException { 277 throw new IOException ("Not supported"); 278 } 279 280 public void setAttribute (String attrName, Object value) throws IOException { 281 delegate.setAttribute (attrName, value); 282 } 283 284 public String getName () { 285 return delegate.getName (); 286 } 287 288 public java.io.InputStream getInputStream () throws java.io.FileNotFoundException { 289 return new java.io.ByteArrayInputStream (RUNNING.content.getBytes ()); 290 } 291 292 public FileSystem getFileSystem () throws FileStateInvalidException { 293 return delegate.getFileSystem (); 294 } 295 296 public FileObject getFileObject (String name, String ext) { 297 return null; 298 } 299 300 public String getExt () { 301 return delegate.getExt (); 302 } 303 304 public FileObject[] getChildren () { 305 return null; 306 } 307 308 public java.util.Enumeration getAttributes () { 309 return delegate.getAttributes (); 310 } 311 312 public FileObject createData (String name, String ext) throws IOException { 313 throw new IOException ("Not supported"); 314 } 315 316 public FileObject getParent () { 317 return delegate.getParent (); 318 } 319 320 public long getSize () { 321 return RUNNING.expectedSize; 322 } 323 324 public boolean isData () { 325 return true; 326 } 327 328 public boolean isFolder () { 329 return false; 330 } 331 332 public boolean isReadOnly () { 333 return false; 334 } 335 336 public boolean isRoot () { 337 return false; 338 } 339 340 public boolean isValid () { 341 return delegate.isValid (); 342 } 343 344 public java.util.Date lastModified () { 345 return RUNNING.date; 346 } 347 348 public FileLock lock () throws IOException { 349 return delegate.lock (); 350 } 351 352 public Object writeReplace () { 353 return new Replace (); 354 } 355 } 356 357 private static final class Replace extends Object implements java.io.Serializable { 358 static final long serialVersionUID = 2L; 359 360 public Object readResolve () { 361 return RUNNING.fileObject; 362 } 363 } 364 365 366 private static final class DES extends DataEditorSupport 367 implements OpenCookie, EditCookie { 368 public DES (DataObject obj, Env env) { 369 super (obj, env); 370 } 371 372 public org.openide.windows.CloneableTopComponent.Ref getRef () { 373 return allEditors; 374 } 375 376 } 377 378 379 private static final class MyEnv extends DataEditorSupport.Env { 380 static final long serialVersionUID = 1L; 381 382 public MyEnv (DataObject obj) { 383 super (obj); 384 } 385 386 protected FileObject getFile () { 387 return super.getDataObject ().getPrimaryFile (); 388 } 389 390 protected FileLock takeLock () throws IOException { 391 return super.getDataObject ().getPrimaryFile ().lock (); 392 } 393 394 } 395 396 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 397 public Lkp () { 398 this (new org.openide.util.lookup.InstanceContent ()); 399 } 400 401 private Lkp (org.openide.util.lookup.InstanceContent ic) { 402 super (ic); 403 404 ic.add (new Pool ()); 405 } 406 407 } 409 private static final class Pool extends org.openide.loaders.DataLoaderPool { 410 protected java.util.Enumeration loaders () { 411 return org.openide.util.Enumerations.singleton(MyLoader.get ()); 412 } 413 } 414 415 public static final class MyLoader extends org.openide.loaders.UniFileLoader { 416 public int primary; 417 418 public static MyLoader get () { 419 return (MyLoader)MyLoader.findObject (MyLoader.class, true); 420 } 421 422 public MyLoader() { 423 super(MyDataObject.class.getName ()); 424 getExtensions ().addExtension ("obj"); 425 } 426 protected String displayName() { 427 return "MyPart"; 428 } 429 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 430 return new MyDataObject(this, primaryFile); 431 } 432 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 433 primary++; 434 return new org.openide.loaders.FileEntry (obj, primaryFile); 435 } 436 } 437 public static final class MyDataObject extends MultiDataObject 438 implements CookieSet.Factory { 439 public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException { 440 super(folder, l); 441 getCookieSet ().add (OpenCookie.class, this); 442 } 443 444 public org.openide.nodes.Node.Cookie createCookie (Class klass) { 445 return new DES (this, new MyEnv (this)); 446 } 447 448 protected Node createNodeDelegate() { 449 return new MyNode(this, Children.LEAF); 450 } 451 } 452 453 454 public static final class MyNode extends DataNode { 455 456 public MyNode (DataObject obj, Children ch) { 457 super(obj, ch); 458 } 459 460 public String getHtmlDisplayName() { 461 return "<b>" + getDisplayName() + "</b>"; 462 } 463 } 464 465 } 466 | Popular Tags |