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.lang.ref.Reference; |
46 |
import java.lang.ref.WeakReference; |
47 |
import java.util.Date; |
48 |
import org.netbeans.junit.NbTestCase; |
49 |
import org.openide.filesystems.FileChangeAdapter; |
50 |
import org.openide.filesystems.FileEvent; |
51 |
import org.openide.filesystems.FileObject; |
52 |
import org.openide.filesystems.FileUtil; |
53 |
|
54 |
public class ExternalTouchTest extends NbTestCase { |
55 |
|
56 |
public ExternalTouchTest(String testName) { |
57 |
super(testName); |
58 |
} |
59 |
|
60 |
public void testChangeInChildrenNoticed() throws Exception { |
61 |
clearWorkDir(); |
62 |
FileObject testFolder = FileUtil.toFileObject(getWorkDir()); |
63 |
|
64 |
FileObject fileObject1 = testFolder.createData("fileObject1"); |
65 |
final Date lm = fileObject1.lastModified(); |
66 |
assertNotNull("Just to initialize the stamp", lm); |
67 |
FileObject[] arr = testFolder.getChildren(); |
68 |
assertEquals("One child", 1, arr.length); |
69 |
assertEquals("Right child", fileObject1, arr[0]); |
70 |
|
71 |
File file = FileUtil.toFile(fileObject1); |
72 |
assertNotNull("File found", file); |
73 |
Reference<FileObject> ref = new WeakReference<FileObject>(fileObject1); |
74 |
arr = null; |
75 |
fileObject1 = null; |
76 |
assertGC("File Object can disappear", ref); |
77 |
|
78 |
Thread.sleep(1000); |
79 |
|
80 |
class L extends FileChangeAdapter { |
81 |
int cnt; |
82 |
FileEvent event; |
83 |
|
84 |
@Override |
85 |
public void fileChanged(FileEvent fe) { |
86 |
cnt++; |
87 |
event = fe; |
88 |
} |
89 |
} |
90 |
L listener = new L(); |
91 |
testFolder.addFileChangeListener(listener); |
92 |
|
93 |
FileOutputStream os = new FileOutputStream(file); |
94 |
os.write(10); |
95 |
os.close(); |
96 |
|
97 |
if (lm.getTime() > file.lastModified() - 50) { |
98 |
fail("New modification time shall be at last 50ms after the original one: " + (file.lastModified() - lm.getTime())); |
99 |
} |
100 |
|
101 |
testFolder.refresh(); |
102 |
|
103 |
assertEquals("Change notified", 1, listener.cnt); |
104 |
assertEquals("Right file", file, FileUtil.toFile(listener.event.getFile())); |
105 |
} |
106 |
public void testNewChildNoticed() throws Exception { |
107 |
clearWorkDir(); |
108 |
FileObject testFolder = FileUtil.toFileObject(getWorkDir()); |
109 |
FileObject fileObject1 = testFolder.createData("fileObject1"); |
110 |
assertNotNull("Just to initialize the stamp", fileObject1.lastModified()); |
111 |
FileObject[] arr = testFolder.getChildren(); |
112 |
assertEquals("One child", 1, arr.length); |
113 |
assertEquals("Right child", fileObject1, arr[0]); |
114 |
|
115 |
File file = FileUtil.toFile(fileObject1); |
116 |
assertNotNull("File found", file); |
117 |
arr = null; |
118 |
fileObject1 = null; |
119 |
Reference<FileObject> ref = new WeakReference<FileObject>(fileObject1); |
120 |
assertGC("File Object can disappear", ref); |
121 |
|
122 |
Thread.sleep(100); |
123 |
|
124 |
class L extends FileChangeAdapter { |
125 |
int cnt; |
126 |
FileEvent event; |
127 |
|
128 |
@Override |
129 |
public void fileDataCreated(FileEvent fe) { |
130 |
cnt++; |
131 |
event = fe; |
132 |
} |
133 |
|
134 |
} |
135 |
L listener = new L(); |
136 |
testFolder.addFileChangeListener(listener); |
137 |
|
138 |
File nfile = new File(file.getParentFile(), "new.txt"); |
139 |
nfile.createNewFile(); |
140 |
|
141 |
testFolder.refresh(); |
142 |
|
143 |
assertEquals("Change notified", 1, listener.cnt); |
144 |
assertEquals("Right file", nfile, FileUtil.toFile(listener.event.getFile())); |
145 |
} |
146 |
public void testDeleteOfAChildNoticed() throws Exception { |
147 |
clearWorkDir(); |
148 |
FileObject testFolder = FileUtil.toFileObject(getWorkDir()); |
149 |
FileObject fileObject1 = testFolder.createData("fileObject1"); |
150 |
assertNotNull("Just to initialize the stamp", fileObject1.lastModified()); |
151 |
FileObject[] arr = testFolder.getChildren(); |
152 |
assertEquals("One child", 1, arr.length); |
153 |
assertEquals("Right child", fileObject1, arr[0]); |
154 |
|
155 |
File file = FileUtil.toFile(fileObject1); |
156 |
assertNotNull("File found", file); |
157 |
arr = null; |
158 |
fileObject1 = null; |
159 |
Reference<FileObject> ref = new WeakReference<FileObject>(fileObject1); |
160 |
assertGC("File Object can disappear", ref); |
161 |
|
162 |
Thread.sleep(100); |
163 |
|
164 |
class L extends FileChangeAdapter { |
165 |
int cnt; |
166 |
FileEvent event; |
167 |
|
168 |
@Override |
169 |
public void fileDeleted(FileEvent fe) { |
170 |
cnt++; |
171 |
event = fe; |
172 |
} |
173 |
|
174 |
} |
175 |
L listener = new L(); |
176 |
testFolder.addFileChangeListener(listener); |
177 |
|
178 |
file.delete(); |
179 |
|
180 |
testFolder.refresh(); |
181 |
|
182 |
assertEquals("Change notified", 1, listener.cnt); |
183 |
assertEquals("Right file", file, FileUtil.toFile(listener.event.getFile())); |
184 |
} |
185 |
} |