KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > ReloadTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.text;
21
22
23 import java.beans.*;
24 import java.io.*;
25 import java.util.Date JavaDoc;
26 import java.util.logging.*;
27 import javax.swing.SwingUtilities JavaDoc;
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 /** Test to simulate problem #46885
35  * @author Jaroslav Tulach
36  */

37 public class ReloadTest extends NbTestCase
38 implements CloneableEditorSupport.Env {
39     /** the support to work with */
40     private transient CES support;
41
42     // Env variables
43
private transient String JavaDoc content = "";
44     private transient boolean valid = true;
45     private transient boolean modified = false;
46     /** if not null contains message why this document cannot be modified */
47     private transient String JavaDoc cannotBeModified;
48     private transient Date JavaDoc date = new Date JavaDoc ();
49     private transient PropertyChangeSupport propL = new PropertyChangeSupport (this);
50     private transient VetoableChangeListener vetoL;
51
52     private Logger err;
53     private CharSequence JavaDoc log;
54     
55     public ReloadTest (String JavaDoc s) {
56         super(s);
57     }
58
59     /** For subclasses to change to more nb like kits. */
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 JavaDoc {
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 JavaDoc 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 JavaDoc (oldtime));
106         
107         waitAWT ();
108         
109         String JavaDoc s1 = doc.getText (0, doc.getLength ());
110         assertEquals ("Text has not been updated", s, s1);
111     }
112
113     private void waitAWT () throws Exception JavaDoc {
114         err.info("wait for AWT begin");
115         assertFalse ("Not in AWT", SwingUtilities.isEventDispatchThread ());
116         SwingUtilities.invokeAndWait (new Runnable JavaDoc () { public void run () { }});
117         err.info("wait for AWT ends");
118     }
119     
120     //
121
// Implementation of the CloneableEditorSupport.Env
122
//
123

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 JavaDoc getMimeType() {
145         return "text/plain";
146     }
147     
148     public Date JavaDoc 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 JavaDoc (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 JavaDoc notify = cannotBeModified;
177             IOException e = new IOException () {
178                 public String JavaDoc 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     /** Implementation of the CES */
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 JavaDoc messageName() {
204             return "Name";
205         }
206         
207         protected String JavaDoc messageOpened() {
208             return "Opened";
209         }
210         
211         protected String JavaDoc messageOpening() {
212             return "Opening";
213         }
214         
215         protected String JavaDoc messageSave() {
216             return "Save";
217         }
218         
219         protected String JavaDoc 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