If you are writing a test for a DataObject, you need to set up enough of the DataLoader infrastructure that DataObject.find() will locate your DataLoader and call it to create your DataObject subtype.
First, use the setup code described in Testing Things That Use File Objects. Add to the test's setUp() method a call to FileUtil.setMIMEType() to manually assign the file extension to the MIME type of your DataLoader.
FileUtil.setMIMEType("mf", "text/x-manifest");
(setMIMEType() is deprecated with respect to usage from inside a module, but it is fine to use it in a unit test).
(For XML file subtypes, FileUtil.setMIMEType() on *.xml is not likely to work. You can instead register a MIMEResolver in default lookup which does whatever you need.)
No special code is needed in NetBeans 6.5 for this. Declarative MIME resolvers are loaded automatically from unit tests.
Second, you need to make sure your DataLoader is registered in the default Lookup so that DataObject.find() will find it. In 6.0, the New File Type template will set this up automatically by creating the correct file in test/unit/META-INF/services. (Or you can get better control by using org.openide.util.test.MockLookup.)
No special code is needed in NetBeans 6.5 for this. Loaders are available from unit tests automatically.
Example code is below. (You should first put MasterFS on the test classpath.)
private static final String BAD_MANIFEST_CONTENT =
"Manifest-Version: 1.0\n" +
"junk junk junk\n" +
"some more junk\n";
private static final String GOOD_MANIFEST_CONTENT =
"Manifest-Version: 1.0\n" +
"Java-Bean: true\n" +
"OpenIDE-Module-Name: com.foo.bar\n\n";
private FileObject bad;
private FileObject good;
private DataObject badDob;
private DataObject goodDob;
protected void setUp() throws Exception {
clearWorkDir();
FileUtil.setMIMEType("mf", "text/x-manifest");
File goodManifest = new File(getWorkDir(), "good.mf");
writeFile(GOOD_MANIFEST_CONTENT, goodManifest);
good = FileUtil.toFile(goodManifest);
goodDob = DataObject.find(good);
File badManifest = new File(getWorkDir(), "bad.mf");
writeFile(BAD_MANIFEST_CONTENT, badManifest);
bad = FileUtil.toFile(badManifest);
badDob = DataObject.find(bad);
}
private void writeFile(String content, File file) throws Exception {
FileOutputStream os = new FileOutputStream(file);
try {
os.write(content.getBytes("UTF-8"));
} finally {
os.close();
}
}