1   package com.atlassian.plugin.osgi.util;
2   
3   import com.atlassian.plugin.osgi.factory.transform.StubHostComponentRegistration;
4   import com.atlassian.plugin.osgi.factory.OsgiPlugin;
5   import com.atlassian.plugin.osgi.container.PackageScannerConfiguration;
6   import com.atlassian.plugin.osgi.container.impl.DefaultPackageScannerConfiguration;
7   import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
8   import com.atlassian.plugin.osgi.hostcomponents.impl.MockRegistration;
9   import com.mockobjects.dynamic.Mock;
10  import com.mockobjects.dynamic.C;
11  import junit.framework.TestCase;
12  
13  import java.io.IOException;
14  import javax.servlet.ServletContext;
15  import javax.print.attribute.HashAttributeSet;
16  import javax.swing.table.TableModel;
17  import javax.swing.table.DefaultTableModel;
18  import javax.swing.text.AttributeSet;
19  import java.util.*;
20  import java.util.jar.Manifest;
21  
22  import org.twdata.pkgscanner.ExportPackage;
23  import org.osgi.framework.Bundle;
24  import org.osgi.framework.Constants;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  public class TestOsgiHeaderUtil extends TestCase 
29  {
30  
31      public void testFindReferredPackages() throws IOException
32      {
33          String foundPackages = OsgiHeaderUtil.findReferredPackages(new ArrayList<HostComponentRegistration>()
34          {{
35              add(new StubHostComponentRegistration(OsgiHeaderUtil.class));
36          }});
37  
38          assertTrue(foundPackages.contains(HostComponentRegistration.class.getPackage().getName()));
39  
40      }
41  
42      public void testFindReferredPackagesWithVersion() throws IOException
43      {
44          String foundPackages = OsgiHeaderUtil.findReferredPackages(new ArrayList<HostComponentRegistration>()
45          {{
46              add(new StubHostComponentRegistration(OsgiHeaderUtil.class));
47          }}, Collections.singletonMap(HostComponentRegistration.class.getPackage().getName(), "1.0"));
48  
49          assertTrue(foundPackages.contains(HostComponentRegistration.class.getPackage().getName()+";version=1.0"));
50  
51      }
52  
53      public void testGetPluginKeyBundle()
54      {
55          Dictionary headers = new Hashtable();
56          headers.put(Constants.BUNDLE_VERSION, "1.0");
57          headers.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
58          
59          Bundle bundle = mock(Bundle.class);
60          when(bundle.getSymbolicName()).thenReturn("foo");
61          when(bundle.getHeaders()).thenReturn(headers);
62  
63          assertEquals("foo-1.0", OsgiHeaderUtil.getPluginKey(bundle));
64  
65          headers.put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "bar");
66          assertEquals("bar", OsgiHeaderUtil.getPluginKey(bundle));
67      }
68  
69      public void testGetPluginKeyManifest()
70      {
71          Manifest mf = new Manifest();
72          mf.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.0");
73          mf.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, "foo");
74  
75          assertEquals("foo-1.0", OsgiHeaderUtil.getPluginKey(mf));
76  
77          mf.getMainAttributes().putValue(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "bar");
78          assertEquals("bar", OsgiHeaderUtil.getPluginKey(mf));
79      }
80  }