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 0e10e4502e5bfa1833689b19ed57fe2730c65935 Author: Stefan Seifert <[email protected]> AuthorDate: Thu Jan 28 15:55:06 2016 +0000 SLING-5462 ensure service references are sorted ascending by service ranking, not descending git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1727389 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 60 ----------------- .../testing/mock/osgi/MockServiceReference.java | 9 ++- .../testing/mock/osgi/MockBundleContextTest.java | 2 +- .../mock/osgi/MockServiceReferencesSortTest.java | 58 +++++++++++++++- .../mock/osgi/RealServiceReferencesSortTest.java | 49 -------------- .../testing/mock/osgi/ServiceReferencesUtil.java | 77 ---------------------- 6 files changed, 60 insertions(+), 195 deletions(-) diff --git a/pom.xml b/pom.xml index aac673c..81baf92 100644 --- a/pom.xml +++ b/pom.xml @@ -34,11 +34,6 @@ <name>Apache Sling Testing OSGi Mock</name> <description>Mock implementation of selected OSGi APIs.</description> - <properties> - <pax-exam.version>4.6.0</pax-exam.version> - <pax-link.version>2.4.3</pax-link.version> - </properties> - <scm> <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock</connection> <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock</developerConnection> @@ -126,61 +121,6 @@ <scope>test</scope> </dependency> - <dependency> - <groupId>org.ops4j.pax.exam</groupId> - <artifactId>pax-exam-container-forked</artifactId> - <version>${pax-exam.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.ops4j.pax.exam</groupId> - <artifactId>pax-exam-junit4</artifactId> - <version>${pax-exam.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.ops4j.pax.exam</groupId> - <artifactId>pax-exam-link-mvn</artifactId> - <version>${pax-exam.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.ops4j.pax.url</groupId> - <artifactId>pax-url-aether</artifactId> - <version>${pax-link.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.ops4j.pax.url</groupId> - <artifactId>pax-url-wrap</artifactId> - <version>${pax-link.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-atinject_1.0_spec</artifactId> - <version>1.0</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.ops4j.base</groupId> - <artifactId>ops4j-base-lang</artifactId> - <version>1.2.3</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.ops4j.base</groupId> - <artifactId>ops4j-base-net</artifactId> - <version>1.2.3</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.ops4j.pax.tinybundles</groupId> - <artifactId>tinybundles</artifactId> - <version>1.0.0</version> - <scope>test</scope> - </dependency> - </dependencies> <build> 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 6af3a37..dfe84c6 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 @@ -81,14 +81,13 @@ class MockServiceReference<T> implements ServiceReference<T> { if (!(obj instanceof MockServiceReference)) { return 0; } - // sort by decreasing by service ranking, and secondary increasing by - // service id + // sort by ascending by service ranking, and secondary ascending by service id Integer serviceRanking = getServiceRanking(); - Integer otherServiceRanking = ((MockServiceReference) obj).getServiceRanking(); - int serviceRankingCompare = otherServiceRanking.compareTo(serviceRanking); + Integer otherServiceRanking = ((MockServiceReference)obj).getServiceRanking(); + int serviceRankingCompare = serviceRanking.compareTo(otherServiceRanking); if (serviceRankingCompare == 0) { Long serviceId = getServiceId(); - Long otherServiceId = ((MockServiceReference) obj).getServiceId(); + Long otherServiceId = ((MockServiceReference)obj).getServiceId(); return serviceId.compareTo(otherServiceId); } else { return serviceRankingCompare; 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 2a5c44e..9fc5dc8 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 @@ -85,7 +85,7 @@ public class MockBundleContextTest { String clazz3 = Integer.class.getName(); Object service3 = new Object(); - Dictionary<String, Object> properties3 = getServiceProperties(100L); + Dictionary<String, Object> properties3 = getServiceProperties(-100L); ServiceRegistration reg3 = bundleContext.registerService(clazz3, service3, properties3); // test get service references 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 cdf085d..b34add1 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 @@ -19,11 +19,22 @@ package org.apache.sling.testing.mock.osgi; 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; import org.junit.Test; import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; /** Test the service-ranking based sorting of mock service references */ public class MockServiceReferencesSortTest { @@ -39,10 +50,51 @@ public class MockServiceReferencesSortTest { public void tearDown() { MockOsgi.shutdown(bundleContext); } - + @Test public void testServicesOrder() { - // TODO this should return the same result as in RealServiceReferencesSortTest - assertEquals("54321", ServiceReferencesUtil.getSortedServicesString(bundleContext)); + assertEquals("12345", getSortedServicesString(bundleContext)); + } + + private static ServiceRegistration<?> registerStringService(BundleContext ctx, 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); + } + + /** 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; + try { + refs = ctx.getServiceReferences(String.class.getName(), null); + } catch(InvalidSyntaxException ise) { + fail("Unexpected InvalidSyntaxException"); + } + assertNotNull("Expecting our service references", refs); + Arrays.sort(refs); + + final StringBuilder sb = new StringBuilder(); + for(ServiceReference<?> ref : refs) { + sb.append(ctx.getService(ref).toString()); + ctx.ungetService(ref); + } + + for(ServiceRegistration<?> reg : toCleanup) { + reg.unregister(); + } + + return sb.toString(); } + } \ No newline at end of file diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/RealServiceReferencesSortTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/RealServiceReferencesSortTest.java deleted file mode 100644 index 4ec7694..0000000 --- a/src/test/java/org/apache/sling/testing/mock/osgi/RealServiceReferencesSortTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.sling.testing.mock.osgi; - -import static org.junit.Assert.assertEquals; -import static org.ops4j.pax.exam.CoreOptions.junitBundles; -import static org.ops4j.pax.exam.CoreOptions.options; - -import javax.inject.Inject; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.ops4j.pax.exam.Configuration; -import org.ops4j.pax.exam.Option; -import org.ops4j.pax.exam.junit.PaxExam; -import org.osgi.framework.BundleContext; - -@RunWith(PaxExam.class) -/** Test the service-ranking based sorting of real OSGi framework service references */ -public class RealServiceReferencesSortTest { - @Inject - protected BundleContext bundleContext; - - @Configuration - public Option[] config() { - return options(junitBundles()); - } - - @Test - public void testServicesOrder() { - assertEquals("12345", ServiceReferencesUtil.getSortedServicesString(bundleContext)); - } -} \ No newline at end of file diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/ServiceReferencesUtil.java b/src/test/java/org/apache/sling/testing/mock/osgi/ServiceReferencesUtil.java deleted file mode 100644 index 22f33ed..0000000 --- a/src/test/java/org/apache/sling/testing/mock/osgi/ServiceReferencesUtil.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.sling.testing.mock.osgi; - -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.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; - -class ServiceReferencesUtil { - - static private ServiceRegistration<?> registerStringService(BundleContext ctx, 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); - } - - /** Register services with a specific ranking, sort their references and - * return their concatenated toString() values. - * Use to test service references sorting. - */ - 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; - try { - refs = ctx.getServiceReferences(String.class.getName(), null); - } catch(InvalidSyntaxException ise) { - fail("Unexpected InvalidSyntaxException"); - } - assertNotNull("Expecting our service references", refs); - Arrays.sort(refs); - - final StringBuilder sb = new StringBuilder(); - for(ServiceReference<?> ref : refs) { - sb.append(ctx.getService(ref).toString()); - ctx.ungetService(ref); - } - - for(ServiceRegistration<?> reg : toCleanup) { - reg.unregister(); - } - - return sb.toString(); - } -} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
