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