KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DataLoaderOrigTest


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.loaders;
21
22
23 import org.openide.filesystems.*;
24 import java.io.IOException JavaDoc;
25 import java.util.*;
26 import org.netbeans.junit.*;
27 import java.beans.PropertyChangeListener JavaDoc;
28
29 /** Test basic functionality of data loaders.
30  * @author Jesse Glick
31  */

32 public class DataLoaderOrigTest extends NbTestCase {
33
34     public DataLoaderOrigTest(String JavaDoc name) {
35         super(name);
36     }
37     
38     protected void setUp() throws IOException JavaDoc {
39         clearWorkDir();
40     }
41
42     public void testSimpleLoader() throws Exception JavaDoc {
43         DataLoader l = DataLoader.getLoader(SimpleUniFileLoader.class);
44         DataLoaderPool pool;
45         AddLoaderManuallyHid.addRemoveLoader(l, true);
46         try {
47             pool = DataLoaderPool.getDefault ();
48             assertTrue(Arrays.asList(pool.toArray()).contains(l));
49             FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String JavaDoc[] {
50                 "folder/file.simple",
51             });
52             FileObject fo = lfs.findResource("folder/file.simple");
53             assertNotNull(fo);
54             DataObject dob = DataObject.find(fo);
55             assertEquals(SimpleDataObject.class, dob.getClass());
56         } finally {
57             AddLoaderManuallyHid.addRemoveLoader(l, false);
58         }
59         assertFalse(Arrays.asList(pool.toArray()).contains(l));
60         TestUtilHid.destroyLocalFileSystem(getName());
61     }
62
63     /** Test for bugfix #23065
64      */

65     public void testDataObjectFind() throws Exception JavaDoc {
66         DataLoader l = DataLoader.getLoader(SimpleUniFileLoader.class);
67         DataLoaderPool pool;
68         AddLoaderManuallyHid.addRemoveLoader(l, true);
69         try {
70             pool = DataLoaderPool.getDefault ();
71             assertTrue(Arrays.asList(pool.toArray()).contains(l));
72             FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String JavaDoc[] {
73                 "folder/file.simple",
74             });
75             FileObject fo = lfs.findResource("folder/file.simple");
76             assertNotNull(fo);
77             
78             DataObject jdo = DataObject.find(fo);
79             for (int i = 0; i < 5000; i++) {
80                 FileObject primary = jdo.getPrimaryFile();
81                 jdo.setValid(false);
82                 jdo = DataObject.find(primary);
83                 assertNotNull(jdo);
84                 assertTrue(jdo.isValid());
85             }
86             
87         } finally {
88             AddLoaderManuallyHid.addRemoveLoader(l, false);
89         }
90         TestUtilHid.destroyLocalFileSystem(getName());
91     }
92     
93     public static final class SimpleUniFileLoader extends UniFileLoader {
94         public SimpleUniFileLoader() {
95             super(SimpleDataObject.class.getName());
96         }
97         protected void initialize() {
98             super.initialize();
99             getExtensions().addExtension("simple");
100         }
101         protected String JavaDoc displayName() {
102             return "Simple";
103         }
104         protected MultiDataObject createMultiObject(FileObject pf) throws IOException JavaDoc {
105             return new SimpleDataObject(pf, this);
106         }
107     }
108     public static final class SimpleDataObject extends MultiDataObject {
109         private ArrayList supp = new ArrayList ();
110         
111         public SimpleDataObject(FileObject pf, MultiFileLoader loader) throws IOException JavaDoc {
112             super(pf, loader);
113         }
114         
115         /** Access method to modify cookies
116          * @return cookie set of this data object
117          */

118         public final org.openide.nodes.CookieSet cookieSet () {
119             return getCookieSet ();
120         }
121         
122         /** Getter for list of listeners attached to the data object.
123          */

124         public final Enumeration listeners () {
125             return Collections.enumeration (supp);
126         }
127         
128         public void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
129             super.addPropertyChangeListener (l);
130             supp.add (l);
131         }
132
133         public void removePropertyChangeListener (PropertyChangeListener JavaDoc l) {
134             super.removePropertyChangeListener (l);
135             supp.remove (l);
136         }
137     }
138     
139     /** Test that finding a two-part object (here folder + file, with folder prim)
140      * works reliably regardless of timing, if both parts in fact exist at the time
141      * DataObject.find is called. Important for XML window system.
142      * @see "#15928"
143      * @author Jesse Glick
144      */

