@Test
  public void testModuleDelegatesToFramework() throws Exception {
    ModuleIdentifier identifierF = ModuleIdentifier.create("framework");
    ModuleSpec.Builder specBuilderF = ModuleSpec.build(identifierF);
    PathFilter importFilter = getSystemFilter();
    PathFilter exportFilter = PathFilters.acceptAll();
    specBuilderF.addDependency(
        DependencySpec.createSystemDependencySpec(importFilter, exportFilter, getSystemPaths()));
    importFilter = PathFilters.in(getFrameworkPaths());
    exportFilter = PathFilters.acceptAll();
    FrameworkLocalLoader localLoader = new FrameworkLocalLoader(Bundle.class.getClassLoader());
    specBuilderF.addDependency(
        DependencySpec.createLocalDependencySpec(
            importFilter, exportFilter, localLoader, getFrameworkPaths()));
    addModuleSpec(specBuilderF.create());

    ModuleIdentifier identifierA = ModuleIdentifier.create("moduleA");
    ModuleSpec.Builder specBuilderA = ModuleSpec.build(identifierA);
    VirtualFileResourceLoader resourceLoaderA = new VirtualFileResourceLoader(virtualFileA);
    specBuilderA.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoaderA));
    specBuilderA.addDependency(DependencySpec.createModuleDependencySpec(identifierF));
    specBuilderA.addDependency(DependencySpec.createLocalDependencySpec());
    addModuleSpec(specBuilderA.create());

    assertLoadClass(identifierA, "org.osgi.framework.Bundle");
    assertLoadClass(identifierA, "javax.security.auth.x500.X500Principal");
  }
 @Test
 public void testAvailableOnModule() throws Exception {
   ModuleIdentifier identifierA = ModuleIdentifier.create("moduleA");
   ModuleSpec.Builder specBuilderA = ModuleSpec.build(identifierA);
   VirtualFileResourceLoader resourceLoaderA = new VirtualFileResourceLoader(virtualFileA);
   specBuilderA.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoaderA));
   specBuilderA.addDependency(DependencySpec.createLocalDependencySpec());
   addModuleSpec(specBuilderA.create());
   assertLoadClass(identifierA, "javax.security.auth.x500.X500Principal", identifierA);
 }
  @Test
  public void testTwoHopDelegation() throws Exception {

    ModuleIdentifier identifierB = ModuleIdentifier.create("moduleB");
    ModuleSpec.Builder specBuilderB = ModuleSpec.build(identifierB);
    PathFilter importFilter = getSystemFilter();
    PathFilter exportFilter = PathFilters.acceptAll();
    specBuilderB.addDependency(
        DependencySpec.createSystemDependencySpec(importFilter, exportFilter, getSystemPaths()));
    addModuleSpec(specBuilderB.create());

    ModuleIdentifier identifierA = ModuleIdentifier.create("moduleA");
    ModuleSpec.Builder specBuilderA = ModuleSpec.build(identifierA);
    VirtualFileResourceLoader resourceLoaderA = new VirtualFileResourceLoader(virtualFileA);
    specBuilderA.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoaderA));
    specBuilderA.addDependency(DependencySpec.createModuleDependencySpec(identifierB));
    specBuilderA.addDependency(DependencySpec.createLocalDependencySpec());
    addModuleSpec(specBuilderA.create());

    assertLoadClass(identifierB, "javax.security.auth.x500.X500Principal", null);
    assertLoadClass(identifierA, "javax.security.auth.x500.X500Principal", null);
  }
  @Test
  public void testAvailableFrameworkModule() throws Exception {
    ModuleIdentifier identifierF = ModuleIdentifier.create("framework");
    ModuleSpec.Builder specBuilderF = ModuleSpec.build(identifierF);
    PathFilter importFilter = PathFilters.in(getFrameworkPaths());
    PathFilter exportFilter = PathFilters.acceptAll();
    FrameworkLocalLoader localLoader = new FrameworkLocalLoader(Bundle.class.getClassLoader());
    specBuilderF.addDependency(
        DependencySpec.createLocalDependencySpec(
            importFilter, exportFilter, localLoader, getFrameworkPaths()));
    addModuleSpec(specBuilderF.create());

    assertLoadClass(identifierF, "org.osgi.framework.Bundle");
    assertLoadClassFail(identifierF, "javax.security.auth.x500.X500Principal");
  }
  @Test
  public void testNotAvailableOnSystemModule() throws Exception {
    ModuleIdentifier identifierA = ModuleIdentifier.create("moduleA");
    ModuleSpec.Builder specBuilderA = ModuleSpec.build(identifierA);
    VirtualFileResourceLoader resourceLoaderA = new VirtualFileResourceLoader(virtualFileA);
    specBuilderA.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoaderA));
    PathFilter importFilter = getSystemFilter();
    PathFilter exportFilter = PathFilters.acceptAll();
    specBuilderA.addDependency(
        DependencySpec.createSystemDependencySpec(importFilter, exportFilter, getSystemPaths()));
    specBuilderA.addDependency(DependencySpec.createLocalDependencySpec());
    addModuleSpec(specBuilderA.create());

    assertLoadClassFail(identifierA, "org.osgi.framework.Bundle");
    assertLoadClass(identifierA, "javax.security.auth.x500.X500Principal");
  }
  @Override
  public ModuleSpec get(ModuleLoader loader, ModuleIdentifier id) {
    if (getId().equals(id)) {
      Builder builder = ModuleSpec.build(id);
      builder.addDependency(
          DependencySpec.createClassLoaderDependencySpec(
              PathFilters.acceptAll(),
              PathFilters.acceptAll(),
              AbstractModuleSpecProvider.class.getClassLoader(),
              getPaths()));
      builder.addDependency(
          DependencySpec.createClassLoaderDependencySpec(
              PathFilters.acceptAll(),
              PathFilters.acceptAll(),
              ClassLoader.getSystemClassLoader(),
              getPaths()));

      configure(loader, builder);

      return builder.create();
    }
    return null;
  }
 public void addModule(AbstractRevision bundleRev, ModuleSpec moduleSpec) {
   ModuleIdentifier identifier = moduleSpec.getModuleIdentifier();
   if (modules.get(identifier) != null)
     throw new IllegalStateException("Module already exists: " + identifier);
   modules.put(identifier, new ModuleHolder(bundleRev, moduleSpec));
 }