1 package com.atlassian.plugin.osgi.factory;
2
3 import com.atlassian.plugin.PluginArtifact;
4 import com.atlassian.plugin.osgi.container.OsgiContainerManager;
5 import com.google.common.collect.ImmutableMap;
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.osgi.framework.Bundle;
9 import org.osgi.framework.Constants;
10
11 import java.io.File;
12 import java.util.Dictionary;
13 import java.util.Hashtable;
14
15 import static com.atlassian.plugin.ReferenceMode.FORBID_REFERENCE;
16 import static com.atlassian.plugin.ReferenceMode.PERMIT_REFERENCE;
17 import static org.hamcrest.Matchers.sameInstance;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertThat;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 public class TestOsgiPluginUninstalledHelper {
25
26 final private String key = "key";
27 private OsgiContainerManager mgr;
28 private OsgiPluginUninstalledHelper helper;
29
30 @Before
31 public void setUp() {
32 final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
33 mgr = mock(OsgiContainerManager.class);
34 when(pluginArtifact.getReferenceMode()).thenReturn(FORBID_REFERENCE);
35 helper = new OsgiPluginUninstalledHelper(key, mgr, pluginArtifact);
36 }
37
38 @Test
39 public void testInstall() {
40 final Dictionary<String, String> dict = new Hashtable<>();
41 dict.put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, key);
42 final Bundle bundle = mock(Bundle.class);
43 when(bundle.getHeaders()).thenReturn(dict);
44 when(bundle.getSymbolicName()).thenReturn(key);
45 when(mgr.installBundle(null, FORBID_REFERENCE)).thenReturn(bundle);
46 assertEquals(bundle, helper.install());
47 }
48
49 @Test(expected = IllegalArgumentException.class)
50 public void testInstallDifferentSymbolicName() {
51 final Dictionary<String, String> dict = new Hashtable<>();
52 final Bundle bundle = mock(Bundle.class);
53 when(bundle.getHeaders()).thenReturn(dict);
54 when(bundle.getSymbolicName()).thenReturn("bar");
55 when(mgr.installBundle(null, FORBID_REFERENCE)).thenReturn(bundle);
56
57 helper.install();
58 }
59
60 @Test
61 public void testInstallDifferentSymbolicNameButAltassianKeyFound() {
62 final Dictionary<String, String> dict = new Hashtable<>();
63 dict.put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, key);
64 final Bundle bundle = mock(Bundle.class);
65 when(bundle.getHeaders()).thenReturn(dict);
66 when(bundle.getSymbolicName()).thenReturn("bar");
67 when(mgr.installBundle(null, FORBID_REFERENCE)).thenReturn(bundle);
68
69 helper.install();
70 }
71
72 @Test
73 public void testPluginArtifactThatAllowsReferenceIsInstalledByReference() {
74 final String bundleSymbolicName = getClass().getName() + ".testBundle";
75 final String bundleVersion = "1.2";
76 final String pluginKey = bundleSymbolicName + "-" + bundleVersion;
77
78 final File jarFile = new File("some.jar");
79 final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
80 when(pluginArtifact.toFile()).thenReturn(jarFile);
81 when(pluginArtifact.getReferenceMode()).thenReturn(PERMIT_REFERENCE);
82 final Bundle expectedBundle = mock(Bundle.class);
83
84 final Dictionary<String, String> headers = new Hashtable<>(ImmutableMap.<String, String>builder()
85 .put(Constants.BUNDLE_SYMBOLICNAME, bundleSymbolicName)
86 .put(Constants.BUNDLE_VERSION, bundleVersion)
87 .build());
88 when(expectedBundle.getSymbolicName()).thenReturn(bundleSymbolicName);
89 when(expectedBundle.getHeaders()).thenReturn(headers);
90 final OsgiContainerManager osgiContainerManager = mock(OsgiContainerManager.class);
91
92 when(osgiContainerManager.installBundle(jarFile, PERMIT_REFERENCE)).thenReturn(expectedBundle);
93 final OsgiPluginUninstalledHelper osgiPluginUninstalledHelper =
94 new OsgiPluginUninstalledHelper(pluginKey, osgiContainerManager, pluginArtifact);
95 final Bundle actualBundle = osgiPluginUninstalledHelper.install();
96 assertThat(actualBundle, sameInstance(expectedBundle));
97 verify(osgiContainerManager).installBundle(jarFile, PERMIT_REFERENCE);
98 }
99 }