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.PluginException;
8 import com.atlassian.plugin.PluginParseException;
9 import com.atlassian.plugin.XmlPluginArtifact;
10 import com.atlassian.plugin.event.impl.DefaultPluginEventManager;
11 import com.atlassian.plugin.osgi.container.OsgiContainerManager;
12 import com.atlassian.plugin.osgi.container.impl.DefaultOsgiPersistentCache;
13 import com.atlassian.plugin.test.PluginJarBuilder;
14 import com.atlassian.plugin.test.PluginTestUtils;
15 import com.google.common.collect.ImmutableMap;
16 import com.mockobjects.dynamic.C;
17 import com.mockobjects.dynamic.Mock;
18 import junit.framework.TestCase;
19 import org.apache.commons.io.FileUtils;
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.net.URISyntaxException;
29 import java.util.ArrayList;
30 import java.util.Dictionary;
31 import java.util.Hashtable;
32
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35
36 public class TestOsgiPluginFactory extends TestCase
37 {
38 OsgiPluginFactory factory;
39
40 private File tmpDir;
41 private File jar;
42 OsgiContainerManager osgiContainerManager;
43 private Mock mockBundle;
44 private Mock mockSystemBundle;
45
46 @Override
47 public void setUp() throws IOException, URISyntaxException
48 {
49 tmpDir = PluginTestUtils.createTempDirectory(TestOsgiPluginFactory.class);
50 osgiContainerManager = mock(OsgiContainerManager.class);
51 ServiceTracker tracker = mock(ServiceTracker.class);
52 when(tracker.getServices()).thenReturn(new Object[0]);
53 when(osgiContainerManager.getServiceTracker(ModuleDescriptorFactory.class.getName())).thenReturn(tracker);
54
55 factory = new OsgiPluginFactory(PluginAccessor.Descriptor.FILENAME, (String) null, new DefaultOsgiPersistentCache(tmpDir), osgiContainerManager, new DefaultPluginEventManager());
56 jar = new PluginJarBuilder("someplugin").addPluginInformation("plugin.key", "My Plugin", "1.0").build();
57
58 mockBundle = new Mock(Bundle.class);
59 final Dictionary<String, String> dict = new Hashtable<String, String>();
60 dict.put(Constants.BUNDLE_DESCRIPTION, "desc");
61 dict.put(Constants.BUNDLE_VERSION, "1.0");
62 mockBundle.matchAndReturn("getHeaders", dict);
63
64 mockSystemBundle = new Mock(Bundle.class);
65 final Dictionary<String, String> sysDict = new Hashtable<String, String>();
66 sysDict.put(Constants.BUNDLE_DESCRIPTION, "desc");
67 sysDict.put(Constants.BUNDLE_VERSION, "1.0");
68 mockSystemBundle.matchAndReturn("getHeaders", sysDict);
69 mockSystemBundle.matchAndReturn("getLastModified", System.currentTimeMillis());
70 mockSystemBundle.matchAndReturn("getSymbolicName", "system.bundle");
71
72 Mock mockSysContext = new Mock(BundleContext.class);
73 mockSystemBundle.matchAndReturn("getBundleContext", mockSysContext.proxy());
74
75 mockSysContext.matchAndReturn("getServiceReference", C.ANY_ARGS, null);
76 mockSysContext.matchAndReturn("getService", C.ANY_ARGS, new Mock(PackageAdmin.class).proxy());
77 }
78
79 @Override
80 public void tearDown() throws IOException
81 {
82 factory = null;
83 FileUtils.cleanDirectory(tmpDir);
84 jar.delete();
85 }
86
87 public void testCreateOsgiPlugin() throws PluginParseException
88 {
89 mockBundle.expectAndReturn("getSymbolicName", "plugin.key");
90 when(osgiContainerManager.getHostComponentRegistrations()).thenReturn(new ArrayList());
91 when(osgiContainerManager.getBundles()).thenReturn(new Bundle[] {(Bundle) mockSystemBundle.proxy()});
92 final Plugin plugin = factory.create(new JarPluginArtifact(jar), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
93 assertNotNull(plugin);
94 assertTrue(plugin instanceof OsgiPlugin);
95 }
96
97 public void testCreateOsgiPluginWithBadVersion() throws PluginParseException, IOException
98 {
99 jar = new PluginJarBuilder("someplugin").addPluginInformation("plugin.key", "My Plugin", "beta.1.0").build();
100 mockBundle.expectAndReturn("getSymbolicName", "plugin.key");
101 when(osgiContainerManager.getHostComponentRegistrations()).thenReturn(new ArrayList());
102 when(osgiContainerManager.getBundles()).thenReturn(new Bundle[] {(Bundle) mockSystemBundle.proxy()});
103 try
104 {
105 factory.create(new JarPluginArtifact(jar), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
106 fail("Should have complained about osgi version");
107 }
108 catch (PluginParseException ex)
109 {
110
111 }
112 }
113
114 public void testCreateOsgiPluginWithBadVersion2() throws PluginParseException, IOException
115 {
116 jar = new PluginJarBuilder("someplugin").addPluginInformation("plugin.key", "My Plugin", "3.2-rc1").build();
117 mockBundle.expectAndReturn("getSymbolicName", "plugin.key");
118 when(osgiContainerManager.getHostComponentRegistrations()).thenReturn(new ArrayList());
119 when(osgiContainerManager.getBundles()).thenReturn(new Bundle[] {(Bundle) mockSystemBundle.proxy()});
120 Plugin plugin = factory.create(new JarPluginArtifact(jar), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
121 assertTrue(plugin instanceof OsgiPlugin);
122 }
123
124 public void testCreateOsgiPluginWithManifestKey() throws PluginParseException, IOException
125 {
126 mockBundle.expectAndReturn("getSymbolicName", "plugin.key");
127 when(osgiContainerManager.getHostComponentRegistrations()).thenReturn(new ArrayList());
128 when(osgiContainerManager.getBundles()).thenReturn(new Bundle[] {(Bundle) mockSystemBundle.proxy()});
129
130 final File pluginFile = new PluginJarBuilder("loadwithxml")
131 .manifest(new ImmutableMap.Builder<String, String>()
132 .put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "somekey")
133 .put(Constants.BUNDLE_VERSION, "1.0")
134 .build())
135 .build();
136
137 final Plugin plugin = factory.create(new JarPluginArtifact(pluginFile), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
138 assertNotNull(plugin);
139 assertTrue(plugin instanceof OsgiPlugin);
140 }
141
142 public void testCreateOsgiPluginWithManifestKeyNoVersion() throws PluginParseException, IOException
143 {
144 mockBundle.expectAndReturn("getSymbolicName", "plugin.key");
145 when(osgiContainerManager.getHostComponentRegistrations()).thenReturn(new ArrayList());
146 when(osgiContainerManager.getBundles()).thenReturn(new Bundle[] {(Bundle) mockSystemBundle.proxy()});
147
148 final File pluginFile = new PluginJarBuilder("loadwithxml")
149 .manifest(new ImmutableMap.Builder<String, String>()
150 .put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "somekey")
151 .build())
152 .build();
153
154 try
155 {
156 factory.create(new JarPluginArtifact(pluginFile), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
157 fail("Should have failed due to no version");
158 }
159 catch (IllegalArgumentException ex)
160 {
161
162 }
163 }
164
165 public void testCanLoadWithXml() throws PluginParseException, IOException
166 {
167 final File plugin = new PluginJarBuilder("loadwithxml").addPluginInformation("foo.bar", "", "1.0").build();
168 final String key = factory.canCreate(new JarPluginArtifact(plugin));
169 assertEquals("foo.bar", key);
170 }
171
172 public void testCanLoadWithXmlArtifact() throws PluginParseException, IOException
173 {
174 File xmlFile = new File(tmpDir, "plugin.xml");
175 FileUtils.writeStringToFile(xmlFile, "<somexml />");
176 final String key = factory.canCreate(new XmlPluginArtifact(xmlFile));
177 assertNull(key);
178 }
179
180 public void testCanLoadJarWithNoManifest() throws PluginParseException, IOException
181 {
182 final File plugin = new PluginJarBuilder("loadwithxml")
183 .addResource("foo.xml", "<foo/>")
184 .buildWithNoManifest();
185 final String key = factory.canCreate(new JarPluginArtifact(plugin));
186 assertNull(key);
187 }
188
189 public void testCanLoadNoXml() throws PluginParseException, IOException
190 {
191 final File plugin = new PluginJarBuilder("loadwithxml").build();
192 final String key = factory.canCreate(new JarPluginArtifact(plugin));
193 assertNull(key);
194 }
195
196 public void testCanLoadNoXmlButWithManifestEntry() throws PluginParseException, IOException
197 {
198 final File plugin = new PluginJarBuilder("loadwithxml")
199 .manifest(new ImmutableMap.Builder<String, String>()
200 .put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "somekey")
201 .put(Constants.BUNDLE_VERSION, "1.0")
202 .build())
203 .build();
204 final String key = factory.canCreate(new JarPluginArtifact(plugin));
205 assertEquals("somekey", key);
206 }
207
208 public void testCanLoadNoXmlButWithManifestEntryNoVersion() throws PluginParseException, IOException
209 {
210 final File plugin = new PluginJarBuilder("loadwithxml")
211 .manifest(new ImmutableMap.Builder<String, String>()
212 .put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "somekey")
213 .build())
214 .build();
215 final String key = factory.canCreate(new JarPluginArtifact(plugin));
216 assertNull(key);
217 }
218
219 }