You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2017/10/12 15:42:07 UTC
svn commit: r1811983 - in /sling/branches/testing/mocks/osgi-mock-1.x/src:
main/java/org/apache/sling/testing/mock/osgi/
test/java/org/apache/sling/testing/mock/osgi/
test/java/org/apache/sling/testing/mock/osgi/context/
Author: sseifert
Date: Thu Oct 12 15:42:07 2017
New Revision: 1811983
URL: http://svn.apache.org/viewvc?rev=1811983&view=rev
Log:
SLING-7193 MockServiceReference.compareTo needs to implement lowest-first ranking - but BundleContext.getService() should return service with highest ranking
Modified:
sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
Modified: sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
URL: http://svn.apache.org/viewvc/sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java?rev=1811983&r1=1811982&r2=1811983&view=diff
==============================================================================
--- sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java (original)
+++ sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java Thu Oct 12 15:42:07 2017
@@ -22,6 +22,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
+import java.util.Comparator;
import java.util.Dictionary;
import java.util.List;
import java.util.Map;
@@ -262,7 +263,13 @@ class MockBundleContext implements Bundl
@Override
public ServiceReference[] getServiceReferences(final String clazz, final String filter) throws InvalidSyntaxException {
- Set<ServiceReference> result = new TreeSet<ServiceReference>();
+ Set<ServiceReference> result = new TreeSet<ServiceReference>(new Comparator<ServiceReference>() {
+ @Override
+ public int compare(ServiceReference o1, ServiceReference o2) {
+ // reverse sort order to get highest ranking first
+ return o2.compareTo(o1);
+ }
+ });
for (MockServiceRegistration serviceRegistration : this.registeredServices) {
if (serviceRegistration.matches(clazz, filter)) {
result.add(serviceRegistration.getReference());
Modified: sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
URL: http://svn.apache.org/viewvc/sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java?rev=1811983&r1=1811982&r2=1811983&view=diff
==============================================================================
--- sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java (original)
+++ sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java Thu Oct 12 15:42:07 2017
@@ -72,19 +72,19 @@ public class MockBundleContextTest {
@Test
public void testServiceRegistration() throws InvalidSyntaxException {
// prepare test services
- String[] clazzes2 = new String[] { String.class.getName(), Integer.class.getName() };
- Object service2 = new Object();
- Dictionary properties2 = getServiceProperties(null);
- ServiceRegistration reg2 = bundleContext.registerService(clazzes2, service2, properties2);
-
- String clazz1 = String.class.getName();
+ String[] clazzes1 = new String[] { String.class.getName(), Integer.class.getName() };
Object service1 = new Object();
- Dictionary properties1 = getServiceProperties(null);
- ServiceRegistration reg1 = bundleContext.registerService(clazz1, service1, properties1);
+ Dictionary<String, Object> properties2 = ranking(null);
+ ServiceRegistration reg1 = bundleContext.registerService(clazzes1, service1, properties2);
+
+ String clazz2 = String.class.getName();
+ Object service2 = new Object();
+ Dictionary<String, Object> properties1 = ranking(null);
+ ServiceRegistration reg2 = bundleContext.registerService(clazz2, service2, properties1);
String clazz3 = Integer.class.getName();
Object service3 = new Object();
- Dictionary properties3 = getServiceProperties(-100L);
+ Dictionary<String, Object> properties3 = ranking(-100);
ServiceRegistration reg3 = bundleContext.registerService(clazz3, service3, properties3);
// test get service references
@@ -92,17 +92,21 @@ public class MockBundleContextTest {
assertSame(reg1.getReference(), refString);
ServiceReference refInteger = bundleContext.getServiceReference(Integer.class.getName());
- assertSame(reg3.getReference(), refInteger);
+ assertSame(reg1.getReference(), refInteger);
ServiceReference[] refsString = bundleContext.getServiceReferences(String.class.getName(), null);
assertEquals(2, refsString.length);
assertSame(reg1.getReference(), refsString[0]);
assertSame(reg2.getReference(), refsString[1]);
+ ServiceReference[] refColString = bundleContext.getServiceReferences(String.class.getName(), null);
+ assertEquals(2, refColString.length);
+ assertSame(reg1.getReference(), refColString[0]);
+
ServiceReference[] refsInteger = bundleContext.getServiceReferences(Integer.class.getName(), null);
assertEquals(2, refsInteger.length);
- assertSame(reg3.getReference(), refsInteger[0]);
- assertSame(reg2.getReference(), refsInteger[1]);
+ assertSame(reg1.getReference(), refsInteger[0]);
+ assertSame(reg3.getReference(), refsInteger[1]);
ServiceReference[] allRefsString = bundleContext.getAllServiceReferences(String.class.getName(), null);
assertArrayEquals(refsString, allRefsString);
@@ -110,7 +114,7 @@ public class MockBundleContextTest {
// test get services
assertSame(service1, bundleContext.getService(refsString[0]));
assertSame(service2, bundleContext.getService(refsString[1]));
- assertSame(service3, bundleContext.getService(refInteger));
+ assertSame(service1, bundleContext.getService(refInteger));
// unget does nothing
bundleContext.ungetService(refsString[0]);
@@ -123,7 +127,7 @@ public class MockBundleContextTest {
// prepare test services
String clazz1 = String.class.getName();
Object service1 = new Object();
- Dictionary properties1 = getServiceProperties(null);
+ Dictionary properties1 = ranking(null);
ServiceRegistration reg1 = bundleContext.registerService(clazz1, service1, properties1);
assertNotNull(bundleContext.getServiceReference(clazz1));
@@ -133,15 +137,6 @@ public class MockBundleContextTest {
assertNull(bundleContext.getServiceReference(clazz1));
}
-
- private Dictionary getServiceProperties(final Long serviceRanking) {
- Dictionary<String, Object> props = new Hashtable<String, Object>();
- if (serviceRanking != null) {
- props.put(Constants.SERVICE_RANKING, serviceRanking);
- }
- return props;
- }
-
@Test
public void testGetBundles() throws Exception {
assertEquals(0, bundleContext.getBundles().length);
@@ -218,4 +213,41 @@ public class MockBundleContextTest {
assertEquals(childFile.getParentFile(), rootFile);
}
+
+ @Test
+ public void testGetServiceOrderWithRanking() {
+ bundleContext.registerService(String.class.getName(), "service1", ranking(10));
+ bundleContext.registerService(String.class.getName(), "service2", ranking(20));
+ bundleContext.registerService(String.class.getName(), "service3", ranking(5));
+
+ // should return service with highest ranking
+ ServiceReference ref = bundleContext.getServiceReference(String.class.getName());
+ String service = (String)bundleContext.getService(ref);
+ assertEquals("service2", service);
+
+ bundleContext.ungetService(ref);
+ }
+
+ @Test
+ public void testGetServiceOrderWithoutRanking() {
+ bundleContext.registerService(String.class.getName(), "service1", ranking(null));
+ bundleContext.registerService(String.class.getName(), "service2", ranking(null));
+ bundleContext.registerService(String.class.getName(), "service3", ranking(null));
+
+ // should return service with lowest service id = which was registered first
+ ServiceReference ref = bundleContext.getServiceReference(String.class.getName());
+ String service = (String)bundleContext.getService(ref);
+ assertEquals("service1", service);
+
+ bundleContext.ungetService(ref);
+ }
+
+ private static Dictionary<String, Object> ranking(final Integer serviceRanking) {
+ Dictionary<String, Object> props = new Hashtable<String, Object>();
+ if (serviceRanking != null) {
+ props.put(Constants.SERVICE_RANKING, serviceRanking);
+ }
+ return props;
+ }
+
}
Modified: sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
URL: http://svn.apache.org/viewvc/sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java?rev=1811983&r1=1811982&r2=1811983&view=diff
==============================================================================
--- sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java (original)
+++ sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java Thu Oct 12 15:42:07 2017
@@ -102,11 +102,11 @@ public class OsgiContextImplTest {
Set<String> myService2 = new HashSet<String>();
context.registerService(Set.class, myService2);
- // expected: descending order because ordering descending by service id
+ // expected: ascending order because ordering ascending by service ID
serviceResults = context.getServices(Set.class, null);
assertEquals(2, serviceResults.length);
- assertSame(myService1, serviceResults[1]);
- assertSame(myService2, serviceResults[0]);
+ assertSame(myService1, serviceResults[0]);
+ assertSame(myService2, serviceResults[1]);
}
@Test