+ private class RegisterServiceAnswer implements Answer<ServiceRegistration<?>> {
+ @Override
+ public ServiceRegistration<?> answer(final InvocationOnMock invocation) throws Throwable {
+ final Object[] args = invocation.getArguments();
+
+ Preconditions.checkArgument(args.length == 3, "Unexpected arguments size (expected 3 was %s)", args.length);
+
+ final Object serviceTypeRaw = args[0];
+ final Object serviceInstance = args[1];
+ @SuppressWarnings("unchecked")
+ final Dictionary<String, ?> props = (Dictionary<String, ?>) args[2];
+
+ if (serviceTypeRaw instanceof Class) {
+ final Class<?> serviceType = (Class<?>) serviceTypeRaw;
+ invokeServiceHandler(serviceInstance, serviceType, props);
+ } else if (serviceTypeRaw instanceof String[]) {
+ for (final String className : (String[]) serviceTypeRaw) {
+ invokeServiceHandler(serviceInstance, className, props);
+ }
+ } else if (serviceTypeRaw instanceof String) {
+ invokeServiceHandler(serviceInstance, (String) serviceTypeRaw, props);
+ } else {
+ throw new IllegalStateException(
+ "Not handling service registration of type, Unknown type" + serviceTypeRaw);
+ }
+
+ return AbstractConfigTest.this.mockedServiceRegistration;
+ }
+
+ public void invokeServiceHandler(final Object serviceInstance, final String className,
+ final Dictionary<String, ?> props) {
+ try {
+ final Class<?> serviceType = Class.forName(className);
+ invokeServiceHandler(serviceInstance, serviceType, props);
+ } catch (final ClassNotFoundException e) {
+ throw new IllegalStateException("Not handling service registration of type " + className, e);
+ }
+ }
+
+ private void invokeServiceHandler(final Object serviceInstance, final Class<?> serviceType,
+ final Dictionary<String, ?> props) {
+ final BundleContextServiceRegistrationHandler serviceRegistrationHandler =
+ getBundleContextServiceRegistrationHandler(serviceType);
+
+ if (serviceRegistrationHandler != null) {
+ serviceRegistrationHandler.handleServiceRegistration(serviceType, serviceInstance, props);
+ }
+ }