1 19 20 package org.openide.text; 21 import java.io.IOException ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.util.Date ; 24 import javax.swing.JComponent ; 25 import javax.swing.JEditorPane ; 26 import javax.swing.text.Document ; 27 import junit.framework.*; 28 29 import org.netbeans.junit.*; 30 31 import org.openide.util.Mutex; 32 import org.openide.util.Lookup; 33 import org.openide.util.lookup.*; 34 35 39 public class NetworkConnectionLostTest extends NbTestCase 40 implements CloneableEditorSupport.Env { 41 42 private CloneableEditorSupport support; 43 44 private InstanceContent ic; 45 46 47 private String content = ""; 49 private boolean valid = true; 50 private boolean modified = false; 51 private java.util.Date date = new java.util.Date (); 52 private java.beans.PropertyChangeSupport propL = new java.beans.PropertyChangeSupport (this); 53 private java.beans.VetoableChangeListener vetoL; 54 private IOException toThrow; 55 56 57 public NetworkConnectionLostTest (java.lang.String testName) { 58 super(testName); 59 } 60 61 public static void main(java.lang.String [] args) { 62 junit.textui.TestRunner.run(suite()); 63 } 64 65 public static Test suite() { 66 TestSuite suite = new NbTestSuite(NetworkConnectionLostTest.class); 67 68 return suite; 69 } 70 71 72 protected void setUp () { 73 System.setProperty ("org.openide.util.Lookup", "org.openide.text.NetworkConnectionLostTest$Lkp"); 74 75 ic = new InstanceContent (); 76 support = new CES (this, new AbstractLookup (ic)); 77 } 78 79 public void testModifyTheFileAndThenPreventItToBeSavedOnClose() throws Exception { 80 Document doc = support.openDocument(); 81 82 doc.insertString(0, "Ahoj", null); 83 assertTrue("Modified", support.isModified()); 84 85 support.open(); 86 waitEQ(); 87 88 JEditorPane [] arr = getPanes(); 89 assertNotNull("There is one opened pane", arr); 90 91 java.awt.Component c = arr[0]; 92 while (!(c instanceof CloneableEditor)) { 93 c = c.getParent(); 94 } 95 CloneableEditor ce = (CloneableEditor)c; 96 97 toThrow = new IOException ("NetworkConnectionLost"); 98 99 DD.toReturn = 0; 101 boolean result = ce.close(); 102 assertFalse("Refused to save due to the exception", result); 103 waitEQ(); 104 105 assertNotNull("There was a question", DD.options); 106 107 String txt = doc.getText(0, doc.getLength()); 108 assertEquals("The right text is there", txt, "Ahoj"); 109 assertEquals("Nothing has been saved", "", content); 110 111 arr = getPanes(); 112 assertNotNull("Panes are still open", arr); 113 } 114 115 private JEditorPane [] getPanes() { 116 return Mutex.EVENT.readAccess(new Mutex.Action<JEditorPane []>() { 117 public JEditorPane [] run() { 118 return support.getOpenedPanes(); 119 } 120 }); 121 } 122 123 124 private void waitEQ() throws InterruptedException , InvocationTargetException { 125 javax.swing.SwingUtilities.invokeAndWait(new Runnable () { 126 public void run () { 127 } 128 }); 129 } 130 134 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 135 propL.addPropertyChangeListener (l); 136 } 137 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 138 propL.removePropertyChangeListener (l); 139 } 140 141 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 142 assertNull ("This is the first veto listener", vetoL); 143 vetoL = l; 144 } 145 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 146 assertEquals ("Removing the right veto one", vetoL, l); 147 vetoL = null; 148 } 149 150 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 151 return support; 152 } 153 154 public String getMimeType() { 155 return "text/plain"; 156 } 157 158 public java.util.Date getTime() { 159 return date; 160 } 161 162 public java.io.InputStream inputStream() throws java.io.IOException { 163 return new java.io.ByteArrayInputStream (content.getBytes ()); 164 } 165 public java.io.OutputStream outputStream() throws java.io.IOException { 166 if (toThrow != null) { 167 throw toThrow; 168 } 169 170 class ContentStream extends java.io.ByteArrayOutputStream { 171 public void close () throws java.io.IOException { 172 super.close (); 173 content = new String (toByteArray ()); 174 } 175 } 176 177 return new ContentStream (); 178 } 179 180 public boolean isValid() { 181 return valid; 182 } 183 184 public boolean isModified() { 185 return modified; 186 } 187 188 public void markModified() throws java.io.IOException { 189 modified = true; 190 } 191 192 public void unmarkModified() { 193 modified = false; 194 } 195 196 197 private static final class CES extends CloneableEditorSupport { 198 public CES (Env env, Lookup l) { 199 super (env, l); 200 } 201 202 protected String messageName() { 203 return "Name"; 204 } 205 206 protected String messageOpened() { 207 return "Opened"; 208 } 209 210 protected String messageOpening() { 211 return "Opening"; 212 } 213 214 protected String messageSave() { 215 return "Save"; 216 } 217 218 protected String messageToolTip() { 219 return "ToolTip"; 220 } 221 222 } 224 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 228 public Lkp () { 229 this (new org.openide.util.lookup.InstanceContent ()); 230 } 231 232 private Lkp (org.openide.util.lookup.InstanceContent ic) { 233 super (ic); 234 ic.add (new DD ()); 235 } 236 } 237 238 240 private static final class DD extends org.openide.DialogDisplayer { 241 public static Object [] options; 242 public static int toReturn = -1; 243 244 public java.awt.Dialog createDialog(org.openide.DialogDescriptor descriptor) { 245 throw new IllegalStateException ("Not implemented"); 246 } 247 248 public Object notify(org.openide.NotifyDescriptor descriptor) { 249 assertNull (options); 250 if (toReturn == -1) { 251 fail("Not specified what we shall return: " + toReturn); 252 } 253 options = descriptor.getOptions(); 254 Object r = options[toReturn]; 255 toReturn = -1; 256 return r; 257 } 258 259 } 261 } 262 | Popular Tags |