145     public void testFindTwoPart() throws Exception JavaDoc {
146         DataLoader l = DataLoader.getLoader(TwoPartLoader.class);
147         AddLoaderManuallyHid.addRemoveLoader(l, true);
148         try {
149             FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String JavaDoc[] {
150                 "folder/part/",
151                 "folder/part.ext",
152                 "folder/trash.txt",
153             });
154             // This method will be the easy version, harder versions below.
155
FileObject fo = lfs.findResource("folder/part");
156             assertEquals(TwoPartObject.class, DataObject.find(fo).getClass());
157         } finally {
158             AddLoaderManuallyHid.addRemoveLoader(l, false);
159         }
160         TestUtilHid.destroyLocalFileSystem(getName());
161     }
162     
163 // /** Known to fail frequently. */
164
// public void testFindTwoPart1() throws Exception {
165
// abstractTestFindTwoPart(false, false, false);
166
// }
167
// /** Not known to fail, but not very surprising if it does. */
168
// public void testFindTwoPart2() throws Exception {
169
// abstractTestFindTwoPart(false, false, true);
170
// }
171
// /** Not known to fail, but not very surprising if it does. */
172
// public void testFindTwoPart3() throws Exception {
173
// abstractTestFindTwoPart(false, true, false);
174
// }
175
// /** Not known to fail, but not very surprising if it does. */
176
// public void testFindTwoPart4() throws Exception {
177
// abstractTestFindTwoPart(false, true, true);
178
// }
179
// /** Known to fail frequently. */
180
// public void testFindTwoPart5() throws Exception {
181
// abstractTestFindTwoPart(true, false, false);
182
// }
183
/** Hopefully will not fail: supposed to be safest option. */
184     public void testFindTwoPart6() throws Exception JavaDoc {
185         abstractTestFindTwoPart(true, false, true);
186     }
187 // /** Known to fail occasionally. */
188
// public void testFindTwoPart7() throws Exception {
189
// abstractTestFindTwoPart(true, true, false);
190
// }
191
// /** Known to fail occasionally. */
192
// public void testFindTwoPart8() throws Exception {
193
// abstractTestFindTwoPart(true, true, true);
194
// }
195

