1 package com.atlassian.plugin.osgi.factory;
2
3 import com.atlassian.plugin.JarPluginArtifact;
4 import com.atlassian.plugin.ModuleDescriptor;
5 import com.atlassian.plugin.ModuleDescriptorFactory;
6 import com.atlassian.plugin.Plugin;
7 import com.atlassian.plugin.PluginArtifact;
8 import com.atlassian.plugin.impl.UnloadablePlugin;
9 import com.atlassian.plugin.osgi.container.OsgiContainerManager;
10 import com.atlassian.plugin.test.PluginJarBuilder;
11 import org.dom4j.Element;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Mock;
16 import org.mockito.junit.MockitoJUnitRunner;
17 import org.osgi.util.tracker.ServiceTracker;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.instanceOf;
25 import static org.hamcrest.Matchers.is;
26 import static org.hamcrest.Matchers.notNullValue;
27 import static org.hamcrest.Matchers.nullValue;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verifyZeroInteractions;
31 import static org.mockito.Mockito.when;
32
33 @RunWith(MockitoJUnitRunner.Silent.class)
34 public class TestOsgiBundleFactory {
35 @Mock
36 private OsgiContainerManager osgiContainerManager;
37 @Mock
38 private ModuleDescriptorFactory moduleDescriptorFactory;
39 @Mock
40 private ServiceTracker serviceTracker;
41
42 private OsgiBundleFactory osgiBundleFactory;
43
44 @Before
45 public void setUp() {
46 when(osgiContainerManager.getServiceTracker(anyString())).thenReturn(serviceTracker);
47
48 osgiBundleFactory = new OsgiBundleFactory(osgiContainerManager);
49 }
50
51 @Test
52 public void canCreateReturnsPluginKeyForJarFileContainingBundle() throws Exception {
53 final String pluginKey = osgiBundleFactory.canCreate(TestOsgiBundlePlugin.getBundleJarPluginArtifact());
54 assertThat(pluginKey, is(TestOsgiBundlePlugin.PLUGIN_KEY));
55 }
56
57 @Test
58 public void canCreateReturnsNullForJarFileContainingBundleAndPlugin() throws Exception {
59 final String pluginKey = osgiBundleFactory.canCreate(getBundlePluginJarPluginArtifact());
60 assertThat(pluginKey, nullValue());
61 }
62
63 @Test
64 public void canCreateReturnsNullForPluginJar() throws Exception {
65 final String pluginKey = osgiBundleFactory.canCreate(getPluginJarPluginArtifact());
66 assertThat(pluginKey, nullValue());
67 }
68
69 @Test
70 public void canCreateReturnsNullForNonFileArtifact() throws Exception {
71 final String pluginKey = osgiBundleFactory.canCreate(getNonFilePluginArtifact());
72 assertThat(pluginKey, nullValue());
73 }
74
75 @Test
76 public void createReturnsConfiguredPluginWhenBundleInstallSucceeds() throws Exception {
77 final PluginArtifact pluginArtifact = TestOsgiBundlePlugin.getBundleJarPluginArtifact();
78 final Plugin plugin = osgiBundleFactory.create(pluginArtifact, moduleDescriptorFactory);
79 assertThat(plugin, notNullValue());
80 assertThat(plugin, instanceOf(OsgiBundlePlugin.class));
81 TestOsgiBundlePlugin.assertThatPluginIsConfigured(plugin);
82
83
84 verifyZeroInteractions(osgiContainerManager);
85 }
86
87 @Test
88 public void createReturnsUnloadablePluginForJarFileContainingBundleAndPlugin() throws Exception {
89 final Plugin plugin = osgiBundleFactory.create(getBundlePluginJarPluginArtifact(), moduleDescriptorFactory);
90 assertThat(plugin, notNullValue());
91 assertThat(plugin, instanceOf(UnloadablePlugin.class));
92 }
93
94 @Test
95 public void createReturnsUnloadablePluginForPluginJar() throws Exception {
96 final Plugin plugin = osgiBundleFactory.create(getPluginJarPluginArtifact(), moduleDescriptorFactory);
97 assertThat(plugin, notNullValue());
98 assertThat(plugin, instanceOf(UnloadablePlugin.class));
99 }
100
101 @Test
102 public void createReturnsUnloadablePluginForNonFileArtifact() throws Exception {
103 final Plugin plugin = osgiBundleFactory.create(getNonFilePluginArtifact(), moduleDescriptorFactory);
104 assertThat(plugin, notNullValue());
105 assertThat(plugin, instanceOf(UnloadablePlugin.class));
106 }
107
108 @Test
109 public void createModuleIncorrectPluginType() {
110 final Plugin plugin = mock(Plugin.class);
111 final Element module = mock(Element.class);
112
113 assertThat(osgiBundleFactory.createModule(plugin, module, moduleDescriptorFactory), nullValue());
114 }
115
116 @Test
117 public void createModule() throws IllegalAccessException, ClassNotFoundException, InstantiationException {
118 final OsgiBundlePlugin plugin = mock(OsgiBundlePlugin.class);
119 final Element module = mock(Element.class);
120 final ModuleDescriptor moduleDescriptor = mock(ModuleDescriptor.class);
121
122 when(module.getName()).thenReturn("william");
123 when(moduleDescriptorFactory.hasModuleDescriptor("william")).thenReturn(true);
124 when(moduleDescriptorFactory.getModuleDescriptor("william")).thenReturn(moduleDescriptor);
125
126 assertThat(osgiBundleFactory.createModule(plugin, module, moduleDescriptorFactory), is(moduleDescriptor));
127 }
128
129 private static PluginArtifact getBundlePluginJarPluginArtifact() throws IOException {
130 return new JarPluginArtifact(new PluginJarBuilder("somebundleplugin")
131 .manifest(TestOsgiBundlePlugin.getBundleManifest())
132 .addPluginInformation(TestOsgiBundlePlugin.PLUGIN_KEY, TestOsgiBundlePlugin.NAME, TestOsgiBundlePlugin.VERSION, 2)
133 .build());
134 }
135
136 private static PluginArtifact getPluginJarPluginArtifact() throws IOException {
137 return new JarPluginArtifact(new PluginJarBuilder("someplugin")
138 .addPluginInformation(TestOsgiBundlePlugin.PLUGIN_KEY, TestOsgiBundlePlugin.NAME, TestOsgiBundlePlugin.VERSION, 2)
139 .build());
140 }
141
142 private static PluginArtifact getNonFilePluginArtifact() throws IOException {
143 final File tempFile = File.createTempFile("foo", "bar");
144 final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
145 when(pluginArtifact.getName()).thenReturn(tempFile.getPath());
146 when(pluginArtifact.getInputStream()).thenAnswer(invocation -> new FileInputStream(tempFile));
147 when(pluginArtifact.toFile()).thenReturn(tempFile);
148 return pluginArtifact;
149 }
150 }