2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.config.manager.impl.osgi;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyObject;
16 import static org.mockito.Matchers.anyString;
17 import static org.mockito.Mockito.doAnswer;
18 import static org.mockito.Mockito.doNothing;
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.reset;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.verifyZeroInteractions;
24 import java.util.Collection;
25 import java.util.Dictionary;
26 import java.util.Map.Entry;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.mockito.invocation.InvocationOnMock;
33 import org.mockito.stubbing.Answer;
34 import org.opendaylight.controller.config.api.DependencyResolver;
35 import org.opendaylight.controller.config.api.DependencyResolverFactory;
36 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
37 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
38 import org.opendaylight.controller.config.spi.Module;
39 import org.opendaylight.controller.config.spi.ModuleFactory;
40 import org.osgi.framework.Bundle;
41 import org.osgi.framework.BundleContext;
42 import org.osgi.framework.BundleEvent;
43 import org.osgi.framework.ServiceRegistration;
45 public class ModuleFactoryBundleTrackerTest {
48 private Bundle bundle;
50 private BundleContext context;
52 private ServiceRegistration<?> reg;
54 private BlankTransactionServiceTracker blankTxTracker;
57 public void setUp() throws Exception {
58 MockitoAnnotations.initMocks(this);
59 doAnswer(new Answer<Object>() {
61 public Object answer(final InvocationOnMock invocation) throws Throwable {
62 return getClass().getClassLoader().loadClass((String) invocation.getArguments()[0]);
64 }).when(bundle).loadClass(anyString());
65 doReturn("mockBundle").when(bundle).toString();
66 doReturn("mockBundleContext").when(context).toString();
67 doReturn(context).when(bundle).getBundleContext();
68 doReturn(100L).when(bundle).getBundleId();
69 doReturn(reg).when(context).registerService(anyString(), anyObject(), any(Dictionary.class));
73 public void testRegisterFactory() throws Exception {
74 Entry<ModuleFactory, Bundle> entry = ModuleFactoryBundleTracker.registerFactory(
75 TestingFactory.class.getName(), bundle);
76 assertEquals(TestingFactory.currentInstance, entry.getKey());
77 assertEquals(bundle, entry.getValue());
81 public void testRegisterFactoryInstantiateEx() throws Exception {
83 ModuleFactoryBundleTracker.registerFactory(WrongConstructorTestingFactory.class.getName(), bundle);
84 } catch (Exception e) {
85 verifyZeroInteractions(context);
86 assertNotNull(e.getCause());
87 assertEquals(InstantiationException.class, e.getCause().getClass());
91 fail("Cannot register without proper constructor");
95 public void testRegisterFactoryInstantiateExAccess() throws Exception {
97 ModuleFactoryBundleTracker.registerFactory(NoAccessConstructorTestingFactory.class.getName(), bundle);
98 } catch (Exception e) {
99 verifyZeroInteractions(context);
100 assertNotNull(e.getCause());
101 assertEquals(IllegalAccessException.class, e.getCause().getClass());
105 fail("Cannot register without proper constructor");
109 public void testRegisterFactoryNotExtending() throws Exception {
111 ModuleFactoryBundleTracker.registerFactory(NotExtendingTestingFactory.class.getName(), bundle);
112 } catch (Exception e) {
113 verifyZeroInteractions(context);
117 fail("Cannot register without extend");
121 public void testRegisterFactoryNotExisting() throws Exception {
123 ModuleFactoryBundleTracker.registerFactory("Unknown class", bundle);
124 } catch (Exception e) {
125 verifyZeroInteractions(context);
126 assertNotNull(e.getCause());
127 assertEquals(ClassNotFoundException.class, e.getCause().getClass());
131 fail("Cannot register without extend");
135 public void testBundleAddAndRemove() throws Exception {
136 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
137 doReturn(getClass().getResource("/module-factories/module-factory-ok")).when(bundle).getEntry(anyString());
138 tracker.addingBundle(bundle, null);
140 Collection<Entry<ModuleFactory, BundleContext>> entries = tracker.getModuleFactoryEntries();
141 assertNotNull(entries);
142 assertEquals(1, entries.size());
143 Entry<ModuleFactory, BundleContext> entry = entries.iterator().next();
144 assertEquals(TestingFactory.currentInstance, entry.getKey());
145 assertEquals(context, entry.getValue());
147 doNothing().when(blankTxTracker).blankTransaction();;
149 BundleEvent mockEvent = mock(BundleEvent.class);
150 doReturn(BundleEvent.STOPPING).when(mockEvent).getType();
152 tracker.removedBundle(bundle, mockEvent, bundle);
154 entries = tracker.getModuleFactoryEntries();
155 assertNotNull(entries);
156 assertEquals(0, entries.size());
158 verify(blankTxTracker).blankTransaction();
161 doReturn(BundleEvent.STOPPED).when(mockEvent).getType();
163 tracker.addingBundle(bundle, mockEvent);
165 assertEquals(0, tracker.getModuleFactoryEntries().size());
169 public void testAddingBundleError() throws Exception {
170 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
171 doReturn(getClass().getResource("/module-factories/module-factory-fail")).when(bundle).getEntry(anyString());
173 tracker.addingBundle(bundle, mock(BundleEvent.class));
174 } catch (Exception e) {
175 verifyZeroInteractions(context);
179 fail("Cannot register");
182 static class WrongConstructorTestingFactory extends TestingFactory {
183 WrongConstructorTestingFactory(final String randomParam) {
187 static class NotExtendingTestingFactory {}
189 static class NoAccessConstructorTestingFactory extends TestingFactory {
190 private NoAccessConstructorTestingFactory() {
194 static class TestingFactory implements ModuleFactory {
196 static TestingFactory currentInstance;
199 currentInstance = this;
203 public String getImplementationName() {
208 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
209 throw new UnsupportedOperationException();
213 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
214 throw new UnsupportedOperationException();
218 public boolean isModuleImplementingServiceInterface(final Class<? extends AbstractServiceInterface> serviceInterface) {
219 throw new UnsupportedOperationException();
223 public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
224 throw new UnsupportedOperationException();
228 public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory, final BundleContext bundleContext) {
229 throw new UnsupportedOperationException();