KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > unit > org > openide > filesystems > FilesystemBugs


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 unit.org.openide.filesystems;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Properties JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29 import org.openide.filesystems.FileChangeAdapter;
30 import org.openide.filesystems.FileEvent;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileSystem;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.JarFileSystem;
35 import org.openide.filesystems.LocalFileSystem;
36 import org.openide.filesystems.MultiFileSystem;
37 import org.openide.filesystems.Repository;
38 import org.openide.filesystems.URLMapper;
39 import org.openide.filesystems.XMLFileSystem;
40 import org.openide.util.Lookup;
41
42 /**
43  *
44  * @author pz97949
45  */

46 public class FilesystemBugs extends NbTestCase {
47     final String JavaDoc FOLDER1 = "F";
48 final String JavaDoc FO1 ="testNFOADelete";
49     private int counter;
50     /** Creates a new instance of FilesystemBugs */
51     public FilesystemBugs(String JavaDoc name) {
52         super(name);
53     }
54     /**
55     * Let's have a file hierarchy A/B/C. If you decide to delete the folder A then listeners registered to
56     * file objects B and C do not have any notion these file objects were deleted. IMO it is incorrect
57     * behavior.
58     * It also hurts the org.netbeans.core.xml.FileEntityResolver.update implementation.
59    */

60
61     public void testNotifyOfSubFoldersAfterDelete23929 () throws IOException JavaDoc {
62         counter = 0;
63         if (canGenWriteFolder()) {
64             // create tree an register listener
65
//
66
FileObject folder = getWriteFolder();
67            log(folder.toString());
68            folder = getSubFolder(folder, FOLDER1);
69            FileObject tmpFolder = folder;
70            System.out.println(folder);
71            for (int i = 0 ; i < 10 ; i++ ) {
72                FileObject subFolder = getSubFolder(tmpFolder,FOLDER1 + i);
73                subFolder.addFileChangeListener(new TestFileChangeListener());
74                for (int j = 0 ; j < 10 ; j++ ) {
75                    FileObject fo = getFileObject(tmpFolder, FO1 + j);
76                    fo.addFileChangeListener(new TestFileChangeListener());
77                }
78                tmpFolder = subFolder;
79            }
80            // delete tree and check counts of calls (must be 209)
81
//
82
try {
83               folder.delete();
84                assertTrue ("test failed, deleted " + counter + " != 209" , counter == 209);
85            } catch (Exception JavaDoc e) {
86                assertTrue("cannot delete folder",false);
87            }
88            
89         } else {
90              log ("[OK] cannot get write folder on " + getFSType());
91         }
92     }
93     /** get/create subfolder in folder
94      */

95     protected FileObject getSubFolder(FileObject folder, String JavaDoc name) {
96         try {
97             FileObject fo = folder.getFileObject(name);
98             if (fo == null) {
99                 return folder.createFolder(name);
100             }
101             return fo;
102         } catch (IOException JavaDoc e) {
103             e.printStackTrace();
104             assertTrue("cannot get subFolder " + name + " in " + folder.toString(),false);
105         }
106         return null;
107     }
108     /** get/create FilObject in folder
109      */

110     protected FileObject getFileObject(FileObject folder, String JavaDoc name) {
111         try {
112             FileObject fo = folder.getFileObject(name);
113             if (fo == null) {
114                 return folder.createData(name);
115             }
116             return fo;
117         } catch (Exception JavaDoc e) {
118             assertTrue("cannot get subFolder " + name + " in " + folder.toString(),false);
119         }
120         return null;
121     }
122     
123     private class TestFileChangeListener extends FileChangeAdapter {
124         public void fileDeleted(FileEvent ev) {
125             log("Delete: " + ev.getFile().getPath());
126             counter++;
127         }
128     }
129        
130         
131     
132     protected boolean canGenWriteFolder() {
133         return true;
134     }
135     protected FileObject getWriteFolder() throws IOException JavaDoc {
136         String JavaDoc folderName = "unit/org/openide/filesystems/";
137         FileObject folder = Repository.getDefault().findResource(folderName);
138         assertNotNull("folder : " + folder + " doesn't exists.",folder);
139         FileObject myFolder = folder.getFileObject("data");
140         if (myFolder == null) {
141            myFolder = folder.createFolder("data");
142         }
143         return folder;
144     }
145     
146     protected String JavaDoc getFSType() {
147         return "LocalFileSystem";
148     }
149
150      /** #8124 When attributes are deleted from file objects, so that no attributes remain set
151       * on any file objects in a folder, the .nbattrs file should be deleted. When no
152       * attributes remain on a particular file object, that <fileobject> tag should be
153       * deleted from the .nbattrs even if others remain.
154       */

155      public void testDeleteAttrsFileAfterDeleteAttrib() throws IOException JavaDoc {
156          FileObject folder = getWriteFolder();
157          FileObject fo = folder.getFileObject("testAttr");
158          if (fo == null) {
159             fo = folder.createData("testAttr");
160          }
161          // set any attribute
162
fo.setAttribute("blbost","blbost");
163          // flush
164
System.gc();
165          System.gc();
166          // delete all attributes
167
FileObject fos[] = folder.getChildren();
168          for (int i = 0 ; i < fos.length ; i++ ) {
169              Enumeration JavaDoc keys = fos[i].getAttributes();
170              while (keys.hasMoreElements()) {
171                  fos[i].setAttribute((String JavaDoc)keys.nextElement(),null);
172              }
173          }
174          // flush
175
System.gc();
176          System.gc();
177          // test if exists .nbattrs
178
File JavaDoc nbattrs = new File JavaDoc(FileUtil.toFile(folder),".nbattrs");
179          assertTrue("Empty nbattrs exists in folder:" + FileUtil.toFile(folder) , nbattrs.exists() == false);
180          
181          
182      }
183 /**#30397 FileURL.encodeFileObject(FileSystem fs, FileObject
184 fo) breakes the public contract of java.net.URL by
185 setting the hostname as null value. Even if the
186 URL constructor implementation permits passing
187 null value the javadoc does not. It has fatal
188 impact on comparisons of URL objects then (see
189 URLStreamHandler.hostsEqual). Following code
190 always fails*/

191
192      public void testURLContract() throws IOException JavaDoc {
193          FileObject fo = getWriteFolder();
194           URL JavaDoc u = fo.getURL();
195          assertEquals(u, new URL JavaDoc(u.toExternalForm()));
196      }
197 /** #10507 After implementation of issue 18220 it has been
198 realized that pluggable mapping is also needed
199 from URL to FileObject, I suggest to implement it
200 by adding methods:
201
202 static FileObject findFileObject (URL url);
203 abstract FileObject getFileObject (URL url);
204
205 both able to return null. Please do it now before
206 the URLMapper appears in the 3.4 release.
207 */

208
209      public void testFindFOfromURL() throws IOException JavaDoc {
210          FileObject fo = getWriteFolder();
211           URL JavaDoc u = fo.getURL();
212           URLMapper urlMapper = (URLMapper) Lookup.getDefault().lookup(URLMapper.class);
213           FileObject fos[] = urlMapper.findFileObjects(u);
214           assertTrue("url " + u + " must to only one FileObject",fos.length==1);
215           assertTrue("The references must be equal.", fo == fos[0]);
216           
217      }
218      /* http://installer.netbeans.org/issues/show_bug.cgi?id=26400
219       *If I have a multifilesystem and set an attribute
220     on one of its fileobjects, I will get strange new
221     attributes on all parent folders of this fileobject.
222
223     Example will make it clear:
224
225     If I set attribute "version" on fileobject fileA
226     which lies in folders /folder1/folder2, then if
227     you ask for all attributes on folder2, you will
228     get an attribute with name "fileA\version".
229     Similarly on folder1 you will get attribute
230     "folder2\fileA\version". If I look into .nbattrs
231     file there is just one attribute with name
232     "folder1\folder2\fileA\version".
233       */

234     public void testMultiAttrsBug26400() throws Exception JavaDoc {
235             File JavaDoc f1,f2;
236             File JavaDoc dir = new File JavaDoc(File.createTempFile("fsdf","eew").getParentFile(),"aret");
237             dir.mkdirs();
238             assertTrue(dir.isDirectory());
239             f1 = new File JavaDoc(dir,"tm26400a");
240             f2 = new File JavaDoc(dir,"tm26400b");
241             f1.mkdir();
242             f2.mkdir();
243             LocalFileSystem lfs1 = new LocalFileSystem();
244             LocalFileSystem lfs2 = new LocalFileSystem();
245             lfs1.setRootDirectory(f1);
246             lfs2.setRootDirectory(f2);
247             ///
248

249             MultiFileSystem mfs = new MultiFileSystem(new FileSystem[]{lfs1,lfs2});
250             FileObject rootMfs = mfs.getRoot();
251         // FileObject fomc = rootMfs.createData("c");
252
FileObject folder = getSubFolder(rootMfs,"a");
253             FileObject fo = getFileObject(folder, "b");
254             fo.setAttribute("attr","value");
255             assertTrue("folder contains attribute", folder.getAttributes().hasMoreElements() == false);
256             assertTrue("FileObject doesn't contain attribute attr.", fo.getAttribute("attr").equals("value"));
257     }
258 /*
259  URL returned must be terminated by "/" if fileobject represents folder.
260 Othervise it violated URL specs and it causes troubles while contructing contexted URL:
261
262   new URL(folder.getURL(), "test.txt");
263
264 is now always searched in parent folder.
265  */

266
267
268     public void testFolderSlashUrl () throws Exception JavaDoc {
269           URL JavaDoc u = getWriteFolder().getURL();
270           assertTrue("invalid url of directory",u.getPath().endsWith("/"));
271     }
272      
273   ////////////////////////
274

275       public void testOpenJarManifestAsResource() throws Exception JavaDoc {
276         Repository repo = Repository.getDefault ();
277         JarFileSystem jfs = new JarFileSystem ();
278         File JavaDoc nbHome = new File JavaDoc(System.getProperty("netbeans.home"));
279         File JavaDoc jarName = new File JavaDoc(new File JavaDoc (nbHome,"lib"), "openide.jar");
280         jfs.setJarFile (jarName);
281         repo.addFileSystem (jfs);
282         repo.removeFileSystem (jfs);
283         //try {
284
//FileObject fo = jfs.findResource ("META-INF/MANIFEST.MF");
285
FileObject fo = jfs.findResource ("org/openide/filesystems/Repository.class");
286             assertTrue("FileObject must to be valid", fo.isValid ());
287             try {
288               assertNotNull("FileObject must to have InputStream (fo.getInputStream ()");
289             } catch (Exception JavaDoc e) {
290                 e.printStackTrace(getLog());
291                 fail("Exception " + e);
292             }
293                 
294         //} finally {
295
// repo.removeFileSystem (jfs);
296
//}
297

298     }
299     public void testBackSlashAttribute33459() throws IOException JavaDoc {
300         FileObject fo = getWriteFolder();
301         String JavaDoc attribName = "y\\u2dasfas";
302         System.gc();
303         System.gc();
304         try {
305            fo.setAttribute(attribName,attribName);
306            System.gc();
307            System.gc();
308            assertTrue("Attribute is not equal", fo.getAttribute(attribName).equals(attribName));
309            System.out.println("ok");
310         } catch (Exception JavaDoc e) {
311             log(e.toString());
312             e.printStackTrace();
313             fail("Exception :" + e ) ;
314         }
315     }
316     
317      public static NbTestSuite suite() {
318          NbTestSuite suite = new NbTestSuite();
319          suite.addTest(new FilesystemBugs("testDeleteAttrsFileAfterDeleteAttrib"));
320          suite.addTest(new FilesystemBugs("testMultiAttrsBug26400"));
321          suite.addTest(new FilesystemBugs("testNotifyOfSubFoldersAfterDelete23929"));
322          suite.addTest(new FilesystemBugs("testURLContract"));
323          suite.addTest(new FilesystemBugs("testFindFOfromURL"));
324          suite.addTest(new FilesystemBugs("testOpenJarManifestAsResource"));
325          suite.addTest(new FilesystemBugs("testFolderSlashUrl"));
326          suite.addTest(new FilesystemBugs("testBackSlashAttribute33459"));
327          return suite;
328      }
329     
330      public void testMultiLayers19725() throws Exception JavaDoc {
331          XMLFileSystem origFs = new XMLFileSystem(FilesystemBugs.class.getResource("layer1.xml"));
332          XMLFileSystem brandedFs = new XMLFileSystem(FilesystemBugs.class.getResource("layer2.xml"));
333          MultiFileSystem mfs = new MultiFileSystem(new FileSystem[]{brandedFs,origFs});
334          FileObject fo = mfs.findResource("myfolder");
335          log("file1/file2 = " + fo.getAttribute("file1/file2").toString());
336          log("file2/file1 = " + fo.getAttribute("file2/file1").toString());
337          assertTrue(fo.getAttribute("file1/file2").equals(Boolean.FALSE));
338          assertTrue(fo.getAttribute("file2/file1").equals(Boolean.TRUE));
339      }
340      
341     /**
342      * @param args the command line arguments
343      */

344     public static void main(String JavaDoc[] args) {
345         junit.textui.TestRunner.run(new FilesystemBugs("testMultiLayers19725"));
346     }
347     
348 }
349
Popular Tags