1 18 package org.openide.text; 19 20 import java.beans.PropertyChangeListener ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.lang.ref.WeakReference ; 25 import java.util.Arrays ; 26 import javax.swing.SwingUtilities ; 27 import javax.swing.text.Document ; 28 import javax.swing.text.EditorKit ; 29 import junit.framework.Test; 30 import junit.framework.TestSuite; 31 import org.netbeans.junit.MockServices; 32 import org.netbeans.junit.NbTestCase; 33 import org.netbeans.junit.NbTestSuite; 34 import org.openide.util.Exceptions; 35 import org.openide.util.Lookup; 36 import org.openide.util.lookup.AbstractLookup; 37 import org.openide.util.lookup.InstanceContent; 38 39 43 public class CloneableEditorSupportRedirectorTest extends NbTestCase 44 implements CloneableEditorSupport.Env { 45 46 private InstanceContent ic; 47 Redirector red; 48 49 50 private String content = ""; 52 private boolean valid = true; 53 private boolean modified = false; 54 55 private String cannotBeModified; 56 private java.util.Date date = new java.util.Date (); 57 private java.util.List <PropertyChangeListener > propL = new java.util.ArrayList <PropertyChangeListener >(); 58 private java.beans.VetoableChangeListener vetoL; 59 60 61 62 public CloneableEditorSupportRedirectorTest(String testName) { 63 super(testName); 64 } 65 66 public static Test suite() { 67 TestSuite suite = new NbTestSuite(CloneableEditorSupportRedirectorTest.class); 68 69 return suite; 70 } 71 72 73 protected void setUp () { 74 ic = new InstanceContent (); 75 CES support = new CES (this, new AbstractLookup(ic)); 76 77 MockServices.setServices(Redirector.class); 78 red = Lookup.getDefault().lookup(Redirector.class); 79 assertNotNull(red); 80 81 CloneableEditorSupportRedirectorTest t = new CloneableEditorSupportRedirectorTest(""); 82 red.master = support; 83 InstanceContent slave = new InstanceContent(); 84 red.slave = new CES(t, new AbstractLookup (slave)); 85 slave.add(red.master); 86 } 87 88 public void testSameDocument() throws Exception { 89 javax.swing.text.Document doc = red.slave.openDocument (); 90 assertNotNull (doc); 91 92 assertSame(doc, red.master.getDocument()); 93 94 String s = doc.getText (0, doc.getLength ()); 95 assertEquals ("Same text as in the stream", content, s); 96 97 assertFalse ("No redo", red.slave.getUndoRedo ().canRedo ()); 98 assertFalse ("No undo", red.slave.getUndoRedo ().canUndo ()); 99 } 100 101 public void testLineLookupIsPropagated () throws Exception { 102 content = "Line1\nLine2\n"; 103 Integer template = new Integer (1); 104 ic.add (template); 106 red.master.openDocument(); 108 109 Line.Set set = red.master.getLineSet(); 110 assertSame("Same lines", set, red.slave.getLineSet()); 111 java.util.List list = set.getLines(); 112 assertEquals ("Three lines", 3, list.size ()); 113 114 Line l = (Line)list.get (0); 115 Integer i = l.getLookup ().lookup (Integer .class); 116 assertEquals ("The original integer", template, i); 117 ic.remove (template); 118 i = l.getLookup ().lookup (Integer .class); 119 assertNull ("Lookup is dynamic, so now there is nothing", i); 120 } 121 122 123 public void testGetInputStream () throws Exception { 124 content = "goes\nto\nInputStream"; 125 String added = "added before\n"; 126 javax.swing.text.Document doc = red.master.openDocument (); 127 assertNotNull (doc); 128 129 doc.insertString(0, added, null); 131 compareStreamWithString(red.master.getInputStream(), added + content); 132 compareStreamWithString(red.slave.getInputStream(), added + content); 133 } 134 135 public void testGetInputStreamWhenClosed () throws Exception { 136 content = "basic\ncontent"; 137 compareStreamWithString(red.master.getInputStream(), content); 138 compareStreamWithString(red.slave.getInputStream(), content); 139 assertNull("The document is supposed to be still closed", red.master.getDocument ()); 141 } 142 143 public void testDocumentCannotBeModified () throws Exception { 144 content = "Ahoj\nMyDoc"; 145 cannotBeModified = "No, you cannot modify this document in this test"; 146 147 javax.swing.text.Document doc = red.master.openDocument (); 148 assertNotNull (doc); 149 150 assertFalse ("Nothing to undo", red.master.getUndoRedo ().canUndo ()); 151 152 doc.insertString (0, "Kuk", null); 154 155 String modifiedForAWhile = doc.getText (0, 3); 156 158 assertFalse ("The document cannot be modified", red.master.getUndoRedo ().canUndo ()); 159 160 String s = doc.getText (0, doc.getLength ()); 161 assertEquals ("The document is now the same as at the begining", content, s); 162 163 assertEquals ("Message has been shown to user in status bar", cannotBeModified, org.openide.awt.StatusDisplayer.getDefault ().getStatusText ()); 164 } 165 166 public void testDocumentCanBeGarbageCollectedWhenClosed () throws Exception { 167 content = "Ahoj\nMyDoc"; 168 javax.swing.text.Document doc = red.master.openDocument (); 169 assertNotNull (doc); 170 171 assertTrue ("Document is loaded", red.master.isDocumentLoaded ()); 172 assertTrue ("Document is loaded", red.slave.isDocumentLoaded ()); 173 assertTrue ("Can be closed without problems", red.slave.close ()); 174 assertFalse ("Document is not loaded", red.master.isDocumentLoaded ()); 175 assertFalse ("Document is not loaded", red.slave.isDocumentLoaded ()); 176 177 WeakReference <?> ref = new WeakReference <Document >(doc); 178 doc = null; 179 180 assertGC ("Document can dissapear", ref); 181 } 182 183 187 public void testWrapEditorComponent() { 188 javax.swing.JPanel panel = new javax.swing.JPanel (); 189 assertSame(red.master.wrapEditorComponent(panel), panel); 190 assertSame(red.slave.wrapEditorComponent(panel), panel); 191 } 192 193 public void testAfterOpenOfSlaveThereAreMasterPanes() throws Exception { 194 red.slave.open(); 195 196 class Check implements Runnable { 197 public void run() { 198 assertTrue("Some panes are now open", red.master.getOpenedPanes() != null); 199 } 200 } 201 Check check = new Check(); 202 203 SwingUtilities.invokeAndWait(check); 204 } 205 206 public void testGetEditorKit() { 207 EditorKit kit = CloneableEditorSupport.getEditorKit("text/plain"); 208 assertNotNull("EditorKit should never be null", kit); 209 assertEquals("Wrong default EditorKit", "org.openide.text.CloneableEditorSupport$PlainEditorKit", kit.getClass().getName()); 211 } 212 213 private void compareStreamWithString(InputStream is, String s) throws Exception { 214 int i; 215 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 216 while ((i = is.read()) != -1) { 217 baos.write(i); 218 } 219 byte b1[] = baos.toByteArray(); 220 byte b2[] = s.getBytes(); 221 assertTrue("Same bytes as would result from the string: " + s, Arrays.equals(b1, b2)); 222 } 223 224 228 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 229 propL.add (l); 230 } 231 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 232 propL.remove (l); 233 } 234 235 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 236 assertNull ("This is the first veto listener", vetoL); 237 vetoL = l; 238 } 239 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 240 assertEquals ("Removing the right veto one", vetoL, l); 241 vetoL = null; 242 } 243 244 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 245 return red.master; 246 } 247 248 public String getMimeType() { 249 return "text/plain"; 250 } 251 252 public java.util.Date getTime() { 253 return date; 254 } 255 256 public java.io.InputStream inputStream() throws java.io.IOException { 257 return new java.io.ByteArrayInputStream (content.getBytes ()); 258 } 259 public java.io.OutputStream outputStream() throws java.io.IOException { 260 class ContentStream extends java.io.ByteArrayOutputStream { 261 public void close () throws java.io.IOException { 262 super.close (); 263 content = new String (toByteArray ()); 264 } 265 } 266 267 return new ContentStream (); 268 } 269 270 public boolean isValid() { 271 return valid; 272 } 273 274 public boolean isModified() { 275 return modified; 276 } 277 278 public void markModified() throws java.io.IOException { 279 if (cannotBeModified != null) { 280 final String notify = cannotBeModified; 281 IOException e = new IOException () { 282 public String getLocalizedMessage () { 283 return notify; 284 } 285 }; 286 Exceptions.attachLocalizedMessage(e, cannotBeModified); 287 throw e; 288 } 289 290 modified = true; 291 } 292 293 public void unmarkModified() { 294 modified = false; 295 } 296 297 298 private static final class CES extends CloneableEditorSupport { 299 public CES (Env env, Lookup l) { 300 super (env, l); 301 } 302 303 protected String messageName() { 304 return "Name"; 305 } 306 307 protected String messageOpened() { 308 return "Opened"; 309 } 310 311 protected String messageOpening() { 312 return "Opening"; 313 } 314 315 protected String messageSave() { 316 return "Save"; 317 } 318 319 protected String messageToolTip() { 320 return "ToolTip"; 321 } 322 323 } 324 325 326 public static final class Redirector extends CloneableEditorSupportRedirector { 327 CES master; 328 CES slave; 329 330 protected CloneableEditorSupport redirect(Lookup ces) { 331 return ces.lookup(CloneableEditorSupport.class); 332 } 333 } 334 } 335 | Popular Tags |