Class OsgiContextImpl
java.lang.Object
org.apache.sling.testing.mock.osgi.context.OsgiContextImpl
Defines OSGi context objects and helper methods.
Should not be used directly but via the OsgiContext JUnit rule or extension.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected org.osgi.service.component.ComponentContext
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal @NotNull org.osgi.framework.BundleContext
final @NotNull org.osgi.service.component.ComponentContext
final <ServiceType>
ServiceTypegetService
(@NotNull Class<ServiceType> serviceType) Lookup a single servicefinal <ServiceType>
@NotNull ServiceType @NotNull []getServices
(@NotNull Class<ServiceType> serviceType, @Nullable String filter) Lookup one or several servicesfinal <T> T
registerInjectActivateService
(@NotNull Class<T> componentClass) Injects dependencies, activates and registers a DS component in the mocked OSGi environment.final <T> T
registerInjectActivateService
(@NotNull Class<T> componentClass, @NotNull Object @NotNull ... properties) Injects dependencies, activates and registers a DS component in the mocked OSGi environment.final <T> T
registerInjectActivateService
(@NotNull Class<T> componentClass, @Nullable Map<String, Object> properties) Injects dependencies, activates and registers a DS component in the mocked OSGi environment.final <T> T
registerInjectActivateService
(T component) Injects dependencies, activates and registers a DS component in the mocked OSGi environment.final <T> T
registerInjectActivateService
(T component, @NotNull Object @NotNull ... properties) Injects dependencies, activates and registers a DS component in the mocked OSGi environment.final <T> T
registerInjectActivateService
(T component, @Nullable Map<String, Object> properties) Injects dependencies, activates and registers a DS component in the mocked OSGi environment.final <T> T
registerService
(@Nullable Class<T> serviceClass, T service) Registers a service in the mocked OSGi environment.final <T> T
registerService
(@Nullable Class<T> serviceClass, T service, @NotNull Object @NotNull ... properties) Registers a service in the mocked OSGi environment.final <T> T
registerService
(@Nullable Class<T> serviceClass, T service, @Nullable Map<String, Object> properties) Registers a service in the mocked OSGi environment.final <T> T
registerService
(T service) Registers a service in the mocked OSGi environment.protected void
setUp()
Setup actions before test method executionprotected void
tearDown()
Teardown actions after test method execution
-
Field Details
-
componentContext
protected org.osgi.service.component.ComponentContext componentContext
-
-
Constructor Details
-
OsgiContextImpl
public OsgiContextImpl()
-
-
Method Details
-
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 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 instanceproperties
- 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 instanceproperties
- 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 classproperties
- 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 classproperties
- 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 thefilter
string is not a valid OSGi service filter string.
-