1 19 20 package org.openide.filesystems; 21 import java.io.*; 22 26 public class RepositoryTestHid extends TestBaseHid { 27 Repository repo; 28 FileSystem defFs; 29 String pkg = "/root/folder1/folder2"; 30 String name = "resource"; 31 String ext = "ext"; 32 String resource1 = pkg+"/"+name+"."+ext; 33 34 public RepositoryTestHid(String testName) { 35 super (testName); 36 } 37 38 41 protected String [] getResources(String testName) { 42 return new String [] {resource1}; 43 } 44 45 protected void setUp() throws java.lang.Exception { 46 super.setUp(); 47 defFs = TestUtilHid.createLocalFileSystem("defaultFs", new String [] {} ); 48 repo = new Repository (defFs); 49 } 50 51 54 58 59 60 public void testGetDefaultFileSystem() { 61 FileSystem retFs = repo.getDefaultFileSystem(); 62 fsAssert("Default file system should not be null", retFs != null); 63 fsAssert(defFs + " should be default file system", defFs.equals(retFs)); 64 } 65 66 67 68 public void testAddFileSystem() { 69 repo.addFileSystem(testedFS); 70 repo.addFileSystem(testedFS); 71 int count = 0; 72 java.util.Enumeration en = repo.getFileSystems(); 73 while (en.hasMoreElements()) { 74 if (en.nextElement().equals(testedFS)) 75 count++; 76 } 77 fsAssert(testedFS+" can be present in Repository only once.",count == 1); 78 } 79 80 81 public void testAddFileSystem2() { 82 if (!testedFS.getSystemName().equals("")) { 83 repo.addFileSystem(testedFS); 84 Repository repo2 = new Repository (testedFS); 85 int count = 0; 86 if (repo.findFileSystem(testedFS.getSystemName()) != null) 87 count++; 88 if (repo2.findFileSystem(testedFS.getSystemName()) != null) 89 count++; 90 91 fsAssert(" FileSystem can be included only in one Repository.",count == 1); 92 } 93 } 94 95 96 97 public void testRemoveFileSystem() { 98 if (!testedFS.getSystemName().equals("")) { 99 repo.addFileSystem(testedFS); 100 FileSystem retFs = repo.findFileSystem(testedFS.getSystemName()); 101 fsAssert(testedFS+" was added to Repository and was not found.",testedFS.equals(retFs)); 102 repo.removeFileSystem(retFs); 103 retFs = repo.findFileSystem(retFs.getSystemName()); 104 fsAssert(testedFS + " " + testedFS.getSystemName() +" was removed from Repository and is still present.",retFs == null); 105 } 106 } 107 108 109 public void testReorder() { 110 MultiFileSystem mfs = new MultiFileSystem (new FileSystem[] {testedFS}); 111 repo.addFileSystem(testedFS); 112 repo.addFileSystem(mfs); 113 repo.reorder(new int[] {2,0,1}); 114 FileSystem[] fss = repo.toArray(); 115 fsAssert("Wrong reordered",fss[0] == mfs && fss[1] == defFs && fss[2] == testedFS); 116 } 117 118 119 public void testGetFileSystems() { 120 repo.addFileSystem(testedFS); 121 java.util.Enumeration en = repo.getFileSystems(); 122 FileSystem[] fss = new FileSystem[2]; 123 for (int i = 0; en.hasMoreElements(); i++) { 124 fss[i] = (FileSystem)en.nextElement(); 125 } 126 fsAssert("Expected two elements in enumeration",fss.length == 2); 127 fsAssert("Expected two different elements in enumeration",fss[0] != fss[1]); 128 fsAssert("Wrong filesystems in enumeration", 129 (fss[0] == defFs && fss[1] == testedFS) || (fss[1] == defFs && fss[0] == testedFS)); 130 } 131 132 133 public void testFileSystems() { 134 testGetFileSystems(); 135 } 136 137 138 public void testToArray() { 139 FileSystem[] fss; 140 fss = repo.toArray(); 141 fsAssert("Expected one element in enumeration",fss.length == 1 && fss[0] == defFs); 142 143 repo.addFileSystem(testedFS); 144 fss = repo.toArray(); 145 fsAssert("Expected two elements in enumeration",fss.length == 2 && fss[1] == testedFS); 146 147 MultiFileSystem mfs = new MultiFileSystem (new FileSystem[] {testedFS}); 148 repo.addFileSystem(mfs); 149 fss = repo.toArray(); 150 fsAssert("Expected two elements in enumeration",fss.length == 3 && fss[2] == mfs); 151 } 152 153 154 public void testFindFileSystem() { 155 fsAssert("Default file system expected to in Repository", 156 repo.findFileSystem(defFs.getSystemName()) != null); 157 158 } 159 160 166 167 173 174 187 188 201 202 203 public void testAddRepositoryListener() { 204 RepList repList = new RepList (); 205 repo.addRepositoryListener(repList); 206 repo.addFileSystem(testedFS); 207 fsAssert("Expected one event",repList.getAdded () == 1); 208 fsAssert("Unexpected event",repList.getRemoved () == 0); 209 fsAssert("Unexpected event",repList.getReordered () == 0); 210 } 211 212 213 public void testRemoveRepositoryListener() { 214 RepList repList = new RepList (); 215 repo.addRepositoryListener(repList); 216 repo.removeRepositoryListener(repList); 217 repo.addFileSystem(testedFS); 218 fsAssert("Expected one event",repList.getAdded () == 0); 219 fsAssert("Unexpected event",repList.getRemoved () == 0); 220 fsAssert("Unexpected event",repList.getReordered () == 0); 221 } 222 223 224 public void testRepfileFolderCreated() throws IOException { 225 FileSystem fs = this.testedFS; 226 FileObject root = fs.getRoot (); 227 repo.addFileSystem(fs); 228 if (!fs.isReadOnly () && !root.isReadOnly()) { 229 root.getChildren(); 230 registerDefaultListener (repo); 231 root.createFolder("testtset"); 232 fileFolderCreatedAssert ("unexpecetd event count",1); 233 } 234 } 235 236 public void testRepfileDataCreated() throws IOException { 237 FileSystem fs = this.testedFS; 238 FileObject root = fs.getRoot (); 239 repo.addFileSystem(fs); 240 if (!fs.isReadOnly () && !root.isReadOnly()) { 241 root.getChildren(); 242 registerDefaultListener (repo); 243 FileObject newF = root.createData("testfile","txe"); 244 fileDataCreatedAssert ("unexpecetd event count",1); 245 } 246 247 } 248 249 public void testRepfileRenamed() throws IOException { 250 FileSystem fs = this.testedFS; 251 FileObject root = fs.getRoot (); 252 repo.addFileSystem(fs); 253 if (!fs.isReadOnly () && !root.isReadOnly()) { 254 root.getChildren(); 255 registerDefaultListener (repo); 256 FileObject newF = root.createData("testfile","txe"); 257 FileLock fLock = newF.lock(); 258 try { 259 newF.rename(fLock,"obscure","uni"); 260 } finally { 261 fLock.releaseLock(); 262 } 263 264 fileRenamedAssert("unexpecetd event count",1); 265 } 266 267 } 268 269 public void testRepfileDeleted() throws IOException { 270 FileSystem fs = this.testedFS; 271 FileObject root = fs.getRoot (); 272 repo.addFileSystem(fs); 273 if (!fs.isReadOnly () && !root.isReadOnly()) { 274 root.getChildren(); 275 registerDefaultListener (repo); 276 FileObject newF = root.createData("testfile","txe"); 277 FileLock fLock = newF.lock(); 278 try { 279 newF.delete(fLock); 280 } finally { 281 fLock.releaseLock(); 282 } 283 284 fileDeletedAssert("unexpecetd event count",1); 285 } 286 287 } 288 289 public class RepList implements RepositoryListener{ 290 int added = 0; 291 int removed = 0; 292 int reordered = 0; 293 public void fileSystemAdded(RepositoryEvent ev) { 294 added++; 295 } 296 public void fileSystemRemoved(RepositoryEvent ev) { 297 removed++; 298 } 299 public void fileSystemPoolReordered(RepositoryReorderedEvent ev) { 300 reordered++; 301 } 302 303 int getAdded () { 304 return added; 305 } 306 int getRemoved () { 307 return removed; 308 } 309 int getReordered () { 310 return reordered; 311 } 312 } 313 } 314 | Popular Tags |