From cd4f1165ebb7e678df406c64aec648a415c63758 Mon Sep 17 00:00:00 2001 From: "A. J. David Bosschaert" Date: Wed, 24 Sep 2014 12:09:19 +0000 Subject: [PATCH] [FELIX-4640] Missing framework capabilities. This commit fixes the issue. New unit test included. Note that OBR still passes the OSGi R5 repository CT. git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1627285 13f79535-47bb-0310-9956-ffa450edef68 --- bundlerepository.osgi-ct/pom.xml | 2 +- .../impl/LocalResourceImpl.java | 54 +++++-------------- .../impl/OSGiRepositoryImplTest.java | 32 +++++++++++ .../impl/OSGiRepositoryXMLTest.java | 3 ++ .../impl/RepositoryAdminTest.java | 13 +++-- .../impl/RepositoryImplTest.java | 8 ++- .../impl/ResolverImplTest.java | 16 ++++-- .../bundlerepository/impl/StaxParserTest.java | 17 +++--- main/pom.xml | 2 +- 9 files changed, 90 insertions(+), 57 deletions(-) diff --git a/bundlerepository.osgi-ct/pom.xml b/bundlerepository.osgi-ct/pom.xml index 9ce96139816..320ab2f2309 100644 --- a/bundlerepository.osgi-ct/pom.xml +++ b/bundlerepository.osgi-ct/pom.xml @@ -32,7 +32,7 @@ how to prime the repository with the provided repository xml file. org.apache.felix.bundlerepository.osgi-ct - 1.7.0-SNAPSHOT + 2.0.3-SNAPSHOT scm:svn:http://svn.apache.org/repos/asf/felix/trunk/bundlerepository.osgi-ct scm:svn:https://svn.apache.org/repos/asf/felix/trunk/bundlerepository.osgi-ct diff --git a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LocalResourceImpl.java b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LocalResourceImpl.java index 1272b411892..e3d4c274826 100644 --- a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LocalResourceImpl.java +++ b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/LocalResourceImpl.java @@ -20,6 +20,7 @@ import java.util.Dictionary; import java.util.HashSet; +import java.util.Map; import java.util.Set; import java.util.StringTokenizer; @@ -29,6 +30,7 @@ import org.osgi.framework.Constants; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; +import org.osgi.framework.wiring.BundleRevision; public class LocalResourceImpl extends ResourceImpl { @@ -87,49 +89,21 @@ public void close() { } } } -/* TODO: OBR - Fix system capabilities. - // Create a case-insensitive map. - Map map = new TreeMap(new Comparator() { - public int compare(Object o1, Object o2) + // Add all the OSGi capabilities from the system bundle as repo capabilities + BundleRevision br = m_bundle.adapt(BundleRevision.class); + for (org.osgi.resource.Capability cap : br.getCapabilities(null)) + { + CapabilityImpl bcap = new CapabilityImpl(cap.getNamespace()); + for (Map.Entry entry : cap.getAttributes().entrySet()) { - return o1.toString().compareToIgnoreCase(o2.toString()); + bcap.addProperty(new FelixPropertyAdapter(entry)); } - }); - map.put( - Constants.FRAMEWORK_VERSION, - m_context.getProperty(Constants.FRAMEWORK_VERSION)); - map.put( - Constants.FRAMEWORK_VENDOR, - m_context.getProperty(Constants.FRAMEWORK_VENDOR)); - map.put( - Constants.FRAMEWORK_LANGUAGE, - m_context.getProperty(Constants.FRAMEWORK_LANGUAGE)); - map.put( - Constants.FRAMEWORK_OS_NAME, - m_context.getProperty(Constants.FRAMEWORK_OS_NAME)); - map.put( - Constants.FRAMEWORK_OS_VERSION, - m_context.getProperty(Constants.FRAMEWORK_OS_VERSION)); - map.put( - Constants.FRAMEWORK_PROCESSOR, - m_context.getProperty(Constants.FRAMEWORK_PROCESSOR)); -// map.put( -// FelixConstants.FELIX_VERSION_PROPERTY, -// m_context.getProperty(FelixConstants.FELIX_VERSION_PROPERTY)); - Map[] capMaps = (Map[]) bundleMap.get("capability"); - if (capMaps == null) - { - capMaps = new Map[] { map }; - } - else - { - Map[] newCaps = new Map[capMaps.length + 1]; - newCaps[0] = map; - System.arraycopy(capMaps, 0, newCaps, 1, capMaps.length); - capMaps = newCaps; + for (Map.Entry entry : cap.getDirectives().entrySet()) + { + bcap.addDirective(entry.getKey(), entry.getValue()); + } + addCapability(bcap); } - bundleMap.put("capability", capMaps); -*/ } } diff --git a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java index 7bc728abd7d..7f95a0860f0 100644 --- a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java +++ b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java @@ -29,6 +29,8 @@ import junit.framework.TestCase; +import org.apache.felix.bundlerepository.Reason; +import org.apache.felix.bundlerepository.Resolver; import org.apache.felix.utils.log.Logger; import org.mockito.Mockito; import org.osgi.framework.Bundle; @@ -37,6 +39,7 @@ import org.osgi.framework.namespace.BundleNamespace; import org.osgi.framework.namespace.IdentityNamespace; import org.osgi.framework.namespace.PackageNamespace; +import org.osgi.framework.wiring.BundleRevision; import org.osgi.resource.Capability; import org.osgi.resource.Requirement; import org.osgi.resource.Resource; @@ -198,11 +201,40 @@ public void testRepositoryContent() throws Exception { assertTrue(Arrays.equals(expectedBytes, actualBytes)); } + public void testSystemBundleCapabilities() throws Exception { + RepositoryAdminImpl repoAdmin = createRepositoryAdmin(); + Resolver resolver = repoAdmin.resolver(); + RequirementImpl req = new RequirementImpl("some.system.cap"); + req.setFilter("(sys.cap=something)"); + resolver.add(req); + ResourceImpl res = new ResourceImpl(); + res.addRequire(req); + + resolver.add(res); + assertTrue(resolver.resolve()); + + // This should add the system bundle repo to the resolved set. + org.apache.felix.bundlerepository.Resource sysBundleRes = repoAdmin.getSystemRepository().getResources()[0]; + Reason[] reason = resolver.getReason(sysBundleRes); + assertTrue(reason.length >= 1); + assertEquals(req, reason[0].getRequirement()); + } + private RepositoryAdminImpl createRepositoryAdmin() throws Exception { Bundle sysBundle = Mockito.mock(Bundle.class); Mockito.when(sysBundle.getHeaders()).thenReturn(new Hashtable()); + BundleRevision br = Mockito.mock(BundleRevision.class); + Mockito.when(sysBundle.adapt(BundleRevision.class)).thenReturn(br); + Capability cap1 = new OSGiCapabilityImpl("some.system.cap", + Collections.singletonMap("sys.cap", "something"), + Collections.singletonMap("x", "y")); + Capability cap2 = new OSGiCapabilityImpl("some.system.cap", + Collections.singletonMap("sys.cap", "somethingelse"), + Collections.emptyMap()); + Mockito.when(br.getCapabilities(null)).thenReturn(Arrays.asList(cap1, cap2)); + BundleContext bc = Mockito.mock(BundleContext.class); Mockito.when(bc.getBundle(0)).thenReturn(sysBundle); Mockito.when(sysBundle.getBundleContext()).thenReturn(bc); diff --git a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java index a70fe17d3a6..631f0610b4e 100644 --- a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java +++ b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java @@ -35,6 +35,7 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.Version; import org.osgi.framework.namespace.IdentityNamespace; +import org.osgi.framework.wiring.BundleRevision; import org.osgi.resource.Capability; import org.osgi.resource.Requirement; import org.osgi.resource.Resource; @@ -135,6 +136,8 @@ private RepositoryAdminImpl createRepositoryAdmin() throws Exception { Bundle sysBundle = Mockito.mock(Bundle.class); Mockito.when(sysBundle.getHeaders()).thenReturn(new Hashtable()); + BundleRevision br = Mockito.mock(BundleRevision.class); + Mockito.when(sysBundle.adapt(BundleRevision.class)).thenReturn(br); BundleContext bc = Mockito.mock(BundleContext.class); Mockito.when(bc.getBundle(0)).thenReturn(sysBundle); diff --git a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java index 70e38b7fe03..df6f0997d13 100644 --- a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java +++ b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java @@ -19,9 +19,11 @@ package org.apache.felix.bundlerepository.impl; import java.net.URL; +import java.util.Collections; import java.util.Hashtable; import junit.framework.TestCase; + import org.apache.felix.bundlerepository.Resource; import org.apache.felix.utils.filter.FilterImpl; import org.apache.felix.utils.log.Logger; @@ -33,6 +35,8 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.BundleListener; import org.osgi.framework.ServiceListener; +import org.osgi.framework.wiring.BundleRevision; +import org.osgi.resource.Capability; public class RepositoryAdminTest extends TestCase { @@ -54,8 +58,9 @@ public void testResourceFilterOnCapabilities() throws Exception private RepositoryAdminImpl createRepositoryAdmin() throws Exception { - BundleContext bundleContext = (BundleContext) EasyMock.createMock(BundleContext.class); - Bundle systemBundle = (Bundle) EasyMock.createMock(Bundle.class); + BundleContext bundleContext = EasyMock.createMock(BundleContext.class); + Bundle systemBundle = EasyMock.createMock(Bundle.class); + BundleRevision systemBundleRevision = EasyMock.createMock(BundleRevision.class); Activator.setContext(bundleContext); EasyMock.expect(bundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes(); @@ -64,6 +69,8 @@ private RepositoryAdminImpl createRepositoryAdmin() throws Exception EasyMock.expect(systemBundle.getRegisteredServices()).andReturn(null); EasyMock.expect(new Long(systemBundle.getBundleId())).andReturn(new Long(0)).anyTimes(); EasyMock.expect(systemBundle.getBundleContext()).andReturn(bundleContext); + EasyMock.expect(systemBundleRevision.getCapabilities(null)).andReturn(Collections.emptyList()); + EasyMock.expect(systemBundle.adapt(BundleRevision.class)).andReturn(systemBundleRevision); bundleContext.addBundleListener((BundleListener) EasyMock.anyObject()); bundleContext.addServiceListener((ServiceListener) EasyMock.anyObject()); EasyMock.expect(bundleContext.getBundles()).andReturn(new Bundle[] { systemBundle }); @@ -73,7 +80,7 @@ public Object answer() throws Throwable { return FilterImpl.newInstance((String) c.getValue()); } }).anyTimes(); - EasyMock.replay(new Object[] { bundleContext, systemBundle }); + EasyMock.replay(new Object[] { bundleContext, systemBundle, systemBundleRevision }); RepositoryAdminImpl repoAdmin = new RepositoryAdminImpl(bundleContext, new Logger(bundleContext)); diff --git a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryImplTest.java b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryImplTest.java index 5e4b459b8ed..d46d8cbe44a 100644 --- a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryImplTest.java +++ b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryImplTest.java @@ -19,6 +19,7 @@ package org.apache.felix.bundlerepository.impl; import java.net.URL; +import java.util.Collections; import java.util.Dictionary; import java.util.Hashtable; import java.util.Map; @@ -35,6 +36,8 @@ import org.osgi.framework.Filter; import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; +import org.osgi.framework.wiring.BundleRevision; +import org.osgi.resource.Capability; public class RepositoryImplTest extends TestCase { @@ -96,6 +99,7 @@ private RepositoryAdminImpl createRepositoryAdmin() throws Exception { BundleContext bundleContext = EasyMock.createMock(BundleContext.class); Bundle systemBundle = EasyMock.createMock(Bundle.class); + BundleRevision systemBundleRevision = EasyMock.createMock(BundleRevision.class); Activator.setContext(bundleContext); EasyMock.expect(bundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes(); @@ -104,6 +108,8 @@ private RepositoryAdminImpl createRepositoryAdmin() throws Exception EasyMock.expect(systemBundle.getRegisteredServices()).andReturn(null); EasyMock.expect(new Long(systemBundle.getBundleId())).andReturn(new Long(0)).anyTimes(); EasyMock.expect(systemBundle.getBundleContext()).andReturn(bundleContext); + EasyMock.expect(systemBundleRevision.getCapabilities(null)).andReturn(Collections.emptyList()); + EasyMock.expect(systemBundle.adapt(BundleRevision.class)).andReturn(systemBundleRevision); bundleContext.addBundleListener((BundleListener) EasyMock.anyObject()); bundleContext.addServiceListener((ServiceListener) EasyMock.anyObject()); EasyMock.expect(bundleContext.getBundles()).andReturn(new Bundle[] { systemBundle }); @@ -121,7 +127,7 @@ public boolean matches(Map map) { return true; } }).anyTimes(); - EasyMock.replay(new Object[] { bundleContext, systemBundle }); + EasyMock.replay(new Object[] { bundleContext, systemBundle, systemBundleRevision }); RepositoryAdminImpl repoAdmin = new RepositoryAdminImpl(bundleContext, new Logger(bundleContext)); diff --git a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResolverImplTest.java b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResolverImplTest.java index 7bd8f6b4f35..3ec9288dfcb 100644 --- a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResolverImplTest.java +++ b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/ResolverImplTest.java @@ -19,11 +19,14 @@ package org.apache.felix.bundlerepository.impl; import java.net.URL; +import java.util.Collections; import java.util.Hashtable; import junit.framework.TestCase; +import org.apache.felix.bundlerepository.Repository; import org.apache.felix.bundlerepository.Requirement; +import org.apache.felix.bundlerepository.Resolver; import org.apache.felix.bundlerepository.Resource; import org.apache.felix.utils.filter.FilterImpl; import org.apache.felix.utils.log.Logger; @@ -35,8 +38,8 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.BundleListener; import org.osgi.framework.ServiceListener; -import org.apache.felix.bundlerepository.Repository; -import org.apache.felix.bundlerepository.Resolver; +import org.osgi.framework.wiring.BundleRevision; +import org.osgi.resource.Capability; public class ResolverImplTest extends TestCase { @@ -138,8 +141,9 @@ public static void main(String[] args) throws Exception private RepositoryAdminImpl createRepositoryAdmin() throws Exception { - BundleContext bundleContext = (BundleContext) EasyMock.createMock(BundleContext.class); - Bundle systemBundle = (Bundle) EasyMock.createMock(Bundle.class); + BundleContext bundleContext = EasyMock.createMock(BundleContext.class); + Bundle systemBundle = EasyMock.createMock(Bundle.class); + BundleRevision systemBundleRevision = EasyMock.createMock(BundleRevision.class); Activator.setContext(bundleContext); EasyMock.expect(bundleContext.getProperty(RepositoryAdminImpl.REPOSITORY_URL_PROP)) @@ -150,6 +154,8 @@ private RepositoryAdminImpl createRepositoryAdmin() throws Exception EasyMock.expect(systemBundle.getRegisteredServices()).andReturn(null); EasyMock.expect(new Long(systemBundle.getBundleId())).andReturn(new Long(0)).anyTimes(); EasyMock.expect(systemBundle.getBundleContext()).andReturn(bundleContext); + EasyMock.expect(systemBundleRevision.getCapabilities(null)).andReturn(Collections.emptyList()); + EasyMock.expect(systemBundle.adapt(BundleRevision.class)).andReturn(systemBundleRevision); bundleContext.addBundleListener((BundleListener) EasyMock.anyObject()); bundleContext.addServiceListener((ServiceListener) EasyMock.anyObject()); EasyMock.expect(bundleContext.getBundles()).andReturn(new Bundle[] { systemBundle }); @@ -159,7 +165,7 @@ public Object answer() throws Throwable { return FilterImpl.newInstance((String) c.getValue()); } }).anyTimes(); - EasyMock.replay(new Object[] { bundleContext, systemBundle }); + EasyMock.replay(new Object[] { bundleContext, systemBundle, systemBundleRevision }); RepositoryAdminImpl repoAdmin = new RepositoryAdminImpl(bundleContext, new Logger(bundleContext)); diff --git a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/StaxParserTest.java b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/StaxParserTest.java index e2e5725df1f..54a3a918b89 100644 --- a/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/StaxParserTest.java +++ b/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/StaxParserTest.java @@ -19,13 +19,14 @@ package org.apache.felix.bundlerepository.impl; import java.net.URL; +import java.util.Collections; import java.util.Hashtable; import junit.framework.TestCase; -import org.apache.felix.bundlerepository.impl.PullParser; + +import org.apache.felix.bundlerepository.Repository; import org.apache.felix.bundlerepository.Resolver; import org.apache.felix.bundlerepository.Resource; -import org.apache.felix.bundlerepository.impl.StaxParser; import org.apache.felix.utils.filter.FilterImpl; import org.apache.felix.utils.log.Logger; import org.easymock.Capture; @@ -36,7 +37,8 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.BundleListener; import org.osgi.framework.ServiceListener; -import org.apache.felix.bundlerepository.Repository; +import org.osgi.framework.wiring.BundleRevision; +import org.osgi.resource.Capability; public class StaxParserTest extends TestCase { @@ -138,8 +140,9 @@ public static void main(String[] args) throws Exception private RepositoryAdminImpl createRepositoryAdmin(Class repositoryParser) throws Exception { - BundleContext bundleContext = (BundleContext) EasyMock.createMock(BundleContext.class); - Bundle systemBundle = (Bundle) EasyMock.createMock(Bundle.class); + BundleContext bundleContext = EasyMock.createMock(BundleContext.class); + Bundle systemBundle = EasyMock.createMock(Bundle.class); + BundleRevision systemBundleRevision = EasyMock.createMock(BundleRevision.class); Activator.setContext(bundleContext); EasyMock.expect(bundleContext.getProperty(RepositoryAdminImpl.REPOSITORY_URL_PROP)) @@ -152,6 +155,8 @@ private RepositoryAdminImpl createRepositoryAdmin(Class repositoryParser) throws EasyMock.expect(systemBundle.getRegisteredServices()).andReturn(null); EasyMock.expect(new Long(systemBundle.getBundleId())).andReturn(new Long(0)).anyTimes(); EasyMock.expect(systemBundle.getBundleContext()).andReturn(bundleContext); + EasyMock.expect(systemBundleRevision.getCapabilities(null)).andReturn(Collections.emptyList()); + EasyMock.expect(systemBundle.adapt(BundleRevision.class)).andReturn(systemBundleRevision); bundleContext.addBundleListener((BundleListener) EasyMock.anyObject()); bundleContext.addServiceListener((ServiceListener) EasyMock.anyObject()); EasyMock.expect(bundleContext.getBundles()).andReturn(new Bundle[] { systemBundle }); @@ -161,7 +166,7 @@ public Object answer() throws Throwable { return FilterImpl.newInstance((String) c.getValue()); } }).anyTimes(); - EasyMock.replay(new Object[] { bundleContext, systemBundle }); + EasyMock.replay(new Object[] { bundleContext, systemBundle, systemBundleRevision }); RepositoryAdminImpl repoAdmin = new RepositoryAdminImpl(bundleContext, new Logger(bundleContext)); diff --git a/main/pom.xml b/main/pom.xml index 6156ae556e0..3ed359941ff 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -47,7 +47,7 @@ 0.12.1 0.10.0 0.14.0 - 2.0.2 + 2.0.3-SNAPSHOT