KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DataShadowTest


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.loaders;
21
22 import java.io.File JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import junit.framework.AssertionFailedError;
29 import org.netbeans.junit.NbTestCase;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileStateInvalidException;
32 import org.openide.filesystems.FileSystem;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.LocalFileSystem;
35 import org.openide.filesystems.Repository;
36 import org.openide.nodes.Node;
37 import org.openide.util.Utilities;
38
39 /** Test things about shadows and broken shadows, etc.
40  * @author Jaroslav Tulach
41  */

42 public class DataShadowTest extends NbTestCase
43 implements java.net.URLStreamHandlerFactory JavaDoc {
44     /** original object */
45     private DataObject original;
46     /** folder to work with */
47     private DataFolder folder;
48     /** fs we work on */
49     private FileSystem lfs;
50
51     private Logger JavaDoc err;
52     
53     static {
54         // to handle nbfs urls...
55
java.net.URL.setURLStreamHandlerFactory (new DataShadowTest (null));
56     }
57     
58     public DataShadowTest (String JavaDoc name) {
59         super(name);
60     }
61
62     protected Level JavaDoc logLevel() {
63         return Level.FINER;
64     }
65     
66     protected void setUp() throws Exception JavaDoc {
67         
68         lfs = Repository.getDefault ().getDefaultFileSystem ();
69         
70         FileObject[] delete = lfs.getRoot().getChildren();
71         for (int i = 0; i < delete.length; i++) {
72             delete[i].delete();
73         }
74         
75         FileObject fo = FileUtil.createData (lfs.getRoot (), getName () + "/folder/original.txt");
76         assertNotNull(fo);
77         original = DataObject.find (fo);
78         assertFalse ("Just to be sure that this is not shadow", original instanceof DataShadow);
79         assertFalse ("And is some kind of subclass of DO", original.getClass () == DataObject.class);
80         fo = FileUtil.createFolder (lfs.getRoot (), getName () + "/modify");
81         assertNotNull(fo);
82         assertTrue (fo.isFolder ());
83         folder = DataFolder.findFolder (fo);
84         
85         Repository.getDefault ().addFileSystem (lfs);
86         
87         err = Logger.getLogger(getName());
88     }
89     
90     public java.net.URLStreamHandler JavaDoc createURLStreamHandler(String JavaDoc protocol) {
91         if (protocol.equals ("nbfs")) {
92             return FileUtil.nbfsURLStreamHandler ();
93         }
94         return null;
95     }
96     
97     public void testCreateTheShadow61175() throws Exception JavaDoc {
98         if (!Utilities.isUnix()) {
99             return;
100         }
101         
102         final LocalFileSystem lfs = new LocalFileSystem() {
103             public Status getStatus() {
104                 return new TestStatus(this);
105             }
106             
107             class TestStatus implements FileSystem.Status {
108                 FileSystem lfs;
109                 TestStatus(FileSystem lfs) {
110                     this.lfs = lfs;
111                 }
112                 public String JavaDoc annotateName(String JavaDoc name, java.util.Set JavaDoc files) {
113                     if (files.size() > 0) {
114                         try {
115                             FileSystem fs = ((FileObject)files.toArray()[0]).getFileSystem();
116                             assertEquals(fs, lfs);
117                         } catch(FileStateInvalidException fsx) {}
118                         
119                     }
120                     return name;
121                 }
122                 
123                 public java.awt.Image JavaDoc annotateIcon(java.awt.Image JavaDoc icon, int iconType, java.util.Set JavaDoc files) {
124                     return icon;
125                 }
126             }
127         };
128         
129         
130         lfs.setRootDirectory(new File JavaDoc("/"));
131         Repository.getDefault().addFileSystem(lfs);
132         
133         
134         DataFolder what = DataFolder.findFolder(lfs.getRoot());
135         assertNotNull(what);
136         FileObject whereFo = Repository.getDefault().getDefaultFileSystem().getRoot();
137         assertNotNull(whereFo);
138         
139         DataFolder where = DataFolder.findFolder(whereFo);
140         assertNotNull(where);
141         
142         DataShadow shade = what.createShadow(where);
143         assertNotNull(shade);
144         
145         Node node= shade.getNodeDelegate();
146         assertNotNull(node);
147         node.getIcon (java.beans.BeanInfo.ICON_COLOR_16x16);
148         
149     }
150     
151     public void testBrokenShadow55115 () throws Exception JavaDoc {
152         FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
153         FileObject brokenShadow = FileUtil.createData(sfs.getRoot(),"brokenshadows/brokon.shadow");
154         assertNotNull (brokenShadow);
155         // intentionally not set attribute "originalFile" to let that shadow be broken
156
//brokenShadow.setAttribute("originalFile", null);
157
BrokenDataShadow bds = (BrokenDataShadow)DataObject.find(brokenShadow);
158         assertNotNull (bds);
159         URL JavaDoc url = bds.getUrl();
160         //this call proves #55115 - but just in case if there is reachable masterfs
161
// - probably unwanted here
162
bds.refresh();
163         
164         //If masterfs isn't reachable - second test crucial for URL,File, FileObject conversions
165
// not necessary to be able to convert - but at least no IllegalArgumentException is expected
166
if ("file".equals(url.getProtocol())) {
167             new File JavaDoc (URI.create(url.toExternalForm()));
168         }
169     }
170     
171     public void testCreateTheShadow () throws Exception JavaDoc {
172         DataShadow shade = original.createShadow (folder);
173         
174         assertEquals ("Shadow's original is the one", original, shade.getOriginal ());
175         
176         Object JavaDoc cookie = shade.getCookie (DataObject.class);
177         assertEquals ("The shadow is own data object", shade, cookie);
178         
179         cookie = shade.getCookie (original.getClass ());
180         assertEquals ("But it also returns the original when requested", original, cookie);
181         
182         URL JavaDoc u = DataShadow.readURL(shade.getPrimaryFile());
183         assertEquals("DataShadow's URL must point to the Original", original.getPrimaryFile().getURL(), u);
184     }
185     
186     public void testDeleteInvalidatesCreateCreates () throws Exception JavaDoc {
187         doDeleteInvalidatesCreateCreates (true);
188     }
189     
190     /* This is not implemented and could cause problems when module is enabled
191      * and there is a link to a file in its layer - this link could possibly
192      * not be updated (until creation of another data object)
193      */

194     public void testDeleteInvalidatesCreateCreatesJustOnFileSystemLevel () throws Exception JavaDoc {
195         doDeleteInvalidatesCreateCreates (false);
196     }
197
198     private void doDeleteInvalidatesCreateCreates (boolean createDataObjectOrNot) throws Exception JavaDoc {
199         DataShadow shade = original.createShadow (folder);
200         FileObject primary = shade.getPrimaryFile ();
201
202         assertTrue ("Is valid now", shade.isValid ());
203         err.info("Before delete");
204         original.delete ();
205         err.info("After delete");
206         
207         assertFalse ("Shadow is not valid anymore", shade.isValid ());
208         assertFalse ("Original is gone", original.isValid ());
209         
210         err.info("Going to find new shadow");
211         DataObject shade2 = DataObject.find (primary);
212         err.info("Found: " + shade2);
213         assertEquals ("Represents broken shadow (a bit implemetnation detail, but useful test)", BrokenDataShadow.class, shade2.getClass ());
214         assertFalse ("Is not data shadow", shade2 instanceof DataShadow);
215         
216         // recreates the original
217
err.info("Before recreation of the original");
218         FileObject original2 = FileUtil.createData (lfs.getRoot (), original.getPrimaryFile ().getPath ());
219         err.info("Original is there: " + original2);
220         DataObject obj2;
221         
222         if (createDataObjectOrNot) {
223             err.info("Now get the data object");
224             obj2 = DataObject.find (original2);
225             err.info("Object is there: " + obj2);
226         }
227         
228         assertFalse ("Previous is not valid anymore", shade2.isValid ());
229         
230         DataObject shade3 = DataObject.find (primary);
231         err.info("Shade3 is here: " + shade3);
232         assertTrue ("it is a data shadow again", shade3 instanceof DataShadow);
233         assertEquals ("Points to the same filename", original.getPrimaryFile ().getPath (), ((DataShadow)shade3).getOriginal ().getPrimaryFile ().getPath ());
234
235         err.info("Before find for " + original2);
236         assertEquals ("But of course the original is newly created", DataObject.find (original2), ((DataShadow)shade3).getOriginal ());
237
238         err.info("Before shade.getOriginal()");
239         assertEquals ("The old shadow is updated & getOriginal() waits for that to happen", original2, shade.getOriginal().getPrimaryFile());
240     }
241
242     public void testDeleteInvalidatesCreateCreatesWhenChangeHappensInAtomicAction () throws Exception JavaDoc {
243         DataShadow shade = original.createShadow (folder);
244         FileObject primary = shade.getPrimaryFile ();
245
246         assertTrue ("Is valid now", shade.isValid ());
247         
248         class DeleteCreate implements FileSystem.AtomicAction {
249             public FileObject fo;
250             
251             public void run () throws java.io.IOException JavaDoc {
252                 FileSystem fs = original.getPrimaryFile ().getFileSystem ();
253                 String JavaDoc create = original.getPrimaryFile ().getPath ();
254                 original.getPrimaryFile ().delete ();
255                 
256                 fo = FileUtil.createData (fs.getRoot (), create);
257             }
258         }
259         DeleteCreate deleteCreate = new DeleteCreate ();
260         original.getPrimaryFile ().getFileSystem ().runAtomicAction (deleteCreate);
261         
262         assertTrue ("Shadow is valid (again)", shade.isValid ());
263         assertFalse ("Original is gone", original.isValid ());
264         DataObject orig = DataObject.find (deleteCreate.fo);
265         if (orig == original) {
266             fail ("new original shall be created");
267         }
268         assertTrue ("New orig is valid", orig.isValid ());
269         
270         // life would be nicer without this sleep, but somewhere inside
271
// the DataShadow validation a request is send to RP with a delay
272
// to not slow down regular apps. If you managed to kill next line,
273
// you will have done the right job. Meanwhile it is here:
274
Thread.sleep (2000);
275         
276         assertEquals ("Shadow's original is updated", orig, shade.getOriginal ());
277     }
278     
279     public void testRenameUpdatesTheShadowIfItExists () throws Exception JavaDoc {
280         DataShadow shade = original.createShadow (folder);
281         FileObject primary = shade.getPrimaryFile ();
282         
283         original.rename ("newname.txt");
284         
285         WeakReference JavaDoc ref = new WeakReference JavaDoc (shade);
286         shade = null;
287         assertGC ("Shadow can disappear", ref);
288         
289         DataObject obj = DataObject.find (primary);
290         assertEquals ("It is shadow", DataShadow.class, obj.getClass ());
291         shade = (DataShadow)obj;
292         
293         assertEquals ("And points to original with updated name", original, shade.getOriginal ());
294     }
295     
296     public void testRenameDoesNotUpdateTheShadowIfItDoesNotExist () throws Exception JavaDoc {
297         //
298
// Not sure if this is the desired behaviour, however it is the
299
// one currently implemented
300
//
301

302         DataShadow shade = original.createShadow (folder);
303         FileObject primary = shade.getPrimaryFile ();
304         
305         WeakReference JavaDoc ref = new WeakReference JavaDoc (shade);
306         shade = null;
307         assertGC ("Shadow can disappear", ref);
308         
309         original.rename ("newname");
310         
311         
312         DataObject obj = DataObject.find (primary);
313         assertEquals ("It is broken shadow", BrokenDataShadow.class, obj.getClass ());
314     }
315     
316     public void testBrokenShadowNodeProperties() throws Exception JavaDoc {
317         DataShadow shade = original.createShadow (folder);
318         FileObject primary = shade.getPrimaryFile ();
319         
320         assertTrue ("Is valid now", shade.isValid ());
321         original.delete ();
322
323         DataObject obj = DataObject.find (primary);
324         assertEquals ("Instance class must be BrokenDataShadow", BrokenDataShadow.class, obj.getClass ());
325         
326         Node node = obj.getNodeDelegate ();
327         
328         Node.Property link = findProperty (node, "BrokenLink");
329         assertNotNull ("Link must be non null string", (String JavaDoc)link.getValue ());
330         
331         assertTrue ("Is writeable", link.canWrite ());
332         // this will revalidate the link
333
FileObject fo = FileUtil.createData (lfs.getRoot (), getName () + "/folder/orig.txt");
334         link.setValue (fo.getURL().toExternalForm());
335        
336         assertFalse ("The change of link should turn the shadow to valid one and invalidate this broken shadow", obj.isValid ());
337         
338         DataObject newObj = DataObject.find (primary);
339         assertEquals ("This is a shadow", DataShadow.class, newObj.getClass ());
340         shade = (DataShadow)newObj;
341         
342         assertEquals ("Points to the new file", getName () + "/folder/orig.txt", shade.getOriginal ().getPrimaryFile ().getPath ());
343     }
344     
345     public void testFindOriginalFromAnonymousFilesystem() throws Exception JavaDoc {
346         // Helpful for XML layer editing.
347
FileSystem fs = FileUtil.createMemoryFileSystem();
348         FileObject orig = FileUtil.createData(fs.getRoot(), "path/to/orig");
349         FileObject shadow = FileUtil.createData(fs.getRoot(), "link.shadow");
350         shadow.setAttribute("originalFile", "path/to/orig");
351         assertEquals("found the right original file", DataObject.find(orig), DataShadow.deserialize(shadow));
352         orig = FileUtil.createData(fs.getRoot(), "path to orig");
353         shadow = FileUtil.createData(fs.getRoot(), "link2.shadow");
354         shadow.setAttribute("originalFile", "path to orig");
355         assertEquals("found the right original file", DataObject.find(orig), DataShadow.deserialize(shadow));
356     }
357     
358     private static Node.Property findProperty (Node n, String JavaDoc name) {
359         Node.PropertySet[] arr = n.getPropertySets ();
360         StringBuffer JavaDoc names = new StringBuffer JavaDoc ();
361         
362         String JavaDoc prefix = "";
363         for (int i = 0; i < arr.length; i++) {
364             Node.PropertySet set = arr[i];
365             Node.Property[] properties = set.getProperties ();
366             for (int j = 0; j < properties.length; j++) {
367                 Node.Property p = properties[j];
368                 if (name.equals (p.getName ())) {
369                     return p;
370                 }
371                 names.append (prefix);
372                 names.append (p.getName ());
373                 prefix = ", ";
374             }
375         }
376         
377         fail ("Cannot find property \"" + name + "\" in node " + n + " it has only " + names + " propeties.");
378         return null;
379     }
380 }
381
Popular Tags