View Javadoc

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.osgi.container.OsgiContainerException;
8   import com.atlassian.plugin.osgi.container.OsgiContainerManager;
9   import com.atlassian.plugin.test.PluginJarBuilder;
10  
11  import com.google.common.collect.ImmutableMap;
12  import com.mockobjects.dynamic.C;
13  import com.mockobjects.dynamic.Mock;
14  import junit.framework.TestCase;
15  
16  import org.osgi.framework.Bundle;
17  import org.osgi.framework.Constants;
18  
19  import java.io.*;
20  import java.net.URISyntaxException;
21  import java.util.Dictionary;
22  import java.util.Hashtable;
23  
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.when;
26  import static org.mockito.Mockito.withSettings;
27  
28  public class TestOsgiBundleFactory extends TestCase {
29      OsgiBundleFactory deployer;
30      Mock mockOsgi;
31  
32      @Override
33      public void setUp() throws IOException, URISyntaxException
34      {
35          mockOsgi = new Mock(OsgiContainerManager.class);
36          deployer = new OsgiBundleFactory((OsgiContainerManager) mockOsgi.proxy(), new DefaultPluginEventManager());
37      }
38  
39      @Override
40      public void tearDown()
41      {
42          deployer = null;
43      }
44      public void testCanDeploy() throws PluginParseException, IOException
45      {
46          File bundle = new PluginJarBuilder("someplugin")
47              .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
48                          "Import-Package: javax.swing\n" +
49                          "Bundle-SymbolicName: my.foo.symbolicName\n" +
50                          "Bundle-Version: 1.0\n")
51              .build();
52          assertEquals("my.foo.symbolicName-1.0", deployer.canCreate(new JarPluginArtifact(bundle)));
53      }
54  
55      public void testCanDeployStaticPluginWithManifest() throws PluginParseException, IOException
56      {
57          File bundle = new PluginJarBuilder("someplugin")
58              .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
59                          "Import-Package: javax.swing\n" +
60                          "Bundle-SymbolicName: my.foo.symbolicName\n" +
61                          "Bundle-Version: 1.0\n")
62              .addPluginInformation("foo", "Foo", "1.0", 1)
63              .build();
64          assertEquals(null, deployer.canCreate(new JarPluginArtifact(bundle)));
65      }
66  
67      public void testCanDeployNoBundle() throws IOException, PluginParseException {
68  
69          File plugin = new PluginJarBuilder("someplugin")
70              .addPluginInformation("my.foo.symb", "name", "1.0")
71              .build();
72          assertNull(deployer.canCreate(new JarPluginArtifact(plugin)));
73      }
74  
75      public void testCanDeployNonJar() throws IOException, PluginParseException {
76  
77          final File tmp = File.createTempFile("foo", "bar");
78          assertNull(deployer.canCreate(new PluginArtifact()
79          {
80              public boolean doesResourceExist(String name)
81              {
82                  return false;
83              }
84  
85              public InputStream getResourceAsStream(String fileName) throws PluginParseException {
86                  return null;
87              }
88  
89              public String getName() {
90                  return tmp.getPath();
91              }
92  
93              public InputStream getInputStream() {
94                  try {
95                      return new FileInputStream(tmp);
96                  } catch (FileNotFoundException e) {
97                      e.printStackTrace();
98                      return null;
99                  }
100             }
101 
102             public File toFile()
103             {
104                 return tmp;
105             }
106 
107             @Override
108             public boolean containsJavaExecutableCode()
109             {
110                 return false;
111             }
112         }));
113     }
114 
115     public void testDeploy() throws PluginParseException, IOException {
116         File bundle = new PluginJarBuilder("someplugin")
117             .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
118                         "Import-Package: javax.swing\n" +
119                         "Bundle-SymbolicName: my.foo.symbolicName\n")
120             .build();
121 
122         Mock mockBundle = new Mock(Bundle.class);
123         Dictionary<String, String> dict = new Hashtable<String, String>();
124         dict.put(Constants.BUNDLE_DESCRIPTION, "desc");
125         dict.put(Constants.BUNDLE_VERSION, "1.0");
126         dict.put(Constants.BUNDLE_VENDOR, "acme");
127         dict.put(Constants.BUNDLE_NAME, "myplugin");
128         mockBundle.matchAndReturn("getHeaders", dict);
129         mockBundle.expectAndReturn("getSymbolicName", "my.foo.symbolicName");
130         mockOsgi.expectAndReturn("installBundle", C.ANY_ARGS, mockBundle.proxy());
131         Plugin plugin = deployer.create(new JarPluginArtifact(bundle), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
132         assertNotNull(plugin);
133         assertTrue(plugin instanceof OsgiBundlePlugin);
134         assertEquals("acme", plugin.getPluginInformation().getVendorName());
135         assertEquals("myplugin", plugin.getName());
136         assertEquals("desc", plugin.getPluginInformation().getDescription());
137         assertNull(plugin.getI18nNameKey());
138         mockOsgi.verify();
139     }
140 
141     public void testDeployFail() throws PluginParseException, IOException {
142         File bundle = new PluginJarBuilder("someplugin")
143             .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
144                         "Import-Package: javax.swing\n" +
145                         "Bundle-SymbolicName: my.foo.symbolicName\n")
146             .build();
147         //noinspection ThrowableInstanceNeverThrown
148         mockOsgi.expectAndThrow("installBundle", C.ANY_ARGS, new OsgiContainerException("Bad install"));
149         Plugin plugin = deployer.create(new JarPluginArtifact(bundle), (ModuleDescriptorFactory) new Mock(ModuleDescriptorFactory.class).proxy());
150         assertNotNull(plugin);
151         assertTrue(plugin instanceof UnloadablePlugin);
152         mockOsgi.verify();
153     }
154 
155     public void testPluginArtifactThatAllowsReferenceIsInstalledByReference() throws IOException
156     {
157         final File jarFile = new File("some.jar");
158         final PluginArtifact pluginArtifact = mock(
159                 PluginArtifact.class,
160                 withSettings().extraInterfaces(PluginArtifact.AllowsReference.class));
161         when(pluginArtifact.toFile()).thenReturn(jarFile);
162         when(((PluginArtifact.AllowsReference) pluginArtifact).allowsReference()).thenReturn(true);
163         final Bundle bundle = mock(Bundle.class);
164         // Need a basic set of headers for the Plugin to get created
165         final String bundleSymbolicName = getClass().getName() + ".testBundle";
166         final String bundleVersion = "1.2";
167         final String bundleName = "Test Bundle";
168         final Dictionary<String, String> headers = new Hashtable<String, String>(ImmutableMap.<String, String>builder()
169                 .put(Constants.BUNDLE_SYMBOLICNAME, bundleSymbolicName)
170                 .put(Constants.BUNDLE_VERSION, bundleVersion)
171                 .put(Constants.BUNDLE_NAME, bundleName)
172                 .put(Constants.BUNDLE_DESCRIPTION, "A Bundle for Testing")
173                 .put(Constants.BUNDLE_VENDOR, "Some Bundle Vendor")
174                 .build());
175         when(bundle.getSymbolicName()).thenReturn(bundleSymbolicName);
176         when(bundle.getHeaders()).thenReturn(headers);
177         final OsgiContainerManager osgiContainerManager = mock(
178                 OsgiContainerManager.class,
179                 withSettings().extraInterfaces(OsgiContainerManager.AllowsReferenceInstall.class));
180         // Have our mock OsgiContainerManager respond only to a reference install (the true in the next line)
181         when(((OsgiContainerManager.AllowsReferenceInstall) osgiContainerManager).installBundle(jarFile, true)).thenReturn(bundle);
182         final OsgiBundleFactory osgiBundleFactory = new OsgiBundleFactory(osgiContainerManager, mock(PluginEventManager.class));
183         final Plugin plugin = osgiBundleFactory.create(pluginArtifact, mock(ModuleDescriptorFactory.class));
184         assertEquals(plugin.getName(), bundleName);
185     }
186 }