1 19 20 21 package org.openide.text; 22 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.lang.reflect.Method ; 27 import java.util.Collections ; 28 import java.util.HashSet ; 29 import java.util.Set ; 30 import javax.swing.SwingUtilities ; 31 import javax.swing.text.BadLocationException ; 32 import javax.swing.text.Document ; 33 import javax.swing.text.Position ; 34 import javax.swing.text.StyledDocument ; 35 36 import junit.textui.TestRunner; 37 38 import org.netbeans.junit.NbTestCase; 39 import org.netbeans.junit.NbTestSuite; 40 import org.openide.actions.*; 41 import org.openide.cookies.CloseCookie; 42 import org.openide.cookies.EditCookie; 43 44 import org.openide.cookies.EditorCookie; 45 import org.openide.cookies.OpenCookie; 46 import org.openide.cookies.PrintCookie; 47 import org.openide.cookies.SaveCookie; 48 import org.openide.filesystems.FileLock; 49 import org.openide.filesystems.FileObject; 50 import org.openide.filesystems.FileStateInvalidException; 51 import org.openide.filesystems.FileSystem; 52 import org.openide.filesystems.FileUtil; 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.io.NbMarshalledObject; 68 import org.openide.util.Lookup; 69 import org.openide.util.LookupListener; 70 import org.openide.util.NbBundle; 71 import org.openide.util.actions.SystemAction; 72 import org.openide.windows.CloneableOpenSupport; 73 import org.openide.windows.WindowManager; 74 75 76 78 public class PeterZMoveTest extends NbTestCase { 79 String content = ""; 81 long expectedSize = -1; 82 java.util.Date date = new java.util.Date (); 83 84 FileObject fileObject; 85 org.openide.filesystems.FileSystem fs; 86 static PeterZMoveTest RUNNING; 87 static { 88 System.setProperty ("org.openide.util.Lookup", "org.openide.text.PeterZMoveTest$Lkp"); 89 } 90 91 public PeterZMoveTest(String s) { 92 super(s); 93 } 94 95 protected void setUp () throws Exception { 96 RUNNING = this; 97 98 fs = org.openide.filesystems.FileUtil.createMemoryFileSystem (); 99 org.openide.filesystems.Repository.getDefault ().addFileSystem (fs); 100 org.openide.filesystems.FileObject root = fs.getRoot (); 101 fileObject = org.openide.filesystems.FileUtil.createData (root, "my.obj"); 102 } 103 104 protected void tearDown () throws Exception { 105 waitEQ (); 106 107 RUNNING = null; 108 org.openide.filesystems.Repository.getDefault ().removeFileSystem (fs); 109 } 110 111 protected boolean runInEQ() { 112 return false; 113 } 114 115 private void waitEQ () throws Exception { 116 javax.swing.SwingUtilities.invokeAndWait (new Runnable () { public void run () { } }); 117 } 118 119 DES support () throws Exception { 120 DataObject obj = DataObject.find (fileObject); 121 122 assertEquals ("My object was created", MyDataObject.class, obj.getClass ()); 123 Object cookie = obj.getCookie (org.openide.cookies.OpenCookie.class); 124 assertNotNull ("Our object has this cookie", cookie); 125 assertEquals ("It is my cookie", DES.class, cookie.getClass ()); 126 127 return (DES)cookie; 128 } 129 130 131 private DataObject obj; 132 public void testWhenMovingAFileNoLockshallBetaken () throws Exception { 133 DES sup = support (); 134 assertFalse ("It is closed now", support ().isDocumentLoaded ()); 135 136 Lookup lkp = sup.getLookup (); 137 obj = (DataObject)lkp.lookup (DataObject.class); 138 assertNotNull ("DataObject found", obj); 139 140 Document d = sup.openDocument (); 141 assertTrue ("It is open now", support ().isDocumentLoaded ()); 142 143 d.insertString(0, "Ahoj", null); 144 145 assertTrue("Really modified", sup.isModified()); 146 147 sup.saveDocument(); 148 149 FileObject fo = FileUtil.createFolder(obj.getFolder().getPrimaryFile(), "newfold"); 150 DataFolder nf = DataFolder.findFolder(fo); 151 152 obj.move(nf); 153 154 FileLock lock = obj.getPrimaryFile().lock(); 155 assertNotNull("It is possible to take another lock", lock); 156 } 157 158 159 private static final class DES extends DataEditorSupport 160 implements OpenCookie, EditCookie { 161 public DES (DataObject obj, Env env) { 162 super (obj, env); 163 } 164 165 public org.openide.windows.CloneableTopComponent.Ref getRef () { 166 return allEditors; 167 } 168 169 } 170 171 172 private static final class MyEnv extends DataEditorSupport.Env { 173 static final long serialVersionUID = 1L; 174 175 public MyEnv (DataObject obj) { 176 super (obj); 177 } 178 179 protected FileObject getFile () { 180 return super.getDataObject ().getPrimaryFile (); 181 } 182 183 protected FileLock takeLock () throws IOException { 184 return super.getDataObject ().getPrimaryFile ().lock (); 185 } 186 187 } 188 189 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 190 public Lkp () { 191 this (new org.openide.util.lookup.InstanceContent ()); 192 } 193 194 private Lkp (org.openide.util.lookup.InstanceContent ic) { 195 super (ic); 196 197 ic.add (new Pool ()); 198 } 199 200 } 202 private static final class Pool extends org.openide.loaders.DataLoaderPool { 203 protected java.util.Enumeration loaders () { 204 return org.openide.util.Enumerations.singleton(MyLoader.get ()); 205 } 206 } 207 208 public static final class MyLoader extends org.openide.loaders.UniFileLoader { 209 public int primary; 210 211 public static MyLoader get () { 212 return (MyLoader)MyLoader.findObject (MyLoader.class, true); 213 } 214 215 public MyLoader() { 216 super(MyDataObject.class.getName ()); 217 getExtensions ().addExtension ("obj"); 218 } 219 protected String displayName() { 220 return "MyPart"; 221 } 222 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 223 return new MyDataObject(this, primaryFile); 224 } 225 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 226 primary++; 227 return new org.openide.loaders.FileEntry (obj, primaryFile); 228 } 229 } 230 public static final class MyDataObject extends MultiDataObject 231 implements CookieSet.Factory { 232 public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException { 233 super(folder, l); 234 getCookieSet ().add (OpenCookie.class, this); 235 } 236 237 public org.openide.nodes.Node.Cookie createCookie (Class klass) { 238 return new DES (this, new MyEnv (this)); 239 } 240 241 242 243 } 244 245 } 246 | Popular Tags |