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.2 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit fe2625ed84bbaf65ae4a8a8e9841e821a03e3c3c Author: Stefan Seifert <[email protected]> AuthorDate: Thu Jan 28 17:16:40 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/trunk/testing/mocks/osgi-mock@1727409 13f79535-47bb-0310-9956-ffa450edef68 --- .../testing/mock/osgi/MockServiceReference.java | 28 +++++++++++----------- .../testing/mock/osgi/MockBundleContextTest.java | 10 ++++---- .../mock/osgi/context/OsgiContextImplTest.java | 5 ++-- 3 files changed, 22 insertions(+), 21 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 dfe84c6..3d3d9ae 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,12 +34,19 @@ class MockServiceReference<T> implements ServiceReference<T> { private final Bundle bundle; private final MockServiceRegistration<T> serviceRegistration; + private volatile Comparable<Object> comparable; public MockServiceReference(final Bundle bundle, final MockServiceRegistration<T> 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 public Bundle getBundle() { return this.bundle; @@ -50,6 +59,7 @@ class MockServiceReference<T> implements ServiceReference<T> { */ 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<T> implements ServiceReference<T> { @Override public int hashCode() { - return ((Long) getServiceId()).hashCode(); + return comparable.hashCode(); } @Override @@ -73,7 +83,7 @@ class MockServiceReference<T> implements ServiceReference<T> { 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<T> implements ServiceReference<T> { 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 9fc5dc8..c2b18ee 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,16 +73,16 @@ public class MockBundleContextTest { @Test public void testServiceRegistration() throws InvalidSyntaxException { // prepare test services - String clazz1 = String.class.getName(); - Object service1 = new Object(); - Dictionary<String, Object> 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<String, Object> properties2 = getServiceProperties(null); ServiceRegistration reg2 = bundleContext.registerService(clazzes2, service2, properties2); + String clazz1 = String.class.getName(); + Object service1 = new Object(); + Dictionary<String, Object> properties1 = getServiceProperties(null); + ServiceRegistration reg1 = bundleContext.registerService(clazz1, service1, properties1); + String clazz3 = Integer.class.getName(); Object service3 = new Object(); Dictionary<String, Object> 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 59990d5..84513e5 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]>.
