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 @Override
101 public boolean containsJavaExecutableCode()
102 {
103 return false;
104 }
105 }));
106 }
107
108 public void testDeploy() throws PluginParseException, IOException {
109 File bundle = new PluginJarBuilder("someplugin")
110 .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
111 "Import-Package: javax.swing\n" +
112 "Bundle-SymbolicName: my.foo.symbolicName\n")
113 .build();
114
115 Mock mockBundle = new Mock(Bundle.class);
116 Dictionary<String, String> dict = new Hashtable<String, String>();
117 dict.put(Constants.BUNDLE_DESCRIPTION, "desc");
118 dict.put(Constants.BUNDLE_VERSION, "1.0");
119 dict.put(Constants.BUNDLE_VENDOR, "acme");
120 dict.put(Constants.BUNDLE_NAME, "myplugin");
121 mockBundle.matchAndReturn("getHeaders", dict);
122 mockBundle.expectAndReturn("getSymbolicName", "my.foo.symbolicName");
123 mockOsgi.expectAndReturn("installBundle", C.ANY_ARGS, mockBundle.proxy());
124 Plugin plugin = deployer.create(new JarPluginArtifact(bundle), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
125 assertNotNull(plugin);
126 assertTrue(plugin instanceof OsgiBundlePlugin);
127 assertEquals("acme", plugin.getPluginInformation().getVendorName());
128 assertEquals("myplugin", plugin.getName());
129 assertEquals("desc", plugin.getPluginInformation().getDescription());
130 assertNull(plugin.getI18nNameKey());
131 mockOsgi.verify();
132 }
133
134 public void testDeployFail() throws PluginParseException, IOException {
135 File bundle = new PluginJarBuilder("someplugin")
136 .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
137 "Import-Package: javax.swing\n" +
138 "Bundle-SymbolicName: my.foo.symbolicName\n")
139 .build();
140
141 mockOsgi.expectAndThrow("installBundle", C.ANY_ARGS, new OsgiContainerException("Bad install"));
142 Plugin plugin = deployer.create(new JarPluginArtifact(bundle), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
143 assertNotNull(plugin);
144 assertTrue(plugin instanceof UnloadablePlugin);
145 mockOsgi.verify();
146 }
147 }