X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2FAbstractConfigTest.java;h=64ce14e8f1733b8ab28465a9edd4a191141a6a09;hp=81b0921660539b822b4c0f3216b704f052500210;hb=723d215fdf7ebb98d3fa3cbd05e524b9b413c955;hpb=1c8c991d5e3c6f2cb771dc1785ecbcb08c035999 diff --git a/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/AbstractConfigTest.java b/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/AbstractConfigTest.java index 81b0921660..64ce14e8f1 100644 --- a/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/AbstractConfigTest.java +++ b/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/AbstractConfigTest.java @@ -8,7 +8,6 @@ package org.opendaylight.controller.config.manager.impl; import com.google.common.base.Preconditions; -import com.google.common.collect.Maps; import junit.framework.Assert; import org.junit.After; import org.mockito.Matchers; @@ -26,19 +25,25 @@ import org.opendaylight.controller.config.util.ConfigRegistryJMXClient; import org.opendaylight.controller.config.util.ConfigTransactionJMXClient; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.management.InstanceAlreadyExistsException; import javax.management.MBeanServer; import javax.management.ObjectName; +import javax.management.RuntimeMBeanException; import java.io.Closeable; import java.io.InputStream; import java.lang.management.ManagementFactory; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.util.Dictionary; import java.util.List; -import java.util.Map; import java.util.Set; import static org.junit.Assert.assertEquals; @@ -66,8 +71,16 @@ public abstract class AbstractConfigTest extends protected BundleContext mockedContext = mock(BundleContext.class); protected ServiceRegistration mockedServiceRegistration; - protected Map getBundleContextServiceRegistrationHandlers() { - return Maps.newHashMap(); + private static final Logger logger = LoggerFactory.getLogger(AbstractConfigTest.class); + + // Default handler for OSGi service registration + private static final BundleContextServiceRegistrationHandler noopServiceRegHandler = new BundleContextServiceRegistrationHandler() { + @Override + public void handleServiceRegistration(Object serviceInstance) {} + }; + + protected BundleContextServiceRegistrationHandler getBundleContextServiceRegistrationHandler(Class serviceType) { + return noopServiceRegHandler; } // this method should be called in @Before @@ -166,9 +179,8 @@ public abstract class AbstractConfigTest extends protected ObjectName createTestConfigBean( ConfigTransactionJMXClient transaction, String implementationName, String name) throws InstanceAlreadyExistsException { - ObjectName nameCreated = transaction.createModule(implementationName, + return transaction.createModule(implementationName, name); - return nameCreated; } protected void assertBeanCount(int i, String configMXBeanName) { @@ -204,26 +216,68 @@ public abstract class AbstractConfigTest extends } private class RegisterServiceAnswer implements Answer { + @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); - Preconditions.checkArgument(args.length == 3); + Preconditions.checkArgument(args.length == 3, "Unexpected arguments size (expected 3 was %s)", args.length); - Preconditions.checkArgument(args[0] instanceof Class); - Class serviceType = (Class) args[0]; + Object serviceTypeRaw = args[0]; Object serviceInstance = args[1]; - BundleContextServiceRegistrationHandler serviceRegistrationHandler = getBundleContextServiceRegistrationHandlers() - .get(serviceType); + if (serviceTypeRaw instanceof Class) { + Class serviceType = (Class) serviceTypeRaw; + invokeServiceHandler(serviceInstance, serviceType); + + } else if(serviceTypeRaw instanceof String[]) { + for (String className : (String[]) serviceTypeRaw) { + try { + Class serviceType = Class.forName(className); + invokeServiceHandler(serviceInstance, serviceType); + } catch (ClassNotFoundException e) { + logger.warn("Not handling service registration of type {} ", className, e); + } + } + + } else + logger.debug("Not handling service registration of type {}, Unknown type", serviceTypeRaw); + + return mockedServiceRegistration; + } - Preconditions.checkArgument(serviceType.isAssignableFrom(serviceInstance.getClass())); + private void invokeServiceHandler(Object serviceInstance, Class serviceType) { + BundleContextServiceRegistrationHandler serviceRegistrationHandler = getBundleContextServiceRegistrationHandler(serviceType); if (serviceRegistrationHandler != null) { serviceRegistrationHandler.handleServiceRegistration(serviceType.cast(serviceInstance)); } - - return mockedServiceRegistration; } } + + /** + * Expand inner exception wrapped by JMX + * + * @param innerObject jmx proxy which will be wrapped and returned + */ + protected 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; + } + }