KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > LocalFileSystemTestHid


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.filesystems;
21
22 import java.io.File JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.SyncFailedException JavaDoc;
25
26 public class LocalFileSystemTestHid extends TestBaseHid {
27     private static String JavaDoc[] resources = new String JavaDoc [] {
28         "A/B/C.java",
29         "A/C/A"
30     };
31
32     public LocalFileSystemTestHid (String JavaDoc testName) {
33         super(testName);
34     }
35
36
37     protected String JavaDoc[] getResources (String JavaDoc testName) {
38         return resources;
39     }
40
41     /** We delete a folder and then listen on changes fired on one of its children. */
42     public void testExternalRemoveChildrenEvents () throws Exception JavaDoc {
43         FileObject fo = testedFS.findResource ("A/B/C.java");
44         
45         assertNotNull(fo);
46         
47         if (!fo.isData ()) {
48             fail ("Not data");
49         }
50         
51         java.util.Date JavaDoc date = fo.lastModified ();
52         
53         registerDefaultListener (fo);
54         
55         assertNotNull (fo.getParent());
56         
57         java.io.File JavaDoc f = (java.io.File JavaDoc)fo.getParent ().getAttribute ("java.io.File");
58         
59         assertNotNull (f);
60
61         // wait a while before delete so the last modification date can be different
62
Thread.sleep (100);
63         
64         if (!removeRec (f)) {
65             fail ("Cannot delete " + f);
66         }
67         
68         if (f.exists()) {
69             fail ("File still exits: " + f);
70         }
71         
72         
73         // refresh content of the object
74
fo.refresh();
75         
76         // BTW. this could be used as workaround that updates the parents children
77
// but the system should work even without this call
78
// fo.getParent ().refresh ();
79

80
81         // date should change, should not it?
82
java.util.Date JavaDoc now = fo.lastModified ();
83         if (date.equals (now)) {
84             fail ("Last modified date before delete (" + date + ") is same as after (" + now + ")");
85         }
86
87         // input stream should not be created
88
InputStream JavaDoc is = null;
89         try {
90             fo.getInputStream ();
91             fail ("Input stream was created");
92         } catch (java.io.FileNotFoundException JavaDoc ex) {
93             // ok
94
} finally {
95             if (is != null) is.close();
96         }
97         
98         // no change event should be fired, but we should receive one delete event
99
fileChangedAssert ("Change event", 0);
100         fileDeletedAssert ("Delete event", 1);
101         
102         
103         // the file should loose its validity (because it is deleted)
104
if (fo.isValid ()) {
105             fail ("File is still valid: " + fo);
106         }
107         
108         // and the parent file object should update itself not to reference
109
// the file anymore
110
FileObject[] arr = fo.getParent ().getChildren ();
111         
112         if (arr.length > 0) {
113             fail ("Parent's children not updated yet: " + arr.length + " [0] = " + arr[0]);
114         }
115     }
116     
117     /** External & internal creation of the a file fails.
118      */

119     public void testExternalInternal () throws Exception JavaDoc {
120         FileObject fo = testedFS.findResource ("A/C");
121         
122         // read the content of the children
123
FileObject[] arr = fo.getChildren ();
124         if (arr.length != 1) {
125             fail ("Strange children in subfolder: " + java.util.Arrays.asList (arr));
126         }
127         
128         assertNotNull (fo);
129         
130         if (!fo.isFolder ()) {
131             fail ("Not folder");
132         }
133         
134         
135         
136         File JavaDoc f = (File JavaDoc)fo.getAttribute ("java.io.File");
137         assertNotNull (f);
138         
139         File JavaDoc c = new File JavaDoc (f, "A.child");
140         c.createNewFile();
141         
142         if (fo.getFileObject ("A.child") != null) {
143             fail ("the file created by external modification should not be found until refresh if the value is in cache");
144         }
145             
146         try {
147             FileObject oc = fo.createData ("A.child");
148             fail ("A child has been created event it should not");
149         } catch (SyncFailedException JavaDoc ex) {
150             // ok, we expected that the synchronization will be broken
151
}
152         
153         // now we do the refresh
154
fo.refresh ();
155         
156         // and the result has to be good
157
if (fo.getFileObject ("A.child") == null) {
158             fail ("the file is still not noticed in local file system");
159         }
160         
161         
162         // another part of the test that demonstrates usage of FileUtil.createData
163
// and shows that it should work even the cache is not insync
164

165         c = new File JavaDoc (f, "B.child");
166         c.createNewFile();
167         
168         if (fo.getFileObject ("B.child") != null) {
169             fail ("The cache should not be up-to-date");
170         }
171         
172         FileObject ok = FileUtil.createData (fo, "B.child");
173         
174         if (!c.equals (ok.getAttribute ("java.io.File"))) {
175             fail ("The created file is not the same");
176         }
177     }
178     
179     /** Deletes folder or file, etc.
180      * @param f the file
181      * @return false if delete failed
182      */

183     private static boolean removeRec (java.io.File JavaDoc f) {
184         
185         if (f.isDirectory ()) {
186             java.io.File JavaDoc arr[] = f.listFiles();
187
188             for (int i = 0; i < arr.length; i++) {
189                 if (!removeRec (arr[i])) {
190                     return false;
191                 }
192             }
193         }
194         
195         return f.delete ();
196     }
197 }
198
Popular Tags