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