Re: How to unit test for stuff using InstalledFileLocator?

"sproger1" <[email protected]>
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <[email protected]>
Mock-up your own InstallFileLocator class and add it to the lookup so that your JUnit tests will finds the resources needed.  The following is an example taken from the netbeans source-code for adding ant resources to the default InstalledFileLocator for tests purposes.

    static {
        MockServices.setServices(IFL.class);
    }
    
    public static final class IFL extends InstalledFileLocator {
        public File locate(String name, String module, boolean loc) {
            String antHomeS = System.getProperty("test.ant.home");
            if (antHomeS == null) {
                throw new Error("Tests will not run unless test.ant.home and test.ant.bridge system properties are defined");
            }
            final File antHome = new File(antHomeS);
            final File antBridge = new File(System.getProperty("test.ant.bridge"));
            final File antJar = new File(new File(antHome, "lib"), "ant.jar");
            if (name.equals("ant")) {
                return antHome;
            } else if (name.equals("ant/nblib/bridge.jar")) {
                return antBridge;
            } else if (name.equals("ant/nblib")) {
                return antBridge.getParentFile();
            } else if (name.equals("ant/lib/ant.jar")) {
                return antJar;
            } else {
                return null;
            }
        }
    }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.