sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sseif...@apache.org
Subject svn commit: r1727411 - 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/
Date Thu, 28 Jan 2016 17:36:08 GMT
Author: sseifert
Date: Thu Jan 28 17:36:08 2016
New Revision: 1727411

URL: http://svn.apache.org/viewvc?rev=1727411&view=rev
Log:
SLING-5462 switch to comparable implementation from commons.osgi and change wrong assumptions
in unit test

Modified:
    sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.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/MockServiceReference.java
URL: http://svn.apache.org/viewvc/sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java?rev=1727411&r1=1727410&r2=1727411&view=diff
==============================================================================
--- sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java
(original)
+++ sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java
Thu Jan 28 17:36:08 2016
@@ -20,7 +20,9 @@ package org.apache.sling.testing.mock.os
 
 import java.util.Collections;
 import java.util.Dictionary;
+import java.util.Map;
 
+import org.apache.sling.commons.osgi.ServiceUtil;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
@@ -32,10 +34,17 @@ class MockServiceReference implements Se
 
     private final Bundle bundle;
     private final MockServiceRegistration serviceRegistration;
+    private volatile Comparable<Object> comparable;
 
     public MockServiceReference(final Bundle bundle, final MockServiceRegistration serviceRegistration)
{
         this.bundle = bundle;
         this.serviceRegistration = serviceRegistration;
+        this.comparable = buildComparable();
+    }
+
+    private Comparable<Object> buildComparable() {
+        Map<String,Object> props = MapUtil.toMap(serviceRegistration.getProperties());
+        return ServiceUtil.getComparableForServiceRanking(props);
     }
 
     @Override
@@ -50,6 +59,7 @@ class MockServiceReference implements Se
      */
     public void setProperty(final String key, final Object value) {
         this.serviceRegistration.getProperties().put(key, value);
+        this.comparable = buildComparable();
     }
 
     @Override
@@ -65,7 +75,7 @@ class MockServiceReference implements Se
 
     @Override
     public int hashCode() {
-        return ((Long) getServiceId()).hashCode();
+        return comparable.hashCode();
     }
 
     @Override
@@ -73,7 +83,7 @@ class MockServiceReference implements Se
         if (!(obj instanceof MockServiceReference)) {
             return false;
         }
-        return ((Long) getServiceId()).equals(((MockServiceReference) obj).getServiceId());
+        return comparable.equals(((MockServiceReference)obj).comparable);
     }
 
     @Override
@@ -81,17 +91,7 @@ class MockServiceReference implements Se
         if (!(obj instanceof MockServiceReference)) {
             return 0;
         }
-        // sort by ascending by service ranking, and secondary ascending by service id
-        Integer serviceRanking = getServiceRanking();
-        Integer otherServiceRanking = ((MockServiceReference)obj).getServiceRanking();
-        int serviceRankingCompare = serviceRanking.compareTo(otherServiceRanking);
-        if (serviceRankingCompare == 0) {
-            Long serviceId = getServiceId();
-            Long otherServiceId = ((MockServiceReference)obj).getServiceId();
-            return serviceId.compareTo(otherServiceId);
-        } else {
-            return serviceRankingCompare;
-        }
+        return comparable.compareTo(((MockServiceReference)obj).comparable);
     }
 
     long getServiceId() {

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=1727411&r1=1727410&r2=1727411&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 Jan 28 17:36:08 2016
@@ -72,16 +72,16 @@ public class MockBundleContextTest {
     @Test
     public void testServiceRegistration() throws InvalidSyntaxException {
         // prepare test services
-        String clazz1 = String.class.getName();
-        Object service1 = new Object();
-        Dictionary properties1 = getServiceProperties(null);
-        ServiceRegistration reg1 = bundleContext.registerService(clazz1, service1, properties1);
-
         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();
+        Object service1 = new Object();
+        Dictionary properties1 = getServiceProperties(null);
+        ServiceRegistration reg1 = bundleContext.registerService(clazz1, service1, properties1);
+
         String clazz3 = Integer.class.getName();
         Object service3 = new Object();
         Dictionary properties3 = getServiceProperties(-100L);

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=1727411&r1=1727410&r2=1727411&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 Jan 28 17:36:08 2016
@@ -88,9 +88,10 @@ public class OsgiContextImplTest {
         Set<String> myService2 = new HashSet<String>();
         context.registerService(Set.class, myService2);
 
+        // expected: descending order because ordering descending by service id
         Set[] serviceResults = context.getServices(Set.class, null);
-        assertSame(myService1, serviceResults[0]);
-        assertSame(myService2, serviceResults[1]);
+        assertSame(myService1, serviceResults[1]);
+        assertSame(myService2, serviceResults[0]);
     }
 
     @Test



Mime
View raw message