+
+ protected CodecRegistry getCodecRegistry() {
+ return mock(CodecRegistry.class);
+ }
+
+ public static interface BundleContextServiceRegistrationHandler {
+
+ void handleServiceRegistration(Class<?> clazz, Object serviceInstance, Dictionary<String, ?> props);
+
+ }
+
+ private class RegisterServiceAnswer implements Answer<ServiceRegistration<?>> {
+
+ @Override
+ public ServiceRegistration<?> answer(InvocationOnMock invocation) throws Throwable {
+ Object[] args = invocation.getArguments();
+
+ Preconditions.checkArgument(args.length == 3, "Unexpected arguments size (expected 3 was %s)", args.length);
+
+ Object serviceTypeRaw = args[0];
+ Object serviceInstance = args[1];
+ Dictionary<String, ?> props = (Dictionary<String, ?>) args[2];
+
+ if (serviceTypeRaw instanceof Class) {
+ Class<?> serviceType = (Class<?>) serviceTypeRaw;
+ invokeServiceHandler(serviceInstance, serviceType, props);
+
+ } else if(serviceTypeRaw instanceof String[]) {
+ for (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 mockedServiceRegistration;
+ }
+
+ public void invokeServiceHandler(Object serviceInstance, String className, Dictionary<String, ?> props) {
+ try {
+ Class<?> serviceType = Class.forName(className);
+ invokeServiceHandler(serviceInstance, serviceType, props);
+ } catch (ClassNotFoundException e) {
+ throw new IllegalStateException("Not handling service registration of type " + className, e);
+ }
+ }
+
+ private void invokeServiceHandler(Object serviceInstance, Class<?> serviceType, Dictionary<String, ?> props) {
+ BundleContextServiceRegistrationHandler serviceRegistrationHandler = getBundleContextServiceRegistrationHandler(serviceType);
+
+ if (serviceRegistrationHandler != null) {
+ serviceRegistrationHandler.handleServiceRegistration(serviceType, serviceInstance, props);
+ }
+ }
+ }
+
+ /**
+ * Expand inner exception wrapped by JMX
+ *
+ * @param innerObject jmx proxy which will be wrapped and returned
+ */
+ protected <T> T rethrowCause(final T innerObject) {
+
+ Object proxy = Proxy.newProxyInstance(innerObject.getClass().getClassLoader(),
+ innerObject.getClass().getInterfaces(), new InvocationHandler() {
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ try {
+ return method.invoke(innerObject, args);
+ } catch (InvocationTargetException e) {
+ try {
+ throw e.getTargetException();
+ } catch (RuntimeMBeanException e2) {
+ throw e2.getTargetException();
+ }
+ }
+ }
+ }
+ );
+ return (T) proxy;
+ }
+