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