sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-testing-osgi-mock] 03/05: SLING-5667 MockBundleContext.getServiceReference(...) does not return service reference with highest ranking
Date Tue, 07 Nov 2017 10:21:13 GMT
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.0.4
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 1c6b58649be856e2b40ca1a7ef4a8a2f2c21775b
Author: Stefan Seifert <sseifert@apache.org>
AuthorDate: Fri Apr 29 16:54:38 2016 +0000

    SLING-5667 MockBundleContext.getServiceReference(...) does not return service reference
with highest ranking
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1741665
13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  2 +-
 .../sling/testing/mock/osgi/MockEventAdmin.java    |  5 +-
 .../testing/mock/osgi/MockServiceReference.java    |  3 +-
 .../testing/mock/osgi/MockBundleContextTest.java   | 20 +++----
 .../mock/osgi/MockServiceReferencesSortTest.java   | 70 ++++++++++++++--------
 .../mock/osgi/context/OsgiContextImplTest.java     |  8 ++-
 6 files changed, 67 insertions(+), 41 deletions(-)

diff --git a/pom.xml b/pom.xml
index ad787fe..d076409 100644
--- a/pom.xml
+++ b/pom.xml
@@ -59,7 +59,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.osgi</artifactId>
-            <version>2.2.0</version>
+            <version>2.4.0</version>
             <scope>compile</scope>
         </dependency>
 
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockEventAdmin.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockEventAdmin.java
index d182d1f..86c4eb5 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockEventAdmin.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockEventAdmin.java
@@ -33,6 +33,7 @@ import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.ReferencePolicy;
 import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.Order;
 import org.apache.sling.commons.osgi.ServiceUtil;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.event.Event;
@@ -106,13 +107,13 @@ public final class MockEventAdmin implements EventAdmin {
     
     protected void bindEventHandler(EventHandler eventHandler, Map<String, Object>
props) {
         synchronized (eventHandlers) {
-            eventHandlers.put(ServiceUtil.getComparableForServiceRanking(props), new EventHandlerItem(eventHandler,
props));
+            eventHandlers.put(ServiceUtil.getComparableForServiceRanking(props, Order.DESCENDING),
new EventHandlerItem(eventHandler, props));
         }
     }
 
     protected void unbindEventHandler(EventHandler eventHandler, Map<String, Object>
props) {
         synchronized (eventHandlers) {
-            eventHandlers.remove(ServiceUtil.getComparableForServiceRanking(props));
+            eventHandlers.remove(ServiceUtil.getComparableForServiceRanking(props, Order.DESCENDING));
         }
     }
     
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java
index 3d3d9ae..32ebd02 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java
@@ -22,6 +22,7 @@ import java.util.Collections;
 import java.util.Dictionary;
 import java.util.Map;
 
+import org.apache.sling.commons.osgi.Order;
 import org.apache.sling.commons.osgi.ServiceUtil;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
@@ -44,7 +45,7 @@ class MockServiceReference<T> implements ServiceReference<T>
{
     
     private Comparable<Object> buildComparable() {
         Map<String,Object> props = MapUtil.toMap(serviceRegistration.getProperties());
-        return ServiceUtil.getComparableForServiceRanking(props);
+        return ServiceUtil.getComparableForServiceRanking(props, Order.DESCENDING);
     }
     
     @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 c2b18ee..5dba8d6 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
@@ -73,15 +73,15 @@ 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();
+        String[] clazzes1 = new String[] { String.class.getName(), Integer.class.getName()
};
+        Object service1 = new Object();
         Dictionary<String, Object> properties2 = getServiceProperties(null);
-        ServiceRegistration reg2 = bundleContext.registerService(clazzes2, service2, properties2);
+        ServiceRegistration reg1 = bundleContext.registerService(clazzes1, service1, properties2);
 
-        String clazz1 = String.class.getName();
-        Object service1 = new Object();
+        String clazz2 = String.class.getName();
+        Object service2 = new Object();
         Dictionary<String, Object> properties1 = getServiceProperties(null);
-        ServiceRegistration reg1 = bundleContext.registerService(clazz1, service1, properties1);
+        ServiceRegistration reg2 = bundleContext.registerService(clazz2, service2, properties1);
 
         String clazz3 = Integer.class.getName();
         Object service3 = new Object();
@@ -93,7 +93,7 @@ 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);
@@ -106,8 +106,8 @@ public class MockBundleContextTest {
 
         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);
@@ -115,7 +115,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]);
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java
b/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java
index b34add1..e60622b 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java
@@ -22,10 +22,8 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Hashtable;
-import java.util.List;
 
 import org.junit.After;
 import org.junit.Before;
