+
+ protected BindingRuntimeContext getBindingRuntimeContext() {
+ return mock(BindingRuntimeContext.class);
+ }
+
+ public interface BundleContextServiceRegistrationHandler {
+ void handleServiceRegistration(Class<?> clazz, Object serviceInstance, Dictionary<String, ?> props);
+ }
+
+ 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);
+ }
+ }
+ }
+
+ /**
+ * Expand inner exception wrapped by JMX
+ *
+ * @param innerObject jmx proxy which will be wrapped and returned
+ */
+ protected <T> T rethrowCause(final T innerObject) {
+ @SuppressWarnings("unchecked")
+ final T proxy = (T)Proxy.newProxyInstance(innerObject.getClass().getClassLoader(),
+ innerObject.getClass().getInterfaces(), new InvocationHandler() {
+ @Override
+ public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+ try {
+ return method.invoke(innerObject, args);
+ } catch (final InvocationTargetException e) {
+ try {
+ throw e.getTargetException();
+ } catch (final RuntimeMBeanException e2) {
+ throw e2.getTargetException();
+ }
+ }
+ }
+ }
+ );
+ return proxy;
+ }
+
+ /**
+ * removes contents of the directory
+ * @param dir to be cleaned
+ * @throws IOException
+ */
+ protected void cleanDirectory(final File dir) throws IOException {
+ if (!dir.isDirectory()) {
+ throw new IllegalStateException("dir must be a directory");
+ }
+
+ final File[] files = dir.listFiles();
+ if (files == null) {
+ throw new IOException("Failed to list contents of " + dir);
+ }
+
+ for (final File file : files) {
+ if (file.isDirectory()) {
+ cleanDirectory(dir);
+ }
+ file.delete();
+ }
+ }