1 19 20 package org.openide.text; 21 22 23 import java.beans.*; 24 import java.io.*; 25 import java.util.Date ; 26 import java.util.logging.*; 27 import javax.swing.SwingUtilities ; 28 import javax.swing.text.*; 29 import org.netbeans.junit.*; 30 import org.openide.util.Exceptions; 31 import org.openide.util.Lookup; 32 import org.openide.windows.*; 33 34 37 public class ReloadTest extends NbTestCase 38 implements CloneableEditorSupport.Env { 39 40 private transient CES support; 41 42 private transient String content = ""; 44 private transient boolean valid = true; 45 private transient boolean modified = false; 46 47 private transient String cannotBeModified; 48 private transient Date date = new Date (); 49 private transient PropertyChangeSupport propL = new PropertyChangeSupport (this); 50 private transient VetoableChangeListener vetoL; 51 52 private Logger err; 53 private CharSequence log; 54 55 public ReloadTest (String s) { 56 super(s); 57 } 58 59 60 protected EditorKit createEditorKit () { 61 return null; 62 } 63 64 65 protected boolean runInEQ() { 66 return false; 67 } 68 69 protected Level logLevel() { 70 return Level.ALL; 71 } 72 73 74 protected void setUp () { 75 support = new CES (this, Lookup.EMPTY); 76 77 log = Log.enable("", Level.ALL); 78 79 80 err = Logger.getLogger(getName()); 81 } 82 83 84 public void testRefreshProblem46885 () throws Exception { 85 StyledDocument doc = support.openDocument (); 86 87 doc.insertString (0, "A text", null); 88 support.saveDocument (); 89 90 content = "New"; 91 propL.firePropertyChange (CloneableEditorSupport.Env.PROP_TIME, null, null); 92 93 waitAWT (); 94 95 String s = doc.getText (0, doc.getLength ()); 96 assertEquals ("Text has been updated", content, s); 97 98 99 long oldtime = System.currentTimeMillis (); 100 doc.insertString (0, "A text", null); 101 support.saveDocument (); 102 s = doc.getText (0, doc.getLength ()); 103 104 content = "NOT TO be loaded"; 105 propL.firePropertyChange (CloneableEditorSupport.Env.PROP_TIME, null, new Date (oldtime)); 106 107 waitAWT (); 108 109 String s1 = doc.getText (0, doc.getLength ()); 110 assertEquals ("Text has not been updated", s, s1); 111 } 112 113 private void waitAWT () throws Exception { 114 err.info("wait for AWT begin"); 115 assertFalse ("Not in AWT", SwingUtilities.isEventDispatchThread ()); 116 SwingUtilities.invokeAndWait (new Runnable () { public void run () { }}); 117 err.info("wait for AWT ends"); 118 } 119 120 124 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 125 propL.addPropertyChangeListener (l); 126 } 127 public synchronized void removePropertyChangeListener(PropertyChangeListener l) { 128 propL.removePropertyChangeListener (l); 129 } 130 131 public synchronized void addVetoableChangeListener(VetoableChangeListener l) { 132 assertNull ("This is the first veto listener", vetoL); 133 vetoL = l; 134 } 135 public void removeVetoableChangeListener(VetoableChangeListener l) { 136 assertEquals ("Removing the right veto one", vetoL, l); 137 vetoL = null; 138 } 139 140 public CloneableOpenSupport findCloneableOpenSupport() { 141 return null; 142 } 143 144 public String getMimeType() { 145 return "text/plain"; 146 } 147 148 public Date getTime() { 149 return date; 150 } 151 152 public InputStream inputStream() throws IOException { 153 return new ByteArrayInputStream (content.getBytes ()); 154 } 155 public OutputStream outputStream() throws IOException { 156 class ContentStream extends ByteArrayOutputStream { 157 public void close () throws IOException { 158 super.close (); 159 content = new String (toByteArray ()); 160 } 161 } 162 163 return new ContentStream (); 164 } 165 166 public boolean isValid() { 167 return valid; 168 } 169 170 public boolean isModified() { 171 return modified; 172 } 173 174 public void markModified() throws IOException { 175 if (cannotBeModified != null) { 176 final String notify = cannotBeModified; 177 IOException e = new IOException () { 178 public String getLocalizedMessage () { 179 return notify; 180 } 181 }; 182 Exceptions.attachLocalizedMessage(e, cannotBeModified); 183 throw e; 184 } 185 186 modified = true; 187 } 188 189 public void unmarkModified() { 190 modified = false; 191 } 192 193 194 private final class CES extends CloneableEditorSupport { 195 public CES (Env env, Lookup l) { 196 super (env, l); 197 } 198 199 public CloneableTopComponent.Ref getRef () { 200 return allEditors; 201 } 202 203 protected String messageName() { 204 return "Name"; 205 } 206 207 protected String messageOpened() { 208 return "Opened"; 209 } 210 211 protected String messageOpening() { 212 return "Opening"; 213 } 214 215 protected String messageSave() { 216 return "Save"; 217 } 218 219 protected String messageToolTip() { 220 return "ToolTip"; 221 } 222 223 protected EditorKit createEditorKit () { 224 EditorKit retValue = ReloadTest.this.createEditorKit (); 225 if (retValue == null) { 226 retValue = super.createEditorKit(); 227 } 228 return retValue; 229 } 230 } 231 } 232 | Popular Tags |