KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.openide.text;
22
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Set JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32 import javax.swing.JEditorPane JavaDoc;
33 import javax.swing.text.BadLocationException JavaDoc;
34 import javax.swing.text.Position JavaDoc;
35 import javax.swing.text.StyledDocument JavaDoc;
36
37 import junit.textui.TestRunner;
38
39 import org.netbeans.junit.NbTestCase;
40 import org.netbeans.junit.NbTestSuite;
41 import org.openide.actions.*;
42 import org.openide.cookies.CloseCookie;
43 import org.openide.cookies.EditCookie;
44
45 import org.openide.cookies.EditorCookie;
46 import org.openide.cookies.OpenCookie;
47 import org.openide.cookies.PrintCookie;
48 import org.openide.cookies.SaveCookie;
49 import org.openide.filesystems.FileLock;
50 import org.openide.filesystems.FileObject;
51 import org.openide.filesystems.FileStateInvalidException;
52 import org.openide.filesystems.FileSystem;
53 import org.openide.filesystems.Repository;
54 import org.openide.loaders.DataFolder;
55 import org.openide.loaders.DataNode;
56 import org.openide.loaders.DataObject;
57 import org.openide.loaders.DataObjectExistsException;
58 import org.openide.loaders.ExtensionList;
59 import org.openide.loaders.MultiDataObject;
60 import org.openide.loaders.MultiFileLoader;
61 import org.openide.loaders.UniFileLoader;
62 import org.openide.nodes.Children;
63 import org.openide.nodes.CookieSet;
64 import org.openide.nodes.Node;
65 import org.openide.text.CloneableEditorSupport;
66 import org.openide.util.HelpCtx;
67 import org.openide.util.Mutex;
68 import org.openide.util.io.NbMarshalledObject;
69 import org.openide.util.Lookup;
70 import org.openide.util.LookupListener;
71 import org.openide.util.NbBundle;
72 import org.openide.util.actions.SystemAction;
73 import org.openide.windows.CloneableOpenSupport;
74 import org.openide.windows.WindowManager;
75
76
77 /**
78  */

