Package org.apache.sling.testing.mock.osgi.context

# 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.ComponentContext`
`componentContext`

## Constructor Summary

Constructors
Constructor
Description
`OsgiContextImpl()`

## Method Summary

Modifier and Type
Method
Description
`final @NotNull org.osgi.framework.BundleContext`
`bundleContext()`
`final @NotNull org.osgi.service.component.ComponentContext`
`componentContext()`
`final <ServiceType>
ServiceType`
`getService(@NotNull Class<ServiceType> serviceType)`
Lookup a single service
`final <ServiceType>
ServiceType @NotNull []`
`getServices(@NotNull Class<ServiceType> serviceType, @Nullable String filter)`
Lookup one or several services
`final <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 execution
`protected void`
`tearDown()`
Teardown actions after test method execution

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## 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 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> 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.