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.doReturn;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.verifyZeroInteractions;
23 import java.util.Dictionary;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.stubbing.Answer;
31 import org.opendaylight.controller.config.api.DependencyResolver;
32 import org.opendaylight.controller.config.api.DependencyResolverFactory;
33 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
34 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
35 import org.opendaylight.controller.config.spi.Module;
36 import org.opendaylight.controller.config.spi.ModuleFactory;
37 import org.osgi.framework.Bundle;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.BundleEvent;
40 import org.osgi.framework.ServiceRegistration;
42 public class ModuleFactoryBundleTrackerTest {
45 private Bundle bundle;
47 private BundleContext context;
49 private ServiceRegistration<?> reg;
52 public void setUp() throws Exception {
53 MockitoAnnotations.initMocks(this);
54 doAnswer(new Answer<Object>() {
56 public Object answer(final InvocationOnMock invocation) throws Throwable {
57 return getClass().getClassLoader().loadClass((String) invocation.getArguments()[0]);
59 }).when(bundle).loadClass(anyString());
60 doReturn("mockBundle").when(bundle).toString();
61 doReturn(context).when(bundle).getBundleContext();
62 doReturn(reg).when(context).registerService(anyString(), anyObject(), any(Dictionary.class));
66 public void testRegisterFactory() throws Exception {
67 ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
68 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
72 public void testRegisterFactoryInstantiateEx() throws Exception {
74 ModuleFactoryBundleTracker.registerFactory(WrongConstructorTestingFactory.class.getName(), bundle);
75 } catch (Exception e) {
76 verifyZeroInteractions(context);
77 assertNotNull(e.getCause());
78 assertEquals(InstantiationException.class, e.getCause().getClass());
82 fail("Cannot register without proper constructor");
86 public void testRegisterFactoryInstantiateExAccess() throws Exception {
88 ModuleFactoryBundleTracker.registerFactory(NoAccessConstructorTestingFactory.class.getName(), bundle);
89 } catch (Exception e) {
90 verifyZeroInteractions(context);
91 assertNotNull(e.getCause());
92 assertEquals(IllegalAccessException.class, e.getCause().getClass());
96 fail("Cannot register without proper constructor");
100 public void testRegisterFactoryNotExtending() throws Exception {
102 ModuleFactoryBundleTracker.registerFactory(NotExtendingTestingFactory.class.getName(), bundle);
103 } catch (Exception e) {
104 verifyZeroInteractions(context);
108 fail("Cannot register without extend");
112 public void testRegisterFactoryNotExisting() throws Exception {
114 ModuleFactoryBundleTracker.registerFactory("Unknown class", bundle);
115 } catch (Exception e) {
116 verifyZeroInteractions(context);
117 assertNotNull(e.getCause());
118 assertEquals(ClassNotFoundException.class, e.getCause().getClass());
122 fail("Cannot register without extend");
126 private BlankTransactionServiceTracker blankTxTracker;
129 public void testAddingBundle() throws Exception {
130 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
131 doReturn(getClass().getResource("/module-factories/module-factory-ok")).when(bundle).getEntry(anyString());
132 tracker.addingBundle(bundle, mock(BundleEvent.class));
133 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
137 public void testAddingBundleError() throws Exception {
138 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
139 doReturn(getClass().getResource("/module-factories/module-factory-fail")).when(bundle).getEntry(anyString());
141 tracker.addingBundle(bundle, mock(BundleEvent.class));
142 } catch (Exception e) {
143 verifyZeroInteractions(context);
147 fail("Cannot register");
150 static class WrongConstructorTestingFactory extends TestingFactory {
151 WrongConstructorTestingFactory(final String randomParam) {
155 static class NotExtendingTestingFactory {}
157 static class NoAccessConstructorTestingFactory extends TestingFactory {
158 private NoAccessConstructorTestingFactory() {
162 static class TestingFactory implements ModuleFactory {
164 static TestingFactory currentInstance;
167 currentInstance = this;
171 public String getImplementationName() {
176 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
177 throw new UnsupportedOperationException();
181 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
182 throw new UnsupportedOperationException();
186 public boolean isModuleImplementingServiceInterface(final Class<? extends AbstractServiceInterface> serviceInterface) {
187 throw new UnsupportedOperationException();
191 public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
192 throw new UnsupportedOperationException();
196 public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory, final BundleContext bundleContext) {
197 throw new UnsupportedOperationException();