|
What this is
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.api.project;
import java.io.IOException;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.netbeans.junit.NbTestCase;
import org.netbeans.modules.projectapi.TimedWeakReference;
import org.openide.filesystems.FileObject;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
/* XXX tests needed:
* - testModifiedProjectsNotGCd
* ensure that modified projects cannot be collected
* and that unmodified projects can (incl. after a save)
* - testIsProject
* - testCallFindProjectWithinLoadProjectProhibited
* - testDeleteAndRecreateProject
*/
/**
* Test ProjectManager find and save functionality.
* @author Jesse Glick
*/
public class ProjectManagerTest extends NbTestCase {
static {
// For easier testing.
TimedWeakReference.TIMEOUT = 1000;
}
public ProjectManagerTest(String name) {
super(name);
}
private FileObject scratch;
private FileObject goodproject;
private FileObject goodproject2;
private FileObject badproject;
private FileObject mysteryproject;
private ProjectManager pm;
protected void setUp() throws Exception {
super.setUp();
scratch = TestUtil.makeScratchDir(this);
goodproject = scratch.createFolder("good");
goodproject.createFolder("testproject");
goodproject2 = scratch.createFolder("good2");
goodproject2.createFolder("testproject");
badproject = scratch.createFolder("bad");
badproject.createFolder("testproject").createData("broken");
mysteryproject = scratch.createFolder("mystery");
TestUtil.setLookup(Lookups.singleton(TestUtil.testProjectFactory()));
pm = ProjectManager.getDefault();
pm.reset();
}
protected void tearDown() throws Exception {
scratch = null;
goodproject = null;
badproject = null;
mysteryproject = null;
pm = null;
TestUtil.setLookup(Lookup.EMPTY);
super.tearDown();
}
public void testFindProject() throws Exception {
Project p = null;
try {
p = pm.findProject(goodproject);
} catch (IOException e) {
fail("Should not fail to load goodproject: " + e);
}
assertNotNull("Should have recognized goodproject", p);
assertEquals("Correct project directory set", goodproject, p.getProjectDirectory());
Project p2 = null;
try {
p2 = pm.findProject(badproject);
fail("Should not have succeeded loading badproject");
} catch (IOException e) {
// OK
}
try {
p2 = pm.findProject(mysteryproject);
} catch (IOException e) {
fail("Should not have failed loading mysteryproject: " + e);
}
assertNull("Should not have been able to load mysteryproject", p2);
assertEquals("Repeated find calls should give same result", p, pm.findProject(goodproject));
assertEquals("ProjectFactory was called only once on goodproject", 1, TestUtil.projectLoadCount(goodproject));
}
public void testFindProjectGC() throws Exception {
Project p = null;
try {
p = pm.findProject(goodproject);
} catch (IOException e) {
fail("Should not fail to load goodproject: " + e);
}
assertNotNull("Should have recognized goodproject", p);
assertEquals("ProjectFactory was called once so far on goodproject", 1, TestUtil.projectLoadCount(goodproject));
Reference pref = new WeakReference(p);
p = null;
Thread.sleep(TimedWeakReference.TIMEOUT); // make sure it is not being held strongly
assertGC("Can collect an unused project with project directory still set", pref);
p = pm.findProject(goodproject);
assertNotNull("Can load goodproject again", p);
assertEquals("Correct project directory set", goodproject, p.getProjectDirectory());
assertEquals("ProjectFactory was called again on goodproject", 2, TestUtil.projectLoadCount(goodproject));
pref = new WeakReference(p);
p = null;
Reference dirref = new WeakReference(goodproject);
goodproject = null;
assertGC("Collected the project directory", dirref);
assertGC("Can collect an unused project with project directory discarded", pref);
goodproject = scratch.getFileObject("good");
assertNotNull("goodproject dir still exists", goodproject);
p = pm.findProject(goodproject);
assertNotNull("Can load goodproject yet again", p);
assertEquals("Correct project directory set again", goodproject, p.getProjectDirectory());
assertEquals("ProjectFactory was called only once on new goodproject folder object", 1, TestUtil.projectLoadCount(goodproject));
}
public void testFindProjectDoesNotCacheLoadErrors() throws Exception {
Project p = null;
try {
p = pm.findProject(badproject);
fail("Should not have been able to load badproject");
} catch (IOException e) {
// Expected.
}
FileObject badprojectSubdir = badproject.getFileObject("testproject");
assertNotNull("Has testproject", badprojectSubdir);
FileObject brokenFile = badprojectSubdir.getFileObject("broken");
assertNotNull("Has broken file", brokenFile);
brokenFile.delete();
try {
p = pm.findProject(badproject);
} catch (IOException e) {
fail("badproject has been corrected, should not fail to load now: " + e);
}
assertNotNull("Loaded project", p);
assertEquals("Right project dir", badproject, p.getProjectDirectory());
badprojectSubdir.createData("broken");
Project p2 = null;
try {
p2 = pm.findProject(badproject);
} catch (IOException e) {
fail("badproject is broken on disk but should still be in cache: " + e);
}
assertEquals("Cached badproject", p, p2);
Reference pref = new WeakReference(p);
p = null;
p2 = null;
assertGC("Collected badproject cache", pref);
try {
p = pm.findProject(badproject);
fail("Should not have been able to load badproject now that it is rebroken and not in cache");
} catch (IOException e) {
// Expected.
}
}
public void testModify() throws Exception {
Project p1 = pm.findProject(goodproject);
Project p2 = pm.findProject(goodproject2);
Set/*
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.