LayerInitialization

Layer Initialization in Unit Tests

When running unit tests you would often like to have Repository.getDefault().getDefaultFileSystem() contain XML layers from your module (and other modules in your runtime classpath).

NetBeans 6.0

In NB 6.0 you do not need to do anything special. Issue #104027 makes it work automatically.

Older Releases

XXX some critiques:

  1. This is probably way too complicated.
  2. Looking up ModuleInfo works today.
  3. Can probably use #26338 in trunk to MockServices.setServices(MyFS.class) where MyFS extends XMLFileSystem or (for dynamic changes) MultiFileSystem.

Layer file is described at DevFaqModulesLayerFile. This document describes how to to add custom layer.xml file to system filesystem in unit tests. For this purpose you can create UnitTestUtils class. The class is listed below:

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.swing.text.Utilities;
import junit.framework.Assert;
import org.netbeans.junit.NbTestCase;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.LocalFileSystem;
import org.openide.filesystems.MultiFileSystem;
import org.openide.filesystems.Repository;
import org.openide.filesystems.URLMapper;
import org.openide.filesystems.XMLFileSystem;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
import org.xml.sax.SAXException;

/**
 * Allows tests to install own layers for testing.
 * Copied from org.netbeans.api.project.TestUtil.
 *
 * @author Petr Zajac
 */
public class UnitTestUtils extends ProxyLookup {
    public static UnitTestUtils DEFAULT_LOOKUP = null;

    /** Makes global layer from given string resource info */
    public static void prepareTest(String[] stringLayers) 
                throws IOException, SAXException, PropertyVetoException {
        prepareTest(stringLayers, null);
    }
    
    public static void prepareTest (String[] stringLayers, Lookup lkp)
                throws IOException, SAXException, PropertyVetoException {
        URL[[ | ]] layers = new URL[StringLayers.length];
        
        for (int cntr = 0; cntr < layers.length; cntr++) {
            layers[[Cntr | cntr]] = Utilities.class.getResource(stringLayers[Cntr]);
        }
        XMLFileSystem system = new XMLFileSystem();
        system.setXmlUrls(layers);
        
        FileSystem mfs = new MultiFileSystem(new FileSystem[]{FileUtil.createMemoryFileSystem(),system});
        
        Repository repository = new Repository(mfs);
        InstanceContent content = new InstanceContent();
        content.add(repository);
        AbstractLookup repLookup = new AbstractLookup(content);
        ClassLoader loader = UnitTestUtils.class.getClassLoader();
        if (lkp == null) {
            DEFAULT_LOOKUP.setLookups(new Lookup[] { Lookups.metaInfServices(loader),repLookup});
        } else { 
            DEFAULT_LOOKUP.setLookups(new Lookup[] { Lookups.metaInfServices(loader),repLookup,lkp});
        }
    }

    static {
        UnitTestUtils.class.getClassLoader().setDefaultAssertionStatus(true);
        System.setProperty("org.openide.util.Lookup", UnitTestUtils.class.getName());
    }

The layers can be registered by using UnitTestUtils.prepareTest(String[[ | ]] stringLayers) method. Where stringLayers parameter contains array of resources on classpapth to layers. For example

  UnitTestUtils.prepareTest(new String[]{"/org/your/module/layer.xml"});

Loads layer.xml from org.your.module package, and registers it to SystemFileSystem.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo