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;
15 import java.util.Dictionary;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.mockito.invocation.InvocationOnMock;
22 import org.mockito.stubbing.Answer;
23 import org.opendaylight.controller.config.api.DependencyResolver;
24 import org.opendaylight.controller.config.api.DependencyResolverFactory;
25 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
26 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
27 import org.opendaylight.controller.config.spi.Module;
28 import org.opendaylight.controller.config.spi.ModuleFactory;
29 import org.osgi.framework.Bundle;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.BundleEvent;
32 import org.osgi.framework.ServiceRegistration;
34 public class ModuleFactoryBundleTrackerTest {
37 private Bundle bundle;
39 private BundleContext context;
41 private ServiceRegistration<?> reg;
44 public void setUp() throws Exception {
45 MockitoAnnotations.initMocks(this);
46 doAnswer(new Answer<Object>() {
48 public Object answer(final InvocationOnMock invocation) throws Throwable {
49 return getClass().getClassLoader().loadClass((String) invocation.getArguments()[0]);
51 }).when(bundle).loadClass(anyString());
52 doReturn("mockBundle").when(bundle).toString();
53 doReturn(context).when(bundle).getBundleContext();
54 doReturn(reg).when(context).registerService(anyString(), anyObject(), any(Dictionary.class));
58 public void testRegisterFactory() throws Exception {
59 ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
60 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
64 public void testRegisterFactoryInstantiateEx() throws Exception {
66 ModuleFactoryBundleTracker.registerFactory(WrongConstructorTestingFactory.class.getName(), bundle);
67 } catch (Exception e) {
68 verifyZeroInteractions(context);
69 assertNotNull(e.getCause());
70 assertEquals(InstantiationException.class, e.getCause().getClass());
74 fail("Cannot register without proper constructor");
78 public void testRegisterFactoryInstantiateExAccess() throws Exception {
80 ModuleFactoryBundleTracker.registerFactory(NoAccessConstructorTestingFactory.class.getName(), bundle);
81 } catch (Exception e) {
82 verifyZeroInteractions(context);
83 assertNotNull(e.getCause());
84 assertEquals(IllegalAccessException.class, e.getCause().getClass());
88 fail("Cannot register without proper constructor");
92 public void testRegisterFactoryNotExtending() throws Exception {
94 ModuleFactoryBundleTracker.registerFactory(NotExtendingTestingFactory.class.getName(), bundle);
95 } catch (Exception e) {
96 verifyZeroInteractions(context);
100 fail("Cannot register without extend");
104 public void testRegisterFactoryNotExisting() throws Exception {
106 ModuleFactoryBundleTracker.registerFactory("Unknown class", bundle);
107 } catch (Exception e) {
108 verifyZeroInteractions(context);
109 assertNotNull(e.getCause());
110 assertEquals(ClassNotFoundException.class, e.getCause().getClass());
114 fail("Cannot register without extend");
118 private BlankTransactionServiceTracker blankTxTracker;
121 public void testAddingBundle() throws Exception {
122 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
123 doReturn(getClass().getResource("/module-factories/module-factory-ok")).when(bundle).getEntry(anyString());
124 tracker.addingBundle(bundle, mock(BundleEvent.class));
125 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
129 public void testAddingBundleError() throws Exception {
130 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
131 doReturn(getClass().getResource("/module-factories/module-factory-fail")).when(bundle).getEntry(anyString());
133 tracker.addingBundle(bundle, mock(BundleEvent.class));
134 } catch (Exception e) {
135 verifyZeroInteractions(context);
139 fail("Cannot register");
142 static class WrongConstructorTestingFactory extends TestingFactory {
143 WrongConstructorTestingFactory(final String randomParam) {
147 static class NotExtendingTestingFactory {}
149 static class NoAccessConstructorTestingFactory extends TestingFactory {
150 private NoAccessConstructorTestingFactory() {
154 static class TestingFactory implements ModuleFactory {
156 static TestingFactory currentInstance;
159 currentInstance = this;
163 public String getImplementationName() {
168 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
169 throw new UnsupportedOperationException();
173 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
174 throw new UnsupportedOperationException();
178 public boolean isModuleImplementingServiceInterface(final Class<? extends AbstractServiceInterface> serviceInterface) {
179 throw new UnsupportedOperationException();
183 public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
184 throw new UnsupportedOperationException();
188 public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory, final BundleContext bundleContext) {
189 throw new UnsupportedOperationException();