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