Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2009 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
package org.netbeans.modules.masterfs.filebasedfs.fileobjects; |
42 |
|
43 |
import java.io.File; |
44 |
import java.io.FileOutputStream; |
45 |
import java.io.OutputStream; |
46 |
import java.lang.ref.Reference; |
47 |
import java.lang.ref.WeakReference; |
48 |
import java.util.logging.Level; |
49 |
import java.util.logging.Logger; |
50 |
import org.netbeans.junit.NbTestCase; |
51 |
import org.openide.filesystems.FileAttributeEvent; |
52 |
import org.openide.filesystems.FileChangeAdapter; |
53 |
import org.openide.filesystems.FileChangeListener; |
54 |
import org.openide.filesystems.FileEvent; |
55 |
import org.openide.filesystems.FileObject; |
56 |
import org.openide.filesystems.FileRenameEvent; |
57 |
import org.openide.filesystems.FileUtil; |
58 |
|
59 |
public class ExternalTouchTest extends NbTestCase { |
60 |
private Logger LOG; |
61 |
private FileObject testFolder; |
62 |
|
63 |
public ExternalTouchTest(String testName) { |
64 |
super(testName); |
65 |
} |
66 |
|
67 |
@Override |
68 |
protected Level logLevel() { |
69 |
return Level.FINE; |
70 |
} |
71 |
|
72 |
@Override |
73 |
protected void setUp() throws Exception { |
74 |
clearWorkDir(); |
75 |
|
76 |
LOG = Logger.getLogger("test." + getName()); |
77 |
Logger.getLogger("org.openide.util.Mutex").setUseParentHandlers(false); |
78 |
|
79 |
File dir = new File(getWorkDir(), "test"); |
80 |
dir.mkdirs(); |
81 |
testFolder = FileUtil.toFileObject(dir); |
82 |
assertNotNull("Test folder created", testFolder); |
83 |
|
84 |
} |
85 |
|
86 |
public void testChangeInChildrenNoticed() throws Exception { |
87 |
long lm = System.currentTimeMillis(); |
88 |
FileObject fileObject1 = testFolder.createData("fileObject1"); |
89 |
assertNotNull("Just to initialize the stamp", lm); |
90 |
FileObject[] arr = testFolder.getChildren(); |
91 |
assertEquals("One child", 1, arr.length); |
92 |
assertEquals("Right child", fileObject1, arr[0]); |
93 |
|
94 |
File file = FileUtil.toFile(fileObject1); |
95 |
assertNotNull("File found", file); |
96 |
Reference<FileObject> ref = new WeakReference<FileObject>(fileObject1); |
97 |
arr = null; |
98 |
fileObject1 = null; |
99 |
assertGC("File Object can disappear", ref); |
100 |
|
101 |
Thread.sleep(1000); |
102 |
|
103 |
class L extends FileChangeAdapter { |
104 |
int cnt; |
105 |
FileEvent event; |
106 |
|
107 |
@Override |
108 |
public void fileChanged(FileEvent fe) { |
109 |
LOG.info("file change " + fe.getFile()); |
110 |
cnt++; |
111 |
event = fe; |
112 |
} |
113 |
} |
114 |
L listener = new L(); |
115 |
testFolder.addRecursiveListener(listener); |
116 |
|
117 |
FileOutputStream os = new FileOutputStream(file); |
118 |
os.write(10); |
119 |
os.close(); |
120 |
|
121 |
if (lm > file.lastModified() - 50) { |
122 |
fail("New modification time shall be at last 50ms after the original one: " + (file.lastModified() - lm)); |
123 |
} |
124 |
|
125 |
testFolder.refresh(); |
126 |
|
127 |
assertEquals("Change notified", 1, listener.cnt); |
128 |
assertEquals("Right file", file, FileUtil.toFile(listener.event.getFile())); |
129 |
assertEquals("Right source", file.getParentFile(), FileUtil.toFile((FileObject)listener.event.getSource())); |
130 |
} |
131 |
public void testNewChildNoticed() throws Exception { |
132 |
FileObject fileObject1 = testFolder.createData("fileObject1"); |
133 |
FileObject[] arr = testFolder.getChildren(); |
134 |
assertEquals("One child", 1, arr.length); |
135 |
assertEquals("Right child", fileObject1, arr[0]); |
136 |
|
137 |
File file = FileUtil.toFile(fileObject1); |
138 |
assertNotNull("File found", file); |
139 |
arr = null; |
140 |
fileObject1 = null; |
141 |
Reference<FileObject> ref = new WeakReference<FileObject>(fileObject1); |
142 |
assertGC("File Object can disappear", ref); |
143 |
|
144 |
Thread.sleep(100); |
145 |
|
146 |
class L extends FileChangeAdapter { |
147 |
int cnt; |
148 |
FileEvent event; |
149 |
|
150 |
@Override |
151 |
public void fileDataCreated(FileEvent fe) { |
152 |
cnt++; |
153 |
event = fe; |
154 |
} |
155 |
|
156 |
} |
157 |
L listener = new L(); |
158 |
testFolder.addRecursiveListener(listener); |
159 |
|
160 |
File nfile = new File(file.getParentFile(), "new.txt"); |
161 |
nfile.createNewFile(); |
162 |
|
163 |
testFolder.refresh(); |
164 |
|
165 |
assertEquals("Change notified", 1, listener.cnt); |
166 |
assertEquals("Right file", nfile, FileUtil.toFile(listener.event.getFile())); |
167 |
} |
168 |
public void testDeleteOfAChildNoticed() throws Exception { |
169 |
FileObject fileObject1 = testFolder.createData("fileObject1"); |
170 |
FileObject[] arr = testFolder.getChildren(); |
171 |
assertEquals("One child", 1, arr.length); |
172 |
assertEquals("Right child", fileObject1, arr[0]); |
173 |
|
174 |
File file = FileUtil.toFile(fileObject1); |
175 |
assertNotNull("File found", file); |
176 |
arr = null; |
177 |
fileObject1 = null; |
178 |
Reference<FileObject> ref = new WeakReference<FileObject>(fileObject1); |
179 |
assertGC("File Object can disappear", ref); |
180 |
|
181 |
Thread.sleep(100); |
182 |
|
183 |
class L extends FileChangeAdapter { |
184 |
int cnt; |
185 |
FileEvent event; |
186 |
|
187 |
@Override |
188 |
public void fileDeleted(FileEvent fe) { |
189 |
cnt++; |
190 |
event = fe; |
191 |
} |
192 |
|
193 |
} |
194 |
L listener = new L(); |
195 |
testFolder.addRecursiveListener(listener); |
196 |
|
197 |
file.delete(); |
198 |
|
199 |
testFolder.refresh(); |
200 |
|
201 |
assertEquals("Change notified", 1, listener.cnt); |
202 |
assertEquals("Right file", file, FileUtil.toFile(listener.event.getFile())); |
203 |
} |
204 |
|
205 |
public void testRecursiveListener() throws Exception { |
206 |
FileObject obj = FileUtil.createData(testFolder, "my/sub/children/children.java"); |
207 |
FileObject sub = obj.getParent().getParent(); |
208 |
|
209 |
class L implements FileChangeListener { |
210 |
StringBuilder sb = new StringBuilder(); |
211 |
|
212 |
public void fileFolderCreated(FileEvent fe) { |
213 |
LOG.info("FolderCreated: " + fe.getFile()); |
214 |
sb.append("FolderCreated"); |
215 |
} |
216 |
|
217 |
public void fileDataCreated(FileEvent fe) { |
218 |
LOG.info("DataCreated: " + fe.getFile()); |
219 |
sb.append("DataCreated"); |
220 |
} |
221 |
|
222 |
public void fileChanged(FileEvent fe) { |
223 |
LOG.info("Changed: " + fe.getFile()); |
224 |
sb.append("Changed"); |
225 |
} |
226 |
|
227 |
public void fileDeleted(FileEvent fe) { |
228 |
LOG.info("Deleted: " + fe.getFile()); |
229 |
sb.append("Deleted"); |
230 |
} |
231 |
|
232 |
public void fileRenamed(FileRenameEvent fe) { |
233 |
LOG.info("Renamed: " + fe.getFile()); |
234 |
sb.append("Renamed"); |
235 |
} |
236 |
|
237 |
public void fileAttributeChanged(FileAttributeEvent fe) { |
238 |
if (fe.getName().startsWith("DataEditorSupport.read-only.refresh")) { |
239 |
return; |
240 |
} |
241 |
LOG.info("AttributeChanged: " + fe.getFile()); |
242 |
sb.append("AttributeChanged"); |
243 |
} |
244 |
|
245 |
public void assertMessages(String txt, String msg) { |
246 |
assertEquals(txt, msg, sb.toString()); |
247 |
sb.setLength(0); |
248 |
} |
249 |
} |
250 |
L recursive = new L(); |
251 |
L flat = new L(); |
252 |
|
253 |
sub.addFileChangeListener(flat); |
254 |
LOG.info("Adding listener"); |
255 |
sub.addRecursiveListener(recursive); |
256 |
LOG.info("Adding listener finished"); |
257 |
|
258 |
Thread.sleep(1000); |
259 |
|
260 |
File fo = new File(FileUtil.toFile(obj.getParent()), "sibling.java"); |
261 |
fo.createNewFile(); |
262 |
LOG.info("sibling created, now refresh"); |
263 |
FileUtil.refreshAll(); |
264 |
LOG.info("sibling refresh finished"); |
265 |
|
266 |
// Right now it is DataCreatedChanged |
267 |
// recursive.assertMessages("Creation", "DataCreated"); |
268 |
recursive.assertMessages("Creation", "DataCreatedChanged"); |
269 |
flat.assertMessages("No messages in flat mode", ""); |
270 |
|
271 |
Thread.sleep(1000); |
272 |
|
273 |
final OutputStream os = new FileOutputStream(fo); |
274 |
os.write(10); |
275 |
os.close(); |
276 |
LOG.info("Before refresh"); |
277 |
FileUtil.refreshAll(); |
278 |
LOG.info("After refresh"); |
279 |
|
280 |
flat.assertMessages("No messages in flat mode", ""); |
281 |
// Two changes detected now: |
282 |
// recursive.assertMessages("written", "Changed"); |
283 |
recursive.assertMessages("written", "ChangedChanged"); |
284 |
|
285 |
fo.delete(); |
286 |
FileUtil.refreshAll(); |
287 |
|
288 |
flat.assertMessages("No messages in flat mode", ""); |
289 |
recursive.assertMessages("gone", "Deleted"); |
290 |
|
291 |
new File(FileUtil.toFile(sub), "testFolder").mkdirs(); |
292 |
FileUtil.refreshAll(); |
293 |
|
294 |
flat.assertMessages("Direct Folder notified", "FolderCreated"); |
295 |
recursive.assertMessages("Direct Folder notified", "FolderCreated"); |
296 |
|
297 |
new File(FileUtil.toFile(sub.getParent()), "unimportant.txt").createNewFile(); |
298 |
FileUtil.refreshAll(); |
299 |
|
300 |
flat.assertMessages("No messages in flat mode", ""); |
301 |
recursive.assertMessages("No messages in recursive mode", ""); |
302 |
|
303 |
sub.removeRecursiveListener(recursive); |
304 |
|
305 |
new File(FileUtil.toFile(sub), "test.data").createNewFile(); |
306 |
FileUtil.refreshAll(); |
307 |
|
308 |
// Double notification right now: |
309 |
// flat.assertMessages("Direct file notified", "DataCreated"); |
310 |
flat.assertMessages("Direct file notified", "DataCreatedChanged"); |
311 |
recursive.assertMessages("No longer active", ""); |
312 |
|
313 |
WeakReference<L> ref = new WeakReference<L>(recursive); |
314 |
recursive = null; |
315 |
assertGC("Listener can be GCed", ref); |
316 |
} |
317 |
|
318 |
} |