1 package org.opendaylight.controller.config.manager.impl.osgi;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.fail;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Matchers.anyObject;
8 import static org.mockito.Matchers.anyString;
9 import static org.mockito.Mockito.doAnswer;
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.verifyZeroInteractions;
14 import java.util.Dictionary;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.mockito.invocation.InvocationOnMock;
21 import org.mockito.stubbing.Answer;
22 import org.opendaylight.controller.config.api.DependencyResolver;
23 import org.opendaylight.controller.config.api.DependencyResolverFactory;
24 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
25 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
26 import org.opendaylight.controller.config.spi.Module;
27 import org.opendaylight.controller.config.spi.ModuleFactory;
28 import org.osgi.framework.Bundle;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.BundleEvent;
31 import org.osgi.framework.ServiceRegistration;
33 public class ModuleFactoryBundleTrackerTest {
36 private Bundle bundle;
38 private BundleContext context;
40 private ServiceRegistration<?> reg;
43 public void setUp() throws Exception {
44 MockitoAnnotations.initMocks(this);
45 doAnswer(new Answer<Object>() {
47 public Object answer(final InvocationOnMock invocation) throws Throwable {
48 return getClass().getClassLoader().loadClass((String) invocation.getArguments()[0]);
50 }).when(bundle).loadClass(anyString());
51 doReturn("mockBundle").when(bundle).toString();
52 doReturn(context).when(bundle).getBundleContext();
53 doReturn(reg).when(context).registerService(anyString(), anyObject(), any(Dictionary.class));
57 public void testRegisterFactory() throws Exception {
58 ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
59 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
63 public void testRegisterFactoryInstantiateEx() throws Exception {
65 ModuleFactoryBundleTracker.registerFactory(WrongConstructorTestingFactory.class.getName(), bundle);
66 } catch (Exception e) {
67 verifyZeroInteractions(context);
68 assertNotNull(e.getCause());
69 assertEquals(InstantiationException.class, e.getCause().getClass());
73 fail("Cannot register without proper constructor");
77 public void testRegisterFactoryInstantiateExAccess() throws Exception {
79 ModuleFactoryBundleTracker.registerFactory(NoAccessConstructorTestingFactory.class.getName(), bundle);
80 } catch (Exception e) {
81 verifyZeroInteractions(context);
82 assertNotNull(e.getCause());
83 assertEquals(IllegalAccessException.class, e.getCause().getClass());
87 fail("Cannot register without proper constructor");
91 public void testRegisterFactoryNotExtending() throws Exception {
93 ModuleFactoryBundleTracker.registerFactory(NotExtendingTestingFactory.class.getName(), bundle);
94 } catch (Exception e) {
95 verifyZeroInteractions(context);
99 fail("Cannot register without extend");
103 public void testRegisterFactoryNotExisting() throws Exception {
105 ModuleFactoryBundleTracker.registerFactory("Unknown class", bundle);
106 } catch (Exception e) {
107 verifyZeroInteractions(context);
108 assertNotNull(e.getCause());
109 assertEquals(ClassNotFoundException.class, e.getCause().getClass());
113 fail("Cannot register without extend");
117 private BlankTransactionServiceTracker blankTxTracker;
120 public void testAddingBundle() throws Exception {
121 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
122 doReturn(getClass().getResource("/module-factories/module-factory-ok")).when(bundle).getEntry(anyString());
123 tracker.addingBundle(bundle, mock(BundleEvent.class));
124 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
128 public void testAddingBundleError() throws Exception {
129 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
130 doReturn(getClass().getResource("/module-factories/module-factory-fail")).when(bundle).getEntry(anyString());
132 tracker.addingBundle(bundle, mock(BundleEvent.class));
133 } catch (Exception e) {
134 verifyZeroInteractions(context);
138 fail("Cannot register");
141 static class WrongConstructorTestingFactory extends TestingFactory {
142 WrongConstructorTestingFactory(final String randomParam) {
146 static class NotExtendingTestingFactory {}
148 static class NoAccessConstructorTestingFactory extends TestingFactory {
149 private NoAccessConstructorTestingFactory() {
153 static class TestingFactory implements ModuleFactory {
155 static TestingFactory currentInstance;
158 currentInstance = this;
162 public String getImplementationName() {
167 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
168 throw new UnsupportedOperationException();
172 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
173 throw new UnsupportedOperationException();
177 public boolean isModuleImplementingServiceInterface(final Class<? extends AbstractServiceInterface> serviceInterface) {
178 throw new UnsupportedOperationException();
182 public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
183 throw new UnsupportedOperationException();
187 public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory, final BundleContext bundleContext) {
188 throw new UnsupportedOperationException();