KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > project > FileOwnerQueryTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.project;
21 import java.io.OutputStream JavaDoc;
22 import java.lang.ref.Reference JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.util.zip.CRC32 JavaDoc;
26 import java.util.zip.ZipEntry JavaDoc;
27 import java.util.zip.ZipOutputStream JavaDoc;
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 /**
35  * Test functionality of FileOwnerQuery.
36  * @author Jesse Glick
37  */

38 public class FileOwnerQueryTest extends NbTestCase {
39     
40     public FileOwnerQueryTest(String JavaDoc 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 JavaDoc {
60         TestUtil.setLookup(new Object JavaDoc[] {
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         // make jar:file:/.../projdir/foo.jar!/zipfile/zippedfile
82
FileObject foojar = projdir.createData("foo.jar");
83         FileLock lock = foojar.lock();
84         try {
85             OutputStream JavaDoc os = foojar.getOutputStream(lock);
86             try {
87                 ZipOutputStream JavaDoc zos = new ZipOutputStream JavaDoc(os);
88                 ZipEntry JavaDoc ze = new ZipEntry JavaDoc("zipdir/");
89                 ze.setMethod(ZipEntry.STORED);
90                 ze.setSize(0L);
91                 ze.setCrc(new CRC32 JavaDoc().getValue());
92                 zos.putNextEntry(ze);
93                 ze = new ZipEntry JavaDoc("zipdir/zippedfile");
94                 ze.setMethod(ZipEntry.STORED);
95                 ze.setSize(0L);
96                 ze.setCrc(new CRC32 JavaDoc().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 JavaDoc os = hashedFile.getOutputStream(lock);
115             try {
116                 ZipOutputStream JavaDoc zos = new ZipOutputStream JavaDoc(os);
117                 ZipEntry JavaDoc ze = new ZipEntry JavaDoc("zipdir/");
118                 ze.setMethod(ZipEntry.STORED);
119                 ze.setSize(0L);
120                 ze.setCrc(new CRC32 JavaDoc().getValue());
121                 zos.putNextEntry(ze);
122                 ze = new ZipEntry JavaDoc("zipdir/zippedfile");
123                 ze.setMethod(ZipEntry.STORED);
124                 ze.setSize(0L);
125                 ze.setCrc(new CRC32 JavaDoc().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 JavaDoc {
142         scratch = null;
143         projdir = null;
144         randomfile = null;
145         projfile = null;
146         p = null;
147     }
148     
149     public void testFileOwner() throws Exception JavaDoc {
150         assertEquals("correct project from projfile FileObject", p, FileOwnerQuery.getOwner(projfile));
151         URI JavaDoc 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         // Check that it loads the project even though we have not touched it yet:
158
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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         //XXX: unmarking files.
224
}
225     
226     public void testExternalOwnerURI() throws Exception JavaDoc {
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 JavaDoc {
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         //XXX: unmarking files.
295
}
296     
297     public void testIsProjectDirCollectible() throws Exception JavaDoc {
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 JavaDoc<?> p2WR = new WeakReference JavaDoc<Object JavaDoc>(p2);
306         Reference JavaDoc<?> rootWR = new WeakReference JavaDoc<Object JavaDoc>(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     /**
321      * Tests the issue 60297. GC causes previosly registered extenral roots
322      * for project to be released. Only one extenral root per project is kept.
323      *
324      */

325     public void testIssue60297 () throws Exception JavaDoc {
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 JavaDoc fileObject2URI(FileObject f) throws Exception JavaDoc {
339         return URI.create(f.getURL().toString());
340     }
341     
342     // XXX test URI usage of external owner
343
// XXX test GC of roots and projects used in external ownership:
344
// - the owning Project is not held strongly (just PM's soft cache)
345
// - the root is not held strongly (note - FOQ won't be accurate after it is collected)
346
// XXX test IAE from illegal calls to FOQ.markExternalOwner
347
// XXX test an owner which is above the project directory
348

349 }
350
Popular Tags