1 package com.atlassian.plugin.osgi.factory;
2
3 import com.atlassian.plugin.AutowireCapablePlugin;
4 import junit.framework.TestCase;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.when;
7 import org.osgi.framework.Bundle;
8 import org.osgi.framework.BundleContext;
9 import org.osgi.framework.Constants;
10 import org.osgi.service.packageadmin.PackageAdmin;
11 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
12 import org.springframework.beans.factory.support.StaticListableBeanFactory;
13 import org.springframework.context.support.GenericApplicationContext;
14
15 import java.util.Dictionary;
16 import java.util.Hashtable;
17
18 public class TestOsgiPluginInstalledHelper extends TestCase
19 {
20 private Bundle bundle;
21 private BundleContext bundleContext;
22 private Dictionary dict;
23 private OsgiPluginHelper helper;
24 private PackageAdmin packageAdmin;
25
26 @Override
27 public void setUp()
28 {
29 bundle = mock(Bundle.class);
30 dict = new Hashtable();
31 dict.put(Constants.BUNDLE_DESCRIPTION, "desc");
32 dict.put(Constants.BUNDLE_VERSION, "1.0");
33 when(bundle.getHeaders()).thenReturn(dict);
34 bundleContext = mock(BundleContext.class);
35 when(bundle.getBundleContext()).thenReturn(bundleContext);
36
37 helper = mock(OsgiPluginHelper.class);
38 when(helper.getBundle()).thenReturn(bundle);
39
40 packageAdmin = mock(PackageAdmin.class);
41
42 helper = new OsgiPluginInstalledHelper(bundle, packageAdmin, true);
43 }
44
45 @Override
46 public void tearDown()
47 {
48 bundle = null;
49 packageAdmin = null;
50 helper = null;
51 dict = null;
52 bundleContext = null;
53 }
54
55 public void testAutowireObject() {
56 StaticListableBeanFactory bf = new StaticListableBeanFactory();
57 bf.addBean("child", new ChildBean());
58 DefaultListableBeanFactory autowireBf = new DefaultListableBeanFactory(bf);
59
60 when(bundle.getSymbolicName()).thenReturn("foo");
61 helper.setPluginContainer(new GenericApplicationContext(autowireBf));
62 SetterInjectedBean bean = new SetterInjectedBean();
63 helper.autowire(bean, AutowireCapablePlugin.AutowireStrategy.AUTOWIRE_BY_NAME);
64 assertNotNull(bean.getChild());
65 }
66
67 public void testAutowireNoSpring()
68 {
69 helper = new OsgiPluginInstalledHelper(bundle, packageAdmin, false);
70 Object obj = new Object();
71 try
72 {
73 helper.autowire(obj, AutowireCapablePlugin.AutowireStrategy.AUTOWIRE_AUTODETECT);
74 }
75 catch (RuntimeException ex)
76 {
77 fail("Shouldn't throw any exceptions");
78 throw ex;
79 }
80 }
81
82 public void testAutowireNoSpringButThereShouldBe()
83 {
84 Object obj = new Object();
85 try
86 {
87 helper.autowire(obj, AutowireCapablePlugin.AutowireStrategy.AUTOWIRE_AUTODETECT);
88 fail("Should throw exception");
89 }
90 catch (RuntimeException ex)
91 {
92
93 }
94 }
95
96 public static class ChildBean {
97 }
98
99
100 public static class SetterInjectedBean
101 {
102 private ChildBean child;
103
104 public ChildBean getChild()
105 {
106 return child;
107 }
108
109 public void setChild(ChildBean child)
110 {
111 this.child = child;
112 }
113 }
114 }