196     private static final boolean DEBUG = false;
197     private void abstractTestFindTwoPart(final boolean atomic, final boolean makePrimFirst, final boolean findPrim) throws Exception JavaDoc {
198         if (DEBUG) System.err.println("atomic=" + atomic + " makePrimFirst=" + makePrimFirst + " findPrim=" + findPrim);
199         DataLoader l = DataLoader.getLoader(TwoPartLoader.class);
200         AddLoaderManuallyHid.addRemoveLoader(l, true);
201         try {
202             TestUtilHid.destroyLocalFileSystem(getName());
203             FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String JavaDoc[] {
204                 "folder/trash.txt",
205             });
206             final FileObject filefolder = lfs.findResource("folder");
207             final boolean[] stop = new boolean[] {false};
208             Thread JavaDoc t = new Thread JavaDoc("recognizing objects sometimes") {
209                 public void run() {
210                     try {
211                         DataFolder folder = DataFolder.findFolder(filefolder);
212                         for (int delay = 0; !stop[0]; delay++) {
213                             Thread.sleep(delay * 10);
214                             switch (delay % 3) {
215                             case 0:
216                                 DataObject[] kids = folder.getChildren();
217                                 if (DEBUG) System.err.println("got children; " + kids.length);
218                                 break;
219                             case 1:
220                                 FileObject fo = filefolder.getFileObject("part");
221                                 if (fo != null) {
222                                     // Don't particularly care what the result is
223
// here; mainly just want to make the folder
224
// recognizer do something in hopes of clashing
225
// with the main thread.
226
DataObject o = DataObject.find(fo);
227                                     if (DEBUG) System.err.println("from folder found: " + o);
228                                 } else {
229                                     if (DEBUG) System.err.println("no folder folder/part");
230                                 }
231                                 break;
232                             case 2:
233                                 fo = filefolder.getFileObject("part", "ext");
234                                 if (fo != null) {
235                                     DataObject o = DataObject.find(fo);
236                                     if (DEBUG) System.err.println("from file found: " + o);
237                                 } else {
238                                     if (DEBUG) System.err.println("no file folder/part.ext");
239                                 }
240                                 break;
241                             default:
242                                 throw new IllegalStateException JavaDoc();
243                             }
244                         }
245                     } catch (Exception JavaDoc e) {
246                         e.printStackTrace();
247                     }
248                 }
249             };
250             try {
251                 t.start();
252                 for (int i = 0; i < 35; i++) {
253                     // Sleep a different amount each time.
254
// Total sleep time ~ 5sec.
255
Thread.sleep((i * 17) % 100);
256                     // Create the files.
257
final FileObject[] primSec = new FileObject[2];
258                     //final String name = "part" + i;
259
final String JavaDoc name = "part";
260                     final int pause = (i * 23) % 100;
261                     final int _i = i;
262                     FileSystem.AtomicAction action = new FileSystem.AtomicAction() {
263                         public void run() throws IOException JavaDoc {
264                             try {
265                                 if (makePrimFirst) {
266                                     loadPrim();
267                                     Thread.sleep(pause);
268                                 }
269                                 loadSec();
270                                 if (! makePrimFirst) {
271                                     Thread.sleep(pause);
272                                     loadPrim();
273                                 }
274                             } catch (InterruptedException JavaDoc ie) {
275                                 throw new IOException JavaDoc(ie.toString());
276                             }
277                         }
278                         private void loadPrim() throws IOException JavaDoc {
279                             primSec[0] = filefolder.createFolder(name);
280                         }
281                         private void loadSec() throws IOException JavaDoc {
282                             primSec[1] = filefolder.createData(name, "ext");
283                         }
284                     };
285                     if (atomic) {
286                         lfs.runAtomicAction(action);
287                     } else {
288                         action.run();
289                     }
290                     Thread.sleep((i * 19) % 100);
291                     FileObject tofind = findPrim ? primSec[0] : primSec[1];
292                     DataObject dob = DataObject.find(tofind);
293                     assertEquals("On iteration #" + i + ", found right object", TwoPartObject.class, dob.getClass());
294                     if (DEBUG) System.err.println("it worked, #" + i);
295                     // Clean up for the next round.
296
action = new FileSystem.AtomicAction() {
297                         public void run() throws IOException JavaDoc {
298                             // Delete in reverse order.
299
if (! makePrimFirst) {
300                                 primSec[0].delete();
301                             }
302                             primSec[1].delete();
303                             if (makePrimFirst) {
304                                 primSec[0].delete();
305                             }
306                         }
307                     };
308                     if (atomic) {
309                         lfs.runAtomicAction(action);
310                     } else {
311                         action.run();
312                     }
313                 }
314             } finally {
315                 // Done, stop recognizer.
316
stop[0] = true;
317                 t.join(5000);
318             }
319         } finally {
320             AddLoaderManuallyHid.addRemoveLoader(l, false);
321         }
322         TestUtilHid.destroyLocalFileSystem(getName());
323     }
324     
325     public static final class TwoPartLoader extends MultiFileLoader {
326         public TwoPartLoader() {
327             super(TwoPartObject.class);
328         }
329         protected String JavaDoc displayName() {
330             return "TwoPart";
331         }
332         protected FileObject findPrimaryFile(FileObject fo) {
333             if (fo.isFolder() && ! fo.isRoot() && FileUtil.findBrother(fo, "ext") != null) {
334                 return fo;
335             } else if (fo.isData() && fo.hasExt("ext")) {
336                 FileObject fo2 = fo.getParent().getFileObject(fo.getName());
337                 if (fo2 != null && fo2.isFolder()) {
338                     return fo2;
339                 }
340             }
341             return null;
342         }
343         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
344             return new TwoPartObject(this, primaryFile);
345         }
346         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
347             return new FileEntry.Folder(obj, primaryFile);
348         }
349         protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
350             return new FileEntry(obj, secondaryFile);
351         }
352     }
353     public static final class TwoPartObject extends DataFolder {
354         public TwoPartObject(TwoPartLoader l, FileObject folder) throws DataObjectExistsException {
355             super(folder, l);
356         }
357     }
358     
359     
360 }
361
Popular Tags