SLING-5462 - demonstrate the wrong sorting of mock service references
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1727309 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 81baf92..aac673c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -34,6 +34,11 @@
<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>
@@ -121,6 +126,61 @@
<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/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java
new file mode 100644
index 0000000..cdf085d
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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 org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+
+/** Test the service-ranking based sorting of mock service references */
+public class MockServiceReferencesSortTest {
+
+ private BundleContext bundleContext;
+
+ @Before
+ public void setUp() {
+ bundleContext = MockOsgi.newBundleContext();
+ }
+
+ @After
+ 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));
+ }
+}
\ 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
new file mode 100644
index 0000000..4ec7694
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/RealServiceReferencesSortTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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
new file mode 100644
index 0000000..22f33ed
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/ServiceReferencesUtil.java
@@ -0,0 +1,77 @@
+/*
+ * 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();
+ }
+}