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