1 19 20 package org.openide.text; 21 22 import java.io.PrintStream ; 23 import javax.swing.Action ; 24 import junit.textui.TestRunner; 25 import org.netbeans.junit.*; 26 import org.openide.DialogDescriptor; 27 import org.openide.cookies.EditorCookie; 28 import org.openide.cookies.OpenCookie; 29 import org.openide.filesystems.*; 30 import org.openide.loaders.DataObject; 31 import org.openide.util.actions.SystemAction; 32 33 38 public final class SimpleDESTest extends NbTestCase { 39 40 private FileSystem lfs; 41 private DataObject obj; 42 43 44 public SimpleDESTest(String name) { 45 super(name); 46 } 47 48 protected void setUp() throws java.lang.Exception { 49 clearWorkDir (); 50 51 System.setProperty("org.openide.util.Lookup", "org.openide.text.SimpleDESTest$Lkp"); 52 super.setUp(); 53 54 LocalFileSystem l = new LocalFileSystem (); 55 l.setRootDirectory (getWorkDir ()); 56 lfs = l; 57 58 FileObject fo = FileUtil.createData (lfs.getRoot (), "AA/" + getName () + ".test"); 59 assertNotNull("file not found", fo); 60 obj = DataObject.find(fo); 61 62 assertEquals ("The right class", obj.getClass (), SO.class); 63 } 64 65 public void testHasEditorCookieForResonableContentOfFiles () throws Exception { 66 doCookieCheck (true); 67 } 68 69 private void doCookieCheck (boolean hasEditCookie) throws Exception { 70 EditorCookie c = tryToOpen ( 71 "Ahoj Jardo," + 72 "how are you" + 73 "\t\n\rBye" 74 ); 75 assertNotNull (c); 76 77 assertEquals ( 78 "Next questions results in the same cookie", 79 c, 80 obj.getCookie(EditorCookie.class) 81 ); 82 assertEquals ( 83 "Print cookie is provided", 84 c, 85 obj.getCookie(org.openide.cookies.PrintCookie.class) 86 ); 87 assertEquals ( 88 "CloseCookie as well", 89 c, 90 obj.getCookie(org.openide.cookies.CloseCookie.class) 91 ); 92 93 if (hasEditCookie) { 94 assertEquals ( 95 "EditCookie as well", 96 c, 97 obj.getCookie(org.openide.cookies.EditCookie.class) 98 ); 99 } else { 100 assertNull ( 101 "No EditCookie", 102 obj.getCookie(org.openide.cookies.EditCookie.class) 103 ); 104 105 } 106 107 OpenCookie open = (OpenCookie)obj.getCookie (OpenCookie.class); 108 open.open (); 109 110 javax.swing.text.Document d = c.getDocument(); 111 assertNotNull (d); 112 113 d.insertString(0, "Kuk", null); 114 115 assertNotNull ( 116 "Now there is a save cookie", 117 obj.getCookie (org.openide.cookies.SaveCookie.class) 118 ); 119 } 120 121 public void testItIsPossibleToMaskEditCookie () throws Exception { 122 doCookieCheck (false); 123 } 124 125 private EditorCookie tryToOpen (String content) throws Exception { 126 FileObject fo = obj.getPrimaryFile(); 127 FileLock lock = fo.lock(); 128 PrintStream os = new PrintStream (fo.getOutputStream(lock)); 129 os.print (content); 130 os.close (); 131 lock.releaseLock(); 132 133 return (EditorCookie)obj.getCookie (EditorCookie.class); 134 } 135 136 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 140 public Lkp () { 141 this (new org.openide.util.lookup.InstanceContent ()); 142 } 143 144 private Lkp (org.openide.util.lookup.InstanceContent ic) { 145 super (ic); 146 ic.add (new DLP ()); 147 } 148 } 149 150 private static final class SL extends org.openide.loaders.UniFileLoader { 151 public SL () { 152 super (SO.class.getName ()); 153 getExtensions().addExtension("test"); 154 } 155 protected org.openide.loaders.MultiDataObject createMultiObject(FileObject primaryFile) throws org.openide.loaders.DataObjectExistsException, java.io.IOException { 156 return new SO (primaryFile); 157 } 158 } 160 private static final class SO extends org.openide.loaders.MultiDataObject implements org.openide.nodes.CookieSet.Factory { 161 private org.openide.nodes.Node.Cookie cookie = (org.openide.nodes.Node.Cookie)DataEditorSupport.create(this, getPrimaryEntry(), getCookieSet ()); 162 163 164 public SO (FileObject fo) throws org.openide.loaders.DataObjectExistsException { 165 super (fo, (SL)SL.getLoader(SL.class)); 166 167 if (fo.getNameExt().indexOf ("MaskEdit") == -1) { 168 getCookieSet ().add (cookie); 169 } else { 170 getCookieSet ().add (new Class [] { 171 OpenCookie.class, 172 org.openide.cookies.CloseCookie.class, EditorCookie.class, 173 org.openide.cookies.PrintCookie.class 174 }, this); 175 } 176 } 177 178 179 public org.openide.nodes.Node.Cookie createCookie (Class c) { 180 return cookie; 181 } 182 } 184 private static final class DLP extends org.openide.loaders.DataLoaderPool { 185 protected java.util.Enumeration loaders() { 186 return java.util.Collections.enumeration( 187 java.util.Collections.singleton( 188 SL.getLoader (SL.class) 189 ) 190 ); 191 } 192 } } 194 | Popular Tags |