|
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.awt.Image;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.WeakHashMap;
import junit.framework.Assert;
import org.netbeans.junit.NbTestCase;
import org.netbeans.spi.project.ProjectFactory;
import org.netbeans.spi.project.ProjectState;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.LocalFileSystem;
import org.openide.filesystems.Repository;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
/**
* Help set up org.netbeans.api.project.*Test.
* @author Jesse Glick
*/
public final class TestUtil extends ProxyLookup {
static {
TestUtil.class.getClassLoader().setDefaultAssertionStatus(true);
System.setProperty("org.openide.util.Lookup", TestUtil.class.getName());
Assert.assertEquals(TestUtil.class, Lookup.getDefault().getClass());
}
private static TestUtil DEFAULT;
/** Do not call directly */
public TestUtil() {
Assert.assertNull(DEFAULT);
DEFAULT = this;
setLookups(new Lookup[] {
Lookups.singleton(TestUtil.class.getClassLoader()),
});
}
/**
* Set the global default lookup.
*/
public static void setLookup(Lookup l) {
DEFAULT.setLookups(new Lookup[] {l});
}
/**
* Set the global default lookup with some fixed instances including META-INF/services/*.
*/
public static void setLookup(Object[] instances, ClassLoader cl) {
DEFAULT.setLookups(new Lookup[] {
Lookups.fixed(instances),
Lookups.metaInfServices(cl),
Lookups.singleton(cl),
});
}
/**
* Create a scratch directory for tests.
* Will be in /tmp or whatever, and will be empty.
* If you just need a java.io.File use clearWorkDir + getWorkDir.
*/
public static FileObject makeScratchDir(NbTestCase test) throws IOException {
test.clearWorkDir();
File root = test.getWorkDir();
assert root.isDirectory() && root.list().length == 0;
FileObject fo = FileUtil.toFileObject(root);
if (fo != null) {
// Presumably using masterfs.
return fo;
} else {
// For the benefit of those not using masterfs.
LocalFileSystem lfs = new LocalFileSystem();
try {
lfs.setRootDirectory(root);
} catch (PropertyVetoException e) {
assert false : e;
}
Repository.getDefault().addFileSystem(lfs);
return lfs.getRoot();
}
}
/**
* Delete a file and all subfiles.
*/
public static void deleteRec(File f) throws IOException {
if (f.isDirectory()) {
File[] kids = f.listFiles();
if (kids == null) {
throw new IOException("List " + f);
}
for (int i = 0; i < kids.length; i++) {
deleteRec(kids[i]);
}
}
if (!f.delete()) {
throw new IOException("Delete " + f);
}
}
/**
* Create a testing project factory which recognizes directories containing
* a subdirectory called "testproject".
* If that subdirectory contains a file named "broken" then loading the project
* will fail with an IOException.
*/
public static ProjectFactory testProjectFactory() {
return new TestProjectFactory();
}
/**
* Try to force a GC.
*/
public static void gc() {
System.gc();
System.runFinalization();
System.gc();
}
private static final Map/*
|
| ... 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.