Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.openide.loaders; |
15 |
|
16 |
import org.openide.filesystems.*; |
17 |
import org.openide.loaders.*; |
18 |
import java.beans.*; |
19 |
import java.io.IOException; |
20 |
import junit.textui.TestRunner; |
21 |
import org.netbeans.junit.*; |
22 |
|
23 |
/* |
24 |
* Checks whether a during a modify operation (copy, move) some |
25 |
* other thread can get a grip on unfinished and uncostructed |
26 |
* content on filesystem. |
27 |
* |
28 |
* @author Jaroslav Tulach |
29 |
*/ |
30 |
public class SeparationOfThreadsTest extends NbTestCase { |
31 |
private DataFolder root; |
32 |
private DataFolder to; |
33 |
private DataObject a; |
34 |
private DataObject b; |
35 |
private DataObject res; |
36 |
|
37 |
/** Creates the test */ |
38 |
public SeparationOfThreadsTest(String name) { |
39 |
super(name); |
40 |
} |
41 |
|
42 |
// For each test setup a FileSystem and DataObjects |
43 |
protected void setUp() throws Exception { |
44 |
String fsstruct [] = new String [] { |
45 |
"A.attr", |
46 |
"B.attr", |
47 |
"dir/" |
48 |
}; |
49 |
TestUtilHid.destroyLocalFileSystem (getName()); |
50 |
FileSystem fs = TestUtilHid.createLocalFileSystem (getName(), fsstruct); |
51 |
root = DataFolder.findFolder (fs.getRoot ()); |
52 |
|
53 |
AddLoaderManuallyHid.addRemoveLoader (ALoader.getLoader (ALoader.class), true); |
54 |
AddLoaderManuallyHid.addRemoveLoader (BLoader.getLoader (BLoader.class), true); |
55 |
|
56 |
to = DataFolder.findFolder (fs.findResource (fsstruct[2])); |
57 |
|
58 |
fs.findResource (fsstruct[0]).setAttribute ("A", Boolean.TRUE); |
59 |
|
60 |
a = DataObject.find (fs.findResource (fsstruct[0])); |
61 |
b = DataObject.find (fs.findResource (fsstruct[1])); |
62 |
|
63 |
ALoader loaderA = (ALoader)ALoader.getLoader (ALoader.class); |
64 |
|
65 |
assertEquals ("A is loaded by ALoader", loaderA, a.getLoader()); |
66 |
assertEquals ("B is loaded by BLoader", ALoader.getLoader (BLoader.class), b.getLoader()); |
67 |
|
68 |
|
69 |
synchronized (loaderA) { |
70 |
new Thread ((Runnable)loaderA).start (); |
71 |
loaderA.wait (); |
72 |
} |
73 |
} |
74 |
|
75 |
//Clear all stuff when the test finish |
76 |
protected void tearDown() throws Exception { |
77 |
ALoader loader = (ALoader)ALoader.getLoader(ALoader.class); |
78 |
synchronized (loader) { |
79 |
try { |
80 |
while (!loader.finished) { |
81 |
loader.wait (); |
82 |
} |
83 |
|
84 |
assertNotNull (res); |
85 |
assertEquals ("The right loader synchronously", loader, res.getLoader ()); |
86 |
|
87 |
if (loader.asyncError != null) { |
88 |
throw loader.asyncError; |
89 |
} |
90 |
|
91 |
assertNotNull (loader.asyncRes); |
92 |
assertEquals ("It is the right loader asynchronously", loader, loader.asyncRes.getLoader()); |
93 |
|
94 |
} finally { |
95 |
loader.asyncError = null; |
96 |
loader.currentThread = null; |
97 |
loader.current = null; |
98 |
loader.asyncRes = null; |
99 |
loader.finished = false; |
100 |
// clears any such flag |
101 |
Thread.interrupted(); |
102 |
|
103 |
|
104 |
TestUtilHid.destroyLocalFileSystem (getName()); |
105 |
AddLoaderManuallyHid.addRemoveLoader (ALoader.getLoader (ALoader.class), false); |
106 |
AddLoaderManuallyHid.addRemoveLoader (BLoader.getLoader (BLoader.class), false); |
107 |
|
108 |
// end of test |
109 |
loader.notify (); |
110 |
} |
111 |
} |
112 |
} |
113 |
|
114 |
public static void main (String[] args) throws Exception { |
115 |
TestRunner.run(new NbTestSuite(SeparationOfThreadsTest.class)); |
116 |
} |
117 |
|
118 |
|
119 |
public void testCopy () throws Exception { |
120 |
res = a.copy (to); |
121 |
} |
122 |
|
123 |
public void testCreateFromTemplate () throws Exception { |
124 |
res = a.createFromTemplate (to); |
125 |
} |
126 |
public void testMove () throws Exception { |
127 |
a.move (to); |
128 |
res = a; |
129 |
} |
130 |
public void testRename () throws Exception { |
131 |
a.rename ("AnyThing"); |
132 |
res = a; |
133 |
} |
134 |
|
135 |
// |
136 |
// Inner classes |
137 |
// |
138 |
|
139 |
public static final class ALoader extends UniFileLoader implements Runnable { |
140 |
DataObject asyncRes; |
141 |
Exception asyncError; |
142 |
FileObject current; |
143 |
Thread currentThread; |
144 |
boolean finished; |
145 |
|
146 |
public ALoader() { |
147 |
super(DataObject.class.getName()); |
148 |
} |
149 |
protected void initialize() { |
150 |
super.initialize(); |
151 |
getExtensions().addExtension("attr"); |
152 |
} |
153 |
protected String displayName() { |
154 |
return getClass().getName (); |
155 |
} |
156 |
protected MultiDataObject createMultiObject(FileObject pf) throws IOException { |
157 |
return new MultiDataObject(pf, this); |
158 |
} |
159 |
|
160 |
protected org.openide.loaders.MultiDataObject.Entry createPrimaryEntry(org.openide.loaders.MultiDataObject multiDataObject, org.openide.filesystems.FileObject fileObject) { |
161 |
return new SlowEntry (multiDataObject, fileObject); |
162 |
} |
163 |
|
164 |
// |
165 |
// Notification that the copy is in middle |
166 |
// |
167 |
|
168 |
public void notifyCopied (FileObject current) { |
169 |
synchronized (this) { |
170 |
this.current = current; |
171 |
this.currentThread = Thread.currentThread (); |
172 |
this.notify (); |
173 |
} |
174 |
try { |
175 |
Thread.sleep (500); |
176 |
} catch (InterruptedException ex) { |
177 |
// is interrupted to be wake up sooner |
178 |
} |
179 |
} |
180 |
|
181 |
// |
182 |
// The second thread that waits for the copy operation |
183 |
// |
184 |
public void run () { |
185 |
DataLoader loader = this; |
186 |
synchronized (loader) { |
187 |
// continue execution in setUp |
188 |
loader.notify (); |
189 |
// wait for being notify about copying |
190 |
try { |
191 |
loader.wait (); |
192 |
} catch (InterruptedException ex) { |
193 |
asyncError = ex; |
194 |
} |
195 |
|
196 |
try { |
197 |
asyncRes = DataObject.find (current); |
198 |
currentThread.interrupt(); |
199 |
} catch (IOException ex) { |
200 |
asyncError = ex; |
201 |
} |
202 |
|
203 |
// notify that we have computed everything |
204 |
finished = true; |
205 |
loader.notify (); |
206 |
|
207 |
while (asyncRes != null && asyncError != null) { |
208 |
try { |
209 |
loader.wait (); |
210 |
} catch (InterruptedException ex) { |
211 |
} |
212 |
} |
213 |
} |
214 |
} |
215 |
|
216 |
} |
217 |
|
218 |
public static final class BLoader extends UniFileLoader { |
219 |
public BLoader() { |
220 |
super(DataObject.class.getName()); |
221 |
} |
222 |
protected void initialize() { |
223 |
super.initialize(); |
224 |
getExtensions().addExtension("attr"); |
225 |
} |
226 |
protected String displayName() { |
227 |
return getClass ().getName (); |
228 |
} |
229 |
protected org.openide.filesystems.FileObject findPrimaryFile(org.openide.filesystems.FileObject fileObject) { |
230 |
if (Boolean.TRUE.equals (fileObject.getAttribute ("A"))) { |
231 |
return null; |
232 |
} |
233 |
|
234 |
org.openide.filesystems.FileObject retValue; |
235 |
|
236 |
retValue = super.findPrimaryFile(fileObject); |
237 |
return retValue; |
238 |
} |
239 |
|
240 |
protected MultiDataObject createMultiObject(FileObject pf) throws IOException { |
241 |
return new MultiDataObject(pf, this); |
242 |
} |
243 |
} |
244 |
|
245 |
private static final class SlowEntry extends MultiDataObject.Entry { |
246 |
public SlowEntry (MultiDataObject obj, FileObject fo) { |
247 |
obj.super (fo); |
248 |
} |
249 |
|
250 |
private void notifyCopied (FileObject fo) { |
251 |
ALoader l = (ALoader)ALoader.getLoader(ALoader.class); |
252 |
l.notifyCopied (fo); |
253 |
} |
254 |
|
255 |
public org.openide.filesystems.FileObject copy(org.openide.filesystems.FileObject fileObject, String str) throws java.io.IOException { |
256 |
FileObject ret = fileObject.createData ("copy", "attr"); |
257 |
notifyCopied (ret); |
258 |
ret.setAttribute ("A", Boolean.TRUE); |
259 |
return ret; |
260 |
} |
261 |
|
262 |
public org.openide.filesystems.FileObject createFromTemplate(org.openide.filesystems.FileObject fileObject, String str) throws java.io.IOException { |
263 |
FileObject ret = fileObject.createData ("createFromTemplate", "attr"); |
264 |
notifyCopied (ret); |
265 |
ret.setAttribute ("A", Boolean.TRUE); |
266 |
return ret; |
267 |
} |
268 |
|
269 |
public void delete() throws java.io.IOException { |
270 |
throw new IOException ("Not implemented"); |
271 |
} |
272 |
|
273 |
public org.openide.filesystems.FileObject move(org.openide.filesystems.FileObject fileObject, String str) throws java.io.IOException { |
274 |
FileObject ret = fileObject.createData ("move", "attr"); |
275 |
notifyCopied (ret); |
276 |
ret.setAttribute ("A", Boolean.TRUE); |
277 |
super.getFile ().delete (); |
278 |
return ret; |
279 |
} |
280 |
|
281 |
public org.openide.filesystems.FileObject rename(String str) throws java.io.IOException { |
282 |
FileObject ret = getFile ().getParent ().createData ("rename", "attr"); |
283 |
notifyCopied (ret); |
284 |
ret.setAttribute ("A", Boolean.TRUE); |
285 |
super.getFile ().delete (); |
286 |
return ret; |
287 |
} |
288 |
|
289 |
} |
290 |
} |