1 19 20 package org.openide.loaders; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 import javax.swing.event.ChangeEvent ; 26 import junit.framework.AssertionFailedError; 27 import org.openide.filesystems.*; 28 import org.openide.loaders.*; 29 import java.beans.*; 30 import java.io.IOException ; 31 import java.util.*; 32 import junit.textui.TestRunner; 33 import org.netbeans.junit.*; 34 import org.openide.util.Enumerations; 35 import org.openide.util.RequestProcessor; 36 37 44 public class OperationListenerTest extends LoggingTestCaseHid 45 implements OperationListener { 46 private ArrayList events = new ArrayList (); 47 private FileSystem fs; 48 private DataLoaderPool pool; 49 private Logger err; 50 51 52 public OperationListenerTest(String name) { 53 super(name); 54 } 55 56 protected void setUp() throws Exception { 58 registerIntoLookup(new Pool()); 59 60 String fsstruct [] = new String [] { 61 "source/A.attr", 62 "B.attr", 63 "dir/", 64 "fake/A.instance" 65 }; 66 TestUtilHid.destroyLocalFileSystem (getName()); 67 fs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct); 68 69 err = Logger.getLogger("TEST-" + getName()); 70 71 pool = DataLoaderPool.getDefault (); 72 assertNotNull (pool); 73 assertEquals (Pool.class, pool.getClass ()); 74 75 Pool.setExtra(null); 76 77 err.info("setUp is over"); 78 } 79 80 protected void tearDown() throws Exception { 82 err.info("entering tearDown"); 83 84 pool.removeOperationListener(this); 85 86 err.info("Making sure everything is cleaned"); 87 WeakReference ref = new WeakReference (fs); 88 fs = null; 89 events = null; 90 assertGC("GC the filesystem", ref); 91 err.info("Ok, tearDown finished"); 92 93 } 96 97 101 public void testRecognizeFolder () { 102 pool.addOperationListener(this); 103 DataFolder df = DataFolder.findFolder (fs.findResource ("fake")); 104 DataObject[] arr = df.getChildren (); 105 106 assertEquals ("One child", 1, arr.length); 107 108 assertEvents ("Recognized well", new OperationEvent[] { 109 new OperationEvent (df), 110 new OperationEvent (arr[0]) 111 }); 112 } 113 114 public void testCopyFile() throws Exception { 115 err.info("Before add listener"); 116 pool.addOperationListener(this); 117 err.info("after add listener"); 118 DataObject obj = DataObject.find (fs.findResource ("fake/A.instance")); 119 err.info("object found: " + obj); 120 DataFolder df = DataFolder.findFolder (fs.findResource ("dir")); 121 err.info("folder found: " + df); 122 DataObject n = obj.copy (df); 123 err.info("copy done: " + n); 124 assertEquals ("Copy successfull", n.getFolder(), df); 125 126 err.info("Comparing events"); 127 assertEvents ("All well", new OperationEvent[] { 128 new OperationEvent (obj), 129 new OperationEvent (df), 130 new OperationEvent (n), 131 new OperationEvent.Copy (n, obj) 132 }); 133 } 134 135 public void testBrokenLoader () throws Exception { 136 BrokenLoader loader = (BrokenLoader)DataLoader.getLoader(BrokenLoader.class); 137 138 try { 139 err.info("before setExtra: " + loader); 140 Pool.setExtra(loader); 141 142 err.info("before addOperationListener"); 143 pool.addOperationListener(this); 144 145 loader.acceptableFO = fs.findResource ("source/A.attr"); 146 err.info("File object found: " + loader.acceptableFO); 147 try { 148 DataObject obj = DataObject.find (fs.findResource ("source/A.attr")); 149 fail ("The broken loader throws exception and cannot be created"); 150 } catch (IOException ex) { 151 err.info("Exception thrown correctly:"); 153 err.log(Level.INFO, null, ex); 154 } 155 assertEquals ("Loader created an object", loader, loader.obj.getLoader()); 156 157 err.info("brefore waitFinished"); 158 loader.recognize.waitFinished (); 160 161 err.info("waitFinished done"); 162 163 assertEvents ("One creation notified even if the object is broken", new OperationEvent[] { 164 new OperationEvent (loader.obj), 165 }); 166 } finally { 167 Pool.setExtra(null); 168 } 169 } 170 171 175 private void assertEvents (String txt, OperationEvent[] expected) { 176 boolean failure = false; 177 if (expected.length != events.size ()) { 178 failure = true; 179 } else { 180 for (int i = 0; i < expected.length; i++) { 181 OperationEvent e = expected[i]; 182 OperationEvent r = (OperationEvent)events.get (i); 183 if (e.getClass () != r.getClass ()) { 184 failure = true; 185 break; 186 } 187 if (e.getObject () != r.getObject()) { 188 failure = true; 189 break; 190 } 191 } 192 } 193 194 195 if (failure) { 196 StringBuffer sb = new StringBuffer (); 197 198 int till = Math.max (expected.length, events.size ()); 199 sb.append ("Expected events: " + expected.length + " was: " + events.size () + "\n"); 200 for (int i = 0; i < till; i++) { 201 sb.append (" Expected: "); 202 if (i < expected.length) { 203 sb.append (expected[i].getClass () + " source: " + expected[i].getObject ()); 204 } 205 sb.append ('\n'); 206 sb.append (" Was : "); 207 if (i < events.size ()) { 208 OperationEvent ev = (OperationEvent)events.get (i); 209 sb.append (ev.getClass () + " source: " + ev.getObject ()); 210 } 211 sb.append ('\n'); 212 } 213 214 fail (sb.toString ()); 215 } 216 217 events.clear(); 218 } 219 220 224 public void operationCopy(org.openide.loaders.OperationEvent.Copy ev) { 225 events.add (ev); 226 err.info(" operationCopy: " + ev); 227 } 228 229 public void operationCreateFromTemplate(org.openide.loaders.OperationEvent.Copy ev) { 230 events.add (ev); 231 err.info(" operationCreateFromTemplate: " + ev); 232 } 233 234 public void operationCreateShadow(org.openide.loaders.OperationEvent.Copy ev) { 235 events.add (ev); 236 err.info(" operationCreateShadow: " + ev); 237 } 238 239 public void operationDelete(OperationEvent ev) { 240 events.add (ev); 241 err.info(" operationDelete: " + ev); 242 } 243 244 public void operationMove(org.openide.loaders.OperationEvent.Move ev) { 245 events.add (ev); 246 err.info(" operationMove: " + ev); 247 } 248 249 public void operationPostCreate(OperationEvent ev) { 250 events.add (ev); 251 err.info(" operationPostCreate: " + ev); 252 } 253 254 public void operationRename(org.openide.loaders.OperationEvent.Rename ev) { 255 events.add (ev); 256 err.info(" operationRename: " + ev); 257 } 258 259 260 public static final class BrokenLoader extends UniFileLoader { 264 public FileObject acceptableFO; 265 public RequestProcessor.Task recognize; 266 public MultiDataObject obj; 267 268 public BrokenLoader() { 269 super(MultiDataObject.class.getName ()); 270 } 271 protected String displayName() { 272 return "BrokenLoader"; 273 } 274 protected FileObject findPrimaryFile(FileObject fo) { 275 if (acceptableFO != null && acceptableFO.equals(fo)) { 276 return fo; 277 } 278 return null; 279 } 280 protected MultiDataObject createMultiObject(final FileObject primaryFile) throws DataObjectExistsException, IOException { 281 obj = new MultiDataObject (primaryFile, this); 282 283 assertNull ("Only one invocation of this code allowed", recognize); 284 285 class R implements Runnable { 286 public DataObject found; 287 public void run () { 288 synchronized (this) { 289 notify (); 290 } 291 try { 296 found = DataObject.find (primaryFile); 297 } catch (IOException ex) { 298 fail ("Unexepcted exception: " + ex); 299 } 300 301 assertEquals ("DataObjects are the same", found, obj); 302 } 303 } 304 R run = new R (); 305 synchronized (run) { 306 recognize = RequestProcessor.getDefault ().post (run); 307 try { 308 run.wait (); 309 } catch (InterruptedException ex) { 310 fail ("Unexepcted ex: " + ex); 311 } 312 } 313 314 315 throw new IOException ("I am broken!"); 316 } 317 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 318 return new FileEntry(obj, primaryFile); 319 } 320 321 public void run() { 322 } 323 324 } 325 326 private static final class Pool extends DataLoaderPool { 327 private static DataLoader extra; 328 329 330 protected Enumeration loaders () { 331 if (extra == null) { 332 return Enumerations.empty (); 333 } else { 334 return Enumerations.singleton (extra); 335 } 336 } 337 338 public static void setExtra(DataLoader aExtra) { 339 extra = aExtra; 340 Pool p = (Pool)DataLoaderPool.getDefault(); 341 p.fireChangeEvent(new ChangeEvent (p)); 342 } 343 } 344 } 345 | Popular Tags |