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