1 19 20 package org.openide.loaders; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import org.openide.cookies.OpenCookie; 25 26 import org.openide.filesystems.*; 27 import org.netbeans.junit.*; 28 import java.io.IOException ; 29 import java.util.*; 30 import java.util.logging.Level ; 31 import org.openide.*; 32 import org.openide.util.Enumerations; 33 34 37 public class MultiDataObjectTest extends NbTestCase { 38 FileSystem fs; 39 DataObject one; 40 DataFolder from; 41 DataFolder to; 42 ErrorManager err; 43 44 45 46 public MultiDataObjectTest (String name) { 47 super (name); 48 } 49 50 protected Level logLevel() { 51 return Level.INFO; 52 } 53 54 public void setUp() throws Exception { 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 { 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 [] 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 [] 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 { 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 [] arr1 = one.files().toArray(); 121 err.log(i + " moving the object"); 122 one.move(to); 123 Object [] 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 { 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 { 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 159 implements FileChangeListener, DataLoader.RecognizedFiles, PropertyChangeListener { 160 public boolean stop; 161 private List deleted = Collections.synchronizedList(new ArrayList()); 162 public Exception 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 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 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 { 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 displayName() { 271 return "SimpleLoader"; 272 } 273 protected FileObject findPrimaryFile(FileObject fo) { 274 if (!fo.isFolder()) { 275 277 289 290 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 { 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 suffix) throws IOException { 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 |