1 package com.atlassian.plugin.osgi.factory;
2
3 import com.atlassian.plugin.JarPluginArtifact;
4 import com.atlassian.plugin.ModuleDescriptorFactory;
5 import com.atlassian.plugin.Plugin;
6 import com.atlassian.plugin.PluginAccessor;
7 import com.atlassian.plugin.PluginParseException;
8 import com.atlassian.plugin.XmlPluginArtifact;
9 import com.atlassian.plugin.event.impl.DefaultPluginEventManager;
10 import com.atlassian.plugin.osgi.container.OsgiContainerManager;
11 import com.atlassian.plugin.test.PluginJarBuilder;
12 import com.atlassian.plugin.test.PluginTestUtils;
13 import com.google.common.collect.ImmutableMap;
14 import com.mockobjects.dynamic.C;
15 import com.mockobjects.dynamic.Mock;
16 import org.apache.commons.io.FileUtils;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.Constants;
23 import org.osgi.service.packageadmin.PackageAdmin;
24 import org.osgi.util.tracker.ServiceTracker;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Dictionary;
31 import java.util.Hashtable;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertNotNull;
35 import static org.junit.Assert.assertNull;
36 import static org.junit.Assert.assertTrue;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39
40 public class TestRemotablePluginFactory {
41 private RemotablePluginFactory factory;
42
43 private File tmpDir;
44 private File jar;
45 private OsgiContainerManager osgiContainerManager;
46 private Mock mockBundle;
47 private Mock mockSystemBundle;
48
49 @Before
50 public void setUp() throws IOException {
51 tmpDir = PluginTestUtils.createTempDirectory(TestRemotablePluginFactory.class);
52 osgiContainerManager = mock(OsgiContainerManager.class);
53 ServiceTracker tracker = mock(ServiceTracker.class);
54 when(tracker.getServices()).thenReturn(new Object[0]);
55 when(osgiContainerManager.getServiceTracker(ModuleDescriptorFactory.class.getName())).thenReturn(tracker);
56
57 factory = new RemotablePluginFactory(PluginAccessor.Descriptor.FILENAME, Collections.emptySet(), osgiContainerManager, new DefaultPluginEventManager());
58 jar = new PluginJarBuilder("someplugin").addPluginInformation("plugin.key", "My Plugin", "1.0", 3).build();
59
60 mockBundle = new Mock(Bundle.class);
61 final Dictionary<String, String> dict = new Hashtable<>();
62 dict.put(Constants.BUNDLE_DESCRIPTION, "desc");
63 dict.put(Constants.BUNDLE_VERSION, "1.0");
64 mockBundle.matchAndReturn("getHeaders", dict);
65
66 mockSystemBundle = new Mock(Bundle.class);
67 final Dictionary<String, String> sysDict = new Hashtable<>();
68 sysDict.put(Constants.BUNDLE_DESCRIPTION, "desc");
69 sysDict.put(Constants.BUNDLE_VERSION, "1.0");
70 mockSystemBundle.matchAndReturn("getHeaders", sysDict);
71 mockSystemBundle.matchAndReturn("getLastModified", System.currentTimeMillis());
72 mockSystemBundle.matchAndReturn("getSymbolicName", "system.bundle");
73
74 Mock mockSysContext = new Mock(BundleContext.class);
75 mockSystemBundle.matchAndReturn("getBundleContext", mockSysContext.proxy());
76
77 mockSysContext.matchAndReturn("getServiceReference", C.ANY_ARGS, null);
78 mockSysContext.matchAndReturn("getService", C.ANY_ARGS, new Mock(PackageAdmin.class).proxy());
79 }
80
81 @After
82 public void tearDown() throws IOException {
83 factory = null;
84 FileUtils.cleanDirectory(tmpDir);
85 jar.delete();
86 }
87
88 @Test
89 public void testCreateOsgiPlugin() throws PluginParseException {
90 String key = "plugin.key";
91 mockBundle.expectAndReturn("getSymbolicName", key);
92 when(osgiContainerManager.getHostComponentRegistrations()).thenReturn(new ArrayList<>());
93 when(osgiContainerManager.getBundles()).thenReturn(new Bundle[]{(Bundle) mockSystemBundle.proxy()});
94 final Plugin plugin = factory.create(new JarPluginArtifact(jar), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
95 assertNotNull(plugin);
96 assertTrue(plugin instanceof OsgiPlugin);
97 assertEquals(plugin.getKey(), key);
98 }
99
100 @Test
101 public void testCreateOsgiPluginWithBadVersion() throws PluginParseException, IOException {
102 jar = new PluginJarBuilder("someplugin").addPluginInformation("plugin.key", "My Plugin", "3.2-rc1").build();
103 mockBundle.expectAndReturn("getSymbolicName", "plugin.key");
104 when(osgiContainerManager.getHostComponentRegistrations()).thenReturn(new ArrayList<>());
105 when(osgiContainerManager.getBundles()).thenReturn(new Bundle[]{(Bundle) mockSystemBundle.proxy()});
106 Plugin plugin = factory.create(new JarPluginArtifact(jar), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
107 assertTrue(plugin instanceof OsgiPlugin);
108 }
109
110 @Test
111 public void testCanLoadWithXmlVersion2() throws PluginParseException, IOException {
112 final File plugin = new PluginJarBuilder("loadwithxml").addPluginInformation("foo.bar", "", "1.0", 2).build();
113 assertNull(factory.canCreate(new JarPluginArtifact(plugin)));
114 }
115
116 @Test
117 public void testCanLoadWithXmlVersion3() throws PluginParseException, IOException {
118 final File plugin = new PluginJarBuilder("loadwithxml").addPluginInformation("foo.bar", "", "1.0", 3).build();
119 assertEquals("foo.bar", factory.canCreate(new JarPluginArtifact(plugin)));
120 }
121
122 @Test
123 public void testCanLoadWithXmlArtifact() throws PluginParseException, IOException {
124 File xmlFile = new File(tmpDir, "plugin.xml");
125 FileUtils.writeStringToFile(xmlFile, "<somexml />");
126 final String key = factory.canCreate(new XmlPluginArtifact(xmlFile));
127 assertNull(key);
128 }
129
130 @Test
131 public void testCanLoadNoXml() throws PluginParseException, IOException {
132 final File plugin = new PluginJarBuilder("loadwithxml").build();
133 final String key = factory.canCreate(new JarPluginArtifact(plugin));
134 assertNull(key);
135 }
136
137 @Test
138 public void testCanLoadNoXmlButWithManifestEntry() throws PluginParseException, IOException {
139 final File plugin = new PluginJarBuilder("loadwithxml")
140 .manifest(new ImmutableMap.Builder<String, String>()
141 .put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "somekey")
142 .put(Constants.BUNDLE_VERSION, "1.0")
143 .build())
144 .build();
145 final String key = factory.canCreate(new JarPluginArtifact(plugin));
146 assertNull(key);
147 }
148 }