Class MockOsgi
- java.lang.Object
-
- org.apache.sling.testing.mock.osgi.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 booleanactivate(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext)Simulate activation of service instance.static booleanactivate(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)Simulate activation of service instance.static booleanactivate(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Dictionary<String,Object> properties)Simulate activation of service instance.static booleanactivate(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,Object> properties)Simulate activation of service instance.static <T> TactivateInjectServices(@NotNull Class<T> targetClass, @NotNull org.osgi.framework.BundleContext bundleContext)Simulate OSGi DS dependency injection and activation.static <T> TactivateInjectServices(@NotNull Class<T> targetClass, @NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)Simulate OSGi DS dependency injection and activation.static <T> TactivateInjectServices(@NotNull Class<T> targetClass, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,Object> properties)Simulate OSGi DS dependency injection and activation.static @NotNull ComponentContextBuildercomponentContext()static booleandeactivate(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext)Simulate deactivation of service instance.static booleandeactivate(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)Simulate activation of service instance.static booleandeactivate(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Dictionary<String,Object> properties)Simulate deactivation of service instance.static booleandeactivate(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,Object> properties)Simulate activation of service instance.static booleaninjectServices(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext)Simulate OSGi DS dependency injection.static booleaninjectServices(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,Object> properties)Simulate OSGi DS dependency injection.static booleanmodified(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)Simulate configuration modification of service instance.static booleanmodified(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Dictionary<String,Object> properties)Simulate configuration modification of service instance.static booleanmodified(@NotNull Object target, @NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,Object> properties)Simulate configuration modification of service instance.static @NotNull org.osgi.framework.BundleContextnewBundleContext()static @NotNull org.osgi.service.component.ComponentContextnewComponentContext()static @NotNull org.osgi.service.component.ComponentContextnewComponentContext(@NotNull Object @NotNull ... properties)static @NotNull org.osgi.service.component.ComponentContextnewComponentContext(@NotNull org.osgi.framework.BundleContext bundleContext, @NotNull Object @NotNull ... properties)static @NotNull org.osgi.service.component.ComponentContextnewComponentContext(@NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Dictionary<String,Object> properties)static @NotNull org.osgi.service.component.ComponentContextnewComponentContext(@NotNull org.osgi.framework.BundleContext bundleContext, @Nullable Map<String,Object> properties)static @NotNull org.osgi.service.component.ComponentContextnewComponentContext(@Nullable Dictionary<String,Object> properties)static @NotNull org.osgi.service.component.ComponentContextnewComponentContext(@Nullable Map<String,Object> properties)static @NotNull org.osgi.service.log.LogServicenewLogService(@NotNull Class<?> loggerContext)static voidsendBundleEvent(@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 voidsetConfigForPid(@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 voidsetConfigForPid(@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 voidshutdown(@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
BundleContextinstance
-
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 contextbundleEvent- Bundle event
-
newComponentContext
@NotNull public static @NotNull org.osgi.service.component.ComponentContext newComponentContext()
- Returns:
- Mocked
ComponentContextinstance
-
newComponentContext
@NotNull public static @NotNull org.osgi.service.component.ComponentContext newComponentContext(@Nullable @Nullable Dictionary<String,Object> properties)- Parameters:
properties- Properties- Returns:
- Mocked
ComponentContextinstance
-
newComponentContext
@NotNull public static @NotNull org.osgi.service.component.ComponentContext newComponentContext(@Nullable @Nullable Map<String,Object> properties)- Parameters:
properties- Properties- Returns:
- Mocked
ComponentContextinstance
-
newComponentContext
@NotNull public static @NotNull org.osgi.service.component.ComponentContext newComponentContext(@NotNull @NotNull Object @NotNull ... properties)- Parameters:
properties- Properties- Returns:
- Mocked
ComponentContextinstance
-
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 contextproperties- Properties- Returns:
- Mocked
ComponentContextinstance
-
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 contextproperties- Properties- Returns:
- Mocked
ComponentContextinstance
-
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 contextproperties- Properties- Returns:
- Mocked
ComponentContextinstance
-
componentContext
@NotNull public static @NotNull ComponentContextBuilder componentContext()
- Returns:
ComponentContextBuilderto build a mockedComponentContext
-
newLogService
@NotNull public static @NotNull org.osgi.service.log.LogService newLogService(@NotNull @NotNull Class<?> loggerContext)- Parameters:
loggerContext- Context class for logging- Returns:
- Mocked
LogServiceinstance
-
injectServices
public static boolean injectServices(@NotNull @NotNull Object target, @NotNull @NotNull org.osgi.framework.BundleContext bundleContext)Simulate 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 instancebundleContext- 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)Simulate 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 instancebundleContext- 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)Simulate 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 classbundleContext- 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)Simulate 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 classbundleContext- 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)Simulate 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 classbundleContext- 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
-
activate
public static boolean activate(@NotNull @NotNull Object target, @NotNull @NotNull org.osgi.framework.BundleContext bundleContext)Simulate activation of service instance. Invokes the @Activate annotated method.- Parameters:
target- Service 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)Simulate activation of service instance. Invokes the @Activate annotated method.- Parameters:
target- Service instance.bundleContext- Bundle contextproperties- 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)Simulate activation of service instance. Invokes the @Activate annotated method.- Parameters:
target- Service instance.bundleContext- Bundle contextproperties- 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)Simulate activation of service instance. Invokes the @Activate annotated method.- Parameters:
target- Service instance.bundleContext- Bundle contextproperties- 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)Simulate deactivation of service 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)Simulate deactivation of service instance. Invokes the @Deactivate annotated method.- Parameters:
target- Service instance.bundleContext- Bundle contextproperties- 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)Simulate activation of service instance. Invokes the @Deactivate annotated method.- Parameters:
target- Service instance.bundleContext- Bundle contextproperties- 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)Simulate activation of service instance. Invokes the @Deactivate annotated method.- Parameters:
target- Service instance.bundleContext- Bundle contextproperties- 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)Simulate configuration modification of service instance. Invokes the @Modified annotated method.- Parameters:
target- Service instance.bundleContext- Bundle contextproperties- 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)Simulate configuration modification of service instance. Invokes the @Modified annotated method.- Parameters:
target- Service instance.bundleContext- Bundle contextproperties- 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)Simulate configuration modification of service instance. Invokes the @Modified annotated method.- Parameters:
target- Service instance.bundleContext- Bundle contextproperties- 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 contextpid- PIDproperties- 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 contextpid- PIDproperties- 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
-
-