View Javadoc

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