1 package com.atlassian.plugin.osgi.factory;
2
3 import com.atlassian.plugin.PluginState;
4 import com.atlassian.plugin.descriptors.AbstractModuleDescriptor;
5 import com.atlassian.plugin.descriptors.RequiresRestart;
6 import com.atlassian.plugin.event.PluginEventManager;
7 import junit.framework.TestCase;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.never;
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
12 import org.osgi.framework.Bundle;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.BundleException;
15 import org.osgi.framework.Constants;
16
17 import java.net.MalformedURLException;
18 import java.net.URL;
19 import java.util.Dictionary;
20 import java.util.Hashtable;
21
22 public class TestOsgiPlugin extends TestCase
23 {
24 private Bundle bundle;
25 private OsgiPlugin plugin;
26 private BundleContext bundleContext;
27 private Dictionary dict;
28 private OsgiPluginHelper helper;
29
30 @Override
31 public void setUp()
32 {
33
34
35 bundle = mock(Bundle.class);
36 dict = new Hashtable();
37 dict.put(Constants.BUNDLE_DESCRIPTION, "desc");
38 dict.put(Constants.BUNDLE_VERSION, "1.0");
39 when(bundle.getHeaders()).thenReturn(dict);
40 bundleContext = mock(BundleContext.class);
41 when(bundle.getBundleContext()).thenReturn(bundleContext);
42
43 helper = mock(OsgiPluginHelper.class);
44 when(helper.getBundle()).thenReturn(bundle);
45
46 plugin = new OsgiPlugin(mock(PluginEventManager.class), helper);
47 }
48
49 @Override
50 public void tearDown()
51 {
52 bundle = null;
53 plugin = null;
54 bundleContext = null;
55 }
56
57 public void testEnabled() throws BundleException
58 {
59 when(bundle.getState()).thenReturn(Bundle.RESOLVED);
60 plugin.enable();
61 verify(bundle).start();
62 }
63
64 public void testDisabled() throws BundleException
65 {
66 when(bundle.getState()).thenReturn(Bundle.ACTIVE);
67 plugin.disable();
68 verify(bundle).stop();
69 }
70
71 public void testDisabledOnNonDynamicPlugin() throws BundleException
72 {
73 plugin.addModuleDescriptor(new StaticModuleDescriptor());
74 when(bundle.getState()).thenReturn(Bundle.ACTIVE);
75 plugin.disable();
76 verify(bundle, never()).stop();
77 }
78
79 public void testUninstall() throws BundleException
80 {
81 when(bundle.getState()).thenReturn(Bundle.ACTIVE);
82 plugin.uninstall();
83 assertEquals(plugin.getPluginState(), PluginState.UNINSTALLED);
84 }
85
86 @RequiresRestart
87 public static class StaticModuleDescriptor extends AbstractModuleDescriptor
88 {
89 public Object getModule()
90 {
91 return null;
92 }
93 }
94
95
96 public void testShouldHaveSpringContext() throws MalformedURLException
97 {
98 dict.put(OsgiPlugin.SPRING_CONTEXT, "*;timeout:=60");
99 assertTrue(OsgiPlugin.shouldHaveSpringContext(bundle));
100
101 dict.remove(OsgiPlugin.SPRING_CONTEXT);
102 assertFalse(OsgiPlugin.shouldHaveSpringContext(bundle));
103 when(bundle.getEntry("META-INF/spring/")).thenReturn(new URL("http://foo"));
104 assertTrue(OsgiPlugin.shouldHaveSpringContext(bundle));
105
106
107 }
108 }