1   package com.atlassian.plugin.osgi.factory;
2   
3   import com.atlassian.plugin.*;
4   import com.atlassian.plugin.event.PluginEventManager;
5   import com.atlassian.plugin.event.impl.DefaultPluginEventManager;
6   import com.atlassian.plugin.impl.UnloadablePlugin;
7   import com.atlassian.plugin.loaders.classloading.DeploymentUnit;
8   import com.atlassian.plugin.osgi.container.OsgiContainerException;
9   import com.atlassian.plugin.osgi.container.OsgiContainerManager;
10  import com.atlassian.plugin.test.PluginJarBuilder;
11  import com.mockobjects.dynamic.C;
12  import com.mockobjects.dynamic.Mock;
13  import junit.framework.TestCase;
14  import org.osgi.framework.Bundle;
15  import org.osgi.framework.Constants;
16  
17  import java.io.*;
18  import java.net.URISyntaxException;
19  import java.util.Dictionary;
20  import java.util.Hashtable;
21  
22  public class TestOsgiBundleFactory extends TestCase {
23  
24      OsgiBundleFactory deployer;
25      Mock mockOsgi;
26  
27      @Override
28      public void setUp() throws IOException, URISyntaxException
29      {
30          mockOsgi = new Mock(OsgiContainerManager.class);
31          deployer = new OsgiBundleFactory((OsgiContainerManager) mockOsgi.proxy(), new DefaultPluginEventManager());
32      }
33  
34      @Override
35      public void tearDown()
36      {
37          deployer = null;
38      }
39      public void testCanDeploy() throws PluginParseException, IOException
40      {
41          File bundle = new PluginJarBuilder("someplugin")
42              .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
43                          "Import-Package: javax.swing\n" +
44                          "Bundle-SymbolicName: my.foo.symbolicName\n" +
45                          "Bundle-Version: 1.0\n")
46              .build();
47          assertEquals("my.foo.symbolicName-1.0", deployer.canCreate(new JarPluginArtifact(bundle)));
48      }
49  
50      public void testCanDeployNoBundle() throws IOException, PluginParseException {
51  
52          File plugin = new PluginJarBuilder("someplugin")
53              .addPluginInformation("my.foo.symb", "name", "1.0")
54              .build();
55          assertNull(deployer.canCreate(new JarPluginArtifact(plugin)));
56      }
57  
58      public void testCanDeployNonJar() throws IOException, PluginParseException {
59  
60          final File tmp = File.createTempFile("foo", "bar");
61          assertNull(deployer.canCreate(new PluginArtifact()
62          {
63              public boolean doesResourceExist(String name)
64              {
65                  return false;
66              }
67  
68              public InputStream getResourceAsStream(String fileName) throws PluginParseException {
69                  return null;
70              }
71  
72              public String getName() {
73                  return tmp.getPath();
74              }
75  
76              public InputStream getInputStream() {
77                  try {
78                      return new FileInputStream(tmp);
79                  } catch (FileNotFoundException e) {
80                      e.printStackTrace();
81                      return null;
82                  }
83              }
84  
85              public File toFile()
86              {
87                  return tmp;
88              }
89          }));
90      }
91  
92      public void testDeploy() throws PluginParseException, IOException {
93          File bundle = new PluginJarBuilder("someplugin")
94              .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
95                          "Import-Package: javax.swing\n" +
96                          "Bundle-SymbolicName: my.foo.symbolicName\n")
97              .build();
98  
99          Mock mockBundle = new Mock(Bundle.class);
100         Dictionary<String, String> dict = new Hashtable<String, String>();
101         dict.put(Constants.BUNDLE_DESCRIPTION, "desc");
102         dict.put(Constants.BUNDLE_VERSION, "1.0");
103         dict.put(Constants.BUNDLE_VENDOR, "acme");
104         dict.put(Constants.BUNDLE_NAME, "myplugin");
105         mockBundle.matchAndReturn("getHeaders", dict);
106         mockBundle.expectAndReturn("getSymbolicName", "my.foo.symbolicName");
107         mockOsgi.expectAndReturn("installBundle", C.ANY_ARGS, mockBundle.proxy());
108         Plugin plugin = deployer.create(new JarPluginArtifact(bundle), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
109         assertNotNull(plugin);
110         assertTrue(plugin instanceof OsgiBundlePlugin);
111         assertEquals("acme", plugin.getPluginInformation().getVendorName());
112         assertEquals("myplugin", plugin.getName());
113         assertEquals("desc", plugin.getPluginInformation().getDescription());
114         assertNull(plugin.getI18nNameKey());
115         mockOsgi.verify();
116     }
117 
118     public void testDeployFail() throws PluginParseException, IOException {
119         File bundle = new PluginJarBuilder("someplugin")
120             .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
121                         "Import-Package: javax.swing\n" +
122                         "Bundle-SymbolicName: my.foo.symbolicName\n")
123             .build();
124         //noinspection ThrowableInstanceNeverThrown
125         mockOsgi.expectAndThrow("installBundle", C.ANY_ARGS, new OsgiContainerException("Bad install"));
126         Plugin plugin = deployer.create(new JarPluginArtifact(bundle), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
127         assertNotNull(plugin);
128         assertTrue(plugin instanceof UnloadablePlugin);
129         mockOsgi.verify();
130     }
131 }