Class OsgiContextImpl


  • @ConsumerType
    public class OsgiContextImpl
    extends Object
    Defines OSGi context objects and helper methods. Should not be used directly but via the OsgiContext JUnit rule or extension.
    • Field Detail

      • componentContext

        protected org.osgi.service.component.ComponentContext componentContext
    • Constructor Detail

      • OsgiContextImpl

        public OsgiContextImpl()
    • Method Detail

      • setUp

        protected void setUp()
        Setup actions before test method execution
      • tearDown

        protected void tearDown()
        Teardown actions after test method execution
      • componentContext

        @NotNull
        public final @NotNull org.osgi.service.component.ComponentContext componentContext()
        Returns:
        OSGi component context
      • bundleContext

        @NotNull
        public final @NotNull org.osgi.framework.BundleContext bundleContext()
        Returns:
        OSGi Bundle context
      • registerService

        @NotNull
        public final <T> T registerService​(@NotNull
                                           T service)
        Registers a service in the mocked OSGi environment.
        Type Parameters:
        T - Service type
        Parameters:
        service - Service instance
        Returns:
        Registered service instance
      • registerService

        @NotNull
        public final <T> T registerService​(@Nullable
                                           @Nullable Class<T> serviceClass,
                                           @NotNull
                                           T service)
        Registers a service in the mocked OSGi environment.
        Type Parameters:
        T - Service type
        Parameters:
        serviceClass - Service class
        service - Service instance
        Returns:
        Registered service instance
      • registerService

        @NotNull
        public final <T> T registerService​(@Nullable
                                           @Nullable Class<T> serviceClass,
                                           @NotNull
                                           T service,
                                           @Nullable
                                           @Nullable Map<String,​Object> properties)
        Registers a service in the mocked OSGi environment.
        Type Parameters:
        T - Service type
        Parameters:
        serviceClass - Service class
        service - Service instance
        properties - Service properties (optional)
        Returns:
        Registered service instance
      • registerService

        @NotNull
        public final <T> T registerService​(@Nullable
                                           @Nullable Class<T> serviceClass,
                                           @NotNull
                                           T service,
                                           @NotNull
                                           @NotNull Object @NotNull ... properties)
        Registers a service in the mocked OSGi environment.
        Type Parameters:
        T - Service type
        Parameters:
        serviceClass - Service class
        service - Service instance
        properties - Service properties (optional)
        Returns:
        Registered service instance
      • registerInjectActivateService

        @NotNull
        public final <T> T registerInjectActivateService​(@NotNull
                                                         T component)
        Injects dependencies, activates and registers a DS component in the mocked OSGi environment. Construction injection for OSGi services is supported.
        Type Parameters:
        T - DS Component type
        Parameters:
        component - a DS component instance
        Returns:
        the DS component instance
      • registerInjectActivateService

        @NotNull
        public final <T> T registerInjectActivateService​(@NotNull
                                                         T component,
                                                         @Nullable
                                                         @Nullable Map<String,​Object> properties)
        Injects dependencies, activates and registers a DS component in the mocked OSGi environment. Constructor injection for DS components is supported.
        Type Parameters:
        T - DS Component type
        Parameters:
        component - a DS component instance
        properties - component properties (optional)
        Returns:
        the DS component instance
      • registerInjectActivateService

        @NotNull
        public final <T> T registerInjectActivateService​(@NotNull
                                                         T component,
                                                         @NotNull
                                                         @NotNull Object @NotNull ... properties)
        Injects dependencies, activates and registers a DS component in the mocked OSGi environment. Constructor injection for DS components is supported.
        Type Parameters:
        T - DS Component type
        Parameters:
        component - a DS component instance
        properties - component properties (optional)
        Returns:
        the DS component instance
      • registerInjectActivateService

        @NotNull
        public final <T> T registerInjectActivateService​(@NotNull
                                                         @NotNull Class<T> componentClass)
        Injects dependencies, activates and registers a DS component in the mocked OSGi environment. Constructor injection for DS components is supported.
        Type Parameters:
        T - DS Component type
        Parameters:
        componentClass - a DS component class
        Returns:
        the DS component instance
      • registerInjectActivateService

        @NotNull
        public final <T> T registerInjectActivateService​(@NotNull
                                                         @NotNull Class<T> componentClass,
                                                         @Nullable
                                                         @Nullable Map<String,​Object> properties)
        Injects dependencies, activates and registers a DS component in the mocked OSGi environment. Construction injection for OSGi services is supported.
        Type Parameters:
        T - DS Component type
        Parameters:
        componentClass - a DS component class
        properties - component properties (optional)
        Returns:
        the DS component instance
      • registerInjectActivateService

        @NotNull
        public final <T> T registerInjectActivateService​(@NotNull
                                                         @NotNull Class<T> componentClass,
                                                         @NotNull
                                                         @NotNull Object @NotNull ... properties)
        Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
        Type Parameters:
        T - DS Component type
        Parameters:
        componentClass - a DS component class
        properties - component properties (optional)
        Returns:
        the DS component instance
      • getService

        @Nullable
        public final <ServiceType> ServiceType getService​(@NotNull
                                                          @NotNull Class<ServiceType> serviceType)
        Lookup a single service
        Type Parameters:
        ServiceType - Service type
        Parameters:
        serviceType - The type (interface) of the service.
        Returns:
        The service instance, or null if the service is not available.
      • getServices

        @NotNull
        public final <ServiceType> @NotNull ServiceType @NotNull [] getServices​(@NotNull
                                                                                @NotNull Class<ServiceType> serviceType,
                                                                                @Nullable
                                                                                @Nullable String filter)
        Lookup one or several services
        Type Parameters:
        ServiceType - Service type
        Parameters:
        serviceType - The type (interface) of the service.
        filter - An optional filter (LDAP-like, see OSGi spec)
        Returns:
        The services instances or an empty array.
        Throws:
        RuntimeException - If the filter string is not a valid OSGi service filter string.