Search in sources :

Example 16 with Capability

use of org.osgi.resource.Capability in project aries by apache.

the class Aries1425Test method testToRequirement.

@Test
public void testToRequirement() {
    SubsystemContentHeader header = new SubsystemContentHeader(HEADER_1);
    Collection<SubsystemContentHeader.Clause> clauses = header.getClauses();
    assertEquals("Wrong size", 3, clauses.size());
    Map<String, SubsystemContentHeader.Clause> map = new HashMap<String, SubsystemContentHeader.Clause>(3);
    for (SubsystemContentHeader.Clause clause : clauses) {
        map.put(clause.toString(), clause);
    }
    Resource resource = new Resource() {

        @Override
        public List<Capability> getCapabilities(String namespace) {
            return Collections.emptyList();
        }

        @Override
        public List<Requirement> getRequirements(String namespace) {
            return Collections.emptyList();
        }
    };
    SubsystemContentHeader.Clause clause = map.get(BUNDLE_A);
    Requirement requirement = clause.toRequirement(resource);
    assertTrue("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, BUNDLE_A, Version.emptyVersion, IdentityNamespace.TYPE_FRAGMENT)));
    assertTrue("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, BUNDLE_A, Version.emptyVersion, IdentityNamespace.TYPE_BUNDLE)));
    clause = map.get(BUNDLE_B);
    requirement = clause.toRequirement(resource);
    assertFalse("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, "bundle.b", Version.emptyVersion, IdentityNamespace.TYPE_FRAGMENT)));
    assertTrue("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, "bundle.b", Version.emptyVersion, IdentityNamespace.TYPE_BUNDLE)));
    clause = map.get(BUNDLE_C);
    requirement = clause.toRequirement(resource);
    assertTrue("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, "bundle.c", Version.emptyVersion, IdentityNamespace.TYPE_FRAGMENT)));
    assertFalse("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, "bundle.c", Version.emptyVersion, IdentityNamespace.TYPE_BUNDLE)));
}
Also used : Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) OsgiIdentityCapability(org.apache.aries.subsystem.core.internal.OsgiIdentityCapability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) OsgiIdentityCapability(org.apache.aries.subsystem.core.internal.OsgiIdentityCapability) Test(org.junit.Test)

Example 17 with Capability

use of org.osgi.resource.Capability in project aries by apache.

the class SubsystemResource method findDependency.

private Resource findDependency(ProvisionResourceHeader.Clause clause) {
    Attribute attribute = clause.getAttribute(DeployedContentHeader.Clause.ATTRIBUTE_RESOURCEID);
    long resourceId = attribute == null ? -1 : Long.parseLong(String.valueOf(attribute.getValue()));
    if (resourceId != -1) {
        String type = clause.getType();
        if (IdentityNamespace.TYPE_BUNDLE.equals(type) || IdentityNamespace.TYPE_FRAGMENT.equals(type))
            return Activator.getInstance().getBundleContext().getBundle(0).getBundleContext().getBundle(resourceId).adapt(BundleRevision.class);
        else
            return Activator.getInstance().getSubsystems().getSubsystemById(resourceId);
    }
    OsgiIdentityRequirement requirement = new OsgiIdentityRequirement(clause.getPath(), clause.getDeployedVersion(), clause.getType(), true);
    List<Capability> capabilities = createResolveContext().findProviders(requirement);
    if (capabilities.isEmpty())
        return null;
    return capabilities.get(0).getResource();
}
Also used : Capability(org.osgi.resource.Capability) Attribute(org.apache.aries.subsystem.core.archive.Attribute) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 18 with Capability

use of org.osgi.resource.Capability in project aries by apache.

the class RepositoryServiceRepositoryTest method testFindProviders.

@Test
public void testFindProviders() throws Exception {
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    RepositoryServiceRepository rsr = new RepositoryServiceRepository(bc);
    @SuppressWarnings("unchecked") ServiceReference<Object> sr = EasyMock.createMock(ServiceReference.class);
    @SuppressWarnings("unchecked") ServiceReference<Object> sr2 = EasyMock.createMock(ServiceReference.class);
    @SuppressWarnings("unchecked") ServiceReference<Object> sr3 = EasyMock.createMock(ServiceReference.class);
    EasyMock.expect(bc.getAllServiceReferences("org.osgi.service.repository.Repository", null)).andReturn(new ServiceReference[] { sr, sr2, sr3 }).anyTimes();
    TestRepository tr = new TestRepository();
    EasyMock.expect(bc.getService(sr)).andReturn(tr).anyTimes();
    ToastRepository tr2 = new ToastRepository();
    EasyMock.expect(bc.getService(sr2)).andReturn(tr2).anyTimes();
    Repository tr3 = Creator.create();
    EasyMock.expect(bc.getService(sr3)).andReturn(tr3).anyTimes();
    EasyMock.replay(bc);
    Map<String, String> dirs = Collections.singletonMap("filter", "(org.foo=bar)");
    Requirement req = new TestRequirement("org.foo", dirs);
    Collection<Capability> res = rsr.findProviders(req);
    assertEquals(1, res.size());
    Capability cap = res.iterator().next();
    assertEquals("org.foo", cap.getNamespace());
    assertEquals(1, cap.getAttributes().size());
    assertEquals("bar", cap.getAttributes().get("org.foo"));
    Map<String, String> dirs2 = Collections.singletonMap("filter", "(org.foo=b)");
    Requirement req2 = new TestRequirement("poing", dirs2);
    Collection<Capability> res2 = rsr.findProviders(req2);
    assertEquals(1, res2.size());
    Capability cap2 = res2.iterator().next();
    assertEquals("poing", cap2.getNamespace());
    assertEquals(1, cap2.getAttributes().size());
    assertEquals("b", cap2.getAttributes().get("org.foo"));
    Map<String, String> dirs3 = Collections.singletonMap("filter", "(x=y)");
    Requirement req3 = new TestRequirement("ns1", dirs3);
    Collection<Capability> res3 = rsr.findProviders(req3);
    assertEquals(1, res3.size());
    Capability cap3 = res3.iterator().next();
    assertEquals("ns1", cap3.getNamespace());
    assertEquals(1, cap3.getAttributes().size());
    assertEquals("y", cap3.getAttributes().get("x"));
}
Also used : Capability(org.osgi.resource.Capability) ServiceReference(org.osgi.framework.ServiceReference) Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 19 with Capability

use of org.osgi.resource.Capability in project aries by apache.

the class ResolveContextTest method testInsertHostedCapability.

@Test
public void testInsertHostedCapability() throws Exception {
    Activator prev = getActivator();
    try {
        Activator activator = createActivator();
        setActivator(activator);
        SubsystemResource res = new SubsystemResource(new File("."));
        ResolveContext rc = new ResolveContext(res);
        HostedCapability hc = EasyMock.createNiceMock(HostedCapability.class);
        List<Capability> caps = new ArrayList<Capability>() {

            // Must use add(idx, obj), get the other add() overloads to complain
            @Override
            public boolean add(Capability e) {
                throw new UnsupportedOperationException();
            }

            @Override
            public boolean addAll(Collection<? extends Capability> c) {
                throw new UnsupportedOperationException();
            }

            @Override
            public boolean addAll(int index, Collection<? extends Capability> c) {
                throw new UnsupportedOperationException();
            }
        };
        caps.add(0, EasyMock.createNiceMock(HostedCapability.class));
        assertEquals(1, rc.insertHostedCapability(caps, hc));
        assertSame(hc, caps.get(1));
    } finally {
        setActivator(prev);
    }
}
Also used : Capability(org.osgi.resource.Capability) HostedCapability(org.osgi.service.resolver.HostedCapability) ArrayList(java.util.ArrayList) Collection(java.util.Collection) File(java.io.File) HostedCapability(org.osgi.service.resolver.HostedCapability) Test(org.junit.Test)

Example 20 with Capability

use of org.osgi.resource.Capability in project aries by apache.

the class ResourceHelperTest method testMandatoryDirectiveAndNullFilterDirective.

@Test
public void testMandatoryDirectiveAndNullFilterDirective() {
    Capability cap = new BasicCapability.Builder().namespace(PackageNamespace.PACKAGE_NAMESPACE).attribute(PackageNamespace.PACKAGE_NAMESPACE, "com.foo").attribute("a", "b").attribute("b", "c").attribute("c", "d").directive(PackageNamespace.CAPABILITY_MANDATORY_DIRECTIVE, "b").resource(EasyMock.createNiceMock(Resource.class)).build();
    Requirement req = new BasicRequirement.Builder().namespace(PackageNamespace.PACKAGE_NAMESPACE).resource(EasyMock.createNiceMock(Resource.class)).build();
    assertFalse("Capability should not match requirement", ResourceHelper.matches(req, cap));
}
Also used : Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) Test(org.junit.Test)

Aggregations

Capability (org.osgi.resource.Capability)189 Requirement (org.osgi.resource.Requirement)103 Resource (org.osgi.resource.Resource)60 ArrayList (java.util.ArrayList)38 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)37 HashMap (java.util.HashMap)37 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)35 Collection (java.util.Collection)32 MockRegistry (test.lib.MockRegistry)26 BndEditModel (aQute.bnd.build.model.BndEditModel)25 Version (org.osgi.framework.Version)19 List (java.util.List)18 Repository (org.osgi.service.repository.Repository)17 Test (org.junit.Test)16 File (java.io.File)14 HostedCapability (org.osgi.service.resolver.HostedCapability)12 HashSet (java.util.HashSet)11 LinkedList (java.util.LinkedList)11 Map (java.util.Map)11 RequirementBuilder (aQute.bnd.osgi.resource.RequirementBuilder)8