1 19 20 package org.netbeans.modules.favorites; 21 22 import java.io.File ; 23 import java.io.PrintStream ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Date ; 27 import javax.swing.event.ChangeEvent ; 28 import javax.swing.event.ChangeListener ; 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 name) { 60 super (name); 61 } 62 63 64 67 protected void runTest () throws Throwable { 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 { 79 clearWorkDir(); 80 81 MockServices.setServices(new Class [] {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 folder = new File (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 hidden = new File (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 target = new File (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 { 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 { 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 213 214 215 private Node assertNodeForDataObject(String 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 } 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 listeners = new ArrayList (); 253 public synchronized void addChangeListener(ChangeListener l) { 254 listeners.add(l); 255 } 256 257 public synchronized void removeChangeListener(ChangeListener l) { 258 listeners.remove(l); 259 } 260 261 public void fire() { 262 ChangeEvent ev = new ChangeEvent (this); 263 ChangeListener [] arr; 264 synchronized (this) { 265 arr = (ChangeListener [])listeners.toArray(new ChangeListener [0]); 266 } 267 268 for (int i = 0; i < arr.length; i++) { 269 arr[i].stateChanged(ev); 270 } 271 } 272 } 273 public static final class ErrManager extends ErrorManager { 277 public static final StringBuffer messages = new StringBuffer (); 278 279 private String prefix; 280 281 private static PrintStream log; 282 283 public ErrManager () { 284 this (null); 285 } 286 public ErrManager (String prefix) { 287 this.prefix = prefix; 288 } 289 290 public Throwable annotate (Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, Date date) { 291 return t; 292 } 293 294 public Throwable attachAnnotations (Throwable t, ErrorManager.Annotation[] arr) { 295 return t; 296 } 297 298 public ErrorManager.Annotation[] findAnnotations (Throwable t) { 299 return null; 300 } 301 302 public ErrorManager getInstance (String name) { 303 if ( 304 true 305 ) { 308 return new ErrManager ('[' + name + ']'); 309 } else { 310 return new ErrManager (); 312 } 313 } 314 315 public void log (int severity, String 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 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 } 344 } 345 | Popular Tags |