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  {
38      private RemotablePluginFactory factory;
39  
40      private File tmpDir;
41      private File jar;
42      private 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(TestRemotablePluginFactory.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 RemotablePluginFactory(PluginAccessor.Descriptor.FILENAME, Collections.<Application>emptySet(), osgiContainerManager, new DefaultPluginEventManager());
56          jar = new PluginJarBuilder("someplugin").addPluginInformation("plugin.key", "My Plugin", "1.0", 3).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", "3.2-rc1").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         Plugin plugin = factory.create(new JarPluginArtifact(jar), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
104         assertTrue(plugin instanceof OsgiPlugin);
105     }
106 
107     public void testCanLoadWithXmlVersion2() throws PluginParseException, IOException
108     {
109         final File plugin = new PluginJarBuilder("loadwithxml").addPluginInformation("foo.bar", "", "1.0", 2).build();
110         assertNull(factory.canCreate(new JarPluginArtifact(plugin)));
111     }
112 
113     public void testCanLoadWithXmlVersion3() throws PluginParseException, IOException
114     {
115         final File plugin = new PluginJarBuilder("loadwithxml").addPluginInformation("foo.bar", "", "1.0", 3).build();
116         assertEquals("foo.bar", factory.canCreate(new JarPluginArtifact(plugin)));
117     }
118 
119     public void testCanLoadWithXmlArtifact() throws PluginParseException, IOException
120     {
121         File xmlFile = new File(tmpDir, "plugin.xml");
122         FileUtils.writeStringToFile(xmlFile, "<somexml />");
123         final String key = factory.canCreate(new XmlPluginArtifact(xmlFile));
124         assertNull(key);
125     }
126 
127     public void testCanLoadNoXml() throws PluginParseException, IOException
128     {
129         final File plugin = new PluginJarBuilder("loadwithxml").build();
130         final String key = factory.canCreate(new JarPluginArtifact(plugin));
131         assertNull(key);
132     }
133 
134     public void testCanLoadNoXmlButWithManifestEntry() throws PluginParseException, IOException
135     {
136         final File plugin = new PluginJarBuilder("loadwithxml")
137                 .manifest(new ImmutableMap.Builder<String, String>()
138                         .put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "somekey")
139                         .put(Constants.BUNDLE_VERSION, "1.0")
140                         .build())
141                 .build();
142         final String key = factory.canCreate(new JarPluginArtifact(plugin));
143         assertNull(key);
144     }
145 }