Author: sseifert
Date: Fri Apr 28 15:37:14 2017
New Revision: 1793112
URL: http://svn.apache.org/viewvc?rev=1793112&view=rev
Log:
SLING-6811 osgi-mock: Support ServiceFactory and System Bundle
Modified:
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
Modified:
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java?rev=1793112&r1=1793111&r2=1793112&view=diff
==============================================================================
---
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
(original)
+++
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
Fri Apr 28 15:37:14 2017
@@ -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
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
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();
}
Modified:
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java?rev=1793112&r1=1793111&r2=1793112&view=diff
==============================================================================
---
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
(original)
+++
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
Fri Apr 28 15:37:14 2017
@@ -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 Bundl
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 Bundl
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 Bundl
}
}
- // --- 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();
}
Modified:
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java?rev=1793112&r1=1793111&r2=1793112&view=diff
==============================================================================
---
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
(original)
+++
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
Fri Apr 28 15:37:14 2017
@@ -101,8 +101,15 @@ class MockServiceRegistration<T> impleme
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
Modified:
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java?rev=1793112&r1=1793111&r2=1793112&view=diff
==============================================================================
---
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
(original)
+++
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
Fri Apr 28 15:37:14 2017
@@ -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());
+ }
+
}