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;
22 import java.util.Dictionary;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.mockito.invocation.InvocationOnMock;
29 import org.mockito.stubbing.Answer;
30 import org.opendaylight.controller.config.api.DependencyResolver;
31 import org.opendaylight.controller.config.api.DependencyResolverFactory;
32 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
33 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
34 import org.opendaylight.controller.config.spi.Module;
35 import org.opendaylight.controller.config.spi.ModuleFactory;
36 import org.osgi.framework.Bundle;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.BundleEvent;
39 import org.osgi.framework.ServiceRegistration;
41 public class ModuleFactoryBundleTrackerTest {
44 private Bundle bundle;
46 private BundleContext context;
48 private ServiceRegistration<?> reg;
51 public void setUp() throws Exception {
52 MockitoAnnotations.initMocks(this);
53 doAnswer(new Answer<Object>() {
55 public Object answer(final InvocationOnMock invocation) throws Throwable {
56 return getClass().getClassLoader().loadClass((String) invocation.getArguments()[0]);
58 }).when(bundle).loadClass(anyString());
59 doReturn("mockBundle").when(bundle).toString();
60 doReturn(context).when(bundle).getBundleContext();
61 doReturn(reg).when(context).registerService(anyString(), anyObject(), any(Dictionary.class));
65 public void testRegisterFactory() throws Exception {
66 ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
67 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
71 public void testRegisterFactoryInstantiateEx() throws Exception {
73 ModuleFactoryBundleTracker.registerFactory(WrongConstructorTestingFactory.class.getName(), bundle);
74 } catch (Exception e) {
75 verifyZeroInteractions(context);
76 assertNotNull(e.getCause());
77 assertEquals(InstantiationException.class, e.getCause().getClass());
81 fail("Cannot register without proper constructor");
85 public void testRegisterFactoryInstantiateExAccess() throws Exception {
87 ModuleFactoryBundleTracker.registerFactory(NoAccessConstructorTestingFactory.class.getName(), bundle);
88 } catch (Exception e) {
89 verifyZeroInteractions(context);
90 assertNotNull(e.getCause());
91 assertEquals(IllegalAccessException.class, e.getCause().getClass());
95 fail("Cannot register without proper constructor");
99 public void testRegisterFactoryNotExtending() throws Exception {
101 ModuleFactoryBundleTracker.registerFactory(NotExtendingTestingFactory.class.getName(), bundle);
102 } catch (Exception e) {
103 verifyZeroInteractions(context);
107 fail("Cannot register without extend");
111 public void testRegisterFactoryNotExisting() throws Exception {
113 ModuleFactoryBundleTracker.registerFactory("Unknown class", bundle);
114 } catch (Exception e) {
115 verifyZeroInteractions(context);
116 assertNotNull(e.getCause());
117 assertEquals(ClassNotFoundException.class, e.getCause().getClass());
121 fail("Cannot register without extend");
125 private BlankTransactionServiceTracker blankTxTracker;
128 public void testAddingBundle() throws Exception {
129 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
130 doReturn(getClass().getResource("/module-factories/module-factory-ok")).when(bundle).getEntry(anyString());
131 tracker.addingBundle(bundle, mock(BundleEvent.class));
132 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
136 public void testAddingBundleError() throws Exception {
137 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
138 doReturn(getClass().getResource("/module-factories/module-factory-fail")).when(bundle).getEntry(anyString());
140 tracker.addingBundle(bundle, mock(BundleEvent.class));
141 } catch (Exception e) {
142 verifyZeroInteractions(context);
146 fail("Cannot register");
149 static class WrongConstructorTestingFactory extends TestingFactory {
150 WrongConstructorTestingFactory(final String randomParam) {
154 static class NotExtendingTestingFactory {}
156 static class NoAccessConstructorTestingFactory extends TestingFactory {
157 private NoAccessConstructorTestingFactory() {
161 static class TestingFactory implements ModuleFactory {
163 static TestingFactory currentInstance;
166 currentInstance = this;
170 public String getImplementationName() {
175 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
176 throw new UnsupportedOperationException();
180 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
181 throw new UnsupportedOperationException();
185 public boolean isModuleImplementingServiceInterface(final Class<? extends AbstractServiceInterface> serviceInterface) {
186 throw new UnsupportedOperationException();
190 public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
191 throw new UnsupportedOperationException();
195 public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory, final BundleContext bundleContext) {
196 throw new UnsupportedOperationException();