KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.BadLocationException JavaDoc;
33 import javax.swing.text.Document 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.ErrorManager;
42 import org.openide.ErrorManager.Annotation;
43 import org.openide.actions.*;
44 import org.openide.cookies.CloseCookie;
45 import org.openide.cookies.EditCookie;
46
47 import org.openide.cookies.EditorCookie;
48 import org.openide.cookies.OpenCookie;
49 import org.openide.cookies.PrintCookie;
50 import org.openide.cookies.SaveCookie;
51 import org.openide.filesystems.FileLock;
52 import org.openide.filesystems.FileObject;
53 import org.openide.filesystems.FileStateInvalidException;
54 import org.openide.filesystems.FileSystem;
55 import org.openide.filesystems.Repository;
56 import org.openide.loaders.DataFolder;
57 import org.openide.loaders.DataNode;
58 import org.openide.loaders.DataObject;
59 import org.openide.loaders.DataObjectExistsException;
60 import org.openide.loaders.ExtensionList;
61 import org.openide.loaders.MultiDataObject;
62 import org.openide.loaders.MultiFileLoader;
63 import org.openide.loaders.UniFileLoader;
64 import org.openide.nodes.Children;
65 import org.openide.nodes.CookieSet;
66 import org.openide.nodes.Node;
67 import org.openide.text.CloneableEditorSupport;
68 import org.openide.util.HelpCtx;
69 import org.openide.util.io.NbMarshalledObject;
70 import org.openide.util.Lookup;
71 import org.openide.util.LookupListener;
72 import org.openide.util.NbBundle;
73 import org.openide.util.actions.SystemAction;
74 import org.openide.windows.CloneableOpenSupport;
75 import org.openide.windows.WindowManager;
76
77
78 /**
79  */

80 public class EvenIfReadonlyItNeedsToThrowExceptionTest extends NbTestCase {
81     // for file object support
82
String JavaDoc content = "";
83     long expectedSize = -1;
84     java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
85     
86     MyFileObject fileObject;
87     org.openide.filesystems.FileSystem fs;
88     static {
89         System.setProperty ("org.openide.util.Lookup", "org.openide.text.EvenIfReadonlyItNeedsToThrowExceptionTest$Lkp");
90     }
91     
92     public EvenIfReadonlyItNeedsToThrowExceptionTest(String JavaDoc s) {
93         super(s);
94     }
95     
96     protected void setUp () throws Exception JavaDoc {
97         fs = org.openide.filesystems.FileUtil.createMemoryFileSystem ();
98         org.openide.filesystems.Repository.getDefault ().addFileSystem (fs);
99         org.openide.filesystems.FileObject root = fs.getRoot ();
100         fileObject = new MyFileObject (org.openide.filesystems.FileUtil.createData (root, "my.obj"));
101     }
102     
103     protected void tearDown () throws Exception JavaDoc {
104         waitEQ ();
105         org.openide.filesystems.Repository.getDefault ().removeFileSystem (fs);
106     }
107     
108     protected boolean runInEQ() {
109         return false;
110     }
111     
112     private void waitEQ () throws Exception JavaDoc {
113         javax.swing.SwingUtilities.invokeAndWait (new Runnable JavaDoc () { public void run () { } });
114     }
115
116     DES support () throws Exception JavaDoc {
117         DataObject obj = DataObject.find (fileObject);
118         
119         assertEquals ("My object was created", MyDataObject.class, obj.getClass ());
120         Object JavaDoc cookie = obj.getCookie (org.openide.cookies.OpenCookie.class);
121         assertNotNull ("Our object has this cookie", cookie);
122         assertEquals ("It is my cookie", DES.class, cookie.getClass ());
123         
124         return (DES)cookie;
125     }
126     
127     public void testSaveThrowsException() throws IOException JavaDoc, BadLocationException JavaDoc, Exception JavaDoc {
128         
129         fileObject.canWrite = true;
130         
131         DES des = support();
132         des.open();
133         waitEQ();
134         
135         Document JavaDoc doc = des.openDocument();
136         
137         doc.insertString(0, "Ahoj", null);
138         
139         assertTrue("Now it is modified", des.isModified());
140         
141         fileObject.canWrite = false;
142         
143         try {
144             des.saveDocument();
145             fail("This has to throw exception");
146         } catch (IOException JavaDoc ex) {
147             ErrorManager.Annotation[] ann = ErrorManager.getDefault().findAnnotations(ex);
148             assertNotNull("There are annotations", ann);
149         }
150     }
151     
152     
153     /** File object that let us know what is happening and delegates to certain
154      * instance variables of the test.
155      */

156     private static final class MyFileObject extends org.openide.filesystems.FileObject {
157         private org.openide.filesystems.FileObject delegate;
158         public boolean canWrite;
159         public String JavaDoc content;
160         
161         public MyFileObject (org.openide.filesystems.FileObject del) {
162             delegate = del;
163         }
164
165         public java.io.OutputStream JavaDoc getOutputStream (FileLock lock) throws IOException JavaDoc {
166             class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
167                 public void close () throws java.io.IOException JavaDoc {
168                     super.close ();
169                     content = new String JavaDoc (toByteArray ());
170                 }
171             }
172
173             return new ContentStream ();
174         }
175
176         public void delete (FileLock lock) throws IOException JavaDoc {
177             delegate.delete (lock);
178         }
179
180         public void setImportant (boolean b) {
181             delegate.setImportant (b);
182         }
183
184         public void addFileChangeListener (org.openide.filesystems.FileChangeListener fcl) {
185             delegate.addFileChangeListener (fcl);
186         }
187
188         public void removeFileChangeListener (org.openide.filesystems.FileChangeListener fcl) {
189             delegate.removeFileChangeListener (fcl);
190         }
191
192         public Object JavaDoc getAttribute (String JavaDoc attrName) {
193             return delegate.getAttribute (attrName);
194         }
195
196         public FileObject createFolder (String JavaDoc name) throws IOException JavaDoc {
197             throw new IOException JavaDoc ("Not supported");
198         }
199
200         public void rename (FileLock lock, String JavaDoc name, String JavaDoc ext) throws IOException JavaDoc {
201             throw new IOException JavaDoc ("Not supported");
202         }
203
204         public void setAttribute (String JavaDoc attrName, Object JavaDoc value) throws IOException JavaDoc {
205             delegate.setAttribute (attrName, value);
206         }
207
208         public String JavaDoc getName () {
209             return delegate.getName ();
210         }
211
212         public java.io.InputStream JavaDoc getInputStream () throws java.io.FileNotFoundException JavaDoc {
213             return new java.io.ByteArrayInputStream JavaDoc (new byte[0]);
214         }
215
216         public FileSystem getFileSystem () throws FileStateInvalidException {
217             return delegate.getFileSystem ();
218         }
219
220         public FileObject getFileObject (String JavaDoc name, String JavaDoc ext) {
221             return null;
222         }
223
224         public String JavaDoc getExt () {
225             return delegate.getExt ();
226         }
227
228         public FileObject[] getChildren () {
229             return null;
230         }
231
232         public java.util.Enumeration JavaDoc getAttributes () {
233             return delegate.getAttributes ();
234         }
235
236         public FileObject createData (String JavaDoc name, String JavaDoc ext) throws IOException JavaDoc {
237             throw new IOException JavaDoc ("Not supported");
238         }
239
240         public FileObject getParent () {
241             return delegate.getParent ();
242         }
243
244         public long getSize () {
245             return 0;
246         }
247
248         public boolean isData () {
249             return true;
250         }
251
252         public boolean isFolder () {
253             return false;
254         }
255
256         public boolean isReadOnly () {
257             return !canWrite;
258         }
259
260         public boolean isRoot () {
261             return false;
262         }
263
264         public boolean isValid () {
265             return delegate.isValid ();
266         }
267
268         public java.util.Date JavaDoc lastModified () {
269             return new java.util.Date JavaDoc();
270         }
271
272         public FileLock lock () throws IOException JavaDoc {
273             return delegate.lock ();
274         }
275         
276         public boolean canWrite() {
277             return canWrite;
278         }
279     }
280     
281     /** Implementation of the DES */
282     private static final class DES extends DataEditorSupport
283     implements OpenCookie, EditCookie {
284         public DES (DataObject obj, Env env) {
285             super (obj, env);
286         }
287         
288         public org.openide.windows.CloneableTopComponent.Ref getRef () {
289             return allEditors;
290         }
291         
292     }
293     
294     /** MyEnv that uses DataEditorSupport.Env */
295     private static final class MyEnv extends DataEditorSupport.Env {
296         static final long serialVersionUID = 1L;
297         
298         public MyEnv (DataObject obj) {
299             super (obj);
300         }
301         
302         protected FileObject getFile () {
303             return super.getDataObject ().getPrimaryFile ();
304         }
305
306         protected FileLock takeLock () throws IOException JavaDoc {
307             return super.getDataObject ().getPrimaryFile ().lock ();
308         }
309         
310     }
311
312     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
313         public Lkp () {
314             this (new org.openide.util.lookup.InstanceContent ());
315         }
316         
317         private Lkp (org.openide.util.lookup.InstanceContent ic) {
318             super (ic);
319             
320             ic.add (new Pool ());
321         }
322         
323     } // end of Lkp
324

