PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-osgi/src/test/java/com/atlassian/plugin/osgi/util/TestOsgiHeaderUtil.java

https://bitbucket.org/purewind/atlassian-plugins
Java | 293 lines | 245 code | 46 blank | 2 comment | 0 complexity | da48f45be3e0feccbcc6139fc929d06f MD5 | raw file
  1. package com.atlassian.plugin.osgi.util;
  2. import com.atlassian.plugin.JarPluginArtifact;
  3. import com.atlassian.plugin.PluginArtifact;
  4. import com.atlassian.plugin.PluginParseException;
  5. import com.atlassian.plugin.osgi.factory.OsgiPlugin;
  6. import com.atlassian.plugin.osgi.factory.transform.StubHostComponentRegistration;
  7. import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
  8. import com.atlassian.plugin.osgi.hostcomponents.impl.MockRegistration;
  9. import com.atlassian.plugin.test.PluginJarBuilder;
  10. import com.google.common.collect.Sets;
  11. import org.junit.Assert;
  12. import org.junit.Test;
  13. import org.mockito.Mockito;
  14. import org.osgi.framework.Bundle;
  15. import org.osgi.framework.Constants;
  16. import org.osgi.framework.Version;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.Dictionary;
  25. import java.util.HashMap;
  26. import java.util.Hashtable;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import java.util.jar.JarFile;
  31. import java.util.jar.Manifest;
  32. import static junit.framework.Assert.assertEquals;
  33. import static junit.framework.Assert.assertFalse;
  34. import static junit.framework.Assert.assertTrue;
  35. import static org.hamcrest.Matchers.notNullValue;
  36. import static org.hamcrest.Matchers.nullValue;
  37. import static org.hamcrest.core.Is.is;
  38. import static org.hamcrest.core.IsEqual.equalTo;
  39. import static org.junit.Assert.assertThat;
  40. import static org.mockito.Mockito.mock;
  41. import static org.mockito.Mockito.when;
  42. public class TestOsgiHeaderUtil {
  43. @Test(expected = NullPointerException.class)
  44. public void shouldAssertValueShouldNotBeNull() {
  45. Manifest manifest = new Manifest();
  46. OsgiHeaderUtil.getValidatedAttribute(manifest, "some-random-key");
  47. }
  48. @Test(expected = IllegalArgumentException.class)
  49. public void shouldAssertValueShouldNotBeEmpty() throws IOException {
  50. Manifest manifest = generateManifest();
  51. OsgiHeaderUtil.getValidatedAttribute(manifest, "Some-other");
  52. }
  53. @Test(expected = IllegalArgumentException.class)
  54. public void shouldRaiseAnExceptionWhenValueIsMissing() throws IOException {
  55. Manifest manifest = generateManifest();
  56. OsgiHeaderUtil.getNonEmptyAttribute(manifest, "Some-other");
  57. }
  58. @Test
  59. public void shouldGetValidatedAttribute() throws IOException {
  60. Manifest manifestFile = generateManifest();
  61. assertThat(OsgiHeaderUtil.getValidatedAttribute(manifestFile, "Import-Package"), equalTo("javax.swing"));
  62. }
  63. @Test
  64. public void shouldGetEmptyAttributeWithoutValidation() throws IOException {
  65. Manifest manifest = generateManifest();
  66. assertThat(OsgiHeaderUtil.getAttributeWithoutValidation(manifest, "Some-other"), is(""));
  67. }
  68. @Test
  69. public void shouldGetNullAttributeWithoutValidation() throws IOException {
  70. Manifest manifest = generateManifest();
  71. Assert.assertNull(OsgiHeaderUtil.getAttributeWithoutValidation(manifest, "some-random"));
  72. }
  73. private Manifest generateManifest() throws IOException {
  74. File bundle = new PluginJarBuilder("someplugin")
  75. .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
  76. "Import-Package: javax.swing\n" +
  77. "Bundle-SymbolicName: my.foo.symbolicName\n" +
  78. "Some-other: \n" +
  79. "Bundle-Version: 1.0\n")
  80. .build();
  81. JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(bundle);
  82. InputStream descriptorClassStream = jarPluginArtifact.getResourceAsStream("META-INF/MANIFEST.MF");
  83. return new Manifest(descriptorClassStream);
  84. }
  85. @Test
  86. public void testFindReferredPackages() throws IOException {
  87. Set<String> foundPackages = OsgiHeaderUtil.findReferredPackageNames(new ArrayList<HostComponentRegistration>() {{
  88. add(new StubHostComponentRegistration(OsgiHeaderUtil.class));
  89. }});
  90. assertTrue(foundPackages.contains(HostComponentRegistration.class.getPackage().getName()));
  91. }
  92. @Test
  93. public void testFindReferredPackagesMustNotReturnJavaPackages() throws IOException {
  94. List<HostComponentRegistration> regs = Arrays.<HostComponentRegistration>asList(new MockRegistration(Mockito.mock(DummyClass.class), DummyClass.class));
  95. Set<String> foundPackages = OsgiHeaderUtil.findReferredPackageNames(regs);
  96. assertFalse(foundPackages.contains("java.lang"));
  97. }
  98. @Test
  99. public void testFindReferredPackagesWithVersion() throws IOException {
  100. Map<String, String> foundPackages = OsgiHeaderUtil.findReferredPackageVersions(new ArrayList<HostComponentRegistration>() {{
  101. add(new StubHostComponentRegistration(OsgiHeaderUtil.class));
  102. }}, Collections.singletonMap(HostComponentRegistration.class.getPackage().getName(), "1.0.45"));
  103. assertTrue(foundPackages.containsKey(HostComponentRegistration.class.getPackage().getName()));
  104. assertEquals(foundPackages.get(HostComponentRegistration.class.getPackage().getName()), "1.0.45");
  105. }
  106. @Test
  107. public void testGetPluginKeyBundle() {
  108. Dictionary headers = new Hashtable();
  109. headers.put(Constants.BUNDLE_VERSION, "1.0");
  110. headers.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
  111. Bundle bundle = mock(Bundle.class);
  112. when(bundle.getSymbolicName()).thenReturn("foo");
  113. when(bundle.getHeaders()).thenReturn(headers);
  114. assertEquals("foo-1.0", OsgiHeaderUtil.getPluginKey(bundle));
  115. headers.put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "bar");
  116. assertEquals("bar", OsgiHeaderUtil.getPluginKey(bundle));
  117. }
  118. @Test
  119. public void testGetPluginKeyManifest() {
  120. Manifest mf = new Manifest();
  121. mf.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.0");
  122. mf.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, "foo");
  123. assertEquals("foo-1.0", OsgiHeaderUtil.getPluginKey(mf));
  124. mf.getMainAttributes().putValue(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "bar");
  125. assertEquals("bar", OsgiHeaderUtil.getPluginKey(mf));
  126. }
  127. @Test
  128. public void testGeneratePackageVersionString() {
  129. Map<String, String> input = new HashMap<String, String>();
  130. input.put("foo.bar", "1.2");
  131. input.put("foo.baz", Version.emptyVersion.toString());
  132. String output = OsgiHeaderUtil.generatePackageVersionString(input);
  133. Set<String> set = Sets.newHashSet(output.split("[,]"));
  134. assertTrue(set.contains("foo.bar;version=1.2"));
  135. assertTrue(set.contains("foo.baz"));
  136. assertEquals(2, set.size());
  137. }
  138. @Test
  139. public void getManifestGetsManifest() {
  140. final String manifestString = "Manifest-Version: 1.0\r\nSome-Header: some value\r\n";
  141. final PluginArtifact pluginArtifact = getPluginArtifactWithManifest(manifestString);
  142. final Manifest manifest = OsgiHeaderUtil.getManifest(pluginArtifact);
  143. assertThat(manifest, notNullValue());
  144. assertThat(manifest.getMainAttributes().getValue("Some-Header"), is("some value"));
  145. }
  146. @Test
  147. public void getManifestReturnsNullIfManifestIsMissing() {
  148. final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
  149. // Mockito defaults return null, which is correct for missing resource
  150. final Manifest manifest = OsgiHeaderUtil.getManifest(pluginArtifact);
  151. assertThat(manifest, nullValue());
  152. }
  153. @Test
  154. public void getManifestReturnsNullIfGetResourceAsStreamFails() {
  155. final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
  156. when(pluginArtifact.getResourceAsStream(JarFile.MANIFEST_NAME))
  157. .thenThrow(new PluginParseException("Intentional test fail"));
  158. final Manifest manifest = OsgiHeaderUtil.getManifest(pluginArtifact);
  159. assertThat(manifest, nullValue());
  160. }
  161. @Test
  162. public void getManifestReturnsNullIfResourceStreamThrowsWhenRead() throws Exception {
  163. final InputStream inputStream = new InputStream() {
  164. @Override
  165. public int read() throws IOException {
  166. throw new IOException("Intentional test fail");
  167. }
  168. };
  169. final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
  170. when(pluginArtifact.getResourceAsStream(JarFile.MANIFEST_NAME)).thenReturn(inputStream);
  171. final Manifest manifest = OsgiHeaderUtil.getManifest(pluginArtifact);
  172. assertThat(manifest, nullValue());
  173. }
  174. @Test
  175. public void getManifestReturnsNullIfManifestIsCorrupt() {
  176. // Malformed, because you need a ' ' after the header ':'
  177. final String manifestString = "Manifest-Version: 1.0\r\nSome-Header:some value\r\n";
  178. final PluginArtifact pluginArtifact = getPluginArtifactWithManifest(manifestString);
  179. final Manifest manifest = OsgiHeaderUtil.getManifest(pluginArtifact);
  180. assertThat(manifest, nullValue());
  181. }
  182. @Test
  183. public void containsStarPackageTrue() {
  184. assertThat(OsgiHeaderUtil.containsStarPackage(""
  185. + "javax.servlet;version=\"[3.0.0,3.0.0]\","
  186. + "*;resolution:=\"optional\","
  187. + "org.apache.lucene.search;version=\"[4.4.0.atlassian_02,4.4.0.atlassian_02]\""),
  188. is(true));
  189. }
  190. @Test
  191. public void containsStarPackageFalse() {
  192. assertThat(OsgiHeaderUtil.containsStarPackage(""
  193. + "javax.servlet;version=\"[3.0.0,3.0.0]\","
  194. + "javax.annotation*;resolution:=\"optional\","
  195. + "org.apache.lucene.search;version=\"[4.4.0.atlassian_02,4.4.0.atlassian_02]\""),
  196. is(false));
  197. }
  198. @Test
  199. public void containsStarPackageEmpty() {
  200. assertThat(OsgiHeaderUtil.containsStarPackage(""), is(false));
  201. }
  202. @Test
  203. public void containsStarImportNull() {
  204. assertThat(OsgiHeaderUtil.containsStarPackage(null), is(false));
  205. }
  206. @Test
  207. public void moveStarPackagesToEndTrivial() {
  208. assertThat(OsgiHeaderUtil.moveStarPackageToEnd("*;resolution:=\"optional\"", "myplugin"), is("*;resolution:=\"optional\""));
  209. }
  210. @Test
  211. public void moveStarPackagesToEndComplex() {
  212. final String input = ""
  213. + "javax.servlet*;resolution:=\"optional\","
  214. + "javax.servlet*;resolution:=\"optional\","
  215. + "*;resolution:=\"optional\","
  216. + "*;resolution:=\"optional\","
  217. + "javax.servlet;version=\"[3.0.0,3.0.0]\","
  218. + "javax.servlet;version=\"[3.0.0,3.0.0]\","
  219. + "org.apache.lucene.search;version=\"[4.4.0.atlassian_02,4.4.0.atlassian_02]\"";
  220. final String expected = ""
  221. + "javax.servlet;version=\"[3.0.0,3.0.0]\","
  222. + "javax.servlet~;version=\"[3.0.0,3.0.0]\","
  223. + "org.apache.lucene.search;version=\"[4.4.0.atlassian_02,4.4.0.atlassian_02]\","
  224. + "javax.servlet*;resolution:=\"optional\","
  225. + "javax.servlet*~;resolution:=\"optional\","
  226. + "*;resolution:=\"optional\","
  227. + "*~;resolution:=\"optional\"";
  228. assertThat(OsgiHeaderUtil.moveStarPackageToEnd(input, "myplugin"), is(expected));
  229. }
  230. @Test
  231. public void moveStarPackagesToEndEmpty() {
  232. assertThat(OsgiHeaderUtil.moveStarPackageToEnd("", "myplugin"), is(""));
  233. }
  234. @Test
  235. public void moveStarPackagesToEndNull() {
  236. assertThat(OsgiHeaderUtil.moveStarPackageToEnd(null, "myplugin"), is(""));
  237. }
  238. private PluginArtifact getPluginArtifactWithManifest(final String manifestString) {
  239. final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
  240. final ByteArrayInputStream manifestStream = new ByteArrayInputStream(manifestString.getBytes());
  241. when(pluginArtifact.getResourceAsStream(JarFile.MANIFEST_NAME)).thenReturn(manifestStream);
  242. return pluginArtifact;
  243. }
  244. private static interface DummyClass {
  245. void doSomething(Object obj);
  246. }
  247. }