Package org.openhab.core.test.java
Class JavaOSGiTest
java.lang.Object
org.openhab.core.test.java.JavaTest
org.openhab.core.test.java.JavaOSGiTest
JavaOSGiTest
is an abstract base class for OSGi based tests. It provides convenience methods to register and
unregister mocks as OSGi services. All services, which are registered through the
registerService(java.lang.Object)
methods, are unregistered automatically in the tear down of the test.- Author:
- Markus Rathgeb - Initial contribution
-
Nested Class Summary
Nested classes/interfaces inherited from class org.openhab.core.test.java.JavaTest
JavaTest.LogLevel
-
Field Summary
Fields inherited from class org.openhab.core.test.java.JavaTest
DFL_SLEEP_TIME, DFL_TIMEOUT
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
protected org.osgi.framework.BundleContext
Get the OSGiBundleContext
protected String
getInterfaceName
(Object service) Returns the interface name for a given service object by choosing the first interface.protected <T> @Nullable T
getService
(Class<T> clazz) Get an OSGi service for the given class.protected <T,
I extends T>
@Nullable IgetService
(Class<T> clazz, Class<I> implementationClass) Get the OSGi service for the given service class and the given implementation class.protected <T> @Nullable T
getService
(Class<T> clazz, Predicate<org.osgi.framework.ServiceReference<T>> filter) Get an OSGi service for the given class and the given filter.protected <T,
I extends T>
List<I>getServices
(Class<T> clazz, Class<I> implementationClass) Get all OSGi services for the given service class and the given implementation class.protected <T> List<T>
getServices
(Class<T> clazz, Predicate<org.osgi.framework.ServiceReference<T>> filter) Get all OSGi service for the given class and the given filter.protected org.osgi.framework.ServiceRegistration<?>
registerService
(Object service) Register the given object as OSGi service.protected org.osgi.framework.ServiceRegistration<?>
registerService
(Object service, String interfaceName) Register the given object as OSGi service.protected org.osgi.framework.ServiceRegistration<?>
registerService
(Object service, String[] interfaceNames, Dictionary<String, ?> properties) Register the given object as OSGi service.protected org.osgi.framework.ServiceRegistration<?>
registerService
(Object service, String interfaceName, @Nullable Dictionary<String, ?> properties) Register the given object as OSGi service.protected org.osgi.framework.ServiceRegistration<?>
registerService
(Object service, Dictionary<String, ?> properties) Register the given object as OSGi service.protected void
Registers a volatile storage service.void
protected @Nullable org.osgi.framework.ServiceRegistration<?>
unregisterService
(Object service) Unregister an OSGi service by the given object, that was registered before.protected @Nullable org.osgi.framework.ServiceRegistration<?>
unregisterService
(String interfaceName) Unregister an OSGi service by the given object, that was registered before.Methods inherited from class org.openhab.core.test.java.JavaTest
assertLogMessage, assertNoLogMessage, giveNull, setupInterceptedLogger, stopInterceptedLogger, waitFor, waitFor, waitForAssert, waitForAssert, waitForAssert, waitForAssert, waitForAssert, waitForAssert
-
Field Details
-
bundleContext
@NonNullByDefault({}) protected org.osgi.framework.BundleContext bundleContext
-
-
Constructor Details
-
JavaOSGiTest
public JavaOSGiTest()
-
-
Method Details
-
bindBundleContext
@BeforeEach public void bindBundleContext() -
getService
Get an OSGi service for the given class.- Parameters:
clazz
- class under which the OSGi service is registered- Returns:
- OSGi service or null if no service can be found for the given class
-
getServices
protected <T> List<T> getServices(Class<T> clazz, Predicate<org.osgi.framework.ServiceReference<T>> filter) Get all OSGi service for the given class and the given filter.- Parameters:
clazz
- class under which the OSGi service is registeredfilter
- Predicate to apply to found ServiceReferences- Returns:
- List of OSGi services or empty List if no service can be found for the given class and filter
-
getService
protected <T> @Nullable T getService(Class<T> clazz, Predicate<org.osgi.framework.ServiceReference<T>> filter) Get an OSGi service for the given class and the given filter.- Parameters:
clazz
- class under which the OSGi service is registeredfilter
- Predicate to apply to found ServiceReferences- Returns:
- OSGi service or null if no service can be found for the given class
- Throws:
AssertionError
- if more than one instance of the service is found
-
getService
Get the OSGi service for the given service class and the given implementation class.- Parameters:
clazz
- class under which the OSGi service is registeredimplementationClass
- the implementation class- Returns:
- OSGi service or null if no service can be found for the given classes
- Throws:
AssertionError
- if more than one instance of the service is found
-
getServices
Get all OSGi services for the given service class and the given implementation class.- Parameters:
clazz
- class under which the OSGi services are registeredimplementationClass
- the implementation class of the services- Returns:
- List of OSGi service or empty List if no matching services can be found for the given classes
-
registerService
Register the given object as OSGi service.The first interface is used as OSGi service interface name.
- Parameters:
service
- service to be registered- Returns:
- service registration object
-
registerService
protected org.osgi.framework.ServiceRegistration<?> registerService(Object service, Dictionary<String, ?> properties) Register the given object as OSGi service. The first interface is used as OSGi service interface name.- Parameters:
service
- service to be registeredproperties
- OSGi service properties- Returns:
- service registration object
-
registerService
protected org.osgi.framework.ServiceRegistration<?> registerService(Object service, String interfaceName) Register the given object as OSGi service.The given interface name is used as OSGi service interface name.
- Parameters:
service
- service to be registeredinterfaceName
- interface name of the OSGi service- Returns:
- service registration object
-
registerService
protected org.osgi.framework.ServiceRegistration<?> registerService(Object service, String interfaceName, @Nullable Dictionary<String, ?> properties) Register the given object as OSGi service.The given interface name is used as OSGi service interface name.
- Parameters:
service
- service to be registeredinterfaceName
- interface name of the OSGi serviceproperties
- OSGi service properties- Returns:
- service registration object
-
registerService
protected org.osgi.framework.ServiceRegistration<?> registerService(Object service, String[] interfaceNames, Dictionary<String, ?> properties) Register the given object as OSGi service.The given interface names are used as OSGi service interface name.
- Parameters:
service
- service to be registeredinterfaceNames
- interface names of the OSGi serviceproperties
- OSGi service properties- Returns:
- service registration object
-
unregisterService
Unregister an OSGi service by the given object, that was registered before.The interface name is taken from the first interface of the service object.
- Parameters:
service
- the service- Returns:
- the service registration that was unregistered or null if no service could be found
-
unregisterService
protected @Nullable org.osgi.framework.ServiceRegistration<?> unregisterService(String interfaceName) Unregister an OSGi service by the given object, that was registered before.- Parameters:
interfaceName
- the interface name of the service- Returns:
- the first service registration that was unregistered or null if no service could be found
-
getInterfaceName
Returns the interface name for a given service object by choosing the first interface.- Parameters:
service
- service object- Returns:
- name of the first interface if interfaces are implemented
- Throws:
IllegalArgumentException
- if no interface is implemented
-
getBundleContext
protected org.osgi.framework.BundleContext getBundleContext()Get the OSGiBundleContext
- Returns:
- the bundle context
-
registerVolatileStorageService
protected void registerVolatileStorageService()Registers a volatile storage service. -
unregisterMocks
@AfterEach public void unregisterMocks()
-