Class OsgiContextImpl
- java.lang.Object
-
- org.apache.sling.testing.mock.osgi.context.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 Summary
Fields Modifier and Type Field Description protected org.osgi.service.component.ComponentContextcomponentContext
-
Constructor Summary
Constructors Constructor Description OsgiContextImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description @NotNull org.osgi.framework.BundleContextbundleContext()@NotNull org.osgi.service.component.ComponentContextcomponentContext()<ServiceType>
ServiceTypegetService(@NotNull Class<ServiceType> serviceType)Lookup a single service<ServiceType>
@NotNull ServiceType @NotNull []getServices(@NotNull Class<ServiceType> serviceType, @Nullable String filter)Lookup one or several services<T> TregisterInjectActivateService(@NotNull Class<T> serviceClass)Injects dependencies, activates and registers a service in the mocked OSGi environment.<T> TregisterInjectActivateService(@NotNull Class<T> serviceClass, @NotNull Object @NotNull ... properties)Injects dependencies, activates and registers a service in the mocked OSGi environment.<T> TregisterInjectActivateService(@NotNull Class<T> serviceClass, @Nullable Map<String,Object> properties)Injects dependencies, activates and registers a service in the mocked OSGi environment.<T> TregisterInjectActivateService(T service)Injects dependencies, activates and registers a service in the mocked OSGi environment.<T> TregisterInjectActivateService(T service, @NotNull Object @NotNull ... properties)Injects dependencies, activates and registers a service in the mocked OSGi environment.<T> TregisterInjectActivateService(T service, @Nullable Map<String,Object> properties)Injects dependencies, activates and registers a service in the mocked OSGi environment.<T> TregisterService(@Nullable Class<T> serviceClass, T service)Registers a service in the mocked OSGi environment.<T> TregisterService(@Nullable Class<T> serviceClass, T service, @NotNull Object @NotNull ... properties)Registers a service in the mocked OSGi environment.<T> TregisterService(@Nullable Class<T> serviceClass, T service, @Nullable Map<String,Object> properties)Registers a service in the mocked OSGi environment.<T> TregisterService(T service)Registers a service in the mocked OSGi environment.protected voidsetUp()Setup actions before test method executionprotected voidtearDown()Teardown actions after test method execution
-
-
-
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 classservice- 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 classservice- Service instanceproperties- 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 classservice- Service instanceproperties- Service properties (optional)- Returns:
- Registered service instance
-
registerInjectActivateService
@NotNull public final <T> T registerInjectActivateService(@NotNull T service)Injects dependencies, activates and registers a service in the mocked OSGi environment.- Type Parameters:
T- Service type- Parameters:
service- Service instance- Returns:
- Registered service instance
-
registerInjectActivateService
@NotNull public final <T> T registerInjectActivateService(@NotNull T service, @Nullable @Nullable Map<String,Object> properties)Injects dependencies, activates and registers a service in the mocked OSGi environment.- Type Parameters:
T- Service type- Parameters:
service- Service instanceproperties- Service properties (optional)- Returns:
- Registered service instance
-
registerInjectActivateService
@NotNull public final <T> T registerInjectActivateService(@NotNull T service, @NotNull @NotNull Object @NotNull ... properties)Injects dependencies, activates and registers a service in the mocked OSGi environment. Construction injection for OSGi services is supported.- Type Parameters:
T- Service type- Parameters:
service- Service classproperties- Service properties (optional)- Returns:
- Registered service instance
-
registerInjectActivateService
@NotNull public final <T> T registerInjectActivateService(@NotNull @NotNull Class<T> serviceClass)Injects dependencies, activates and registers a service in the mocked OSGi environment. Construction injection for OSGi services is supported.- Type Parameters:
T- Service type- Parameters:
serviceClass- Service class- Returns:
- Registered service instance
-
registerInjectActivateService
@NotNull public final <T> T registerInjectActivateService(@NotNull @NotNull Class<T> serviceClass, @Nullable @Nullable Map<String,Object> properties)Injects dependencies, activates and registers a service in the mocked OSGi environment. Construction injection for OSGi services is supported.- Type Parameters:
T- Service type- Parameters:
serviceClass- Service classproperties- Service properties (optional)- Returns:
- Registered service instance
-
registerInjectActivateService
@NotNull public final <T> T registerInjectActivateService(@NotNull @NotNull Class<T> serviceClass, @NotNull @NotNull Object @NotNull ... properties)Injects dependencies, activates and registers a service in the mocked OSGi environment.- Type Parameters:
T- Service type- Parameters:
serviceClass- Service instanceproperties- Service properties (optional)- Returns:
- Registered service 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 thefilterstring is not a valid OSGi service filter string.
-
-