1 package org.opendaylight.controller.config.manager.impl.osgi;
3 import static junit.framework.Assert.assertEquals;
4 import static junit.framework.Assert.assertNotNull;
5 import static junit.framework.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;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.mockito.invocation.InvocationOnMock;
23 import org.mockito.stubbing.Answer;
24 import org.opendaylight.controller.config.api.DependencyResolver;
25 import org.opendaylight.controller.config.api.DependencyResolverFactory;
26 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
27 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
28 import org.opendaylight.controller.config.spi.Module;
29 import org.opendaylight.controller.config.spi.ModuleFactory;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.BundleEvent;
33 import org.osgi.framework.ServiceRegistration;
35 public class ModuleFactoryBundleTrackerTest {
38 private Bundle bundle;
40 private BundleContext context;
42 private ServiceRegistration<?> reg;
45 public void setUp() throws Exception {
46 MockitoAnnotations.initMocks(this);
47 doAnswer(new Answer() {
49 public Object answer(final InvocationOnMock invocation) throws Throwable {
50 return getClass().getClassLoader().loadClass((String) invocation.getArguments()[0]);
52 }).when(bundle).loadClass(anyString());
53 doReturn("mockBundle").when(bundle).toString();
54 doReturn(context).when(bundle).getBundleContext();
55 doReturn(reg).when(context).registerService(anyString(), anyObject(), any(Dictionary.class));
59 public void testRegisterFactory() throws Exception {
60 ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
61 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
65 public void testRegisterFactoryInstantiateEx() throws Exception {
67 ModuleFactoryBundleTracker.registerFactory(WrongConstructorTestingFactory.class.getName(), bundle);
68 } catch (Exception e) {
69 verifyZeroInteractions(context);
70 assertNotNull(e.getCause());
71 assertEquals(InstantiationException.class, e.getCause().getClass());
75 fail("Cannot register without proper constructor");
79 public void testRegisterFactoryInstantiateExAccess() throws Exception {
81 ModuleFactoryBundleTracker.registerFactory(NoAccessConstructorTestingFactory.class.getName(), bundle);
82 } catch (Exception e) {
83 verifyZeroInteractions(context);
84 assertNotNull(e.getCause());
85 assertEquals(IllegalAccessException.class, e.getCause().getClass());
89 fail("Cannot register without proper constructor");
93 public void testRegisterFactoryNotExtending() throws Exception {
95 ModuleFactoryBundleTracker.registerFactory(NotExtendingTestingFactory.class.getName(), bundle);
96 } catch (Exception e) {
97 verifyZeroInteractions(context);
101 fail("Cannot register without extend");
105 public void testRegisterFactoryNotExisting() throws Exception {
107 ModuleFactoryBundleTracker.registerFactory("Unknown class", bundle);
108 } catch (Exception e) {
109 verifyZeroInteractions(context);
110 assertNotNull(e.getCause());
111 assertEquals(ClassNotFoundException.class, e.getCause().getClass());
115 fail("Cannot register without extend");
119 private BlankTransactionServiceTracker blankTxTracker;
122 public void testAddingBundle() throws Exception {
123 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
124 doReturn(getClass().getResource("/module-factories/module-factory-ok")).when(bundle).getEntry(anyString());
125 tracker.addingBundle(bundle, mock(BundleEvent.class));
126 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
130 public void testAddingBundleError() throws Exception {
131 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
132 doReturn(getClass().getResource("/module-factories/module-factory-fail")).when(bundle).getEntry(anyString());
134 tracker.addingBundle(bundle, mock(BundleEvent.class));
135 } catch (Exception e) {
136 verifyZeroInteractions(context);
140 fail("Cannot register");
143 static class WrongConstructorTestingFactory extends TestingFactory {
144 WrongConstructorTestingFactory(String randomParam) {
148 static class NotExtendingTestingFactory {}
150 static class NoAccessConstructorTestingFactory extends TestingFactory {
151 private NoAccessConstructorTestingFactory() {
155 static class TestingFactory implements ModuleFactory {
157 static TestingFactory currentInstance;
160 currentInstance = this;
164 public String getImplementationName() {
169 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
170 throw new UnsupportedOperationException();
174 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
175 throw new UnsupportedOperationException();
179 public boolean isModuleImplementingServiceInterface(final Class<? extends AbstractServiceInterface> serviceInterface) {
180 throw new UnsupportedOperationException();
184 public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
185 throw new UnsupportedOperationException();
189 public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory, final BundleContext bundleContext) {
190 throw new UnsupportedOperationException();