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.event.impl.DefaultPluginEventManager;
9   import com.atlassian.plugin.osgi.container.OsgiContainerManager;
10  import com.atlassian.plugin.osgi.container.impl.DefaultOsgiPersistentCache;
11  import com.atlassian.plugin.test.PluginJarBuilder;
12  import com.atlassian.plugin.test.PluginTestUtils;
13  import com.mockobjects.dynamic.C;
14  import com.mockobjects.dynamic.Mock;
15  import junit.framework.TestCase;
16  import org.apache.commons.io.FileUtils;
17  import org.osgi.framework.Bundle;
18  import org.osgi.framework.BundleContext;
19  import org.osgi.framework.Constants;
20  import org.osgi.service.packageadmin.PackageAdmin;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.net.URISyntaxException;
25  import java.util.ArrayList;
26  import java.util.Dictionary;
27  import java.util.Hashtable;
28  
29  public class TestOsgiPluginFactory extends TestCase
30  {
31      OsgiPluginFactory factory;
32  
33      private File tmpDir;
34      private File jar;
35      Mock mockOsgi;
36      private Mock mockBundle;
37      private Mock mockSystemBundle;
38  
39      @Override
40      public void setUp() throws IOException, URISyntaxException
41      {
42          tmpDir = PluginTestUtils.createTempDirectory(TestOsgiPluginFactory.class);
43          mockOsgi = new Mock(OsgiContainerManager.class);
44          factory = new OsgiPluginFactory(PluginAccessor.Descriptor.FILENAME, (String) null, new DefaultOsgiPersistentCache(tmpDir), (OsgiContainerManager) mockOsgi.proxy(), new DefaultPluginEventManager());
45          jar = new PluginJarBuilder("someplugin").addPluginInformation("plugin.key", "My Plugin", "1.0").build();
46  
47          mockBundle = new Mock(Bundle.class);
48          final Dictionary<String, String> dict = new Hashtable<String, String>();
49          dict.put(Constants.BUNDLE_DESCRIPTION, "desc");
50          dict.put(Constants.BUNDLE_VERSION, "1.0");
51          mockBundle.matchAndReturn("getHeaders", dict);
52  
53          mockSystemBundle = new Mock(Bundle.class);
54          final Dictionary<String, String> sysDict = new Hashtable<String, String>();
55          sysDict.put(Constants.BUNDLE_DESCRIPTION, "desc");
56          sysDict.put(Constants.BUNDLE_VERSION, "1.0");
57          mockSystemBundle.matchAndReturn("getHeaders", sysDict);
58          mockSystemBundle.matchAndReturn("getLastModified", System.currentTimeMillis());
59          mockSystemBundle.matchAndReturn("getSymbolicName", "system.bundle");
60  
61          Mock mockSysContext = new Mock(BundleContext.class);
62          mockSystemBundle.matchAndReturn("getBundleContext", mockSysContext.proxy());
63  
64          mockSysContext.matchAndReturn("getServiceReference", C.ANY_ARGS, null);
65          mockSysContext.matchAndReturn("getService", C.ANY_ARGS, new Mock(PackageAdmin.class).proxy());
66      }
67  
68      @Override
69      public void tearDown() throws IOException
70      {
71          factory = null;
72          FileUtils.cleanDirectory(tmpDir);
73          jar.delete();
74      }
75  
76      public void testCreateOsgiPlugin() throws PluginParseException
77      {
78          mockBundle.expectAndReturn("getSymbolicName", "plugin.key");
79          mockOsgi.expectAndReturn("getHostComponentRegistrations", new ArrayList());
80          mockOsgi.expectAndReturn("getServiceTracker", C.ANY_ARGS, null);
81          mockOsgi.matchAndReturn("getBundles", new Bundle[] {(Bundle) mockSystemBundle.proxy()});
82          final Plugin plugin = factory.create(new JarPluginArtifact(jar), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
83          assertNotNull(plugin);
84          assertTrue(plugin instanceof OsgiPlugin);
85          mockOsgi.verify();
86      }
87  
88      public void testCreateOsgiPluginWithBadVersion() throws PluginParseException, IOException
89      {
90          jar = new PluginJarBuilder("someplugin").addPluginInformation("plugin.key", "My Plugin", "beta.1.0").build();
91          mockBundle.expectAndReturn("getSymbolicName", "plugin.key");
92          mockOsgi.expectAndReturn("getServiceTracker", C.ANY_ARGS, null);
93          mockOsgi.expectAndReturn("getHostComponentRegistrations", new ArrayList());
94          mockOsgi.matchAndReturn("getBundles", new Bundle[] {(Bundle) mockSystemBundle.proxy()});
95          try
96          {
97              factory.create(new JarPluginArtifact(jar), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
98              fail("Should have complained about osgi version");
99          }
100         catch (PluginParseException ex)
101         {
102             // expected
103         }
104         mockOsgi.verify();
105     }
106 
107     public void testCreateOsgiPluginWithBadVersion2() throws PluginParseException, IOException
108     {
109         jar = new PluginJarBuilder("someplugin").addPluginInformation("plugin.key", "My Plugin", "3.2-rc1").build();
110         mockBundle.expectAndReturn("getSymbolicName", "plugin.key");
111         mockOsgi.expectAndReturn("getHostComponentRegistrations", new ArrayList());
112         mockOsgi.expectAndReturn("getServiceTracker", C.ANY_ARGS, null);
113         mockOsgi.matchAndReturn("getBundles", new Bundle[] {(Bundle) mockSystemBundle.proxy()});
114         Plugin plugin = factory.create(new JarPluginArtifact(jar), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
115         assertTrue(plugin instanceof OsgiPlugin);
116         mockOsgi.verify();
117     }
118 
119     public void testCanLoadWithXml() throws PluginParseException, IOException
120     {
121         final File plugin = new PluginJarBuilder("loadwithxml").addPluginInformation("foo.bar", "", "1.0").build();
122         final String key = factory.canCreate(new JarPluginArtifact(plugin));
123         assertEquals("foo.bar", key);
124     }
125 
126     public void testCanLoadNoXml() throws PluginParseException, IOException
127     {
128         final File plugin = new PluginJarBuilder("loadwithxml").build();
129         final String key = factory.canCreate(new JarPluginArtifact(plugin));
130         assertNull(key);
131     }
132 
133 }