Class JavaOSGiTest

java.lang.Object
org.openhab.core.test.java.JavaTest
org.openhab.core.test.java.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 Details Link icon

    • bundleContext Link icon

      @NonNullByDefault({}) protected org.osgi.framework.BundleContext bundleContext
  • Constructor Details Link icon

    • JavaOSGiTest Link icon

      public JavaOSGiTest()
  • Method Details Link icon

    • bindBundleContext Link icon

      @BeforeEach public void bindBundleContext()
    • getService Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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
      interfaceNames - interface names of the OSGi service
      properties - OSGi service properties
      Returns:
      service registration object
    • unregisterService Link icon

      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 Link icon

      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 Link icon

      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
    • getBundleContext Link icon

      protected org.osgi.framework.BundleContext getBundleContext()
      Get the OSGi BundleContext
      Returns:
      the bundle context
    • registerVolatileStorageService Link icon

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

      @AfterEach public void unregisterMocks()