79 public class DataEditorSupportTest extends NbTestCase {
80     // for file object support
81
String JavaDoc content = "";
82     long expectedSize = -1;
83     java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
84     
85     MyFileObject fileObject;
86     org.openide.filesystems.FileSystem fs;
87     static DataEditorSupportTest RUNNING;
88     static {
89         System.setProperty ("org.openide.util.Lookup", "org.openide.text.DataEditorSupportTest$Lkp");
90     }
91     
92     public DataEditorSupportTest(String JavaDoc s) {
93         super(s);
94     }
95     
96     protected void setUp () throws Exception JavaDoc {
97         RUNNING = this;
98         
99         fs = org.openide.filesystems.FileUtil.createMemoryFileSystem ();
100         org.openide.filesystems.Repository.getDefault ().addFileSystem (fs);
101         org.openide.filesystems.FileObject root = fs.getRoot ();
102         fileObject = new MyFileObject (org.openide.filesystems.FileUtil.createData (root, "my.obj"));
103     }
104     
105     protected void tearDown () throws Exception JavaDoc {
106         waitEQ ();
107         
108         RUNNING = null;
109         org.openide.filesystems.Repository.getDefault ().removeFileSystem (fs);
110     }
111     
112     protected boolean runInEQ() {
113         return false;
114     }
115     
116     private void waitEQ () throws Exception JavaDoc {
117         javax.swing.SwingUtilities.invokeAndWait (new Runnable JavaDoc () { public void run () { } });
118     }
119
120     DES support () throws Exception JavaDoc {
121         DataObject obj = DataObject.find (fileObject);
122         
123         assertEquals ("My object was created", MyDataObject.class, obj.getClass ());
124         Object JavaDoc cookie = obj.getCookie (org.openide.cookies.OpenCookie.class);
125         assertNotNull ("Our object has this cookie", cookie);
126         assertEquals ("It is my cookie", DES.class, cookie.getClass ());
127         
128         return (DES)cookie;
129     }
130     
131     /** holds the instance of the object so insane is able to find the reference */
132     private DataObject obj;
133     public void testItCanBeGCedIssue57565 () throws Exception JavaDoc {
134         DES sup = support ();
135         assertFalse ("It is closed now", support ().isDocumentLoaded ());
136         
137         Lookup lkp = sup.getLookup ();
138         obj = (DataObject)lkp.lookup (DataObject.class);
139         assertNotNull ("DataObject found", obj);
140         
141         sup.openDocument ();
142         assertTrue ("It is open now", support ().isDocumentLoaded ());
143         
144         assertTrue ("Closed ok", sup.close ());
145         
146         java.lang.ref.WeakReference JavaDoc refLkp = new java.lang.ref.WeakReference JavaDoc (lkp);
147         lkp = null;
148     
149         java.lang.ref.WeakReference JavaDoc ref = new java.lang.ref.WeakReference JavaDoc (sup);
150         sup = null;
151         
152         assertGC ("Can disappear", ref);
153         assertGC ("And its lookup as well", refLkp);
154         
155         
156         
157     }
158     
159     public void testGetOpenedPanesWorksAfterDeserialization () throws Exception JavaDoc {
160         doGetOpenedPanesWorksAfterDeserialization (-1);
161     }
162     public void testGetOpenedPanesWorksAfterDeserializationIfTheFileGetsBig () throws Exception JavaDoc {
163         doGetOpenedPanesWorksAfterDeserialization (1024 * 1024 * 10);
164     }
165     
166     public void test68015 () throws Exception JavaDoc {
167         DES edSupport = support();
168         edSupport.open();
169         
170         waitEQ();
171         
172         edSupport.desEnv().markModified();
173         
174         assertTrue(edSupport.messageName().indexOf('*') != -1);
175         assertTrue(edSupport.messageHtmlName().indexOf('*') != -1);
176     }
177     
178     private void doGetOpenedPanesWorksAfterDeserialization (int size) throws Exception JavaDoc {
179         support().open ();
180         
181         waitEQ ();
182
183         CloneableEditor ed = (CloneableEditor)support().getRef ().getAnyComponent ();
184         
185         JEditorPane JavaDoc[] panes = getPanes();
186         assertNotNull (panes);
187         assertEquals ("One is there", 1, panes.length);
188         
189         NbMarshalledObject obj = new NbMarshalledObject (ed);
190         ed.close ();
191         
192         panes = getPanes();
193         assertNull ("No panes anymore", panes);
194
195         DataObject oldObj = DataObject.find (fileObject);
196         oldObj.setValid (false);
197         
198         expectedSize = size;
199         
200         ed = (CloneableEditor)obj.get ();
201         
202         DataObject newObj = DataObject.find (fileObject);
203         
204         if (oldObj == newObj) {
205             fail ("Object should not be the same, new one shall be created after marking the old invalid");
206         }
207         
208         panes = getPanes ();
209         assertNotNull ("One again", panes);
210         assertEquals ("One is there again", 1, panes.length);
211     }
212
213     private JEditorPane JavaDoc[] getPanes() throws Exception JavaDoc {
214         return Mutex.EVENT.readAccess(new Mutex.ExceptionAction<JEditorPane JavaDoc[]>() {
215             public JEditorPane JavaDoc[] run() throws Exception JavaDoc {
216                 return support().getOpenedPanes ();
217             }
218         });
219     }
220     
221     public void testEnvOutputStreamTakesLock() throws Exception JavaDoc {
222         DataEditorSupport.Env env = (DataEditorSupport.Env)support().desEnv();
223         assertNull(env.fileLock);
224         OutputStream JavaDoc stream = env.outputStream();
225         assertNotNull(stream);
226         stream.close();
227         assertNotNull(env.fileLock);
228         env.fileLock.releaseLock();
229     }
230     
231     /** File object that let us know what is happening and delegates to certain
232      * instance variables of the test.
233      */

234     private static final class MyFileObject extends org.openide.filesystems.FileObject {
235         private org.openide.filesystems.FileObject delegate;
236         
237         public MyFileObject (org.openide.filesystems.FileObject del) {
238             delegate = del;
239         }
240
241         public java.io.OutputStream JavaDoc getOutputStream (FileLock lock) throws IOException JavaDoc {
242             class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
243                 public void close () throws java.io.IOException JavaDoc {
244                     super.close ();
245                     RUNNING.content = new String JavaDoc (toByteArray ());
246                 }
247             }
248
249             return new ContentStream ();
250         }
251
252         public void delete (FileLock lock) throws IOException JavaDoc {
253             delegate.delete (lock);
254         }
255
256         public void setImportant (boolean b) {
257             delegate.setImportant (b);
258         }
259
260         public void addFileChangeListener (org.openide.filesystems.FileChangeListener fcl) {
261             delegate.addFileChangeListener (fcl);
262         }
263
264         public void removeFileChangeListener (org.openide.filesystems.FileChangeListener fcl) {
265             delegate.removeFileChangeListener (fcl);
266         }
267
268         public Object JavaDoc getAttribute (String JavaDoc attrName) {
269             return delegate.getAttribute (attrName);
270         }
271
272         public FileObject createFolder (String JavaDoc name) throws IOException JavaDoc {
273             throw new IOException JavaDoc ("Not supported");
274         }
275
276         public void rename (FileLock lock, String JavaDoc name, String JavaDoc ext) throws IOException JavaDoc {
277             throw new IOException JavaDoc ("Not supported");
278         }
279
280         public void setAttribute (String JavaDoc attrName, Object JavaDoc value) throws IOException JavaDoc {
281             delegate.setAttribute (attrName, value);
282         }
283
284         public String JavaDoc getName () {
285             return delegate.getName ();
286         }
287
288         public java.io.InputStream JavaDoc getInputStream () throws java.io.FileNotFoundException JavaDoc {
289             return new java.io.ByteArrayInputStream JavaDoc (RUNNING.content.getBytes ());
290         }
291
292         public FileSystem getFileSystem () throws FileStateInvalidException {
293             return delegate.getFileSystem ();
294         }
295
296         public FileObject getFileObject (String JavaDoc name, String JavaDoc ext) {
297             return null;
298         }
299
300         public String JavaDoc getExt () {
301             return delegate.getExt ();
302         }
303
304         public FileObject[] getChildren () {
305             return null;
306         }
307
308         public java.util.Enumeration JavaDoc getAttributes () {
309             return delegate.getAttributes ();
310         }
311
312         public FileObject createData (String JavaDoc name, String JavaDoc ext) throws IOException JavaDoc {
313             throw new IOException JavaDoc ("Not supported");
314         }
315
316         public FileObject getParent () {
317             return delegate.getParent ();
318         }
319
320         public long getSize () {
321             return RUNNING.expectedSize;
322         }
323
324         public boolean isData () {
325             return true;
326         }
327
328         public boolean isFolder () {
329             return false;
330         }
331
332         public boolean isReadOnly () {
333             return false;
334         }
335
336         public boolean isRoot () {
337             return false;
338         }
339
340         public boolean isValid () {
341             return delegate.isValid ();
342         }
343
344         public java.util.Date JavaDoc lastModified () {
345             return RUNNING.date;
346         }
347
348         public FileLock lock () throws IOException JavaDoc {
349             return delegate.lock ();
350         }
351         
352         public Object JavaDoc writeReplace () {
353             return new Replace ();
354         }
355     }
356     
357     private static final class Replace extends Object JavaDoc implements java.io.Serializable JavaDoc {
358         static final long serialVersionUID = 2L;
359         
360         public Object JavaDoc readResolve () {
361             return RUNNING.fileObject;
362         }
363     }
364
365     /** Implementation of the DES */
366     private static final class DES extends DataEditorSupport
367     implements OpenCookie, EditCookie {
368         public DES (DataObject obj, Env env) {
369             super (obj, env);
370         }
371         
372         public org.openide.windows.CloneableTopComponent.Ref getRef () {
373             return allEditors;
374         }
375         
376     }
377     
378     /** MyEnv that uses DataEditorSupport.Env */
379     private static final class MyEnv extends DataEditorSupport.Env {
380         static final long serialVersionUID = 1L;
381         
382         public MyEnv (DataObject obj) {
383             super (obj);
384         }
385         
386         protected FileObject getFile () {
387             return super.getDataObject ().getPrimaryFile ();
388         }
389
390         protected FileLock takeLock () throws IOException JavaDoc {
391             return super.getDataObject ().getPrimaryFile ().lock ();
392         }
393         
394     }
395
396     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
397         public Lkp () {
398             this (new org.openide.util.lookup.InstanceContent ());
399         }
400         
401         private Lkp (org.openide.util.lookup.InstanceContent ic) {
402             super (ic);
403             
404             ic.add (new Pool ());
405         }
406         
407     } // end of Lkp
408

