View Javadoc

1   package com.atlassian.plugin.osgi.util;
2   
3   import com.atlassian.plugin.JarPluginArtifact;
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.atlassian.plugin.test.PluginJarBuilder;
9   import com.google.common.collect.Sets;
10  
11  import java.io.File;
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.util.*;
15  import java.util.jar.Manifest;
16  
17  import org.junit.Assert;
18  import org.junit.Test;
19  import org.mockito.Mockito;
20  import org.osgi.framework.Bundle;
21  import org.osgi.framework.Constants;
22  import org.osgi.framework.Version;
23  
24  import static junit.framework.Assert.assertEquals;
25  import static junit.framework.Assert.assertFalse;
26  import static junit.framework.Assert.assertTrue;
27  import static org.hamcrest.core.Is.is;
28  import static org.hamcrest.core.IsEqual.equalTo;
29  import static org.junit.Assert.assertThat;
30  import static org.mockito.Mockito.mock;
31  import static org.mockito.Mockito.when;
32  
33  public class TestOsgiHeaderUtil
34  {
35  
36      @Test(expected = NullPointerException.class)
37      public void shouldAssertValueShouldNotBeNull()
38      {
39          Manifest manifest = new Manifest();
40          OsgiHeaderUtil.getValidatedAttribute(manifest, "some-random-key");
41      }
42  
43  
44      @Test(expected = IllegalArgumentException.class)
45      public void shouldAssertValueShouldNotBeEmpty() throws IOException
46      {
47          Manifest manifest = generateManifest();
48          OsgiHeaderUtil.getValidatedAttribute(manifest, "Some-other");
49      }
50  
51      @Test(expected = IllegalArgumentException.class)
52      public void shouldRaiseAnExceptionWhenValueIsMissing() throws IOException
53      {
54          Manifest manifest = generateManifest();
55          OsgiHeaderUtil.getNonEmptyAttribute(manifest,"Some-other");
56      }
57  
58      @Test
59      public void shouldGetValidatedAttribute() throws IOException
60      {
61          Manifest manifestFile = generateManifest();
62          assertThat(OsgiHeaderUtil.getValidatedAttribute(manifestFile, "Import-Package"), equalTo("javax.swing"));
63      }
64  
65      @Test
66      public void shouldGetEmptyAttributeWithoutValidation() throws IOException
67      {
68          Manifest manifest = generateManifest();
69          assertThat(OsgiHeaderUtil.getAttributeWithoutValidation(manifest, "Some-other"), is(""));
70      }
71  
72      @Test
73      public void shouldGetNullAttributeWithoutValidation() throws IOException
74      {
75          Manifest manifest = generateManifest();
76          Assert.assertNull(OsgiHeaderUtil.getAttributeWithoutValidation(manifest, "some-random"));
77      }
78  
79      private Manifest generateManifest() throws IOException
80      {
81          File bundle = new PluginJarBuilder("someplugin")
82                  .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
83                          "Import-Package: javax.swing\n" +
84                          "Bundle-SymbolicName: my.foo.symbolicName\n" +
85                          "Some-other: \n" +
86                          "Bundle-Version: 1.0\n")
87                  .build();
88          JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(bundle);
89          InputStream descriptorClassStream = jarPluginArtifact.getResourceAsStream("META-INF/MANIFEST.MF");
90          return new Manifest(descriptorClassStream);
91      }
92  
93      @Test
94      public void testFindReferredPackages() throws IOException
95      {
96          Set<String> foundPackages = OsgiHeaderUtil.findReferredPackageNames(new ArrayList<HostComponentRegistration>()
97          {{
98                  add(new StubHostComponentRegistration(OsgiHeaderUtil.class));
99              }});
100 
101         assertTrue(foundPackages.contains(HostComponentRegistration.class.getPackage().getName()));
102     }
103 
104     @Test
105     public void testFindReferredPackagesMustNotReturnJavaPackages() throws IOException
106     {
107         List<HostComponentRegistration> regs = Arrays.<HostComponentRegistration>asList(new MockRegistration(Mockito.mock(DummyClass.class), DummyClass.class));
108 
109         Set<String> foundPackages = OsgiHeaderUtil.findReferredPackageNames(regs);
110 
111         assertFalse(foundPackages.contains("java.lang"));
112     }
113 
114     @Test
115     public void testFindReferredPackagesWithVersion() throws IOException
116     {
117         Map<String, String> foundPackages = OsgiHeaderUtil.findReferredPackageVersions(new ArrayList<HostComponentRegistration>()
118         {{
119                 add(new StubHostComponentRegistration(OsgiHeaderUtil.class));
120             }}, Collections.singletonMap(HostComponentRegistration.class.getPackage().getName(), "1.0.45"));
121 
122         assertTrue(foundPackages.containsKey(HostComponentRegistration.class.getPackage().getName()));
123         assertEquals(foundPackages.get(HostComponentRegistration.class.getPackage().getName()), "1.0.45");
124     }
125 
126     @Test
127     public void testGetPluginKeyBundle()
128     {
129         Dictionary headers = new Hashtable();
130         headers.put(Constants.BUNDLE_VERSION, "1.0");
131         headers.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
132 
133         Bundle bundle = mock(Bundle.class);
134         when(bundle.getSymbolicName()).thenReturn("foo");
135         when(bundle.getHeaders()).thenReturn(headers);
136 
137         assertEquals("foo-1.0", OsgiHeaderUtil.getPluginKey(bundle));
138 
139         headers.put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "bar");
140         assertEquals("bar", OsgiHeaderUtil.getPluginKey(bundle));
141     }
142 
143     @Test
144     public void testGetPluginKeyManifest()
145     {
146         Manifest mf = new Manifest();
147         mf.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.0");
148         mf.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, "foo");
149 
150         assertEquals("foo-1.0", OsgiHeaderUtil.getPluginKey(mf));
151 
152         mf.getMainAttributes().putValue(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "bar");
153         assertEquals("bar", OsgiHeaderUtil.getPluginKey(mf));
154     }
155 
156     @Test
157     public void testGeneratePackageVersionString()
158     {
159         Map<String, String> input = new HashMap<String, String>();
160         input.put("foo.bar", "1.2");
161         input.put("foo.baz", Version.emptyVersion.toString());
162 
163         String output = OsgiHeaderUtil.generatePackageVersionString(input);
164 
165         Set<String> set = Sets.newHashSet(output.split("[,]"));
166 
167         assertTrue(set.contains("foo.bar;version=1.2"));
168         assertTrue(set.contains("foo.baz"));
169         assertEquals(2, set.size());
170     }
171 
172     private static interface DummyClass
173     {
174         void doSomething(Object obj);
175     }
176 
177 }