1 19 20 package org.openide.text; 21 22 23 import java.io.IOException ; 24 import javax.swing.SwingUtilities ; 25 import junit.framework.*; 26 import org.netbeans.junit.*; 27 import org.openide.NotifyDescriptor; 28 import org.openide.util.*; 29 import org.openide.util.lookup.*; 30 31 32 36 public class CloneableEditorUserQuestionTest extends NbTestCase 37 implements CloneableEditorSupport.Env { 38 39 private CloneableEditorSupport support; 40 41 private InstanceContent ic; 42 43 44 private String content = ""; 46 private boolean valid = true; 47 private boolean modified = false; 48 49 private String cannotBeModified; 50 private java.util.Date date = new java.util.Date (); 51 private java.util.List propL = new java.util.ArrayList (); 52 private java.beans.VetoableChangeListener vetoL; 53 private IOException toThrow; 54 55 56 public CloneableEditorUserQuestionTest (java.lang.String testName) { 57 super(testName); 58 } 59 60 public static void main(java.lang.String [] args) { 61 junit.textui.TestRunner.run(suite()); 62 } 63 64 public static Test suite() { 65 TestSuite suite = new NbTestSuite(CloneableEditorUserQuestionTest.class); 66 67 return suite; 68 } 69 70 71 protected void setUp () { 72 System.setProperty ("org.openide.util.Lookup", "org.openide.text.CloneableEditorUserQuestionTest$Lkp"); 73 74 ic = new InstanceContent (); 75 support = new CES (this, new AbstractLookup (ic)); 76 } 77 78 79 public void testExceptionThrownWhenDocumentIsBeingReadInAWT () throws Exception { 80 class Run implements Runnable { 81 public Exception ex; 82 public Error err; 83 public void run () { 84 85 try { 86 doExceptionThrownWhenDocumentIsBeingRead (); 87 } catch (Exception ex) { 88 this.ex = ex; 89 } catch (Error err) { 90 this.err = err; 91 } 92 } 93 } 94 Run r = new Run (); 95 SwingUtilities.invokeAndWait (r); 96 if (r.ex != null) throw r.ex; 97 if (r.err != null) throw r.err; 98 } 99 100 public void testExceptionThrownWhenDocumentIsBeingRead () throws Exception { 101 assertFalse (SwingUtilities.isEventDispatchThread ()); 102 doExceptionThrownWhenDocumentIsBeingRead (); 103 } 104 105 106 public void testOpenDocumentIsLoadedUsingIOException() throws Exception { 107 doOpenDocumentIsLoaded (new IOException ("Plain I/O exc")); 108 } 109 110 public void testOpenDocumentIsLoadedUsingUserQuestionException() throws Exception { 111 class MyEx extends UserQuestionException { 112 private int confirmed; 113 114 public String getLocalizedMessage () { 115 return "locmsg"; 116 } 117 118 public String getMessage () { 119 return "msg"; 120 } 121 122 public void confirmed () { 123 confirmed++; 124 toThrow = null; 125 } 126 } 127 doOpenDocumentIsLoaded (new MyEx ()); 128 } 129 130 private void doOpenDocumentIsLoaded (IOException my) throws Exception { 131 toThrow = my; 132 try{ 133 support.openDocument(); 134 fail ("Document should not be loaded, we throw an exception"); 135 } 136 catch (IOException e){ 137 assertSame ("The expected exception", my, e); 138 } 139 140 assertNull ("No document", support.getDocument()); 141 assertFalse ("Not loaded", support.isDocumentLoaded()); 142 143 toThrow = null; 144 support.openDocument (); 145 146 assertNotNull ("We can later open the document", support.getDocument ()); 147 assertTrue ("And it is correctly marked as loaded", support.isDocumentLoaded ()); 148 } 149 150 private void doExceptionThrownWhenDocumentIsBeingRead () throws Exception { 151 class MyEx extends UserQuestionException { 152 private int confirmed; 153 154 public String getLocalizedMessage () { 155 return "locmsg"; 156 } 157 158 public String getMessage () { 159 return "msg"; 160 } 161 162 public void confirmed () { 163 confirmed++; 164 toThrow = null; 165 } 166 } 167 168 MyEx my = new MyEx (); 169 toThrow = my; 170 171 DD.toReturn = org.openide.NotifyDescriptor.NO_OPTION; 172 support.open (); 173 174 if (!SwingUtilities.isEventDispatchThread ()) { 175 javax.swing.SwingUtilities.invokeAndWait (new Runnable () { public void run () {} }); 176 } 177 178 assertNotNull ("Some otions", DD.options); 179 assertEquals ("Two options", 2, DD.options.length); 180 assertEquals ("Yes", NotifyDescriptor.YES_OPTION, DD.options[0]); 181 assertEquals ("No", NotifyDescriptor.NO_OPTION, DD.options[1]); 182 assertEquals ("confirmed not called", 0, my.confirmed); 183 184 assertNull ("Still no document", support.getDocument ()); 185 186 DD.options = null; 187 DD.toReturn = NotifyDescriptor.YES_OPTION; 188 support.open (); 189 190 if (!SwingUtilities.isEventDispatchThread ()) { 191 javax.swing.SwingUtilities.invokeAndWait (new Runnable () { public void run () {} }); 192 } 193 194 assertEquals ("confirmed called", 1, my.confirmed); 195 assertNotNull ("Some otions", DD.options); 196 assertEquals ("Two options", 2, DD.options.length); 197 assertEquals ("Yes", NotifyDescriptor.YES_OPTION, DD.options[0]); 198 assertEquals ("No", NotifyDescriptor.NO_OPTION, DD.options[1]); 199 DD.options = null; 200 201 assertNotNull ("Document opened", support.getDocument ()); 202 203 } 204 205 209 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 210 propL.add (l); 211 } 212 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 213 propL.remove (l); 214 } 215 216 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 217 assertNull ("This is the first veto listener", vetoL); 218 vetoL = l; 219 } 220 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 221 assertEquals ("Removing the right veto one", vetoL, l); 222 vetoL = null; 223 } 224 225 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 226 return support; 227 } 228 229 public String getMimeType() { 230 return "text/plain"; 231 } 232 233 public java.util.Date getTime() { 234 return date; 235 } 236 237 public java.io.InputStream inputStream() throws java.io.IOException { 238 if (toThrow != null) { 239 throw toThrow; 240 } 241 return new java.io.ByteArrayInputStream (content.getBytes ()); 242 } 243 public java.io.OutputStream outputStream() throws java.io.IOException { 244 class ContentStream extends java.io.ByteArrayOutputStream { 245 public void close () throws java.io.IOException { 246 super.close (); 247 content = new String (toByteArray ()); 248 } 249 } 250 251 return new ContentStream (); 252 } 253 254 public boolean isValid() { 255 return valid; 256 } 257 258 public boolean isModified() { 259 return modified; 260 } 261 262 public void markModified() throws java.io.IOException { 263 if (cannotBeModified != null) { 264 final String notify = cannotBeModified; 265 IOException e = new IOException () { 266 public String getLocalizedMessage () { 267 return notify; 268 } 269 }; 270 Exceptions.attachLocalizedMessage(e, cannotBeModified); 271 throw e; 272 } 273 274 modified = true; 275 } 276 277 public void unmarkModified() { 278 modified = false; 279 } 280 281 282 private static final class CES extends CloneableEditorSupport { 283 public CES (Env env, Lookup l) { 284 super (env, l); 285 } 286 287 protected String messageName() { 288 return "Name"; 289 } 290 291 protected String messageOpened() { 292 return "Opened"; 293 } 294 295 protected String messageOpening() { 296 return "Opening"; 297 } 298 299 protected String messageSave() { 300 return "Save"; 301 } 302 303 protected String messageToolTip() { 304 return "ToolTip"; 305 } 306 307 } 309 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 313 public Lkp () { 314 this (new org.openide.util.lookup.InstanceContent ()); 315 } 316 317 private Lkp (org.openide.util.lookup.InstanceContent ic) { 318 super (ic); 319 ic.add (new DD ()); 320 } 321 } 322 323 325 private static final class DD extends org.openide.DialogDisplayer { 326 public static Object [] options; 327 public static Object toReturn; 328 329 public java.awt.Dialog createDialog(org.openide.DialogDescriptor descriptor) { 330 throw new IllegalStateException ("Not implemented"); 331 } 332 333 public Object notify(org.openide.NotifyDescriptor descriptor) { 334 assertNull (options); 335 assertNotNull (toReturn); 336 options = descriptor.getOptions(); 337 Object r = toReturn; 338 toReturn = null; 339 return r; 340 } 341 342 } 344 } 345 | Popular Tags |