1 19 20 package org.openide.text; 21 22 23 import java.beans.PropertyChangeListener ; 24 import java.io.*; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import javax.swing.JEditorPane ; 28 import junit.framework.*; 29 import org.netbeans.junit.*; 30 import org.openide.util.Lookup; 31 import org.openide.util.Mutex; 32 import org.openide.util.lookup.*; 33 34 35 40 public class ReusableEditorTest extends NbTestCase { 41 CES c1, c2, c3; 42 43 47 public ReusableEditorTest(java.lang.String testName) { 48 super(testName); 49 } 50 51 52 55 protected void setUp () { 56 c1 = createSupport("c1"); 57 c2 = createSupport("c2"); 58 c3 = createSupport("c3"); 59 } 60 61 64 @Override 65 protected void tearDown() { 66 forceClose(c1); 67 forceClose(c2); 68 forceClose(c3); 69 } 70 71 80 public void testReuse() { 81 openAndCheck(c1, Line.SHOW_REUSE); openAndCheck(c2, Line.SHOW_REUSE); assertClosed(c1); openAndCheck(c1, Line.SHOW_REUSE); assertClosed(c2); } 87 88 98 public void testKeepTouched() { 99 openAndCheck(c1, Line.SHOW_REUSE); c1.notifyModified(); openAndCheck(c2, Line.SHOW_REUSE); assertOpened(c1); c2.notifyModified(); c2.notifyUnmodified(); openAndCheck(c3, Line.SHOW_REUSE); assertOpened(c2); assertOpened(c1); 108 } 109 110 126 public void testLeaveNonreusable() { 127 openAndCheck(c1, Line.SHOW_GOTO); openAndCheck(c2, Line.SHOW_REUSE); assertOpened(c1); 131 openAndCheck(c1, Line.SHOW_REUSE); assertOpened(c2); openAndCheck(c3, Line.SHOW_REUSE); assertOpened(c1); 136 assertClosed(c2); } 138 139 149 public void testReuseNewKeepsOld() { 150 openAndCheck(c1, Line.SHOW_REUSE); openAndCheck(c2, Line.SHOW_REUSE_NEW); assertOpened(c1); openAndCheck(c3, Line.SHOW_REUSE); assertClosed(c2); } 156 157 167 public void testReuseNewKeepsOldEvenWhenNotFocused() { 168 openAndCheck(c1, Line.SHOW_REUSE); openAndCheck(c2, Line.SHOW_GOTO); openAndCheck(c3, Line.SHOW_REUSE_NEW); assertOpened(c1); } 173 174 private CES createSupport(String txt) { 175 Env env = new Env(); 176 env.content = txt; 177 CES c = new CES(env, Lookups.singleton(txt)); 178 env.support = c; 179 return c; 180 } 181 182 private void openAndCheck(final CES ces, final int mode) { 183 Mutex.EVENT.readAccess(new Mutex.Action<Void >() { 184 public Void run() { 185 ces.getLineSet().getCurrent(0).show(mode); 186 return null; 187 } 188 189 }); 190 assertOpened(ces); 191 } 192 193 private void forceClose(CES ces) { 194 if (ces.isModified()) ces.notifyUnmodified(); 195 ces.close(); 196 } 197 198 private void assertClosed(CES ces) { 199 assertEquals(0, getOpenedCount(ces)); 200 } 201 202 private void assertOpened(CES ces) { 203 assertEquals(1, getOpenedCount(ces)); 204 } 205 206 private int getOpenedCount(final CES ces) { 207 return Mutex.EVENT.readAccess(new Mutex.Action<Integer >() { 208 public Integer run() { 209 JEditorPane [] panes = ces.getOpenedPanes(); 210 return panes == null ? 0 : panes.length; 211 } 212 }); 213 } 214 215 216 217 private class Env implements CloneableEditorSupport.Env { 221 private String content = ""; 223 private boolean valid = true; 224 private boolean modified = false; 225 private java.util.Date date = new java.util.Date (); 226 private List <PropertyChangeListener > propL = new ArrayList <PropertyChangeListener >(); 227 private java.beans.VetoableChangeListener vetoL; 228 229 CloneableEditorSupport support; 230 231 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 232 propL.add (l); 233 } 234 public synchronized void removePropertyChangeListener(PropertyChangeListener l) { 235 propL.remove (l); 236 } 237 238 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 239 assertNull ("This is the first veto listener", vetoL); 240 vetoL = l; 241 } 242 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 243 assertEquals ("Removing the right veto one", vetoL, l); 244 vetoL = null; 245 } 246 247 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 248 return support; 249 } 250 251 public String getMimeType() { 252 return "text/plain"; 253 } 254 255 public java.util.Date getTime() { 256 return date; 257 } 258 259 public java.io.InputStream inputStream() throws java.io.IOException { 260 return new java.io.ByteArrayInputStream (content.getBytes ()); 261 } 262 public java.io.OutputStream outputStream() throws java.io.IOException { 263 class ContentStream extends java.io.ByteArrayOutputStream { 264 public void close () throws java.io.IOException { 265 super.close (); 266 content = new String (toByteArray ()); 267 } 268 } 269 270 return new ContentStream (); 271 } 272 273 public boolean isValid() { 274 return valid; 275 } 276 277 public boolean isModified() { 278 return modified; 279 } 280 281 public void markModified() throws java.io.IOException { 282 modified = true; 283 } 284 285 public void unmarkModified() { 286 modified = false; 287 } 288 } 289 290 291 private static final class CES extends CloneableEditorSupport { 292 public CES (Env env, Lookup l) { 293 super (env, l); 294 } 295 296 protected String messageName() { 297 return "Name"; 298 } 299 300 protected String messageOpened() { 301 return "Opened"; 302 } 303 304 protected String messageOpening() { 305 return "Opening"; 306 } 307 308 protected String messageSave() { 309 return "Save"; 310 } 311 312 protected String messageToolTip() { 313 return "ToolTip"; 314 } 315 316 } 317 318 } 319 | Popular Tags |