KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32 import javax.swing.text.Document JavaDoc;
33 import javax.swing.text.Position JavaDoc;
34 import javax.swing.text.StyledDocument JavaDoc;
35
36 import junit.textui.TestRunner;
37
38 import org.netbeans.junit.NbTestCase;
39 import org.netbeans.junit.NbTestSuite;
40 import org.openide.actions.*;
41 import org.openide.cookies.CloseCookie;
42 import org.openide.cookies.EditCookie;
43
44 import org.openide.cookies.EditorCookie;
45 import org.openide.cookies.OpenCookie;
46 import org.openide.cookies.PrintCookie;
47 import org.openide.cookies.SaveCookie;
48 import org.openide.filesystems.FileLock;
49 import org.openide.filesystems.FileObject;
50 import org.openide.filesystems.FileStateInvalidException;
51 import org.openide.filesystems.FileSystem;
52 import org.openide.filesystems.FileUtil;
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.io.NbMarshalledObject;
68 import org.openide.util.Lookup;
69 import org.openide.util.LookupListener;
70 import org.openide.util.NbBundle;
71 import org.openide.util.actions.SystemAction;
72 import org.openide.windows.CloneableOpenSupport;
73 import org.openide.windows.WindowManager;
74
75
76 /**
77  */

78 public class PeterZMoveTest extends NbTestCase {
79     // for file object support
80
String JavaDoc content = "";
81     long expectedSize = -1;
82     java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
83
84     FileObject fileObject;
85     org.openide.filesystems.FileSystem fs;
86     static PeterZMoveTest RUNNING;
87     static {
88         System.setProperty ("org.openide.util.Lookup", "org.openide.text.PeterZMoveTest$Lkp");
89     }
90     
91     public PeterZMoveTest(String JavaDoc s) {
92         super(s);
93     }
94     
95     protected void setUp () throws Exception JavaDoc {
96         RUNNING = this;
97         
98         fs = org.openide.filesystems.FileUtil.createMemoryFileSystem ();
99         org.openide.filesystems.Repository.getDefault ().addFileSystem (fs);
100         org.openide.filesystems.FileObject root = fs.getRoot ();
101         fileObject = org.openide.filesystems.FileUtil.createData (root, "my.obj");
102     }
103     
104     protected void tearDown () throws Exception JavaDoc {
105         waitEQ ();
106         
107         RUNNING = null;
108         org.openide.filesystems.Repository.getDefault ().removeFileSystem (fs);
109     }
110     
111     protected boolean runInEQ() {
112         return false;
113     }
114     
115     private void waitEQ () throws Exception JavaDoc {
116         javax.swing.SwingUtilities.invokeAndWait (new Runnable JavaDoc () { public void run () { } });
117     }
118
119     DES support () throws Exception JavaDoc {
120         DataObject obj = DataObject.find (fileObject);
121         
122         assertEquals ("My object was created", MyDataObject.class, obj.getClass ());
123         Object JavaDoc cookie = obj.getCookie (org.openide.cookies.OpenCookie.class);
124         assertNotNull ("Our object has this cookie", cookie);
125         assertEquals ("It is my cookie", DES.class, cookie.getClass ());
126         
127         return (DES)cookie;
128     }
129     
130     /** holds the instance of the object so insane is able to find the reference */
131     private DataObject obj;
132     public void testWhenMovingAFileNoLockshallBetaken () throws Exception JavaDoc {
133         DES sup = support ();
134         assertFalse ("It is closed now", support ().isDocumentLoaded ());
135         
136         Lookup lkp = sup.getLookup ();
137         obj = (DataObject)lkp.lookup (DataObject.class);
138         assertNotNull ("DataObject found", obj);
139         
140         Document JavaDoc d = sup.openDocument ();
141         assertTrue ("It is open now", support ().isDocumentLoaded ());
142
143         d.insertString(0, "Ahoj", null);
144         
145         assertTrue("Really modified", sup.isModified());
146         
147         sup.saveDocument();
148         
149         FileObject fo = FileUtil.createFolder(obj.getFolder().getPrimaryFile(), "newfold");
150         DataFolder nf = DataFolder.findFolder(fo);
151         
152         obj.move(nf);
153         
154         FileLock lock = obj.getPrimaryFile().lock();
155         assertNotNull("It is possible to take another lock", lock);
156     }
157     
158     /** Implementation of the DES */
159     private static final class DES extends DataEditorSupport
160     implements OpenCookie, EditCookie {
161         public DES (DataObject obj, Env env) {
162             super (obj, env);
163         }
164         
165         public org.openide.windows.CloneableTopComponent.Ref getRef () {
166             return allEditors;
167         }
168         
169     }
170     
171     /** MyEnv that uses DataEditorSupport.Env */
172     private static final class MyEnv extends DataEditorSupport.Env {
173         static final long serialVersionUID = 1L;
174         
175         public MyEnv (DataObject obj) {
176             super (obj);
177         }
178         
179         protected FileObject getFile () {
180             return super.getDataObject ().getPrimaryFile ();
181         }
182
183         protected FileLock takeLock () throws IOException JavaDoc {
184             return super.getDataObject ().getPrimaryFile ().lock ();
185         }
186         
187     }
188
189     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
190         public Lkp () {
191             this (new org.openide.util.lookup.InstanceContent ());
192         }
193         
194         private Lkp (org.openide.util.lookup.InstanceContent ic) {
195             super (ic);
196             
197             ic.add (new Pool ());
198         }
199         
200     } // end of Lkp
201

202     private static final class Pool extends org.openide.loaders.DataLoaderPool {
203         protected java.util.Enumeration JavaDoc loaders () {
204             return org.openide.util.Enumerations.singleton(MyLoader.get ());
205         }
206     }
207     
208     public static final class MyLoader extends org.openide.loaders.UniFileLoader {
209         public int primary;
210         
211         public static MyLoader get () {
212             return (MyLoader)MyLoader.findObject (MyLoader.class, true);
213         }
214         
215         public MyLoader() {
216             super(MyDataObject.class.getName ());
217             getExtensions ().addExtension ("obj");
218         }
219         protected String JavaDoc displayName() {
220             return "MyPart";
221         }
222         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
223             return new MyDataObject(this, primaryFile);
224         }
225         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
226             primary++;
227             return new org.openide.loaders.FileEntry (obj, primaryFile);
228         }
229     }
230     public static final class MyDataObject extends MultiDataObject
231     implements CookieSet.Factory {
232         public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException {
233             super(folder, l);
234             getCookieSet ().add (OpenCookie.class, this);
235         }
236
237         public org.openide.nodes.Node.Cookie createCookie (Class JavaDoc klass) {
238             return new DES (this, new MyEnv (this));
239         }
240         
241         
242         
243     }
244     
245 }
246
Popular Tags