409     private static final class Pool extends org.openide.loaders.DataLoaderPool {
410         protected java.util.Enumeration JavaDoc loaders () {
411             return org.openide.util.Enumerations.singleton(MyLoader.get ());
412         }
413     }
414     
415     public static final class MyLoader extends org.openide.loaders.UniFileLoader {
416         public int primary;
417         
418         public static MyLoader get () {
419             return (MyLoader)MyLoader.findObject (MyLoader.class, true);
420         }
421         
422         public MyLoader() {
423             super(MyDataObject.class.getName ());
424             getExtensions ().addExtension ("obj");
425         }
426         protected String JavaDoc displayName() {
427             return "MyPart";
428         }
429         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
430             return new MyDataObject(this, primaryFile);
431         }
432         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
433             primary++;
434             return new org.openide.loaders.FileEntry (obj, primaryFile);
435         }
436     }
437     public static final class MyDataObject extends MultiDataObject
438     implements CookieSet.Factory {
439         public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException {
440             super(folder, l);
441             getCookieSet ().add (OpenCookie.class, this);
442         }
443
444         public org.openide.nodes.Node.Cookie createCookie (Class JavaDoc klass) {
445             return new DES (this, new MyEnv (this));
446         }
447         
448         protected Node createNodeDelegate() {
449             return new MyNode(this, Children.LEAF);
450         }
451     }
452
453     /* Node which always returns non-null getHtmlDisplayName */
454     public static final class MyNode extends DataNode {
455         
456         public MyNode (DataObject obj, Children ch) {
457             super(obj, ch);
458         }
459         
460         public String JavaDoc getHtmlDisplayName() {
461             return "<b>" + getDisplayName() + "</b>";
462         }
463     }
464     
465 }
466
Popular Tags