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 |
"fake/A.instance" |
49 |
}; |
50 |
TestUtilHid.destroyLocalFileSystem (getName()); |
51 |
FileSystem fs = TestUtilHid.createLocalFileSystem (getName(), fsstruct); |
52 |
root = DataFolder.findFolder (fs.getRoot ()); |
53 |
|
54 |
AddLoaderManuallyHid.addRemoveLoader (ALoader.getLoader (ALoader.class), true); |
55 |
AddLoaderManuallyHid.addRemoveLoader (BLoader.getLoader (BLoader.class), true); |
56 |
|
57 |
to = DataFolder.findFolder (fs.findResource (fsstruct[2])); |
58 |
|
59 |
fs.findResource (fsstruct[0]).setAttribute ("A", Boolean.TRUE); |
60 |
|
61 |
a = DataObject.find (fs.findResource (fsstruct[0])); |
62 |
b = DataObject.find (fs.findResource (fsstruct[1])); |
63 |
|
64 |
ALoader loaderA = (ALoader)ALoader.getLoader (ALoader.class); |
65 |
|
66 |
assertEquals ("A is loaded by ALoader", loaderA, a.getLoader()); |
67 |
assertEquals ("B is loaded by BLoader", ALoader.getLoader (BLoader.class), b.getLoader()); |
68 |
|
69 |
// following code tests one bug that I have made during implementation |
70 |
// the runAtomicAction has to be run in finally block as some operation |
71 |
// can throw exceptions. This simulates operation that throws exception |
72 |
try { |
73 |
a.delete (); |
74 |
fail ("Should throw exception"); |
75 |
} catch (IOException ex) { |
76 |
assertEquals ("Not implemented", ex.getMessage ()); |
77 |
} |
78 |
|
79 |
synchronized (loaderA) { |
80 |
new Thread ((Runnable)loaderA).start (); |
81 |
loaderA.wait (); |
82 |
} |
83 |
} |
84 |
|
85 |
//Clear all stuff when the test finish |
86 |
protected void tearDown() throws Exception { |
87 |
ALoader loader = (ALoader)ALoader.getLoader(ALoader.class); |
88 |
synchronized (loader) { |
89 |
try { |
90 |
while (!loader.finished) { |
91 |
loader.wait (); |
92 |
} |
93 |
|
94 |
assertNotNull (res); |
95 |
assertEquals ("The right loader synchronously", loader, res.getLoader ()); |
96 |
|
97 |
if (loader.asyncError != null) { |
98 |
throw loader.asyncError; |
99 |
} |
100 |
|
101 |
assertNotNull (loader.asyncRes); |
102 |
assertEquals ("It is the right loader asynchronously", loader, loader.asyncRes.getLoader()); |
103 |
|
104 |
} finally { |
105 |
loader.asyncError = null; |
106 |
loader.currentThread = null; |
107 |
loader.current = null; |
108 |
loader.asyncRes = null; |
109 |
loader.finished = false; |
110 |
// clears any such flag |
111 |
Thread.interrupted(); |
112 |
|
113 |
|
114 |
TestUtilHid.destroyLocalFileSystem (getName()); |
115 |
AddLoaderManuallyHid.addRemoveLoader (ALoader.getLoader (ALoader.class), false); |
116 |
AddLoaderManuallyHid.addRemoveLoader (BLoader.getLoader (BLoader.class), false); |
117 |
|
118 |
// end of test |
119 |
loader.notify (); |
120 |
} |
121 |
} |
122 |
} |
123 |
|
124 |
public static void main (String[] args) throws Exception { |
125 |
TestRunner.run(new NbTestSuite(SeparationOfThreadsTest.class)); |
126 |
} |
127 |
|
128 |
public void testCopy () throws Exception { |
129 |
res = a.copy (to); |
130 |
} |
131 |
|
132 |
public void testCreateFromTemplate () throws Exception { |
133 |
res = a.createFromTemplate (to); |
134 |
} |
135 |
public void testMove () throws Exception { |
136 |
a.move (to); |
137 |
res = a; |
138 |
} |
139 |
public void testRename () throws Exception { |
140 |
a.rename ("AnyThing"); |
141 |
res = a; |
142 |
} |
143 |
|
144 |
// |
145 |
// Inner classes |
146 |
// |
147 |
|
148 |
public static final class ALoader extends UniFileLoader implements Runnable { |
149 |
DataObject asyncRes; |
150 |
Exception asyncError; |
151 |
FileObject current; |
152 |
Thread currentThread; |
153 |
boolean finished; |
154 |
|
155 |
public ALoader() { |
156 |
super(DataObject.class.getName()); |
157 |
} |
158 |
protected void initialize() { |
159 |
super.initialize(); |
160 |
getExtensions().addExtension("attr"); |
161 |
} |
162 |
protected String displayName() { |
163 |
return getClass().getName (); |
164 |
} |
165 |
protected MultiDataObject createMultiObject(FileObject pf) throws IOException { |
166 |
return new MultiDataObject(pf, this); |
167 |
} |
168 |
|
169 |
protected org.openide.loaders.MultiDataObject.Entry createPrimaryEntry(org.openide.loaders.MultiDataObject multiDataObject, org.openide.filesystems.FileObject fileObject) { |
170 |
return new SlowEntry (multiDataObject, fileObject); |
171 |
} |
172 |
|
173 |
// |
174 |
// Notification that the copy is in middle |
175 |
// |
176 |
|
177 |
public void notifyCopied (FileObject current) { |
178 |
// first of all do some really ugly and complex operation |
179 |
try { |
180 |
DataObject[] arr = DataFolder.findFolder (current.getFileSystem ().findResource ("fake")).getChildren (); |
181 |
assertEquals ("In folder fake there is one object", 1, arr.length); |
182 |
} catch (FileStateInvalidException ex) { |
183 |
fail ("Wrong exception" + ex); |
184 |
} |
185 |
|
186 |
synchronized (this) { |
187 |
this.current = current; |
188 |
this.currentThread = Thread.currentThread (); |
189 |
this.notify (); |
190 |
} |
191 |
int cnt = 1; |
192 |
while (cnt-- > 0) { |
193 |
try { |
194 |
Thread.sleep (500); |
195 |
} catch (InterruptedException ex) { |
196 |
// is interrupted to be wake up sooner |
197 |
} |
198 |
} |
199 |
} |
200 |
|
201 |
// |
202 |
// The second thread that waits for the copy operation |
203 |
// |
204 |
public void run () { |
205 |
DataLoader loader = this; |
206 |
synchronized (loader) { |
207 |
// continue execution in setUp |
208 |
loader.notify (); |
209 |
// wait for being notify about copying |
210 |
try { |
211 |
loader.wait (); |
212 |
} catch (InterruptedException ex) { |
213 |
asyncError = ex; |
214 |
} |
215 |
|
216 |
try { |
217 |
asyncRes = DataObject.find (current); |
218 |
currentThread.interrupt(); |
219 |
} catch (IOException ex) { |
220 |
asyncError = ex; |
221 |
} |
222 |
|
223 |
// notify that we have computed everything |
224 |
finished = true; |
225 |
loader.notify (); |
226 |
|
227 |
while (asyncRes != null && asyncError != null) { |
228 |
try { |
229 |
loader.wait (); |
230 |
} catch (InterruptedException ex) { |
231 |
} |
232 |
} |
233 |
} |
234 |
} |
235 |
|
236 |
} |
237 |
|
238 |
public static final class BLoader extends UniFileLoader { |
239 |
public BLoader() { |
240 |
super(DataObject.class.getName()); |
241 |
} |
242 |
protected void initialize() { |
243 |
super.initialize(); |
244 |
getExtensions().addExtension("attr"); |
245 |
} |
246 |
protected String displayName() { |
247 |
return getClass ().getName (); |
248 |
} |
249 |
protected org.openide.filesystems.FileObject findPrimaryFile(org.openide.filesystems.FileObject fileObject) { |
250 |
if (Boolean.TRUE.equals (fileObject.getAttribute ("A"))) { |
251 |
return null; |
252 |
} |
253 |
|
254 |
org.openide.filesystems.FileObject retValue; |
255 |
|
256 |
retValue = super.findPrimaryFile(fileObject); |
257 |
return retValue; |
258 |
} |
259 |
|
260 |
protected MultiDataObject createMultiObject(FileObject pf) throws IOException { |
261 |
return new MultiDataObject(pf, this); |
262 |
} |
263 |
} |
264 |
|
265 |
private static final class SlowEntry extends MultiDataObject.Entry { |
266 |
public SlowEntry (MultiDataObject obj, FileObject fo) { |
267 |
obj.super (fo); |
268 |
} |
269 |
|
270 |
private void notifyCopied (FileObject fo) { |
271 |
ALoader l = (ALoader)ALoader.getLoader(ALoader.class); |
272 |
l.notifyCopied (fo); |
273 |
} |
274 |
|
275 |
public org.openide.filesystems.FileObject copy(org.openide.filesystems.FileObject fileObject, String str) throws java.io.IOException { |
276 |
FileObject ret = fileObject.createData ("copy", "attr"); |
277 |
notifyCopied (ret); |
278 |
ret.setAttribute ("A", Boolean.TRUE); |
279 |
return ret; |
280 |
} |
281 |
|
282 |
public org.openide.filesystems.FileObject createFromTemplate(org.openide.filesystems.FileObject fileObject, String str) throws java.io.IOException { |
283 |
FileObject ret = fileObject.createData ("createFromTemplate", "attr"); |
284 |
notifyCopied (ret); |
285 |
ret.setAttribute ("A", Boolean.TRUE); |
286 |
return ret; |
287 |
} |
288 |
|
289 |
public void delete() throws java.io.IOException { |
290 |
throw new IOException ("Not implemented"); |
291 |
} |
292 |
|
293 |
public org.openide.filesystems.FileObject move(org.openide.filesystems.FileObject fileObject, String str) throws java.io.IOException { |
294 |
FileObject ret = fileObject.createData ("move", "attr"); |
295 |
notifyCopied (ret); |
296 |
ret.setAttribute ("A", Boolean.TRUE); |
297 |
super.getFile ().delete (); |
298 |
return ret; |
299 |
} |
300 |
|
301 |
public org.openide.filesystems.FileObject rename(String str) throws java.io.IOException { |
302 |
FileObject ret = getFile ().getParent ().createData ("rename", "attr"); |
303 |
notifyCopied (ret); |
304 |
ret.setAttribute ("A", Boolean.TRUE); |
305 |
super.getFile ().delete (); |
306 |
return ret; |
307 |
} |
308 |
|
309 |
} |
310 |
} |