View Javadoc

1   package com.atlassian.plugin.osgi.util;
2   
3   import com.atlassian.plugin.event.PluginEventManager;
4   import com.atlassian.plugin.osgi.factory.transform.StubHostComponentRegistration;
5   import com.atlassian.plugin.osgi.factory.OsgiPlugin;
6   import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
7   import com.atlassian.plugin.osgi.hostcomponents.impl.MockRegistration;
8   import com.google.common.collect.Sets;
9   import junit.framework.TestCase;
10  
11  import java.io.IOException;
12  import java.util.*;
13  import java.util.jar.Manifest;
14  
15  import org.mockito.Mockito;
16  import org.osgi.framework.Bundle;
17  import org.osgi.framework.Constants;
18  import org.osgi.framework.Version;
19  
20  import static org.mockito.Mockito.mock;
21  import static org.mockito.Mockito.when;
22  
23  public class TestOsgiHeaderUtil extends TestCase 
24  {
25  
26      public void testFindReferredPackages() throws IOException
27      {
28          Set<String> foundPackages = OsgiHeaderUtil.findReferredPackageNames(new ArrayList<HostComponentRegistration>()
29          {{
30              add(new StubHostComponentRegistration(OsgiHeaderUtil.class));
31          }});
32  
33          assertTrue(foundPackages.contains(HostComponentRegistration.class.getPackage().getName()));
34      }
35  
36      public void testFindReferredPackagesMustNotReturnJavaPackages() throws IOException
37      {
38          List<HostComponentRegistration> regs = Arrays.<HostComponentRegistration>asList(new MockRegistration(Mockito.mock(DummyClass.class), DummyClass.class));
39  
40          Set<String> foundPackages = OsgiHeaderUtil.findReferredPackageNames(regs);
41  
42          assertFalse(foundPackages.contains("java.lang"));
43      }
44  
45      public void testFindReferredPackagesWithVersion() throws IOException
46      {
47          Map<String, String> foundPackages = OsgiHeaderUtil.findReferredPackageVersions(new ArrayList<HostComponentRegistration>()
48          {{
49              add(new StubHostComponentRegistration(OsgiHeaderUtil.class));
50          }}, Collections.singletonMap(HostComponentRegistration.class.getPackage().getName(), "1.0.45"));
51  
52          assertTrue(foundPackages.containsKey(HostComponentRegistration.class.getPackage().getName()));
53          assertEquals(foundPackages.get(HostComponentRegistration.class.getPackage().getName()), "1.0.45");
54      }
55  
56      public void testGetPluginKeyBundle()
57      {
58          Dictionary headers = new Hashtable();
59          headers.put(Constants.BUNDLE_VERSION, "1.0");
60          headers.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
61          
62          Bundle bundle = mock(Bundle.class);
63          when(bundle.getSymbolicName()).thenReturn("foo");
64          when(bundle.getHeaders()).thenReturn(headers);
65  
66          assertEquals("foo-1.0", OsgiHeaderUtil.getPluginKey(bundle));
67  
68          headers.put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "bar");
69          assertEquals("bar", OsgiHeaderUtil.getPluginKey(bundle));
70      }
71  
72      public void testGetPluginKeyManifest()
73      {
74          Manifest mf = new Manifest();
75          mf.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.0");
76          mf.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, "foo");
77  
78          assertEquals("foo-1.0", OsgiHeaderUtil.getPluginKey(mf));
79  
80          mf.getMainAttributes().putValue(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "bar");
81          assertEquals("bar", OsgiHeaderUtil.getPluginKey(mf));
82      }
83  
84      public void testGeneratePackageVersionString()
85      {
86          Map<String, String> input = new HashMap<String, String>();
87          input.put("foo.bar", "1.2");
88          input.put("foo.baz", Version.emptyVersion.toString());
89  
90          String output = OsgiHeaderUtil.generatePackageVersionString(input);
91  
92          Set<String> set = Sets.newHashSet(output.split("[,]"));
93  
94          assertTrue(set.contains("foo.bar;version=1.2"));
95          assertTrue(set.contains("foo.baz"));
96          assertEquals(2, set.size());
97      }
98  
99      private static interface DummyClass
100     {
101         void doSomething(Object obj);
102     }
103 
104 }