Class JavaOSGiTest


  • @NonNullByDefault
    public class JavaOSGiTest
    extends JavaTest
    JavaOSGiTest is an abstract base class for OSGi based tests. It provides convenience methods to register and unregister mocks as OSGi services. All services, which are registered through the registerService(java.lang.Object) methods, are unregistered automatically in the tear down of the test.
    Author:
    Markus Rathgeb - Initial contribution
    • Field Detail

      • bundleContext

        @NonNullByDefault({})
        protected org.osgi.framework.BundleContext bundleContext
    • Constructor Detail

      • JavaOSGiTest

        public JavaOSGiTest()
    • Method Detail

      • bindBundleContext

        public void bindBundleContext()
      • getService

        protected <T> @Nullable T getService​(Class<T> clazz)
        Get an OSGi service for the given class.
        Parameters:
        clazz - class under which the OSGi service is registered
        Returns:
        OSGi service or null if no service can be found for the given class
      • getServices

        protected <T> List<T> getServices​(Class<T> clazz,
                                          Predicate<org.osgi.framework.ServiceReference<T>> filter)
        Get all OSGi service for the given class and the given filter.
        Parameters:
        clazz - class under which the OSGi service is registered
        filter - Predicate to apply to found ServiceReferences
        Returns:
        List of OSGi services or empty List if no service can be found for the given class and filter
      • getService

        protected <T> @Nullable T getService​(Class<T> clazz,
                                             Predicate<org.osgi.framework.ServiceReference<T>> filter)
        Get an OSGi service for the given class and the given filter.
        Parameters:
        clazz - class under which the OSGi service is registered
        filter - Predicate to apply to found ServiceReferences
        Returns:
        OSGi service or null if no service can be found for the given class
        Throws:
        AssertionError - if more than one instance of the service is found
      • getService

        protected <T,​I extends T> @Nullable I getService​(Class<T> clazz,
                                                               Class<I> implementationClass)
        Get the OSGi service for the given service class and the given implementation class.
        Parameters:
        clazz - class under which the OSGi service is registered
        implementationClass - the implementation class
        Returns:
        OSGi service or null if no service can be found for the given classes
        Throws:
        AssertionError - if more than one instance of the service is found
      • getServices

        protected <T,​I extends T> List<I> getServices​(Class<T> clazz,
                                                            Class<I> implementationClass)
        Get all OSGi services for the given service class and the given implementation class.
        Parameters:
        clazz - class under which the OSGi services are registered
        implementationClass - the implementation class of the services
        Returns:
        List of OSGi service or empty List if no matching services can be found for the given classes
      • registerService

        protected org.osgi.framework.ServiceRegistration<?> registerService​(Object service)
        Register the given object as OSGi service.

        The first interface is used as OSGi service interface name.

        Parameters:
        service - service to be registered
        Returns:
        service registration object
      • registerService

        protected org.osgi.framework.ServiceRegistration<?> registerService​(Object service,
                                                                            Dictionary<String,​?> properties)
        Register the given object as OSGi service. The first interface is used as OSGi service interface name.
        Parameters:
        service - service to be registered
        properties - OSGi service properties
        Returns:
        service registration object
      • registerService

        protected org.osgi.framework.ServiceRegistration<?> registerService​(Object service,
                                                                            String interfaceName)
        Register the given object as OSGi service.

        The given interface name is used as OSGi service interface name.

        Parameters:
        service - service to be registered
        interfaceName - interface name of the OSGi service
        Returns:
        service registration object
      • registerService

        protected org.osgi.framework.ServiceRegistration<?> registerService​(Object service,
                                                                            String interfaceName,
                                                                            @Nullable Dictionary<String,​?> properties)
        Register the given object as OSGi service.

        The given interface name is used as OSGi service interface name.

        Parameters:
        service - service to be registered
        interfaceName - interface name of the OSGi service
        properties - OSGi service properties
        Returns:
        service registration object
      • registerService

        protected org.osgi.framework.ServiceRegistration<?> registerService​(Object service,
                                                                            String[] interfaceNames,
                                                                            Dictionary<String,​?> properties)
        Register the given object as OSGi service.

        The given interface names are used as OSGi service interface name.

        Parameters:
        service - service to be registered
        interfaceName - interface name of the OSGi service
        properties - OSGi service properties
        Returns:
        service registration object
      • unregisterService

        protected @Nullable org.osgi.framework.ServiceRegistration<?> unregisterService​(Object service)
        Unregister an OSGi service by the given object, that was registered before.

        The interface name is taken from the first interface of the service object.

        Parameters:
        service - the service
        Returns:
        the service registration that was unregistered or null if no service could be found
      • unregisterService

        protected @Nullable org.osgi.framework.ServiceRegistration<?> unregisterService​(String interfaceName)
        Unregister an OSGi service by the given object, that was registered before.
        Parameters:
        interfaceName - the interface name of the service
        Returns:
        the first service registration that was unregistered or null if no service could be found
      • getInterfaceName

        protected String getInterfaceName​(Object service)
        Returns the interface name for a given service object by choosing the first interface.
        Parameters:
        service - service object
        Returns:
        name of the first interface if interfaces are implemented
        Throws:
        IllegalArgumentException - if no interface is implemented
      • registerVolatileStorageService

        protected void registerVolatileStorageService()
        Registers a volatile storage service.
      • unregisterMocks

        public void unregisterMocks()