1 19 20 package org.openide.loaders; 21 22 import java.awt.Dialog ; 23 import java.io.PrintStream ; 24 import javax.swing.JEditorPane ; 25 import javax.swing.SwingUtilities ; 26 import javax.swing.text.Document ; 27 import org.openide.DialogDescriptor; 28 import org.openide.DialogDisplayer; 29 import org.openide.NotifyDescriptor; 30 import org.openide.cookies.CloseCookie; 31 import org.openide.cookies.EditCookie; 32 import org.openide.cookies.EditorCookie; 33 import org.openide.cookies.OpenCookie; 34 import org.openide.cookies.PrintCookie; 35 import org.openide.cookies.SaveCookie; 36 import org.openide.filesystems.FileLock; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileSystem; 39 import org.openide.filesystems.Repository; 40 import org.openide.util.Lookup; 41 import org.openide.util.Mutex; 42 import org.openide.util.io.NbMarshalledObject; 43 import org.openide.windows.TopComponent; 44 45 50 public final class DefaultDataObjectHasOpenTest extends LoggingTestCaseHid { 51 52 private FileSystem lfs; 53 private DataObject obj; 54 55 public DefaultDataObjectHasOpenTest(String name) { 56 super(name); 57 } 58 59 protected void setUp() throws Exception { 60 super.setUp(); 61 62 registerIntoLookup(new DD()); 63 64 DD x = (DD)Lookup.getDefault().lookup(DD.class); 65 assertNotNull("DD is there", x); 66 67 String fsstruct [] = new String [] { 68 "AA/a.test" 69 }; 70 71 TestUtilHid.destroyLocalFileSystem(getName()); 72 lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct); 73 Repository.getDefault().addFileSystem(lfs); 74 75 FileObject fo = lfs.findResource("AA/a.test"); 76 assertNotNull("file not found", fo); 77 obj = DataObject.find(fo); 78 79 assertEquals("The right class", obj.getClass(), DefaultDataObject.class); 80 81 assertFalse("Designed to run outside of AWT", SwingUtilities.isEventDispatchThread()); 82 DD.options = null; 83 DD.disableTest = false; 84 } 85 86 private void waitForAWT() throws Exception { 87 Mutex.EVENT.readAccess(new Mutex.Action() { 89 public Object run() { 90 return null; 91 } 92 }); 93 } 94 95 protected void tearDown() throws Exception { 96 waitForAWT(); 97 DD.disableTest = true; 98 99 super.tearDown(); 100 if (obj != null) { 101 CloseCookie cc; 102 cc = (CloseCookie)obj.getCookie(CloseCookie.class); 103 if (cc != null) { 104 DD.toReturn = NotifyDescriptor.NO_OPTION; 105 cc.close(); 106 } 107 } 108 Repository.getDefault().removeFileSystem(lfs); 109 110 waitForAWT(); 111 } 112 113 public void testHasOpenCookie() { 114 assertNotNull(obj.getCookie(OpenCookie.class)); 115 } 116 117 public void testHasEditCookie() { 118 assertNotNull(obj.getCookie(EditCookie.class)); 119 } 120 121 public void testHasEditorCookieForResonableContentOfFiles() throws Exception { 122 EditorCookie c = tryToOpen( 123 "Ahoj Jardo," + 124 "how are you" + 125 "\t\n\rBye.\u00a9\u00e1\u00e9\u00ed\u00f3\u00fa" 126 ); 127 assertNotNull(c); 128 129 doRegularCheck(c); 130 } 131 132 private void doRegularCheck(EditorCookie c) throws Exception { 133 assertEquals( 134 "Next questions results in the same cookie", 135 c, 136 obj.getCookie(EditorCookie.class) 137 ); 138 assertEquals( 139 "Print cookie is provided", 140 c, 141 obj.getCookie(PrintCookie.class) 142 ); 143 assertEquals( 144 "CloseCookie as well", 145 c, 146 obj.getCookie(CloseCookie.class) 147 ); 148 149 OpenCookie open = (OpenCookie)obj.getCookie(OpenCookie.class); 150 open.open(); 151 152 Document d = c.getDocument(); 153 assertNotNull(d); 154 155 d.insertString(0, "Kuk", null); 156 157 assertNotNull( 158 "Now there is a save cookie", 159 obj.getCookie(SaveCookie.class) 160 ); 161 } 162 163 public void testNoEditorCookieForBinaryFiles() throws Exception { 164 EditorCookie c = tryToOpen("\u0003\u0001\u0000"); 165 assertNull(c); 166 } 167 168 public void testOpenAsksAQuestionForBinaryFiles() throws Exception { 169 EditorCookie c = tryToOpen("\u0003\u0001\u0000"); 170 assertNull(c); 171 172 DD.toReturn = DialogDescriptor.CANCEL_OPTION; 173 OpenCookie open = (OpenCookie)obj.getCookie(OpenCookie.class); 174 open.open(); 175 176 assertNotNull("There was a query", DD.options); 177 assertEquals("Yes no options", 2, DD.options.length); 178 assertEquals(DialogDescriptor.OK_OPTION, DD.options[0]); 179 assertEquals(DialogDescriptor.CANCEL_OPTION, DD.options[1]); 180 } 181 182 public void testBinaryFilesQuestionContainsOpenActions() throws Exception { 183 EditorCookie c = tryToOpen("\u0003\u0001\u0000"); 184 assertNull(c); 185 186 DD.toReturn = DialogDescriptor.CLOSED_OPTION; 187 OpenCookie open = (OpenCookie)obj.getCookie(OpenCookie.class); 188 open.open(); 189 190 assertNotNull("There was a query", DD.options); 191 assertEquals("Two options", 2, DD.options.length); 192 assertEquals(DialogDescriptor.OK_OPTION, DD.options[0]); 193 assertEquals(DialogDescriptor.CANCEL_OPTION, DD.options[1]); 194 DD.options = null; 195 196 DD.toReturn = DialogDescriptor.OK_OPTION; 197 open.open(); 198 199 assertNotNull("There was a query", DD.options); 200 assertEquals("Still 2 options", 2, DD.options.length); 201 assertEquals(DialogDescriptor.OK_OPTION, DD.options[0]); 202 assertEquals(DialogDescriptor.CANCEL_OPTION, DD.options[1]); 203 204 c = (EditorCookie)obj.getCookie(EditorCookie.class); 205 assertNotNull("Editor cookie created", c); 206 207 doRegularCheck(c); 208 } 209 210 private EditorCookie tryToOpen(String content) throws Exception { 211 FileObject fo = obj.getPrimaryFile(); 212 FileLock lock = fo.lock(); 213 PrintStream os = new PrintStream (fo.getOutputStream(lock)); 214 os.print(content); 215 os.close(); 216 lock.releaseLock(); 217 218 return (EditorCookie)obj.getCookie(EditorCookie.class); 219 } 220 221 public void testThatTheNameOfDataObjectsNodeContainsTheExtensionIssue18780() throws Exception { 222 String name = obj.getPrimaryFile().getNameExt(); 223 assertEquals("Name contains extension", name, obj.getNodeDelegate().getName()); 224 } 225 226 public void testComponentCanBeSerialized() throws Exception { 227 doComponentCanBeSerialized("Sample\nTest\nTo\nSerialized"); 228 } 229 230 public void testComponentCanBeSerializedEvenItIsBinary() throws Exception { 231 OpenCookie open = (OpenCookie)obj.getCookie(OpenCookie.class); 233 DD.toReturn = DialogDescriptor.OK_OPTION; 234 open.open(); 235 open = null; 236 237 doComponentCanBeSerialized("\u0003\u0001\u0000"); 238 } 239 240 public void testComponentCanBeSerializedEvenItIsBig() throws Exception { 241 OpenCookie open = (OpenCookie)obj.getCookie(OpenCookie.class); 243 DD.toReturn = DialogDescriptor.OK_OPTION; 244 open.open(); 245 open = null; 246 247 FileObject fo = obj.getPrimaryFile(); 248 FileLock lock = fo.lock(); 249 PrintStream os = new PrintStream (fo.getOutputStream(lock)); 250 for (int i = 0; i < 1024 * 1024 / 10; i++) { 251 os.print("Ahoj " + i + "\n"); 252 } 253 os.close(); 254 lock.releaseLock(); 255 assertTrue("Larger than 1Mb", 1024 * 1024 + 1000 < fo.getSize()); 256 257 doComponentCanBeSerialized(null); 258 } 259 260 private void doComponentCanBeSerialized(String txt) throws Exception { 261 final EditorCookie c = (txt != null) ? 262 tryToOpen(txt) : obj.getCookie(EditorCookie.class); 263 assertNotNull(c); 264 c.open(); 265 266 JEditorPane [] arr = Mutex.EVENT.readAccess(new Mutex.Action<JEditorPane []>() { 268 public JEditorPane [] run() { 269 return c.getOpenedPanes(); 270 } 271 }); 272 273 assertNotNull("Something opened", arr); 274 assertEquals("One opened", 1, arr.length); 275 276 Object o = SwingUtilities.getAncestorOfClass(TopComponent.class, arr[0]); 277 assertNotNull("Top component found", o); 278 NbMarshalledObject mar = new NbMarshalledObject(o); 279 280 ((TopComponent)o).close(); 281 282 obj.setValid(false); 283 284 TopComponent tc = (TopComponent)mar.get(); 285 286 assertNotNull("Successfully deserialized", tc); 287 288 if (obj == DataObject.find(obj.getPrimaryFile())) { 289 fail("Strange, obj should be garbage collected" + obj); 290 } 291 } 292 293 295 private static final class DD extends DialogDisplayer { 296 public static Object [] options; 297 public static Object toReturn; 298 public static boolean disableTest; 299 300 public Dialog createDialog(DialogDescriptor descriptor) { 301 throw new IllegalStateException ("Not implemented"); 302 } 303 304 public Object notify(NotifyDescriptor descriptor) { 305 if (disableTest) { 306 return toReturn; 307 } else { 308 assertNull(options); 309 assertNotNull(toReturn); 310 options = descriptor.getOptions(); 311 Object r = toReturn; 312 toReturn = null; 313 return r; 314 } 315 } 316 317 } 319 } 320 | Popular Tags |