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.text.BadLocationException ; 33 import javax.swing.text.Document ; 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.ErrorManager; 42 import org.openide.ErrorManager.Annotation; 43 import org.openide.actions.*; 44 import org.openide.cookies.CloseCookie; 45 import org.openide.cookies.EditCookie; 46 47 import org.openide.cookies.EditorCookie; 48 import org.openide.cookies.OpenCookie; 49 import org.openide.cookies.PrintCookie; 50 import org.openide.cookies.SaveCookie; 51 import org.openide.filesystems.FileLock; 52 import org.openide.filesystems.FileObject; 53 import org.openide.filesystems.FileStateInvalidException; 54 import org.openide.filesystems.FileSystem; 55 import org.openide.filesystems.Repository; 56 import org.openide.loaders.DataFolder; 57 import org.openide.loaders.DataNode; 58 import org.openide.loaders.DataObject; 59 import org.openide.loaders.DataObjectExistsException; 60 import org.openide.loaders.ExtensionList; 61 import org.openide.loaders.MultiDataObject; 62 import org.openide.loaders.MultiFileLoader; 63 import org.openide.loaders.UniFileLoader; 64 import org.openide.nodes.Children; 65 import org.openide.nodes.CookieSet; 66 import org.openide.nodes.Node; 67 import org.openide.text.CloneableEditorSupport; 68 import org.openide.util.HelpCtx; 69 import org.openide.util.io.NbMarshalledObject; 70 import org.openide.util.Lookup; 71 import org.openide.util.LookupListener; 72 import org.openide.util.NbBundle; 73 import org.openide.util.actions.SystemAction; 74 import org.openide.windows.CloneableOpenSupport; 75 import org.openide.windows.WindowManager; 76 77 78 80 public class EvenIfReadonlyItNeedsToThrowExceptionTest extends NbTestCase { 81 String content = ""; 83 long expectedSize = -1; 84 java.util.Date date = new java.util.Date (); 85 86 MyFileObject fileObject; 87 org.openide.filesystems.FileSystem fs; 88 static { 89 System.setProperty ("org.openide.util.Lookup", "org.openide.text.EvenIfReadonlyItNeedsToThrowExceptionTest$Lkp"); 90 } 91 92 public EvenIfReadonlyItNeedsToThrowExceptionTest(String s) { 93 super(s); 94 } 95 96 protected void setUp () throws Exception { 97 fs = org.openide.filesystems.FileUtil.createMemoryFileSystem (); 98 org.openide.filesystems.Repository.getDefault ().addFileSystem (fs); 99 org.openide.filesystems.FileObject root = fs.getRoot (); 100 fileObject = new MyFileObject (org.openide.filesystems.FileUtil.createData (root, "my.obj")); 101 } 102 103 protected void tearDown () throws Exception { 104 waitEQ (); 105 org.openide.filesystems.Repository.getDefault ().removeFileSystem (fs); 106 } 107 108 protected boolean runInEQ() { 109 return false; 110 } 111 112 private void waitEQ () throws Exception { 113 javax.swing.SwingUtilities.invokeAndWait (new Runnable () { public void run () { } }); 114 } 115 116 DES support () throws Exception { 117 DataObject obj = DataObject.find (fileObject); 118 119 assertEquals ("My object was created", MyDataObject.class, obj.getClass ()); 120 Object cookie = obj.getCookie (org.openide.cookies.OpenCookie.class); 121 assertNotNull ("Our object has this cookie", cookie); 122 assertEquals ("It is my cookie", DES.class, cookie.getClass ()); 123 124 return (DES)cookie; 125 } 126 127 public void testSaveThrowsException() throws IOException , BadLocationException , Exception { 128 129 fileObject.canWrite = true; 130 131 DES des = support(); 132 des.open(); 133 waitEQ(); 134 135 Document doc = des.openDocument(); 136 137 doc.insertString(0, "Ahoj", null); 138 139 assertTrue("Now it is modified", des.isModified()); 140 141 fileObject.canWrite = false; 142 143 try { 144 des.saveDocument(); 145 fail("This has to throw exception"); 146 } catch (IOException ex) { 147 ErrorManager.Annotation[] ann = ErrorManager.getDefault().findAnnotations(ex); 148 assertNotNull("There are annotations", ann); 149 } 150 } 151 152 153 156 private static final class MyFileObject extends org.openide.filesystems.FileObject { 157 private org.openide.filesystems.FileObject delegate; 158 public boolean canWrite; 159 public String content; 160 161 public MyFileObject (org.openide.filesystems.FileObject del) { 162 delegate = del; 163 } 164 165 public java.io.OutputStream getOutputStream (FileLock lock) throws IOException { 166 class ContentStream extends java.io.ByteArrayOutputStream { 167 public void close () throws java.io.IOException { 168 super.close (); 169 content = new String (toByteArray ()); 170 } 171 } 172 173 return new ContentStream (); 174 } 175 176 public void delete (FileLock lock) throws IOException { 177 delegate.delete (lock); 178 } 179 180 public void setImportant (boolean b) { 181 delegate.setImportant (b); 182 } 183 184 public void addFileChangeListener (org.openide.filesystems.FileChangeListener fcl) { 185 delegate.addFileChangeListener (fcl); 186 } 187 188 public void removeFileChangeListener (org.openide.filesystems.FileChangeListener fcl) { 189 delegate.removeFileChangeListener (fcl); 190 } 191 192 public Object getAttribute (String attrName) { 193 return delegate.getAttribute (attrName); 194 } 195 196 public FileObject createFolder (String name) throws IOException { 197 throw new IOException ("Not supported"); 198 } 199 200 public void rename (FileLock lock, String name, String ext) throws IOException { 201 throw new IOException ("Not supported"); 202 } 203 204 public void setAttribute (String attrName, Object value) throws IOException { 205 delegate.setAttribute (attrName, value); 206 } 207 208 public String getName () { 209 return delegate.getName (); 210 } 211 212 public java.io.InputStream getInputStream () throws java.io.FileNotFoundException { 213 return new java.io.ByteArrayInputStream (new byte[0]); 214 } 215 216 public FileSystem getFileSystem () throws FileStateInvalidException { 217 return delegate.getFileSystem (); 218 } 219 220 public FileObject getFileObject (String name, String ext) { 221 return null; 222 } 223 224 public String getExt () { 225 return delegate.getExt (); 226 } 227 228 public FileObject[] getChildren () { 229 return null; 230 } 231 232 public java.util.Enumeration getAttributes () { 233 return delegate.getAttributes (); 234 } 235 236 public FileObject createData (String name, String ext) throws IOException { 237 throw new IOException ("Not supported"); 238 } 239 240 public FileObject getParent () { 241 return delegate.getParent (); 242 } 243 244 public long getSize () { 245 return 0; 246 } 247 248 public boolean isData () { 249 return true; 250 } 251 252 public boolean isFolder () { 253 return false; 254 } 255 256 public boolean isReadOnly () { 257 return !canWrite; 258 } 259 260 public boolean isRoot () { 261 return false; 262 } 263 264 public boolean isValid () { 265 return delegate.isValid (); 266 } 267 268 public java.util.Date lastModified () { 269 return new java.util.Date (); 270 } 271 272 public FileLock lock () throws IOException { 273 return delegate.lock (); 274 } 275 276 public boolean canWrite() { 277 return canWrite; 278 } 279 } 280 281 282 private static final class DES extends DataEditorSupport 283 implements OpenCookie, EditCookie { 284 public DES (DataObject obj, Env env) { 285 super (obj, env); 286 } 287 288 public org.openide.windows.CloneableTopComponent.Ref getRef () { 289 return allEditors; 290 } 291 292 } 293 294 295 private static final class MyEnv extends DataEditorSupport.Env { 296 static final long serialVersionUID = 1L; 297 298 public MyEnv (DataObject obj) { 299 super (obj); 300 } 301 302 protected FileObject getFile () { 303 return super.getDataObject ().getPrimaryFile (); 304 } 305 306 protected FileLock takeLock () throws IOException { 307 return super.getDataObject ().getPrimaryFile ().lock (); 308 } 309 310 } 311 312 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 313 public Lkp () { 314 this (new org.openide.util.lookup.InstanceContent ()); 315 } 316 317 private Lkp (org.openide.util.lookup.InstanceContent ic) { 318 super (ic); 319 320 ic.add (new Pool ()); 321 } 322 323 } 325 private static final class Pool extends org.openide.loaders.DataLoaderPool { 326 protected java.util.Enumeration loaders () { 327 return org.openide.util.Enumerations.singleton(MyLoader.get ()); 328 } 329 } 330 331 public static final class MyLoader extends org.openide.loaders.UniFileLoader { 332 public int primary; 333 334 public static MyLoader get () { 335 return (MyLoader)MyLoader.findObject (MyLoader.class, true); 336 } 337 338 public MyLoader() { 339 super(MyDataObject.class.getName ()); 340 getExtensions ().addExtension ("obj"); 341 } 342 protected String displayName() { 343 return "MyPart"; 344 } 345 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 346 return new MyDataObject(this, primaryFile); 347 } 348 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 349 primary++; 350 return new org.openide.loaders.FileEntry (obj, primaryFile); 351 } 352 } 353 public static final class MyDataObject extends MultiDataObject 354 implements CookieSet.Factory { 355 public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException { 356 super(folder, l); 357 getCookieSet ().add (OpenCookie.class, this); 358 } 359 360 public org.openide.nodes.Node.Cookie createCookie (Class klass) { 361 return new DES (this, new MyEnv (this)); 362 } 363 364 protected Node createNodeDelegate() { 365 return new MyNode(this, Children.LEAF); 366 } 367 } 368 369 370 public static final class MyNode extends DataNode { 371 372 public MyNode (DataObject obj, Children ch) { 373 super(obj, ch); 374 } 375 376 public String getHtmlDisplayName() { 377 return "<b>" + getDisplayName() + "</b>"; 378 } 379 } 380 381 } 382 | Popular Tags |