This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch experimental/serviceFactory-remove in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit 14a525504afbb98f9ebb2628233f2444bba7f91e Author: Stefan Seifert <[email protected]> AuthorDate: Tue Dec 7 15:56:22 2021 +0100 remove ServiceFactory support --- .../testing/mock/osgi/MockServiceRegistration.java | 18 ++--------- .../testing/mock/osgi/MockBundleContextTest.java | 27 +---------------- .../testing/mock/osgi/OsgiServiceUtilTest.java | 35 ---------------------- .../testsvc/osgiserviceutil/ServiceFactory1.java | 26 ---------------- 4 files changed, 3 insertions(+), 103 deletions(-) diff --git a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java index 2b3aed7..33bf5e4 100644 --- a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java +++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java @@ -34,7 +34,6 @@ import org.osgi.framework.Bundle; import org.osgi.framework.Constants; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceEvent; -import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; @@ -52,18 +51,12 @@ class MockServiceRegistration<T> implements ServiceRegistration<T>, Comparable<M private final ServiceReference<T> serviceReference; private final MockBundleContext bundleContext; - @SuppressWarnings("unchecked") public MockServiceRegistration(final Bundle bundle, final String[] clazzes, final T service, final Dictionary<String, Object> properties, MockBundleContext bundleContext) { this.serviceId = SERVICE_ID_COUNTER.incrementAndGet(); this.clazzes = new HashSet<String>(Arrays.asList(clazzes)); - if (service instanceof ServiceFactory) { - this.service = ((ServiceFactory<T>)service).getService(bundleContext.getBundle(), this); - } - else { - this.service = service; - } + this.service = service; readOsgiMetadata(); @@ -119,15 +112,8 @@ class MockServiceRegistration<T> implements ServiceRegistration<T>, Comparable<M return clazzes; } - @SuppressWarnings({ "unchecked", "null" }) T getService() { - if (this.service instanceof ServiceFactory) { - ServiceFactory<T> factory = (ServiceFactory<T>)this.service; - return factory.getService(this.bundleContext.getBundle(), this); - } - else { - return this.service; - } + return this.service; } @Override diff --git a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java index 8ca1be7..d986ba4 100644 --- a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java +++ b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java @@ -50,7 +50,6 @@ import org.osgi.framework.Constants; import org.osgi.framework.Filter; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceEvent; -import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; @@ -188,30 +187,6 @@ public class MockBundleContextTest { } @Test - public void testServiceFactoryRegistration() throws InvalidSyntaxException { - // prepare test services - Class<String> clazz = String.class; - final String service = "abc"; - Dictionary<String, Object> properties1 = ranking(null); - ServiceRegistration reg = bundleContext.registerService(clazz, new ServiceFactory<String>() { - @Override - public String getService(Bundle bundle, ServiceRegistration<String> registration) { - return service; - } - @Override - public void ungetService(Bundle bundle, ServiceRegistration<String> registration, String service) { - // do nothing - } - }, properties1); - - ServiceReference<String> ref = bundleContext.getServiceReference(clazz); - assertNotNull(ref); - assertSame(reg.getReference(), ref); - assertSame(service, bundleContext.getService(ref)); - bundleContext.ungetService(ref); - } - - @Test public void testNoServiceReferences() throws InvalidSyntaxException { ServiceReference<?>[] refs = bundleContext.getServiceReferences(String.class.getName(), null); assertNull(refs); @@ -234,7 +209,7 @@ public class MockBundleContextTest { reg1.unregister(); assertNull(bundleContext.getServiceReference(clazz1)); - + try { reg1.unregister(); Assert.fail("Unregistering a non existant service should throw IllegalStateException"); diff --git a/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java b/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java index 3e428ce..7b7afda 100644 --- a/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java +++ b/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java @@ -38,7 +38,6 @@ import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.Service3; import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.Service3OsgiR6; import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.Service4; import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.Service5; -import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceFactory1; import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface1; import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface2; import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface3; @@ -48,10 +47,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; -import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceFactory; -import org.osgi.framework.ServiceRegistration; import com.google.common.collect.ImmutableMap; @@ -209,35 +205,4 @@ public class OsgiServiceUtilTest { assertEquals(true, service5.doRemoteThing()); } - @Test - public void testServiceFactoryViaScr() { - ServiceFactory1 serviceFactory1 = new ServiceFactory1(); - - MockOsgi.injectServices(serviceFactory1, bundleContext); - MockOsgi.activate(serviceFactory1, bundleContext, (Dictionary<String, Object>) null); - bundleContext.registerService(ServiceFactory1.class.getName(), serviceFactory1, null); - - assertSame(serviceFactory1, bundleContext.getService( - bundleContext.getServiceReference(ServiceFactory1.class.getName()))); - } - - @Test - public void testServiceFactoryViaManualRegistration() { - final ServiceFactory1 serviceFactory1 = new ServiceFactory1(); - - bundleContext.registerService(ServiceFactory1.class.getName(), new ServiceFactory() { - @Override - public Object getService(Bundle bundle, ServiceRegistration registration) { - return serviceFactory1; - } - @Override - public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) { - // nothing to do - } - }, null); - - assertSame(serviceFactory1, bundleContext.getService( - bundleContext.getServiceReference(ServiceFactory1.class.getName()))); - } - } diff --git a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/ServiceFactory1.java b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/ServiceFactory1.java deleted file mode 100644 index 3fcbef7..0000000 --- a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/ServiceFactory1.java +++ /dev/null @@ -1,26 +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.testsvc.osgiserviceutil; - -import org.osgi.service.component.annotations.Component; - -@Component(service = ServiceFactory1.class, servicefactory = true) -public class ServiceFactory1 { - -}
