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