@@ -52,33 +50,61 @@ public class MockServiceReferencesSortTest {
     }
 
     @Test
-    public void testServicesOrder() {
-        assertEquals("12345", getSortedServicesString(bundleContext));
+    public void testAllWithRanking() {
+        registerStringServiceWithRanking("A", 3);
+        registerStringServiceWithRanking("B", 5);
+        registerStringServiceWithRanking("C", 4);
+        registerStringServiceWithRanking("D", 1);
+        registerStringServiceWithRanking("E", 2);
+        
+        assertEquals("BCAED", getSortedServicesString());
+        assertEquals("B", bundleContext.getService(bundleContext.getServiceReference(String.class)));
+    }
+
+    @Test
+    public void testAllWithoutRanking() {
+        registerStringServiceWithoutRanking("A");
+        registerStringServiceWithoutRanking("B");
+        registerStringServiceWithoutRanking("C");
+        registerStringServiceWithoutRanking("D");
+        registerStringServiceWithoutRanking("E");
+        
+        assertEquals("ABCDE", getSortedServicesString());
+        assertEquals("A", bundleContext.getService(bundleContext.getServiceReference(String.class)));
+    }
+
+    @Test
+    public void testMixed() {
+        registerStringServiceWithoutRanking("A");
+        registerStringServiceWithRanking("B", 5);
+        registerStringServiceWithoutRanking("C");
+        registerStringServiceWithRanking("D", 10);
+        registerStringServiceWithoutRanking("E");
+        
+        assertEquals("DBACE", getSortedServicesString());
+        assertEquals("D", bundleContext.getService(bundleContext.getServiceReference(String.class)));
+    }
+
+    private ServiceRegistration<?> registerStringServiceWithoutRanking(String serviceValue)
{
+        return bundleContext.registerService(String.class, serviceValue, new Hashtable<String,
Object>());
     }
 
-    private static ServiceRegistration<?> registerStringService(BundleContext ctx,
int index) {
+    private ServiceRegistration<?> registerStringServiceWithRanking(String serviceValue,
int index) {
         final Hashtable<String, Object> props = new Hashtable<String, Object>();
         props.put(Constants.SERVICE_RANKING, new Integer(index));
-        return ctx.registerService(String.class.getName(), String.valueOf(index), props);
+        return bundleContext.registerService(String.class, serviceValue, props);
     }
     
     /** Register services with a specific ranking, sort their references and 
      *  return their concatenated toString() values.
      *  Use to test service references sorting.
      */
-    private static String getSortedServicesString(BundleContext ctx) {
-        final List<ServiceRegistration<?>> toCleanup = new ArrayList<ServiceRegistration<?>>();
-        
-        toCleanup.add(registerStringService(ctx, 3));
-        toCleanup.add(registerStringService(ctx, 5));
-        toCleanup.add(registerStringService(ctx, 4));
-        toCleanup.add(registerStringService(ctx, 1));
-        toCleanup.add(registerStringService(ctx, 2));
-        
-        ServiceReference<?> [] refs = null;
+    private String getSortedServicesString() {
+        ServiceReference<?>[] refs = null;
         try {
-            refs = ctx.getServiceReferences(String.class.getName(), null);
-        } catch(InvalidSyntaxException ise) {
+            refs = bundleContext.getServiceReferences(String.class.getName(), null);
+        }
+        catch(InvalidSyntaxException ise) {
             fail("Unexpected InvalidSyntaxException");
         }
         assertNotNull("Expecting our service references", refs);
@@ -86,12 +112,8 @@ public class MockServiceReferencesSortTest {
         
         final StringBuilder sb = new StringBuilder();
         for(ServiceReference<?> ref : refs) {
-            sb.append(ctx.getService(ref).toString());
-            ctx.ungetService(ref);
-        }
-        
-        for(ServiceRegistration<?> reg : toCleanup) {
-            reg.unregister();
+            sb.append(bundleContext.getService(ref).toString());
+            bundleContext.ungetService(ref);
         }
         
         return sb.toString();
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
b/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
index 84513e5..1977258 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
@@ -87,11 +87,13 @@ public class OsgiContextImplTest {
         context.registerService(Set.class, myService1);
         Set<String> myService2 = new HashSet<String>();
         context.registerService(Set.class, myService2);
+        
+        assertSame(myService1, context.getService(Set.class));
 
-        // expected: descending order because ordering descending by service id
+        // expected: ascending order because ordering ascending by service ID
         Set[] serviceResults = context.getServices(Set.class, null);
-        assertSame(myService1, serviceResults[1]);
-        assertSame(myService2, serviceResults[0]);
+        assertSame(myService1, serviceResults[0]);
+        assertSame(myService2, serviceResults[1]);
     }
 
     @Test

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message