This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.osgi-mock-2.3.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit 3320d00ad5d53491337e2219d00eeffcb12331bc Author: Stefan Seifert <[email protected]> AuthorDate: Fri Apr 28 15:37:14 2017 +0000 SLING-6811 osgi-mock: Support ServiceFactory and System Bundle git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1793112 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/sling/testing/mock/osgi/MockBundle.java | 30 +++++++++++---- .../sling/testing/mock/osgi/MockBundleContext.java | 37 ++++++++++++------ .../testing/mock/osgi/MockServiceRegistration.java | 9 ++++- .../testing/mock/osgi/MockBundleContextTest.java | 45 ++++++++++++++++++++++ 4 files changed, 101 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java index 1fc9483..89ad673 100644 --- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java +++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java @@ -31,6 +31,7 @@ import java.util.Vector; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; import org.osgi.framework.ServiceReference; import org.osgi.framework.Version; @@ -46,16 +47,26 @@ public final class MockBundle implements Bundle { private final long bundleId; private final BundleContext bundleContext; private Map<String, String> headers = ImmutableMap.<String, String>of(); - private String symbolicName = "mock-bundle"; + private String symbolicName; private long lastModified; /** * Constructor * @param bundleContext Bundle context + * @param bundleId Bundle ID */ - public MockBundle(BundleContext bundleContext) { - this.bundleId = ++bundleCounter; + MockBundle(BundleContext bundleContext, long bundleId) { + this.bundleId = bundleId; this.bundleContext = bundleContext; + this.symbolicName = (bundleId == Constants.SYSTEM_BUNDLE_ID ? Constants.SYSTEM_BUNDLE_SYMBOLICNAME : "mock-bundle"); + } + + /** + * Constructor + * @param bundleContext Bundle context + */ + public MockBundle(BundleContext bundleContext) { + this(bundleContext, ++bundleCounter); } @Override @@ -174,14 +185,19 @@ public final class MockBundle implements Bundle { return queryPath; } - // --- unsupported operations --- @Override - public Enumeration<URL> findEntries(final String path, final String filePattern, final boolean recurse) { - throw new UnsupportedOperationException(); + public String getLocation() { + if (bundleId == Constants.SYSTEM_BUNDLE_ID) { + return Constants.SYSTEM_BUNDLE_LOCATION; + } + else { + return null; + } } + // --- unsupported operations --- @Override - public String getLocation() { + public Enumeration<URL> findEntries(final String path, final String filePattern, final boolean recurse) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java index 7c40afa..1c3033e 100644 --- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java +++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java @@ -43,6 +43,7 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleListener; +import org.osgi.framework.Constants; import org.osgi.framework.Filter; import org.osgi.framework.FrameworkListener; import org.osgi.framework.InvalidSyntaxException; @@ -68,8 +69,11 @@ class MockBundleContext implements BundleContext { private final Queue<BundleListener> bundleListeners = new ConcurrentLinkedQueue<BundleListener>(); private final ConfigurationAdmin configAdmin = new MockConfigurationAdmin(); private File dataFileBaseDir; + + private final Bundle systemBundle; public MockBundleContext() { + this.systemBundle = new MockBundle(this, Constants.SYSTEM_BUNDLE_ID); this.bundle = new MockBundle(this); // register configuration admin by default @@ -120,6 +124,12 @@ class MockBundleContext implements BundleContext { return registration; } + @SuppressWarnings("unchecked") + @Override + public <S> ServiceRegistration<S> registerService(Class<S> clazz, ServiceFactory<S> factory, Dictionary<String, ?> properties) { + return registerService(clazz.getName(), factory, properties); + } + /** * Check for already registered services that may be affected by the service registration - either * adding by additional optional references, or creating a conflict in the dependencies. @@ -433,29 +443,32 @@ class MockBundleContext implements BundleContext { } } - // --- unsupported operations --- @Override - public Bundle installBundle(final String s) { - throw new UnsupportedOperationException(); - } - - @Override - public Bundle installBundle(final String s, final InputStream inputStream) { - throw new UnsupportedOperationException(); + public Bundle getBundle(final long bundleId) { + if (bundleId == Constants.SYSTEM_BUNDLE_ID) { + return systemBundle; + } + // otherwise return null - no bundle found + return null; } @Override - public Bundle getBundle(final long l) { - throw new UnsupportedOperationException(); + public Bundle getBundle(String location) { + if (StringUtils.equals(location, Constants.SYSTEM_BUNDLE_LOCATION)) { + return systemBundle; + } + // otherwise return null - no bundle found + return null; } + // --- unsupported operations --- @Override - public Bundle getBundle(String location) { + public Bundle installBundle(final String s) { throw new UnsupportedOperationException(); } @Override - public <S> ServiceRegistration<S> registerService(Class<S> clazz, ServiceFactory<S> factory, Dictionary<String, ?> properties) { + public Bundle installBundle(final String s, final InputStream inputStream) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java index 7141949..7a2e87d 100644 --- a/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java +++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java @@ -101,8 +101,15 @@ class MockServiceRegistration<T> implements ServiceRegistration<T>, Comparable<M return clazzes; } + @SuppressWarnings("unchecked") T getService() { - return this.service; + if (this.service instanceof ServiceFactory) { + ServiceFactory<T> factory = (ServiceFactory<T>)this.service; + return factory.getService(this.bundleContext.getBundle(), this); + } + else { + return this.service; + } } @Override diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java index 5dba8d6..8161dea 100644 --- a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java +++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java @@ -39,6 +39,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.runners.MockitoJUnitRunner; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleListener; @@ -46,6 +47,7 @@ import org.osgi.framework.Constants; import org.osgi.framework.Filter; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceEvent; +import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; @@ -124,6 +126,30 @@ public class MockBundleContextTest { } @Test + public void testServiceFactoryRegistration() throws InvalidSyntaxException { + // prepare test services + Class<String> clazz = String.class; + final String service = "abc"; + Dictionary<String, Object> properties1 = getServiceProperties(null); + ServiceRegistration reg = bundleContext.registerService(clazz, new ServiceFactory<String>() { + @Override + public String getService(Bundle bundle, ServiceRegistration<String> registration) { + return service; + } + @Override + public void ungetService(Bundle bundle, ServiceRegistration<String> registration, String service) { + // do nothing + } + }, properties1); + + ServiceReference<String> ref = bundleContext.getServiceReference(clazz); + assertNotNull(ref); + assertSame(reg.getReference(), ref); + assertSame(service, bundleContext.getService(ref)); + bundleContext.ungetService(ref); + } + + @Test public void testNoServiceReferences() throws InvalidSyntaxException { ServiceReference<?>[] refs = bundleContext.getServiceReferences(String.class.getName(), null); assertNull(refs); @@ -233,4 +259,23 @@ public class MockBundleContextTest { assertEquals(childFile.getParentFile(), rootFile); } + + @Test + public void testSystemBundleById() { + Bundle systemBundle = bundleContext.getBundle(Constants.SYSTEM_BUNDLE_ID); + assertNotNull(systemBundle); + assertEquals(Constants.SYSTEM_BUNDLE_ID, systemBundle.getBundleId()); + assertEquals(Constants.SYSTEM_BUNDLE_SYMBOLICNAME, systemBundle.getSymbolicName()); + assertEquals(Constants.SYSTEM_BUNDLE_LOCATION, systemBundle.getLocation()); + } + + @Test + public void testSystemBundleByLocation() { + Bundle systemBundle = bundleContext.getBundle(Constants.SYSTEM_BUNDLE_LOCATION); + assertNotNull(systemBundle); + assertEquals(Constants.SYSTEM_BUNDLE_ID, systemBundle.getBundleId()); + assertEquals(Constants.SYSTEM_BUNDLE_SYMBOLICNAME, systemBundle.getSymbolicName()); + assertEquals(Constants.SYSTEM_BUNDLE_LOCATION, systemBundle.getLocation()); + } + } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
