Class MockOsgi


  • public final class MockOsgi
    extends Object
    Factory for mock OSGi objects.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean activate​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext)
      Simulates activation of a DS component instance.
      static boolean activate​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)
      Simulates activation of a DS component instance.
      static boolean activate​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Dictionary<String,​Object> properties)
      Simulates activation of a DS component instance.
      static boolean activate​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,​Object> properties)
      Simulates activation of a DS component instance.
      static <T> T activateInjectServices​(@NotNull Class<T> targetClass, @NotNull org.osgi.framework.BundleContext bundleContext)
      Simulates OSGi DS dependency injection and activation.
      static <T> T activateInjectServices​(@NotNull Class<T> targetClass, @NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)
      Simulates OSGi DS dependency injection and activation.
      static <T> T activateInjectServices​(@NotNull Class<T> targetClass, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,​Object> properties)
      Simulates OSGi DS dependency injection and activation.
      static @NotNull ComponentContextBuilder componentContext()  
      static boolean deactivate​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext)
      Simulates deactivation of a DS component instance.
      static boolean deactivate​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)
      Simulates deactivation of a DS component instance.
      static boolean deactivate​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Dictionary<String,​Object> properties)
      Simulates deactivation of a DS component instance.
      static boolean deactivate​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,​Object> properties)
      Simulates deactivation of a DS component instance.
      static boolean injectServices​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext)
      Simulates OSGi DS dependency injection.
      static boolean injectServices​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,​Object> properties)
      Simulates OSGi DS dependency injection.
      static boolean modified​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)
      Simulates configuration modification of a DS component instance.
      static boolean modified​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Dictionary<String,​Object> properties)
      Simulates configuration modification of a DS component instance.
      static boolean modified​(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,​Object> properties)
      Simulates configuration modification of a DS component instance.
      static @NotNull org.osgi.framework.BundleContext newBundleContext()  
      static @NotNull org.osgi.service.component.ComponentContext newComponentContext()  
      static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@NotNull Object @NotNull ... properties)  
      static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)  
      static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Dictionary<String,​Object> properties)  
      static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,​Object> properties)  
      static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@Nullable Dictionary<String,​Object> properties)  
      static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@Nullable Map<String,​Object> properties)  
      static @NotNull org.osgi.service.log.LogService newLogService​(@NotNull Class<?> loggerContext)  
      static <T> T registerInjectActivateService​(@NotNull Class<T> dsComponentClass, @NotNull org.osgi.framework.BundleContext bundleContext)
      Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
      static <T> T registerInjectActivateService​(@NotNull Class<T> dsComponentClass, @NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)
      Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
      static <T> T registerInjectActivateService​(@NotNull Class<T> dsComponentClass, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,​Object> properties)
      Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
      static <T> void registerInjectActivateService​(T component, @NotNull org.osgi.framework.BundleContext bundleContext)
      Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
      static <T> void registerInjectActivateService​(T component, @NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)
      Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
      static <T> void registerInjectActivateService​(T component, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,​Object> properties)
      Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
      static void sendBundleEvent​(@NotNull org.osgi.framework.BundleContext bundleContext, @NotNull org.osgi.framework.BundleEvent bundleEvent)
      Simulates a bundle event on the given bundle context (that is forwarded to registered bundle listeners).
      static void setConfigForPid​(@NotNull org.osgi.framework.BundleContext bundleContext, @NotNull String pid, @NotNull Object @NotNull ... properties)
      Set configuration via ConfigurationAdmin service in bundle context for component with given pid.
      static void setConfigForPid​(@NotNull org.osgi.framework.BundleContext bundleContext, @NotNull String pid, @Nullable Map<String,​Object> properties)
      Set configuration via ConfigurationAdmin service in bundle context for component with given pid.
      static void shutdown​(@NotNull org.osgi.framework.BundleContext bundleContext)
      Deactivates all bundles registered in the mocked bundle context.
    • Method Detail

      • newBundleContext

        @NotNull
        public static @NotNull org.osgi.framework.BundleContext newBundleContext()
        Returns:
        Mocked BundleContext instance
      • sendBundleEvent

        public static void sendBundleEvent​(@NotNull
                                           @NotNull org.osgi.framework.BundleContext bundleContext,
                                           @NotNull
                                           @NotNull org.osgi.framework.BundleEvent bundleEvent)
        Simulates a bundle event on the given bundle context (that is forwarded to registered bundle listeners).
        Parameters:
        bundleContext - Bundle context
        bundleEvent - Bundle event
      • newComponentContext

        @NotNull
        public static @NotNull org.osgi.service.component.ComponentContext newComponentContext()
        Returns:
        Mocked ComponentContext instance
      • newComponentContext

        @NotNull
        public static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@Nullable
                                                                                               @Nullable Dictionary<String,​Object> properties)
        Parameters:
        properties - Properties
        Returns:
        Mocked ComponentContext instance
      • newComponentContext

        @NotNull
        public static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@Nullable
                                                                                               @Nullable Map<String,​Object> properties)
        Parameters:
        properties - Properties
        Returns:
        Mocked ComponentContext instance
      • newComponentContext

        @NotNull
        public static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@NotNull
                                                                                               @NotNull Object @NotNull ... properties)
        Parameters:
        properties - Properties
        Returns:
        Mocked ComponentContext instance
      • newComponentContext

        @NotNull
        public static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@NotNull
                                                                                               @NotNull org.osgi.framework.BundleContext bundleContext,
                                                                                               @Nullable
                                                                                               @Nullable Dictionary<String,​Object> properties)
        Parameters:
        bundleContext - Bundle context
        properties - Properties
        Returns:
        Mocked ComponentContext instance
      • newComponentContext

        @NotNull
        public static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@NotNull
                                                                                               @NotNull org.osgi.framework.BundleContext bundleContext,
                                                                                               @Nullable
                                                                                               @Nullable Map<String,​Object> properties)
        Parameters:
        bundleContext - Bundle context
        properties - Properties
        Returns:
        Mocked ComponentContext instance
      • newComponentContext

        @NotNull
        public static @NotNull org.osgi.service.component.ComponentContext newComponentContext​(@NotNull
                                                                                               @NotNull org.osgi.framework.BundleContext bundleContext,
                                                                                               @NotNull
                                                                                               @NotNull Object @NotNull ... properties)
        Parameters:
        bundleContext - Bundle context
        properties - Properties
        Returns:
        Mocked ComponentContext instance
      • newLogService

        @NotNull
        public static @NotNull org.osgi.service.log.LogService newLogService​(@NotNull
                                                                             @NotNull Class<?> loggerContext)
        Parameters:
        loggerContext - Context class for logging
        Returns:
        Mocked LogService instance
      • injectServices

        public static boolean injectServices​(@NotNull
                                             @NotNull Object target,
                                             @NotNull
                                             @NotNull org.osgi.framework.BundleContext bundleContext)
        Simulates OSGi DS dependency injection. Injects direct references and multiple references. If a some references could not be injected no error is thrown.
        Parameters:
        target - Service instance
        bundleContext - Bundle context from which services are fetched to inject.
        Returns:
        true if all dependencies could be injected, false if the service has no dependencies.
      • injectServices

        public static boolean injectServices​(@NotNull
                                             @NotNull Object target,
                                             @NotNull
                                             @NotNull org.osgi.framework.BundleContext bundleContext,
                                             @Nullable
                                             @Nullable Map<String,​Object> properties)
        Simulates OSGi DS dependency injection. Injects direct references and multiple references. If a some references could not be injected no error is thrown.
        Parameters:
        target - Service instance
        bundleContext - Bundle context from which services are fetched to inject.
        properties - Service properties (used to resolve dynamic reference properties)
        Returns:
        true if all dependencies could be injected, false if the service has no dependencies.
      • activateInjectServices

        @NotNull
        public static <T> T activateInjectServices​(@NotNull
                                                   @NotNull Class<T> targetClass,
                                                   @NotNull
                                                   @NotNull org.osgi.framework.BundleContext bundleContext)
        Simulates OSGi DS dependency injection and activation. Injects direct references and multiple references. If a some references could not be injected no error is thrown. This method instantiates the service instance and also supports constructor injection.
        Type Parameters:
        T - Target class type
        Parameters:
        targetClass - Component/service class
        bundleContext - Bundle context from which services are fetched to inject.
        Returns:
        Component/service instances with injected services
      • activateInjectServices

        @NotNull
        public static <T> T activateInjectServices​(@NotNull
                                                   @NotNull Class<T> targetClass,
                                                   @NotNull
                                                   @NotNull org.osgi.framework.BundleContext bundleContext,
                                                   @Nullable
                                                   @Nullable Map<String,​Object> properties)
        Simulates OSGi DS dependency injection and activation. Injects direct references and multiple references. If a some references could not be injected no error is thrown. This method instantiates the service instance and also supports constructor injection.
        Type Parameters:
        T - Target class type
        Parameters:
        targetClass - Component/service class
        bundleContext - Bundle context from which services are fetched to inject.
        properties - Service properties (used to resolve dynamic reference properties)
        Returns:
        Component/service instances with injected services
      • activateInjectServices

        @NotNull
        public static <T> T activateInjectServices​(@NotNull
                                                   @NotNull Class<T> targetClass,
                                                   @NotNull
                                                   @NotNull org.osgi.framework.BundleContext bundleContext,
                                                   @NotNull
                                                   @NotNull Object @NotNull ... properties)
        Simulates OSGi DS dependency injection and activation. Injects direct references and multiple references. If a some references could not be injected no error is thrown. This method instantiates the service instance and also supports constructor injection.
        Type Parameters:
        T - Target class type
        Parameters:
        targetClass - Component/service class
        bundleContext - Bundle context from which services are fetched to inject.
        properties - Service properties (used to resolve dynamic reference properties)
        Returns:
        Component/service instances with injected services
      • registerInjectActivateService

        @NotNull
        public static final <T> void registerInjectActivateService​(@NotNull
                                                                   T component,
                                                                   @NotNull
                                                                   @NotNull org.osgi.framework.BundleContext bundleContext)
        Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
        Type Parameters:
        T - DS Component type
        Parameters:
        component - a DS component instance
        bundleContext - Bundle context from which services are fetched to inject and which is used for registering new services
      • registerInjectActivateService

        @NotNull
        public static final <T> void registerInjectActivateService​(@NotNull
                                                                   T component,
                                                                   @NotNull
                                                                   @NotNull org.osgi.framework.BundleContext bundleContext,
                                                                   @Nullable
                                                                   @Nullable Map<String,​Object> properties)
        Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
        Type Parameters:
        T - DS Component type
        Parameters:
        component - a DS component instance
        bundleContext - Bundle context from which services are fetched to inject and which is used for registering new services
        properties - component properties (optional)
      • registerInjectActivateService

        @NotNull
        public static final <T> void registerInjectActivateService​(@NotNull
                                                                   T component,
                                                                   @NotNull
                                                                   @NotNull org.osgi.framework.BundleContext bundleContext,
                                                                   @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:
        component - a DS component instance
        bundleContext - Bundle context from which services are fetched to inject and which is used for registering new services.
        properties - component properties (optional)
      • registerInjectActivateService

        @NotNull
        public static final <T> T registerInjectActivateService​(@NotNull
                                                                @NotNull Class<T> dsComponentClass,
                                                                @NotNull
                                                                @NotNull org.osgi.framework.BundleContext bundleContext)
        Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
        Type Parameters:
        T - DS component type
        Parameters:
        dsComponentClass - DS component class
        bundleContext - Bundle context from which services are fetched to inject and which is used for registering new services
        Returns:
        Registered component instance
      • registerInjectActivateService

        @NotNull
        public static final <T> T registerInjectActivateService​(@NotNull
                                                                @NotNull Class<T> dsComponentClass,
                                                                @NotNull
                                                                @NotNull org.osgi.framework.BundleContext bundleContext,
                                                                @Nullable
                                                                @Nullable Map<String,​Object> properties)
        Injects dependencies, activates and registers a DS component in the mocked OSGi environment.
        Type Parameters:
        T - DS component type
        Parameters:
        dsComponentClass - DS component class
        bundleContext - Bundle context from which services are fetched to inject and which is used for registering new services
        properties - component properties (optional)
        Returns:
        Registered component instance
      • registerInjectActivateService

        @NotNull
        public static final <T> T registerInjectActivateService​(@NotNull
                                                                @NotNull Class<T> dsComponentClass,
                                                                @NotNull
                                                                @NotNull org.osgi.framework.BundleContext bundleContext,
                                                                @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:
        dsComponentClass - DS component class
        bundleContext - Bundle context from which services are fetched to inject and which is used for registering new services
        properties - component properties (optional)
        Returns:
        Registered component instance
      • activate

        public static boolean activate​(@NotNull
                                       @NotNull Object target,
                                       @NotNull
                                       @NotNull org.osgi.framework.BundleContext bundleContext)
        Simulates activation of a DS component instance. Invokes the @Activate annotated method.
        Parameters:
        target - DS component instance
        bundleContext - Bundle context
        Returns:
        true if activation method was called. False if no activate method is defined.
      • activate

        public static boolean activate​(@NotNull
                                       @NotNull Object target,
                                       @NotNull
                                       @NotNull org.osgi.framework.BundleContext bundleContext,
                                       @Nullable
                                       @Nullable Dictionary<String,​Object> properties)
        Simulates activation of a DS component instance. Invokes the @Activate annotated method.
        Parameters:
        target - DS component instance
        bundleContext - Bundle context
        properties - Properties
        Returns:
        true if activation method was called. False if no activate method is defined.
      • activate

        public static boolean activate​(@NotNull
                                       @NotNull Object target,
                                       @NotNull
                                       @NotNull org.osgi.framework.BundleContext bundleContext,
                                       @Nullable
                                       @Nullable Map<String,​Object> properties)
        Simulates activation of a DS component instance. Invokes the @Activate annotated method.
        Parameters:
        target - DS component instance
        bundleContext - Bundle context
        properties - Properties
        Returns:
        true if activation method was called. False if no activate method is defined.
      • activate

        public static boolean activate​(@NotNull
                                       @NotNull Object target,
                                       @NotNull
                                       @NotNull org.osgi.framework.BundleContext bundleContext,
                                       @NotNull
                                       @NotNull Object @NotNull ... properties)
        Simulates activation of a DS component instance. Invokes the @Activate annotated method.
        Parameters:
        target - DS component instance
        bundleContext - Bundle context
        properties - Properties
        Returns:
        true if activation method was called. False if no activate method is defined.
      • deactivate

        public static boolean deactivate​(@NotNull
                                         @NotNull Object target,
                                         @NotNull
                                         @NotNull org.osgi.framework.BundleContext bundleContext)
        Simulates deactivation of a DS component instance. Invokes the @Deactivate annotated method.
        Parameters:
        target - Service instance.
        bundleContext - Bundle context.
        Returns:
        true if deactivation method was called. False if no deactivate method is defined.
      • deactivate

        public static boolean deactivate​(@NotNull
                                         @NotNull Object target,
                                         @NotNull
                                         @NotNull org.osgi.framework.BundleContext bundleContext,
                                         @Nullable
                                         @Nullable Dictionary<String,​Object> properties)
        Simulates deactivation of a DS component instance. Invokes the @Deactivate annotated method.
        Parameters:
        target - DS component instance
        bundleContext - Bundle context
        properties - Properties
        Returns:
        true if deactivation method was called. False if no deactivate method is defined.
      • deactivate

        public static boolean deactivate​(@NotNull
                                         @NotNull Object target,
                                         @NotNull
                                         @NotNull org.osgi.framework.BundleContext bundleContext,
                                         @Nullable
                                         @Nullable Map<String,​Object> properties)
        Simulates deactivation of a DS component instance. Invokes the @Deactivate annotated method.
        Parameters:
        target - DS component instance
        bundleContext - Bundle context
        properties - Properties
        Returns:
        true if deactivation method was called. False if no deactivate method is defined.
      • deactivate

        public static boolean deactivate​(@NotNull
                                         @NotNull Object target,
                                         @NotNull
                                         @NotNull org.osgi.framework.BundleContext bundleContext,
                                         @NotNull
                                         @NotNull Object @NotNull ... properties)
        Simulates deactivation of a DS component instance. Invokes the @Deactivate annotated method.
        Parameters:
        target - DS component instance
        bundleContext - Bundle context
        properties - Properties
        Returns:
        true if deactivation method was called. False if no deactivate method is defined.
      • modified

        public static boolean modified​(@NotNull
                                       @NotNull Object target,
                                       @NotNull
                                       @NotNull org.osgi.framework.BundleContext bundleContext,
                                       @Nullable
                                       @Nullable Dictionary<String,​Object> properties)
        Simulates configuration modification of a DS component instance. Invokes the @Modified annotated method.
        Parameters:
        target - DS component instance
        bundleContext - Bundle context
        properties - Properties
        Returns:
        true if modified method was called. False if no modified method is defined.
      • modified

        public static boolean modified​(@NotNull
                                       @NotNull Object target,
                                       @NotNull
                                       @NotNull org.osgi.framework.BundleContext bundleContext,
                                       @Nullable
                                       @Nullable Map<String,​Object> properties)
        Simulates configuration modification of a DS component instance. Invokes the @Modified annotated method.
        Parameters:
        target - DS component instance
        bundleContext - Bundle context
        properties - Properties
        Returns:
        true if modified method was called. False if no modified method is defined.
      • modified

        public static boolean modified​(@NotNull
                                       @NotNull Object target,
                                       @NotNull
                                       @NotNull org.osgi.framework.BundleContext bundleContext,
                                       @NotNull
                                       @NotNull Object @NotNull ... properties)
        Simulates configuration modification of a DS component instance. Invokes the @Modified annotated method.
        Parameters:
        target - DS component instance
        bundleContext - Bundle context
        properties - Properties
        Returns:
        true if modified method was called. False if no modified method is defined.
      • setConfigForPid

        public static void setConfigForPid​(@NotNull
                                           @NotNull org.osgi.framework.BundleContext bundleContext,
                                           @NotNull
                                           @NotNull String pid,
                                           @Nullable
                                           @Nullable Map<String,​Object> properties)
        Set configuration via ConfigurationAdmin service in bundle context for component with given pid.
        Parameters:
        bundleContext - Bundle context
        pid - PID
        properties - Configuration properties
      • setConfigForPid

        public static void setConfigForPid​(@NotNull
                                           @NotNull org.osgi.framework.BundleContext bundleContext,
                                           @NotNull
                                           @NotNull String pid,
                                           @NotNull
                                           @NotNull Object @NotNull ... properties)
        Set configuration via ConfigurationAdmin service in bundle context for component with given pid.
        Parameters:
        bundleContext - Bundle context
        pid - PID
        properties - Configuration properties
      • shutdown

        public static void shutdown​(@NotNull
                                    @NotNull org.osgi.framework.BundleContext bundleContext)
        Deactivates all bundles registered in the mocked bundle context.
        Parameters:
        bundleContext - Bundle context