KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import org.openide.cookies.OpenCookie;
25
26 import org.openide.filesystems.*;
27 import org.netbeans.junit.*;
28 import java.io.IOException JavaDoc;
29 import java.util.*;
30 import java.util.logging.Level JavaDoc;
31 import org.openide.*;
32 import org.openide.util.Enumerations;
33
34 /**
35  * @author Jaroslav Tulach
36  */

37 public class MultiDataObjectTest extends NbTestCase {
38     FileSystem fs;
39     DataObject one;
40     DataFolder from;
41     DataFolder to;
42     ErrorManager err;
43     
44     
45     /** Creates new DataObjectTest */
46     public MultiDataObjectTest (String JavaDoc name) {
47         super (name);
48     }
49     
50     protected Level JavaDoc logLevel() {
51         return Level.INFO;
52     }
53     
54     public void setUp() throws Exception JavaDoc {
55         clearWorkDir();
56         
57         super.setUp();
58         
59         MockServices.setServices(Pool.class);
60         
61         err = ErrorManager.getDefault().getInstance("TEST-" + getName());
62         
63         LocalFileSystem lfs = new LocalFileSystem();
64         lfs.setRootDirectory(getWorkDir());
65         fs = lfs;
66         FileUtil.createData(fs.getRoot(), "from/x.prima");
67         FileUtil.createData(fs.getRoot(), "from/x.seconda");
68         FileUtil.createFolder(fs.getRoot(), "to/");
69         
70         one = DataObject.find(fs.findResource("from/x.prima"));
71         assertEquals(SimpleObject.class, one.getClass());
72         
73         from = one.getFolder();
74         to = DataFolder.findFolder(fs.findResource("to/"));
75         
76         assertEquals("Nothing there", 0, to.getPrimaryFile().getChildren().length);
77     }
78     
79     public void testTheSetOfSecondaryEntriesIsSaidToGetInconsistent() throws Exception JavaDoc {
80         for (int i = 0; i < 10; i++) {
81             err.log(i + " getting children of to");
82             DataObject[] to1 = to.getChildren();
83             err.log(i + " getting children of from");
84             DataObject[] from1 = from.getChildren();
85             err.log(i + " getting files of object1");
86             Object JavaDoc[] arr1 = one.files().toArray();
87             err.log(i + " moving the object");
88             one.move(to);
89             err.log(i + " 2nd children of to");
90             DataObject[] to2 = to.getChildren();
91             err.log(i + " 2nd children of from");
92             DataObject[] from2 = from.getChildren();
93             err.log(i + " 2nd files of object1");
94             Object JavaDoc[] arr2 = one.files().toArray();
95             err.log(i + " checking results");
96             
97             assertEquals("Round " + i + " To is empty: " + Arrays.asList(to1), 0, to1.length);
98             assertEquals("Round " + i + " From has one:" + Arrays.asList(from1), 1, from1.length);
99             assertEquals("Round " + i + " One has two files" + Arrays.asList(arr1), 2, arr1.length);
100             
101             assertEquals("Round " + i + " From is empty after move: " + Arrays.asList(from2), 0, from2.length);
102             assertEquals("Round " + i + " To has one:" + Arrays.asList(to2), 1, to2.length);
103             assertEquals("Round " + i + " One still has two files" + Arrays.asList(arr1), 2, arr1.length);
104             
105             err.log(i + " moving back");
106             one.move(from);
107             err.log(i + " end of cycle");
108         }
109     }
110
111     public void testConsistencyWithABitOfAsynchronicity() throws Exception JavaDoc {
112         err.log(" getting children of to");
113         DataObject[] to1 = to.getChildren();
114         err.log(" getting children of from");
115         DataObject[] from1 = from.getChildren();
116         
117         
118         for (int i = 0; i < 10; i++) {
119             err.log(i + " getting files of object1");
120             Object JavaDoc[] arr1 = one.files().toArray();
121             err.log(i + " moving the object");
122             one.move(to);
123             Object JavaDoc[] arr2 = one.files().toArray();
124             err.log(i + " checking results");
125             
126             assertEquals("Round " + i + " One has two files" + Arrays.asList(arr1), 2, arr1.length);
127             
128             assertEquals("Round " + i + " One still has two files" + Arrays.asList(arr1), 2, arr1.length);
129             
130             err.log(i + " moving back");
131             one.move(from);
132             err.log(i + " end of cycle");
133         }
134     }
135
136     public void testConsistencyWithABitOfAsynchronicityAndNoObservationsThatWouldMangeTheState() throws Exception JavaDoc {
137         err.log(" getting children of to");
138         DataObject[] to1 = to.getChildren();
139         err.log(" getting children of from");
140         DataObject[] from1 = from.getChildren();
141         
142         
143         for (int i = 0; i < 10; i++) {
144             err.log(i + " moving the object");
145             one.move(to);
146             err.log(i + " moving back");
147             one.move(from);
148             err.log(i + " end of cycle");
149         }
150     }
151     
152     public void testConsistencyWithContinuousQueryingForDeletedFiles() throws Exception JavaDoc {
153         err.log(" getting children of to");
154         DataObject[] to1 = to.getChildren();
155         err.log(" getting children of from");
156         DataObject[] from1 = from.getChildren();
157         
158         class Queri extends Thread JavaDoc
159         implements FileChangeListener, DataLoader.RecognizedFiles, PropertyChangeListener JavaDoc {
160             public boolean stop;
161             private List deleted = Collections.synchronizedList(new ArrayList());
162             public Exception JavaDoc problem;
163             
164             public Queri() {
165                 super("Query background thread");
166                 setPriority(MAX_PRIORITY);
167             }
168
169             public void fileFolderCreated(FileEvent fe) {
170             }
171
172             public void fileDataCreated(FileEvent fe) {
173             }
174
175             public void fileChanged(FileEvent fe) {
176             }
177
178             public void fileDeleted(FileEvent fe) {
179                 deleted.add(fe.getFile());
180             }
181
182             public void fileRenamed(FileRenameEvent fe) {
183             }
184
185             public void fileAttributeChanged(FileAttributeEvent fe) {
186             }
187             
188             public void run () {
189                 while(!stop) {
190                     FileObject[] arr = (FileObject[]) deleted.toArray(new FileObject[0]);
191                     DataLoader loader = SimpleLoader.getLoader(SimpleLoader.class);
192                     err.log("Next round");
193                     for (int i = 0; i < arr.length; i++) {
194                         try {
195                             err.log("Checking " + arr[i]);
196                             DataObject x = loader.findDataObject(arr[i], this);
197                             err.log(" has dobj: " + x);
198                         } catch (IOException JavaDoc ex) {
199                             if (problem == null) {
200                                 problem = ex;
201                             }
202                         }
203                     }
204                 }
205             }
206
207             public void markRecognized(FileObject fo) {
208             }
209
210             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
211                 if ("afterMove".equals(evt.getPropertyName())) {
212                     Thread.yield();
213                 }
214             }
215         }
216         
217         Queri que = new Queri();
218         
219         to.getPrimaryFile().addFileChangeListener(que);
220         from.getPrimaryFile().addFileChangeListener(que);
221         
222         que.start();
223         try {
224             for (int i = 0; i < 10; i++) {
225                 err.log(i + " moving the object");
226                 one.move(to);
227                 err.log(i + " moving back");
228                 one.move(from);
229                 err.log(i + " end of cycle");
230             }
231         } finally {
232             que.stop = true;
233         }
234         
235         que.join();
236         if (que.problem != null) {
237             throw que.problem;
238         }
239         
240         assertEquals("Fourty deleted files:" + que.deleted, 40, que.deleted.size());
241     }
242
243     public void testAdditionsToCookieSetAreVisibleInLookup() throws Exception JavaDoc {
244         assertTrue(this.one instanceof SimpleObject);
245         SimpleObject s = (SimpleObject)this.one;
246         
247         class Open implements OpenCookie {
248             public void open() {
249             }
250         }
251         Open openCookie = new Open();
252         
253         
254         s.getCookieSet().add(openCookie);
255         
256         assertSame("Cookie is in the lookup", openCookie, one.getLookup().lookup(OpenCookie.class));
257     }
258
259     
260     public static final class Pool extends DataLoaderPool {
261         protected Enumeration loaders() {
262             return Enumerations.singleton(SimpleLoader.getLoader(SimpleLoader.class));
263         }
264     }
265     
266     public static final class SimpleLoader extends MultiFileLoader {
267         public SimpleLoader() {
268             super(SimpleObject.class);
269         }
270         protected String JavaDoc displayName() {
271             return "SimpleLoader";
272         }
273         protected FileObject findPrimaryFile(FileObject fo) {
274             if (!fo.isFolder()) {
275                 // emulate the behaviour of form data object
276

277                 /* emulate!? this one is written too well ;-)
278                 FileObject primary = FileUtil.findBrother(fo, "prima");
279                 FileObject secondary = FileUtil.findBrother(fo, "seconda");
280                 
281                 if (primary == null || secondary == null) {
282                     return null;
283                 }
284                 
285                 if (primary != fo && secondary != fo) {
286                     return null;
287                 }
288                  */

289                 
290                 // here is the common code for the worse behaviour
291
if (fo.hasExt("prima")) {
292                     return FileUtil.findBrother(fo, "seconda") != null ? fo : null;
293                 }
294                 
295                 if (fo.hasExt("seconda")) {
296                     return FileUtil.findBrother(fo, "prima");
297                 }
298             }
299             return null;
300         }
301         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
302             return new SimpleObject(this, primaryFile);
303         }
304         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
305             return new FileEntry(obj, primaryFile);
306         }
307         protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
308             return new FileEntry(obj, secondaryFile);
309         }
310
311         private void afterMove(FileObject f, FileObject retValue) {
312             firePropertyChange("afterMove", null, null);
313         }
314     }
315     
316     private static final class FE extends FileEntry {
317         public FE(MultiDataObject mo, FileObject fo) {
318             super(mo, fo);
319         }
320
321         public FileObject move(FileObject f, String JavaDoc suffix) throws IOException JavaDoc {
322             FileObject retValue;
323             retValue = super.move(f, suffix);
324             
325             SimpleLoader l = (SimpleLoader)getDataObject().getLoader();
326             l.afterMove(f, retValue);
327             
328             return retValue;
329         }
330         
331         
332     }
333     
334     public static final class SimpleObject extends MultiDataObject {
335         public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException {
336             super(fo, l);
337         }
338     }
339
340 }
341
Popular Tags