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-1.7.2 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit 3a3b1f7319431086585b8a65f59d59ee1d025a39 Author: Stefan Seifert <[email protected]> AuthorDate: Thu Jan 28 17:36:08 2016 +0000 SLING-5462 switch to comparable implementation from commons.osgi and change wrong assumptions in unit test git-svn-id: https://svn.apache.org/repos/asf/sling/branches/testing/mocks/osgi-mock-1.x@1727411 13f79535-47bb-0310-9956-ffa450edef68 --- .../testing/mock/osgi/MockServiceReference.java | 26 +++++++++++----------- .../testing/mock/osgi/MockBundleContextTest.java | 10 ++++----- .../mock/osgi/context/OsgiContextImplTest.java | 5 +++-- 3 files changed, 21 insertions(+), 20 deletions(-) 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 a623550..f210095 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 @@ -20,7 +20,9 @@ package org.apache.sling.testing.mock.osgi; 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 ServiceReference { 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 ServiceReference { */ 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 ServiceReference { @Override public int hashCode() { - return ((Long) getServiceId()).hashCode(); + return comparable.hashCode(); } @Override @@ -73,7 +83,7 @@ class MockServiceReference implements ServiceReference { 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 ServiceReference { 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() { 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 847d02d..13a2599 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 @@ -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); 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 60d5b0b..7086325 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 @@ -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 -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
