KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > favorites > VisibilityQueryWorksTest


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.netbeans.modules.favorites;
21
22 import java.io.File JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Date JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import junit.framework.AssertionFailedError;
30 import org.netbeans.junit.MockServices;
31 import org.netbeans.junit.NbTestCase;
32 import org.netbeans.spi.queries.VisibilityQueryImplementation;
33 import org.openide.ErrorManager;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.filesystems.Repository;
37 import org.openide.loaders.DataFolder;
38 import org.openide.loaders.DataObject;
39 import org.openide.loaders.DataShadow;
40 import org.openide.nodes.Node;
41 import org.openide.util.Lookup;
42
43 public class VisibilityQueryWorksTest extends NbTestCase {
44     private FileObject hiddenFO;
45     private FileObject folderFO;
46     private FileObject targetFO;
47     private FileObject favoritesFO;
48     
49     private DataObject hiddenDO;
50     private DataFolder folderDO;
51     private DataFolder targetDO;
52     private DataFolder favoritesDO;
53     
54     private ErrorManager err;
55
56     private DataFolder rootDO;
57     
58     
59     public VisibilityQueryWorksTest(String JavaDoc name) {
60         super (name);
61     }
62     
63     
64     /** If execution fails we wrap the exception with
65      * new log message.
66      */

67     protected void runTest () throws Throwable JavaDoc {
68         try {
69             super.runTest ();
70         } catch (AssertionFailedError ex) {
71             AssertionFailedError ne = new AssertionFailedError (ex.getMessage () + " Log:\n" + ErrManager.messages);
72             ne.setStackTrace (ex.getStackTrace ());
73             throw ne;
74         }
75     }
76
77     
78     protected void setUp () throws Exception JavaDoc {
79         clearWorkDir();
80
81         MockServices.setServices(new Class JavaDoc[] {ErrManager.class, VQI.class});
82         ((VQI) Lookup.getDefault().lookup(VQI.class)).init();
83
84         ErrManager.log = getLog();
85         err = ErrorManager.getDefault().getInstance("TEST-" + getName() + "");
86         
87         err.log("Starting test");
88         
89         super.setUp ();
90
91         try {
92             File JavaDoc folder = new File JavaDoc(getWorkDir(), "folder");
93             folder.mkdirs();
94             this.folderFO = FileUtil.toFileObject(folder);
95             assertNotNull("Directory object found", folderFO);
96             
97             err.log("folder create");
98
99             File JavaDoc hidden = new File JavaDoc(folder, "a-hidden.txt");
100             hidden.createNewFile();
101             this.hiddenFO = FileUtil.toFileObject(hidden);
102             assertNotNull("File object found", hiddenFO);
103             
104             err.log("a-hidden.txt created");
105
106             File JavaDoc target = new File JavaDoc(getWorkDir(), "target");
107             target.mkdirs();
108             this.targetFO = FileUtil.toFileObject(target);
109             assertNotNull("Directory object found", targetFO);
110             
111             err.log("target created");
112
113             this.favoritesFO = FileUtil.createFolder (Repository.getDefault().getDefaultFileSystem().getRoot(), "Favorites");
114             assertNotNull("Created favorites folder", this.favoritesFO);
115             assertEquals("One child", 1, Repository.getDefault().getDefaultFileSystem().getRoot().getChildren().length);
116             
117             err.log("Favorites created");
118
119             FileObject[] arr = this.favoritesFO.getChildren();
120             for (int i = 0; i < arr.length; i++) {
121                 err.log("Delete: " + arr[i]);
122                 arr[i].delete();
123                 err.log("Done");
124             }
125
126             this.hiddenDO = DataObject.find(hiddenFO);
127             this.folderDO = DataFolder.findFolder(folderFO);
128             this.favoritesDO = DataFolder.findFolder(favoritesFO);
129             this.targetDO = DataFolder.findFolder(targetFO);
130             this.rootDO = DataFolder.findFolder(FileUtil.toFileObject(getWorkDir()));
131             
132             err.log("DataObjects created");
133
134             DataObject res;
135             res = hiddenDO.createShadow(favoritesDO);
136             err.log("shadow created: " + res);
137             res = folderDO.createShadow(favoritesDO);
138             err.log("shadow created: " + res);
139             res = targetDO.createShadow(favoritesDO);
140             err.log("shadow created: " + res);
141             res = rootDO.createShadow(favoritesDO);
142             err.log("shadow created: " + res);
143
144             assertEquals("Four items in favorites", 4, favoritesDO.getChildren().length);
145             err.log("Children are ok");
146             assertEquals("Four items in node favorites", 4, favoritesDO.getNodeDelegate().getChildren().getNodes(true).length);
147             err.log("Nodes are ok");
148         } catch (AssertionFailedError ex) {
149             AssertionFailedError ne = new AssertionFailedError (ex.getMessage () + " Log:\n" + ErrManager.messages);
150             ne.setStackTrace (ex.getStackTrace ());
151             throw ne;
152         }
153             
154     }
155     
156     public void testLinksAreVisibleAllTheTime() throws Exception JavaDoc {
157         Node[] arr = Favorites.getNode().getChildren().getNodes(true);
158         assertNodeForDataObject("hidden object is there", hiddenDO, true, arr);
159         assertNodeForDataObject("folder as well", folderDO, true, arr);
160     }
161
162     public void testHiddenFilesInFoldersAreHidden() throws Exception JavaDoc {
163         Node[] arr = Favorites.getNode().getChildren().getNodes(true);
164         Node f = assertNodeForDataObject("folder as well", folderDO, true, arr);
165         
166         arr = f.getChildren().getNodes(true);
167         
168         assertNodeForDataObject("hidden object is not there", hiddenDO, false, arr);
169         assertEquals("No children at all", 0, arr.length);
170
171         VQI vqi = (VQI) Lookup.getDefault().lookup(VQI.class);
172         vqi.showAll = true;
173         vqi.fire();
174         
175         arr = f.getChildren().getNodes(true);
176         assertNodeForDataObject("hidden object is now there", hiddenDO, true, arr);
177         assertEquals("One child at all", 1, arr.length);
178     }
179
180     /* these tests were created to fix issue 62863, but it is not going
181       to be fixed this way, so leaving commented out...
182      
183     public void testCopyOfFolderIgnoresHiddenFile() throws Exception {
184         doCopyOrCut(true);
185     }
186     public void testCutOfFolderIgnoresHiddenFile() throws Exception {
187         doCopyOrCut(false);
188     }
189     
190     private void doCopyOrCut(boolean copy) throws Exception {
191         Node[] arr = Favorites.getNode().getChildren().getNodes(true);
192         Node f = assertNodeForDataObject("folder is there ", rootDO, true, arr);
193         arr = f.getChildren().getNodes(true);
194         f = assertNodeForDataObject("folder is there ", folderDO, true, arr);
195         Node t = assertNodeForDataObject("target as well", targetDO, true, arr);
196         
197         Transferable trans = copy ? f.clipboardCopy() : f.clipboardCut();
198         PasteType[] pastes = t.getPasteTypes(trans);
199         assertEquals ("One paste", 1, pastes.length);
200         
201         pastes[0].paste();
202         
203         arr = t.getChildren().getNodes(true);
204         assertEquals("No children at all", 0, arr.length);
205         
206         Thread.sleep(1000);
207         
208         assertEquals("No children on loader level", 0, targetDO.getChildren().length);
209         assertEquals("No children on fs level", 0, targetDO.getPrimaryFile().getChildren().length);
210         assertEquals("No children on disk", 0, FileUtil.toFile(targetDO.getPrimaryFile()).list().length);
211     }
212      */

213     
214     /** @return node that contains the data object or null */
215     private Node assertNodeForDataObject(String JavaDoc msg, DataObject obj, boolean shouldBeThere, Node[] arr) {
216         for (int i = 0; i < arr.length; i++) {
217             boolean ok;
218             DataObject in = (DataObject)arr[i].getCookie(DataObject.class);
219             
220             if (obj == in || ((in instanceof DataShadow) && ((DataShadow)in).getOriginal() == obj)) {
221                 if (shouldBeThere) {
222                     return arr[i];
223                 } else {
224                     fail(msg + " at " + i + " as " + arr[i]);
225                 }
226             }
227         }
228         
229         if (shouldBeThere) {
230             fail(msg + " in " + Arrays.asList(arr));
231         }
232         return null;
233     }
234     
235     public static final class VQI implements VisibilityQueryImplementation {
236         
237         public void init() {
238             showAll = false;
239 // listener = null;
240
}
241
242         boolean showAll;
243         
244         public boolean isVisible(FileObject file) {
245             if (showAll) {
246                 return true;
247             }
248             return file.getPath().indexOf("hidden") == -1;
249         }
250
251         
252         private ArrayList JavaDoc listeners = new ArrayList JavaDoc();
253         public synchronized void addChangeListener(ChangeListener JavaDoc l) {
254             listeners.add(l);
255         }
256
257         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
258             listeners.remove(l);
259         }
260         
261         public void fire() {
262             ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
263             ChangeListener JavaDoc[] arr;
264             synchronized (this) {
265                 arr = (ChangeListener JavaDoc[])listeners.toArray(new ChangeListener JavaDoc[0]);
266             }
267             
268             for (int i = 0; i < arr.length; i++) {
269                 arr[i].stateChanged(ev);
270             }
271         }
272     }
273     //
274
// Logging support
275
//
276
public static final class ErrManager extends ErrorManager {
277         public static final StringBuffer JavaDoc messages = new StringBuffer JavaDoc ();
278         
279         private String JavaDoc prefix;
280
281         private static PrintStream JavaDoc log;
282         
283         public ErrManager () {
284             this (null);
285         }
286         public ErrManager (String JavaDoc prefix) {
287             this.prefix = prefix;
288         }
289         
290         public Throwable JavaDoc annotate (Throwable JavaDoc t, int severity, String JavaDoc message, String JavaDoc localizedMessage, Throwable JavaDoc stackTrace, Date JavaDoc date) {
291             return t;
292         }
293         
294         public Throwable JavaDoc attachAnnotations (Throwable JavaDoc t, ErrorManager.Annotation[] arr) {
295             return t;
296         }
297         
298         public ErrorManager.Annotation[] findAnnotations (Throwable JavaDoc t) {
299             return null;
300         }
301         
302         public ErrorManager getInstance (String JavaDoc name) {
303             if (
304                 true
305 // name.startsWith ("org.openide.loaders.FolderList")
306
// || name.startsWith ("org.openide.loaders.FolderInstance")
307
) {
308                 return new ErrManager ('[' + name + ']');
309             } else {
310                 // either new non-logging or myself if I am non-logging
311
return new ErrManager ();
312             }
313         }
314         
315         public void log (int severity, String JavaDoc s) {
316             if (prefix != null) {
317                 messages.append (prefix);
318                 messages.append (s);
319                 messages.append ('\n');
320                 
321                 if (messages.length() > 30000) {
322                     messages.delete(0, 15000);
323                 }
324                 
325                 log.print(prefix);
326                 log.println(s);
327             }
328         }
329         
330         public void notify (int severity, Throwable JavaDoc t) {
331             log (severity, t.getMessage ());
332         }
333         
334         public boolean isNotifiable (int severity) {
335             return prefix != null;
336         }
337         
338         public boolean isLoggable (int severity) {
339             return prefix != null;
340         }
341         
342     } // end of ErrManager
343

344 }
345
Popular Tags