1 package com.atlassian.plugin.osgi.factory;
2
3 import java.util.Dictionary;
4 import java.util.Hashtable;
5 import java.util.List;
6
7 import com.atlassian.plugin.module.ContainerAccessor;
8
9 import com.google.common.collect.ImmutableList;
10
11 import org.apache.felix.framework.resolver.Module;
12 import org.mockito.Mockito;
13 import org.osgi.framework.Bundle;
14 import org.osgi.framework.BundleContext;
15 import org.osgi.framework.Constants;
16 import org.osgi.service.packageadmin.PackageAdmin;
17 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
18 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
19 import org.springframework.context.support.GenericApplicationContext;
20
21 import junit.framework.TestCase;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 public class TestOsgiPluginInstalledHelper extends TestCase
27 {
28
29
30
31 interface ReflectableBundle extends Bundle
32 {
33 List<Module> getModules();
34 }
35
36 private ReflectableBundle bundle;
37 private BundleContext bundleContext;
38 private Dictionary dict;
39 private OsgiPluginHelper helper;
40 private PackageAdmin packageAdmin;
41
42 @Override
43 public void setUp()
44 {
45 bundle = mock(ReflectableBundle.class);
46 dict = new Hashtable();
47 dict.put(Constants.BUNDLE_DESCRIPTION, "desc");
48 dict.put(Constants.BUNDLE_VERSION, "1.0");
49 when(bundle.getHeaders()).thenReturn(dict);
50 bundleContext = mock(BundleContext.class);
51 when(bundle.getBundleContext()).thenReturn(bundleContext);
52
53 helper = mock(OsgiPluginHelper.class);
54 when(helper.getBundle()).thenReturn(bundle);
55
56 packageAdmin = mock(PackageAdmin.class);
57
58 helper = new OsgiPluginInstalledHelper(bundle, packageAdmin);
59 }
60
61 @Override
62 public void tearDown()
63 {
64 bundle = null;
65 packageAdmin = null;
66 helper = null;
67 dict = null;
68 bundleContext = null;
69 }
70
71 public void testCreateBeanWithSpring() {
72 DefaultListableBeanFactory autowireBf = new DefaultListableBeanFactory();
73 autowireBf.registerBeanDefinition("child",
74 BeanDefinitionBuilder.rootBeanDefinition(ChildBean.class).getBeanDefinition());
75
76 when(bundle.getSymbolicName()).thenReturn("foo");
77 helper.setPluginContainer(new GenericApplicationContext(autowireBf));
78 SetterInjectedBean bean = new SetterInjectedBean();
79 helper.getContainerAccessor().injectBean(bean);
80 assertNotNull(bean.getChild());
81 }
82
83 public void testAutowireObjectWithArbitraryContainer()
84 {
85 ContainerAccessor containerAccessor = mock(ContainerAccessor.class);
86 helper.setPluginContainer(containerAccessor);
87 when(containerAccessor.createBean(String.class)).thenReturn(
88 "foo");
89 assertEquals("foo", helper.getContainerAccessor().createBean(String.class));
90
91 }
92
93 public void testAutowireNoPluginContainerButThereShouldBe()
94 {
95 Object obj = new Object();
96 try
97 {
98 helper.getContainerAccessor().injectBean(obj);
99 fail("Should throw exception");
100 }
101 catch (RuntimeException ex)
102 {
103
104 }
105 }
106
107 public void testOnDisableWithoutEnabling()
108 {
109
110 try
111 {
112 helper.onDisable();
113 }
114 catch(NullPointerException e)
115 {
116 fail("NullPointerException encountered.");
117 }
118 }
119
120 public void testBundlesAreResolvedIfOnlyInstalled()
121 {
122 Mockito.when(bundle.getState()).thenReturn(Bundle.INSTALLED);
123 Mockito.when(bundle.getModules()).thenReturn(ImmutableList.<Module>of());
124 helper.getRequiredPlugins();
125 Mockito.verify(packageAdmin).resolveBundles(new Bundle[]{bundle});
126 }
127
128
129 public static class ChildBean {
130 }
131
132
133 public static class SetterInjectedBean
134 {
135 private ChildBean child;
136
137 public ChildBean getChild()
138 {
139 return child;
140 }
141
142 public void setChild(ChildBean child)
143 {
144 this.child = child;
145 }
146 }
147 }