1 19 20 package org.openide.text; 21 import java.lang.reflect.InvocationTargetException ; 22 import javax.swing.JEditorPane ; 23 import javax.swing.text.Document ; 24 import junit.framework.*; 25 26 import org.netbeans.junit.*; 27 import org.openide.DialogDescriptor; 28 import org.openide.cookies.EditorCookie; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.filesystems.LocalFileSystem; 32 import org.openide.loaders.DataObject; 33 import org.openide.util.lookup.*; 34 import org.openide.util.Mutex; 35 36 40 public class ExternalDeleteOfModifiedFileTest extends NbTestCase { 41 private DataObject obj; 42 private EditorCookie edit; 43 44 45 public ExternalDeleteOfModifiedFileTest (java.lang.String testName) { 46 super(testName); 47 } 48 49 public static Test suite() { 50 TestSuite suite = new NbTestSuite(ExternalDeleteOfModifiedFileTest.class); 51 return suite; 52 } 53 54 55 protected void setUp () throws Exception { 56 System.setProperty ("org.openide.util.Lookup", "org.openide.text.ExternalDeleteOfModifiedFileTest$Lkp"); 57 58 59 clearWorkDir(); 60 LocalFileSystem fs = new LocalFileSystem(); 61 fs.setRootDirectory(getWorkDir()); 62 63 FileObject fo = fs.getRoot().createData("Ahoj", "txt"); 64 65 obj = DataObject.find(fo); 66 edit = (EditorCookie)obj.getCookie(EditorCookie.class); 67 assertNotNull("we have editor", edit); 68 } 69 70 public void testModifyTheFileAndThenPreventItToBeSavedOnFileDisappear() throws Exception { 71 Document doc = edit.openDocument(); 72 73 doc.insertString(0, "Ahoj", null); 74 assertTrue("Modified", edit.isModified()); 75 76 edit.open(); 77 waitEQ(); 78 79 JEditorPane [] arr = getPanes(); 80 assertNotNull("There is one opened pane", arr); 81 82 java.awt.Component c = arr[0]; 83 while (!(c instanceof CloneableEditor)) { 84 c = c.getParent(); 85 } 86 CloneableEditor ce = (CloneableEditor)c; 87 88 DD.toReturn = DialogDescriptor.CANCEL_OPTION; 90 91 java.io.File f = FileUtil.toFile(obj.getPrimaryFile()); 92 assertNotNull("There is file behind the fo", f); 93 f.delete(); 94 obj.getPrimaryFile().getParent().refresh(); 95 96 waitEQ(); 97 98 assertNotNull ("Text message was there", DD.message); 99 assertEquals("Ok/cancel type", DialogDescriptor.OK_CANCEL_OPTION, DD.type); 100 101 String txt = doc.getText(0, doc.getLength()); 102 assertEquals("The right text is there", txt, "Ahoj"); 103 104 arr = getPanes(); 105 assertNotNull("Panes are still open", arr); 106 assertTrue("Document is remains modified", edit.isModified()); 107 108 DD.clear(DialogDescriptor.OK_OPTION); 111 112 ce.close(); 113 waitEQ(); 114 115 arr = getPanes(); 116 assertNull("Now everything is closed", arr); 117 } 118 119 private JEditorPane [] getPanes() { 120 return Mutex.EVENT.readAccess(new Mutex.Action<JEditorPane []>() { 121 public JEditorPane [] run() { 122 return edit.getOpenedPanes(); 123 } 124 }); 125 } 126 127 private void waitEQ() throws InterruptedException , java.lang.reflect.InvocationTargetException { 128 javax.swing.SwingUtilities.invokeAndWait(new Runnable () { 129 public void run () { 130 } 131 }); 132 } 133 134 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 138 public Lkp () { 139 this (new org.openide.util.lookup.InstanceContent ()); 140 } 141 142 private Lkp (org.openide.util.lookup.InstanceContent ic) { 143 super (ic); 144 ic.add (new DD ()); 145 } 146 } 147 148 150 private static final class DD extends org.openide.DialogDisplayer { 151 public static Object [] options; 152 public static Object toReturn; 153 public static Object message; 154 public static int type = -1; 155 156 public static void clear(Object t) { 157 type = -1; 158 message = null; 159 options = null; 160 toReturn = t; 161 } 162 163 public java.awt.Dialog createDialog(org.openide.DialogDescriptor descriptor) { 164 throw new IllegalStateException ("Not implemented"); 165 } 166 167 public Object notify(org.openide.NotifyDescriptor descriptor) { 168 assertNull (options); 169 if (type != -1) { 170 fail("Second question: " + type); 171 } 172 if (toReturn == null) { 173 fail("Not specified what we shall return: " + toReturn); 174 } 175 options = descriptor.getOptions(); 176 message = descriptor.getMessage(); 177 type = descriptor.getOptionType(); 178 Object r = toReturn; 179 toReturn = null; 180 return r; 181 } 182 183 } 185 } 186 | Popular Tags |