1 19 20 package org.netbeans.api.project; 21 import java.io.OutputStream ; 22 import java.lang.ref.Reference ; 23 import java.lang.ref.WeakReference ; 24 import java.net.URI ; 25 import java.util.zip.CRC32 ; 26 import java.util.zip.ZipEntry ; 27 import java.util.zip.ZipOutputStream ; 28 import org.netbeans.junit.NbTestCase; 29 import org.netbeans.modules.projectapi.TimedWeakReference; 30 import org.openide.filesystems.FileLock; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 34 38 public class FileOwnerQueryTest extends NbTestCase { 39 40 public FileOwnerQueryTest(String name) { 41 super(name); 42 } 43 44 static { 45 TimedWeakReference.TIMEOUT = 0; 46 } 47 48 private FileObject scratch; 49 private FileObject projdir; 50 private FileObject randomfile; 51 private FileObject projfile; 52 private FileObject projfile2; 53 private FileObject subprojdir; 54 private FileObject subprojfile; 55 private FileObject hashedFile; 56 private Project p; 57 private FileObject zippedfile; 58 59 protected void setUp() throws Exception { 60 TestUtil.setLookup(new Object [] { 61 TestUtil.testProjectFactory(), 62 }); 63 ProjectManager.getDefault().reset(); 64 FileOwnerQuery.reset(); 65 scratch = TestUtil.makeScratchDir(this); 66 projdir = scratch.createFolder("my-project"); 67 projdir.createFolder("testproject"); 68 randomfile = scratch.createData("randomfile"); 69 projfile = projdir.createData("projfile"); 70 FileObject projsubdir = projdir.createFolder("projsubdir"); 71 projfile2 = projsubdir.createData("projfile2"); 72 subprojdir = projdir.createFolder("subproject"); 73 subprojdir.createFolder("testproject"); 74 subprojfile = subprojdir.createData("subprojfile"); 75 scratch.createFolder("external1").createFolder("subdir").createData("file"); 76 scratch.createFolder("external2").createFolder("subdir").createData("file"); 77 scratch.createFolder("external3").createFolder("subproject").createFolder("testproject"); 78 p = ProjectManager.getDefault().findProject(projdir); 79 assertNotNull("found a project successfully", p); 80 81 FileObject foojar = projdir.createData("foo.jar"); 83 FileLock lock = foojar.lock(); 84 try { 85 OutputStream os = foojar.getOutputStream(lock); 86 try { 87 ZipOutputStream zos = new ZipOutputStream (os); 88 ZipEntry ze = new ZipEntry ("zipdir/"); 89 ze.setMethod(ZipEntry.STORED); 90 ze.setSize(0L); 91 ze.setCrc(new CRC32 ().getValue()); 92 zos.putNextEntry(ze); 93 ze = new ZipEntry ("zipdir/zippedfile"); 94 ze.setMethod(ZipEntry.STORED); 95 ze.setSize(0L); 96 ze.setCrc(new CRC32 ().getValue()); 97 zos.putNextEntry(ze); 98 zos.closeEntry(); 99 zos.close(); 100 } finally { 101 os.close(); 102 } 103 } finally { 104 lock.releaseLock(); 105 } 106 FileObject foojarRoot = FileUtil.getArchiveRoot(foojar); 107 assertNotNull("have an archive in " + foojar, foojarRoot); 108 zippedfile = foojarRoot.getFileObject("zipdir/zippedfile"); 109 assertNotNull("zippedfile found in it", zippedfile); 110 111 hashedFile = projdir.createData(".#webapp.jar.1.45"); 112 lock = hashedFile.lock(); 113 try { 114 OutputStream os = hashedFile.getOutputStream(lock); 115 try { 116 ZipOutputStream zos = new ZipOutputStream (os); 117 ZipEntry ze = new ZipEntry ("zipdir/"); 118 ze.setMethod(ZipEntry.STORED); 119 ze.setSize(0L); 120 ze.setCrc(new CRC32 ().getValue()); 121 zos.putNextEntry(ze); 122 ze = new ZipEntry ("zipdir/zippedfile"); 123 ze.setMethod(ZipEntry.STORED); 124 ze.setSize(0L); 125 ze.setCrc(new CRC32 ().getValue()); 126 zos.putNextEntry(ze); 127 zos.closeEntry(); 128 zos.close(); 129 } finally { 130 os.close(); 131 } 132 } finally { 133 lock.releaseLock(); 134 } 135 foojarRoot = FileUtil.getArchiveRoot(hashedFile); 136 assertNotNull("have an archive in " + hashedFile, foojarRoot); 137 hashedFile = foojarRoot.getFileObject("zipdir/zippedfile"); 138 139 } 140 141 protected void tearDown() throws Exception { 142 scratch = null; 143 projdir = null; 144 randomfile = null; 145 projfile = null; 146 p = null; 147 } 148 149 public void testFileOwner() throws Exception { 150 assertEquals("correct project from projfile FileObject", p, FileOwnerQuery.getOwner(projfile)); 151 URI u = FileUtil.toFile(projfile).toURI(); 152 assertEquals("correct project from projfile URI " + u, p, FileOwnerQuery.getOwner(u)); 153 assertEquals("correct project from projfile2 FileObject", p, FileOwnerQuery.getOwner(projfile2)); 154 assertEquals("correct project from projfile2 URI", p, FileOwnerQuery.getOwner(FileUtil.toFile(projfile2).toURI())); 155 assertEquals("correct project from projdir FileObject", p, FileOwnerQuery.getOwner(projdir)); 156 assertEquals("correct project from projdir URI", p, FileOwnerQuery.getOwner(FileUtil.toFile(projdir).toURI())); 157 Project p2 = FileOwnerQuery.getOwner(subprojfile); 159 Project subproj = ProjectManager.getDefault().findProject(subprojdir); 160 assertEquals("correct project from subprojdir FileObject", subproj, p2); 161 assertEquals("correct project from subprojdir URI", subproj, FileOwnerQuery.getOwner(FileUtil.toFile(subprojdir).toURI())); 162 assertEquals("correct project from subprojfile FileObject", subproj, FileOwnerQuery.getOwner(subprojfile)); 163 assertEquals("correct project from subprojfile URI", subproj, FileOwnerQuery.getOwner(FileUtil.toFile(subprojfile).toURI())); 164 assertEquals("no project from randomfile FileObject", null, FileOwnerQuery.getOwner(randomfile)); 165 assertEquals("no project from randomfile URI", null, FileOwnerQuery.getOwner(FileUtil.toFile(randomfile).toURI())); 166 assertEquals("no project in C:\\", null, FileOwnerQuery.getOwner(URI.create("file:/C:/"))); 167 } 168 169 public void testJarOwners() throws Exception { 170 assertEquals("correct owner of a ZIPped file", p, FileOwnerQuery.getOwner(zippedfile)); 171 assertEquals("correct owner of a ZIPped file URL", p, FileOwnerQuery.getOwner(URI.create(zippedfile.getURL().toExternalForm()))); 172 assertEquals("correct owner of a ZIPped file", p, FileOwnerQuery.getOwner(hashedFile)); 173 assertEquals("correct owner of a ZIPped file URL", p, FileOwnerQuery.getOwner(URI.create(hashedFile.getURL().toExternalForm()))); 174 } 175 176 public void testExternalOwner() throws Exception { 177 FileObject ext1 = scratch.getFileObject("external1"); 178 FileObject extfile1 = ext1.getFileObject("subdir/file"); 179 assertEquals("no owner yet", null, FileOwnerQuery.getOwner(extfile1)); 180 FileOwnerQuery.markExternalOwner(ext1, p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 181 assertEquals("now have an owner", p, FileOwnerQuery.getOwner(extfile1)); 182 assertEquals("even for the projdir", p, FileOwnerQuery.getOwner(ext1)); 183 assertEquals("but not for something else", null, FileOwnerQuery.getOwner(scratch)); 184 FileObject ext2 = scratch.getFileObject("external2"); 185 FileObject extfile2 = ext2.getFileObject("subdir/file"); 186 assertEquals("no owner yet", null, FileOwnerQuery.getOwner(extfile2)); 187 Project p2 = ProjectManager.getDefault().findProject(subprojdir); 188 FileOwnerQuery.markExternalOwner(ext2, p2, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 189 assertEquals("now have an owner", p2, FileOwnerQuery.getOwner(extfile2)); 190 assertEquals("even for the projdir", p2, FileOwnerQuery.getOwner(ext2)); 191 assertEquals("but not for something else", null, FileOwnerQuery.getOwner(scratch)); 192 assertEquals("still correct for first proj", p, FileOwnerQuery.getOwner(extfile1)); 193 FileObject ext3 = scratch.getFileObject("external3"); 194 assertEquals("no owner yet", null, FileOwnerQuery.getOwner(ext3)); 195 FileOwnerQuery.markExternalOwner(ext3, p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 196 assertEquals("now have an owner", p, FileOwnerQuery.getOwner(ext3)); 197 FileObject ext3subproj = ext3.getFileObject("subproject"); 198 Project p3 = FileOwnerQuery.getOwner(ext3subproj); 199 assertNotSame("different project", p, p3); 200 assertEquals("but subprojects are not part of it", ProjectManager.getDefault().findProject(ext3subproj), p3); 201 FileOwnerQuery.markExternalOwner(ext3, null, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 202 assertEquals("unmarking an owner works", null, FileOwnerQuery.getOwner(ext3)); 203 } 204 205 public void testExternalOwnerFile() throws Exception { 206 FileObject ext1 = scratch.getFileObject("external1"); 207 FileObject extfile1 = ext1.getFileObject("subdir/file"); 208 assertEquals("no owner yet", null, FileOwnerQuery.getOwner(extfile1)); 209 FileOwnerQuery.markExternalOwner(extfile1, p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 210 assertEquals("now have an owner", p, FileOwnerQuery.getOwner(extfile1)); 211 assertEquals("not for the projdir", null, FileOwnerQuery.getOwner(ext1)); 212 assertEquals("and not for something else", null, FileOwnerQuery.getOwner(scratch)); 213 FileObject ext2 = scratch.getFileObject("external2"); 214 FileObject extfile2 = ext2.getFileObject("subdir/file"); 215 assertEquals("no owner yet", null, FileOwnerQuery.getOwner(extfile2)); 216 Project p2 = ProjectManager.getDefault().findProject(subprojdir); 217 FileOwnerQuery.markExternalOwner(extfile2, p2, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 218 assertEquals("now have an owner", p2, FileOwnerQuery.getOwner(extfile2)); 219 assertEquals("not for the projdir", null, FileOwnerQuery.getOwner(ext2)); 220 assertEquals("and not for something else", null, FileOwnerQuery.getOwner(scratch)); 221 assertEquals("still correct for first proj", p, FileOwnerQuery.getOwner(extfile1)); 222 223 } 225 226 public void testExternalOwnerURI() throws Exception { 227 FileObject ext1 = scratch.getFileObject("external1"); 228 FileObject extfile1 = ext1.getFileObject("subdir/file"); 229 assertEquals("no owner yet through FileObjects", null, FileOwnerQuery.getOwner(extfile1)); 230 assertEquals("no owner yet through URI", null, FileOwnerQuery.getOwner(extfile1)); 231 FileOwnerQuery.markExternalOwner(fileObject2URI(ext1), p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 232 assertEquals("now have an owner through FileObjects", p, FileOwnerQuery.getOwner(extfile1)); 233 assertEquals("now have an owner through URI", p, FileOwnerQuery.getOwner(fileObject2URI(extfile1))); 234 assertEquals("even for the projdir throught FileObjects", p, FileOwnerQuery.getOwner(ext1)); 235 assertEquals("even for the projdir throught URI", p, FileOwnerQuery.getOwner(fileObject2URI(ext1))); 236 assertEquals("but not for something else throught FileObjects", null, FileOwnerQuery.getOwner(scratch)); 237 assertEquals("but not for something else throught URI", null, FileOwnerQuery.getOwner(fileObject2URI(scratch))); 238 FileObject ext2 = scratch.getFileObject("external2"); 239 FileObject extfile2 = ext2.getFileObject("subdir/file"); 240 assertEquals("no owner yet through FileObjects", null, FileOwnerQuery.getOwner(extfile2)); 241 assertEquals("no owner yet through URI", null, FileOwnerQuery.getOwner(fileObject2URI(extfile2))); 242 Project p2 = ProjectManager.getDefault().findProject(subprojdir); 243 FileOwnerQuery.markExternalOwner(fileObject2URI(ext2), p2, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 244 assertEquals("now have an owner through FileObjects", p2, FileOwnerQuery.getOwner(extfile2)); 245 assertEquals("now have an owner through URI", p2, FileOwnerQuery.getOwner(fileObject2URI(extfile2))); 246 assertEquals("even for the projdir through FileObjects", p2, FileOwnerQuery.getOwner(ext2)); 247 assertEquals("even for the projdir through URI", p2, FileOwnerQuery.getOwner(ext2)); 248 assertEquals("but not for something else through FileObjects", null, FileOwnerQuery.getOwner(scratch)); 249 assertEquals("but not for something else through URI", null, FileOwnerQuery.getOwner(fileObject2URI(scratch))); 250 assertEquals("still correct for first proj through FileObjects", p, FileOwnerQuery.getOwner(extfile1)); 251 assertEquals("still correct for first proj through URI", p, FileOwnerQuery.getOwner(fileObject2URI(extfile1))); 252 FileObject ext3 = scratch.getFileObject("external3"); 253 assertEquals("no owner yet through FileObjects", null, FileOwnerQuery.getOwner(ext3)); 254 assertEquals("no owner yet through URI", null, FileOwnerQuery.getOwner(fileObject2URI(ext3))); 255 FileOwnerQuery.markExternalOwner(fileObject2URI(ext3), p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 256 assertEquals("now have an owner through FileObjects", p, FileOwnerQuery.getOwner(ext3)); 257 assertEquals("now have an owner through URI", p, FileOwnerQuery.getOwner(fileObject2URI(ext3))); 258 FileObject ext3subproj = ext3.getFileObject("subproject"); 259 Project p3 = FileOwnerQuery.getOwner(ext3subproj); 260 assertNotSame("different project", p, p3); 261 assertEquals("but subprojects are not part of it", ProjectManager.getDefault().findProject(ext3subproj), p3); 262 FileOwnerQuery.markExternalOwner(fileObject2URI(ext3), null, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 263 assertEquals("unmarking an owner works through FileObjects", null, FileOwnerQuery.getOwner(ext3)); 264 assertEquals("unmarking an owner works through URI", null, FileOwnerQuery.getOwner(fileObject2URI(ext3))); 265 } 266 267 public void testExternalOwnerFileURI() throws Exception { 268 FileObject ext1 = scratch.getFileObject("external1"); 269 FileObject extfile1 = ext1.getFileObject("subdir/file"); 270 assertEquals("no owner yet through FileObjects", null, FileOwnerQuery.getOwner(extfile1)); 271 assertEquals("no owner yet through URI", null, FileOwnerQuery.getOwner(fileObject2URI(extfile1))); 272 FileOwnerQuery.markExternalOwner(fileObject2URI(extfile1), p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 273 assertEquals("now have an owner through FileObjects", p, FileOwnerQuery.getOwner(extfile1)); 274 assertEquals("now have an owner through URI", p, FileOwnerQuery.getOwner(fileObject2URI(extfile1))); 275 assertEquals("not for the projdir through FileObjects", null, FileOwnerQuery.getOwner(ext1)); 276 assertEquals("not for the projdir through URI", null, FileOwnerQuery.getOwner(fileObject2URI(ext1))); 277 assertEquals("and not for something else through FileObjects", null, FileOwnerQuery.getOwner(scratch)); 278 assertEquals("and not for something else through URI", null, FileOwnerQuery.getOwner(fileObject2URI(scratch))); 279 FileObject ext2 = scratch.getFileObject("external2"); 280 FileObject extfile2 = ext2.getFileObject("subdir/file"); 281 assertEquals("no owner yet through FileObjects", null, FileOwnerQuery.getOwner(extfile2)); 282 assertEquals("no owner yet through URI", null, FileOwnerQuery.getOwner(fileObject2URI(extfile2))); 283 Project p2 = ProjectManager.getDefault().findProject(subprojdir); 284 FileOwnerQuery.markExternalOwner(fileObject2URI(extfile2), p2, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 285 assertEquals("now have an owner through FileObjects", p2, FileOwnerQuery.getOwner(extfile2)); 286 assertEquals("now have an owner through URI", p2, FileOwnerQuery.getOwner(fileObject2URI(extfile2))); 287 assertEquals("not for the projdir through FileObjects", null, FileOwnerQuery.getOwner(ext2)); 288 assertEquals("not for the projdir through URI", null, FileOwnerQuery.getOwner(fileObject2URI(ext2))); 289 assertEquals("and not for something else through FileObjects", null, FileOwnerQuery.getOwner(scratch)); 290 assertEquals("and not for something else through URI", null, FileOwnerQuery.getOwner(fileObject2URI(scratch))); 291 assertEquals("still correct for first proj through FileObjects", p, FileOwnerQuery.getOwner(extfile1)); 292 assertEquals("still correct for first proj through URI", p, FileOwnerQuery.getOwner(fileObject2URI(extfile1))); 293 294 } 296 297 public void testIsProjectDirCollectible() throws Exception { 298 Project p2 = ProjectManager.getDefault().findProject(subprojdir); 299 FileObject root = p2.getProjectDirectory(); 300 FileObject ext2 = scratch.getFileObject("external2"); 301 FileObject extfile2 = ext2.getFileObject("subdir/file"); 302 303 FileOwnerQuery.markExternalOwner(fileObject2URI(extfile2), p2, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 304 305 Reference <?> p2WR = new WeakReference <Object >(p2); 306 Reference <?> rootWR = new WeakReference <Object >(root); 307 308 p2 = null; 309 root = null; 310 ext2 = null; 311 extfile2 = null; 312 subprojdir = null; 313 subprojfile = null; 314 315 assertGC("project 2 collected", p2WR); 316 assertGC("project 2's project dir collected", rootWR); 317 } 318 319 320 325 public void testIssue60297 () throws Exception { 326 FileObject ext1 = scratch.getFileObject("external1"); 327 assertEquals("no owner yet", null, FileOwnerQuery.getOwner(ext1)); 328 FileOwnerQuery.markExternalOwner(ext1, p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 329 assertEquals("now have an owner", p, FileOwnerQuery.getOwner(ext1)); 330 FileObject ext2 = scratch.getFileObject("external2"); 331 assertEquals("no owner yet", null, FileOwnerQuery.getOwner(ext2)); 332 FileOwnerQuery.markExternalOwner(ext2, p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 333 System.gc(); 334 assertEquals("now have an owner", p, FileOwnerQuery.getOwner(ext2)); 335 assertEquals("still correct for the first external root", p, FileOwnerQuery.getOwner(ext1)); 336 } 337 338 private static URI fileObject2URI(FileObject f) throws Exception { 339 return URI.create(f.getURL().toString()); 340 } 341 342 349 } 350 | Popular Tags |