PageRenderTime 32ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin/src/test/java/com/atlassian/labs/speakeasy/manager/convention/TestZipTransformer.java

https://github.com/mrdon/speakeasy-plugin
Java | 131 lines | 113 code | 15 blank | 3 comment | 5 complexity | c1b3abeaf7074aea13732afe3ca9beeb MD5 | raw file
  1. package com.atlassian.labs.speakeasy.manager.convention;
  2. import com.atlassian.labs.speakeasy.model.JsonManifest;
  3. import com.atlassian.plugin.JarPluginArtifact;
  4. import com.atlassian.plugin.PluginArtifact;
  5. import com.atlassian.plugin.osgi.factory.OsgiPlugin;
  6. import com.atlassian.plugin.test.PluginJarBuilder;
  7. import com.google.common.collect.ImmutableList;
  8. import com.google.common.collect.ImmutableMap;
  9. import org.apache.commons.io.FileUtils;
  10. import org.apache.commons.io.IOUtils;
  11. import org.junit.Before;
  12. import org.junit.Test;
  13. import org.xml.sax.SAXException;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.StringWriter;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import java.util.jar.JarFile;
  22. import java.util.jar.Manifest;
  23. import static com.google.common.collect.Sets.newHashSet;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertTrue;
  26. import static org.mockito.Mockito.mock;
  27. import static org.osgi.framework.Constants.*;
  28. /**
  29. *
  30. */
  31. public class TestZipTransformer
  32. {
  33. private ZipTransformer zipTransformer;
  34. @Before
  35. public void setUp()
  36. {
  37. zipTransformer = new ZipTransformer(new JsonManifestHandler(), mock(JsonToElementParser.class));
  38. }
  39. @Test
  40. public void testFullTransform() throws IOException, SAXException
  41. {
  42. assertTransform("full", ImmutableList.of(
  43. "css/myextension.css",
  44. "images/image.png",
  45. "js/myextension/main.js",
  46. "atlassian-extension.json"
  47. ), ImmutableMap.of(
  48. BUNDLE_DESCRIPTION, "My simple extension that does wonderful things",
  49. BUNDLE_NAME, "My Extension",
  50. BUNDLE_VERSION, "1",
  51. BUNDLE_VENDOR, "Joe Citizen",
  52. OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "myextension"
  53. ));
  54. }
  55. @Test
  56. public void testMinimalTransform() throws IOException, SAXException
  57. {
  58. assertTransform("minimal", ImmutableList.of(
  59. "atlassian-extension.json"
  60. ), ImmutableMap.of(
  61. BUNDLE_VERSION, "1",
  62. OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "myextension"
  63. ));
  64. }
  65. private void assertTransform(String id, List<String> source, Map<String,String> expectedHeaders) throws IOException, SAXException
  66. {
  67. String prefix = "/" + getClass().getPackage().getName().replace('.', '/') + "/" + id + "/";
  68. PluginJarBuilder builder = new PluginJarBuilder();
  69. Set<String> allDirs = newHashSet();
  70. for (String path : source)
  71. {
  72. builder.addFormattedResource(path, getResource(prefix + path));
  73. String[] dirs = path.split("/");
  74. StringBuilder pwd = new StringBuilder();
  75. for (int x=0; x<dirs.length - 1; x++)
  76. {
  77. pwd.append(dirs[x]).append("/");
  78. if (allDirs.add(pwd.toString()))
  79. {
  80. builder.addResource(pwd.toString(), "");
  81. }
  82. }
  83. }
  84. File jar = builder.buildWithNoManifest();
  85. File zip = new File(jar.getPath() + ".zip");
  86. FileUtils.moveFile(jar, zip);
  87. final JarPluginArtifact jarArtifact = new JarPluginArtifact(zip);
  88. JsonManifest jsonMf = new JsonManifestHandler().read(jarArtifact);
  89. PluginArtifact artifact = zipTransformer.convertConventionZipToPluginJar(jsonMf, jarArtifact);
  90. JarFile jarFile = new JarFile(artifact.toFile());
  91. Manifest mf = jarFile.getManifest();
  92. for (Map.Entry<String,String> entry : expectedHeaders.entrySet())
  93. {
  94. assertEquals(entry.getValue(), mf.getMainAttributes().getValue(entry.getKey()));
  95. }
  96. for (String path : source)
  97. {
  98. assertTrue("Cannot find path in final artifact: " + path, artifact.doesResourceExist(path));
  99. }
  100. }
  101. private String getResource(String path) throws IOException
  102. {
  103. InputStream in = null;
  104. try
  105. {
  106. in = getClass().getResourceAsStream(path);
  107. StringWriter writer = new StringWriter();
  108. IOUtils.copy(in, writer);
  109. return writer.toString();
  110. }
  111. finally
  112. {
  113. IOUtils.closeQuietly(in);
  114. }
  115. }
  116. }