325     private static final class Pool extends org.openide.loaders.DataLoaderPool {
326         protected java.util.Enumeration JavaDoc loaders () {
327             return org.openide.util.Enumerations.singleton(MyLoader.get ());
328         }
329     }
330     
331     public static final class MyLoader extends org.openide.loaders.UniFileLoader {
332         public int primary;
333         
334         public static MyLoader get () {
335             return (MyLoader)MyLoader.findObject (MyLoader.class, true);
336         }
337         
338         public MyLoader() {
339             super(MyDataObject.class.getName ());
340             getExtensions ().addExtension ("obj");
341         }
342         protected String JavaDoc displayName() {
343             return "MyPart";
344         }
345         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
346             return new MyDataObject(this, primaryFile);
347         }
348         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
349             primary++;
350             return new org.openide.loaders.FileEntry (obj, primaryFile);
351         }
352     }
353     public static final class MyDataObject extends MultiDataObject
354     implements CookieSet.Factory {
355         public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException {
356             super(folder, l);
357             getCookieSet ().add (OpenCookie.class, this);
358         }
359
360         public org.openide.nodes.Node.Cookie createCookie (Class JavaDoc klass) {
361             return new DES (this, new MyEnv (this));
362         }
363         
364         protected Node createNodeDelegate() {
365             return new MyNode(this, Children.LEAF);
366         }
367     }
368
369     /* Node which always returns non-null getHtmlDisplayName */
370     public static final class MyNode extends DataNode {
371         
372         public MyNode (DataObject obj, Children ch) {
373             super(obj, ch);
374         }
375         
376         public String JavaDoc getHtmlDisplayName() {
377             return "<b>" + getDisplayName() + "</b>";
378         }
379     }
380     
381 }
382
Popular Tags